V0.7.2 Arroyo Toad

Added support to toggle sensors
This commit is contained in:
Christian Risi 2024-12-09 14:46:31 +00:00
parent 5b358b8bef
commit 92875ecb12
2 changed files with 143 additions and 17 deletions

View File

@ -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")
}
}

View File

@ -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..<devices {
let dev: EdgeDevice = EdgeDevice(
deviceID: i,
dataType: .Temperature,
disconnected: false,
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 200 + UInt.random(in: 200...1000),
sensors: [
0: RealSensor(
sensorID: 0, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
1: RealSensor(
sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
2: RealSensor(
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
],
privateKey: privateKey
)
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
for i in 0..<_sleep {
print("Hi, at \(i)s\n\n")
sleep(1)
IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
}
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
IoTSimulatorCore.nukeAll()
}