V0.7.2 Arroyo Toad
Added support to toggle sensors
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user