diff --git a/Sources/IoT-Simulator-Core/IoT_Simulator_Core.swift b/Sources/IoT-Simulator-Core/IoT_Simulator_Core.swift index 0b8f75a..3c7beda 100644 --- a/Sources/IoT-Simulator-Core/IoT_Simulator_Core.swift +++ b/Sources/IoT-Simulator-Core/IoT_Simulator_Core.swift @@ -14,7 +14,12 @@ public actor IoTSimulatorCore { IoTSimulatorCore.enviroments[environment.location] = environment } - public static func addDevice(location: String, device: EdgeDeviceP) throws { + public static func addDevice( + location: String, + device: EdgeDeviceP, + success: sending @escaping (_ msg: Data) throws -> Void, + failure: sending @escaping () -> Void + ) throws { if let environment = IoTSimulatorCore.enviroments[location] { IoTSimulatorCore.devices["\(device.deviceID)"] = device @@ -25,11 +30,9 @@ public actor IoTSimulatorCore { IoTSimulatorCore.env_dev[location]!.insert(device.deviceID) // schedule work - let task = IoTSimulatorCore.schedule(envID: environment.location, deviceID: device.deviceID) { msg in - print("\(msg)\n\n") - } failure: { - print("Something is wrong") - } + let task = IoTSimulatorCore.schedule( + envID: environment.location, deviceID: device.deviceID, success: success, + failure: failure) IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task } else { @@ -42,6 +45,12 @@ public actor IoTSimulatorCore { return IoTSimulatorCore.enviroments[name] } + public static func addPhysicalData(environmentName: String, physicalData: PhysicalData) { + if let environment = getEnv(name: environmentName) { + environment.setPhysicalData(physicalData.type, physicalData) + } + } + public static func getDev(devID: UInt128) -> EdgeDeviceP? { return IoTSimulatorCore.devices["\(devID)"] } @@ -56,27 +65,57 @@ public actor IoTSimulatorCore { } } + public static func stopDevice(devID: UInt128) -> Bool { + if let task = IoTSimulatorCore.dev_tasks["\(devID)"] { + task.cancel() + return true + } + return false + } + public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool { + + // UGLY: Should throw + if IoTSimulatorCore.getDev(devID: devID) == nil { + return false + } + + // FIXME: Should it throw? + if IoTSimulatorCore.getDev(devID: devID)!.deviceType != .EDGE_SENSOR { + return false + } + + let device = IoTSimulatorCore.getDev(devID: devID)! as! EdgeDevice + + // UGLY: It should throw + if device.sensors.count <= sensorID { + return false + } + + device.sensors[sensorID]!.faulty = !device.sensors[sensorID]!.faulty + + return true + } private static func schedule( envID: sending String, deviceID: UInt128, success: sending @escaping (_ msg: Data) throws -> Void, failure: sending @escaping () -> Void - ) -> Task<(), Never> { - let _devID : String = "\(deviceID)" + ) -> Task<(), Never> { + let _devID: String = "\(deviceID)" return Task { var notCancelled: Bool = true let dev = IoTSimulatorCore.getDev(devID: _devID)! let env = IoTSimulatorCore.getEnv(name: envID)! - + while notCancelled { - print("Device: \(dev.deviceID)") + print("Device: \(dev.deviceID)") do { let message = try dev.work(envrionment: env) - + try success(message) } catch { failure() @@ -86,13 +125,13 @@ public actor IoTSimulatorCore { try await Task.sleep(nanoseconds: UInt64(dev.dutyCicle) * UInt64(1E6)) } catch { notCancelled = false - + } - + } print("Bye, Bye, \(dev.deviceID)\n\n") - + } } diff --git a/Tests/IoT-Simulator-CoreTests/IoTCore-Tests.swift b/Tests/IoT-Simulator-CoreTests/IoTCore-Tests.swift index 09344c7..4303aa7 100644 --- a/Tests/IoT-Simulator-CoreTests/IoTCore-Tests.swift +++ b/Tests/IoT-Simulator-CoreTests/IoTCore-Tests.swift @@ -58,8 +58,22 @@ import MessageUtils privateKey: privateKey ) - try IoTSimulatorCore.addDevice(location: "Delta", device: dev) - try IoTSimulatorCore.addDevice(location: "Delta", device: dev2) + try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in + print(msg) + let _signedMessage = try! deserializeV1(serializedData: msg) + print(_signedMessage.toString()) + }, + failure: { + print("Something went wrong") + }) + try IoTSimulatorCore.addDevice(location: "Delta", device: dev2, success: { msg in + print(msg) + let _signedMessage = try! deserializeV1(serializedData: msg) + print(_signedMessage.toString()) + }, + failure: { + print("Something went wrong") + }) sleep(15) @@ -102,7 +116,18 @@ import MessageUtils privateKey: privateKey ) - try IoTSimulatorCore.addDevice(location: "Delta", device: dev) + try IoTSimulatorCore.addDevice( + location: "Delta", + device: dev, + success: { msg in + print(msg) + let _signedMessage = try! deserializeV1(serializedData: msg) + print(_signedMessage.toString()) + }, + failure: { + print("Something went wrong") + } + ) } let _sleep = 15 @@ -116,3 +141,65 @@ import MessageUtils } + +@Test func stressLoop2() async throws { + + let devices: UInt128 = 1 + + let env = PhysicalEnvironment("Delta") + let truth = PhysicalData(.Temperature, 22) + + env.setPhysicalData(DataType.Temperature, truth) + + IoTSimulatorCore.addEnv(environment: env) + + let signKeyPath = "./Private/privateKey.pem" + + let privateKey = try pem2_P256key(filePath: signKeyPath) + + for i: UInt128 in 0..