From 238aa4568789168586f79e733e833e70fd55c781 Mon Sep 17 00:00:00 2001 From: Tim Tanguay Date: Tue, 17 Mar 2020 21:06:56 -0700 Subject: [PATCH] add disable/enable toggle for device to main menu --- Manager/Sources/RDMUICManager/CLI.swift | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Manager/Sources/RDMUICManager/CLI.swift b/Manager/Sources/RDMUICManager/CLI.swift index cecf2698..059d688e 100644 --- a/Manager/Sources/RDMUICManager/CLI.swift +++ b/Manager/Sources/RDMUICManager/CLI.swift @@ -74,12 +74,13 @@ class CLI { 4. Add Device 5. Edit Device 6. Delete Device + 7. Toggle Device 0. Exit ================ """ print(menu) - let number = askInput("Please select an option", options: [0, 1, 2, 3, 4, 5, 6]) + let number = askInput("Please select an option", options: [0, 1, 2, 3, 4, 5, 6, 7]) print() switch number { case 1: @@ -100,6 +101,9 @@ class CLI { case 6: deleteDevice() return true + case 7: + toggleDevice() + return true default: return false } @@ -720,6 +724,44 @@ class CLI { } } + private func toggleDevice() { + clear() + let devices = Device.getAll() + print("=======================") + print("Select Device to Toggle") + print("=======================") + var i = 1 + for device in devices { + print("\(i): \(device.name)") + i += 1 + } + print("0: Back") + print("=====================") + let index = askInput("Please select an option", options: Array(0...devices.count)) + print() + if index == 0 { + clear() + return + } + let device = devices[index - 1] + device.enabled = 1 - device.enabled + + do { + try device.save() + clear() + BuildController.global.removeDevice(device: device) + let tmpQueue = Threading.getQueue(name: UUID().uuidString, type: .serial) + tmpQueue.dispatch { + Threading.sleep(seconds: 10.0) + BuildController.global.addDevice(device: device) + Threading.destroyQueue(tmpQueue) + } + print("Device toggled.\n\n") + } catch { + print("Failed to toggle device.\n\n") + } + } + // MARK: - Helper Functions private func askInput(_ line: String) -> String {