V0.8.0 Arroyo Toad
Fixed problems related to concurrency and segmentation faults
This commit is contained in:
parent
2ef8cc868b
commit
d05a2c3767
@ -3,22 +3,23 @@ import DataAcquisition
|
|||||||
import Foundation
|
import Foundation
|
||||||
import MessageUtils
|
import MessageUtils
|
||||||
|
|
||||||
public class EdgeDevice: EdgeDeviceP, @unchecked Sendable {
|
public actor EdgeDevice: @preconcurrency EdgeDeviceP {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public let deviceID: UInt128
|
public let deviceID: UInt128
|
||||||
public let deviceType: DeviceType
|
public let deviceType: DeviceType
|
||||||
public let dataType: DataType
|
public let dataType: DataType
|
||||||
public var disconnected: Bool
|
public private(set) var disconnected: Bool
|
||||||
public var compromised: Bool
|
public private(set) var compromised: Bool
|
||||||
public var location: Location
|
public private(set) var location: Location
|
||||||
public var dutyCicle: UInt
|
public private(set) var dutyCicle: UInt
|
||||||
public var sensors: [Int: Sensor]
|
public private(set) var sensors: [Int: Sensor]
|
||||||
public var privateKey: P256.Signing.PrivateKey
|
public private(set) var privateKey: P256.Signing.PrivateKey
|
||||||
public var publicKey: P256.Signing.PublicKey {
|
public var publicKey: P256.Signing.PublicKey {
|
||||||
return self.privateKey.publicKey
|
return self.privateKey.publicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
private let lock: NSLock
|
|
||||||
private var numberOfSensors: Int {
|
private var numberOfSensors: Int {
|
||||||
return sensors.count
|
return sensors.count
|
||||||
}
|
}
|
||||||
@ -41,24 +42,54 @@ public class EdgeDevice: EdgeDeviceP, @unchecked Sendable {
|
|||||||
self.dutyCicle = dutyCicle
|
self.dutyCicle = dutyCicle
|
||||||
self.sensors = sensors
|
self.sensors = sensors
|
||||||
self.privateKey = privateKey
|
self.privateKey = privateKey
|
||||||
self.lock = NSLock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func addSensor(sensor: Sensor) {
|
public func addSensor(sensor: Sensor) async {
|
||||||
self.lock.lock()
|
|
||||||
self.sensors[numberOfSensors + 1] = sensor
|
self.sensors[numberOfSensors + 1] = sensor
|
||||||
self.lock.unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeSensor(id: Int) {
|
public func removeSensor(id: Int) async {
|
||||||
self.lock.lock()
|
|
||||||
self.sensors.removeValue(forKey: id)
|
self.sensors.removeValue(forKey: id)
|
||||||
self.lock.unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func work(envrionment: PhysicalEnvironment) throws -> Data {
|
public func setSensorFaulty(id: Int) async {
|
||||||
|
if id > self.numberOfSensors {
|
||||||
|
// UGLY: Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
let sensor = self.sensors[id]!
|
||||||
|
|
||||||
|
await sensor.toggleFaulty()
|
||||||
|
}
|
||||||
|
|
||||||
|
public func setDisconnected(disconnected: Bool) async {
|
||||||
|
self.disconnected = disconnected
|
||||||
|
}
|
||||||
|
|
||||||
|
public func setCompromised(compromised: Bool) async {
|
||||||
|
self.compromised = compromised
|
||||||
|
}
|
||||||
|
|
||||||
|
public func setLocation(location: Location) async {
|
||||||
|
self.location = location
|
||||||
|
}
|
||||||
|
|
||||||
|
public func dutyCycle(time: UInt) async {
|
||||||
|
self.dutyCicle = time
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getSensor(id: Int) async -> Sensor {
|
||||||
|
if id > self.numberOfSensors {
|
||||||
|
// UGLY: Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
return self.sensors[id]!
|
||||||
|
}
|
||||||
|
|
||||||
|
public func work(envrionment: PhysicalEnvironment) async throws -> Data {
|
||||||
|
|
||||||
|
|
||||||
self.lock.lock()
|
|
||||||
|
|
||||||
// UGLY: Declaring here some variables manually, remove them if you want to offere flexibility
|
// UGLY: Declaring here some variables manually, remove them if you want to offere flexibility
|
||||||
let numberOfSamples: Int = 10
|
let numberOfSamples: Int = 10
|
||||||
@ -75,7 +106,7 @@ public class EdgeDevice: EdgeDeviceP, @unchecked Sendable {
|
|||||||
|
|
||||||
for rowI in 0..<numberOfSamples {
|
for rowI in 0..<numberOfSamples {
|
||||||
for colI in 0..<self.sensors.count {
|
for colI in 0..<self.sensors.count {
|
||||||
rowPointer[rowI]![colI] = self.sensors[colI]!.read(envrionment).value
|
rowPointer[rowI]![colI] = await self.getSensor(id: colI).read(envrionment).value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +136,7 @@ public class EdgeDevice: EdgeDeviceP, @unchecked Sendable {
|
|||||||
|
|
||||||
msgData.append(Data(signature))
|
msgData.append(Data(signature))
|
||||||
|
|
||||||
self.lock.unlock()
|
|
||||||
|
|
||||||
return msgData
|
return msgData
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,22 @@
|
|||||||
import RandomCpp
|
import RandomCpp
|
||||||
|
|
||||||
public class Sensor {
|
public protocol Sensor : Sendable {
|
||||||
|
var sensorID: Int { get }
|
||||||
|
var sensorType: DataType { get }
|
||||||
|
var faulty: Bool { get }
|
||||||
|
|
||||||
|
func setFaulty(value: Bool) async
|
||||||
|
|
||||||
|
func toggleFaulty() async
|
||||||
|
|
||||||
|
func read(_ environment: PhysicalEnvironment) async -> PhysicalData
|
||||||
|
}
|
||||||
|
|
||||||
|
public actor IdealSensor: @preconcurrency Sensor {
|
||||||
|
|
||||||
public let sensorID: Int
|
public let sensorID: Int
|
||||||
public let sensorType: DataType
|
public let sensorType: DataType
|
||||||
public var faulty: Bool
|
public private(set) var faulty: Bool
|
||||||
|
|
||||||
public init(id: Int, sensorType: DataType, faulty: Bool) {
|
public init(id: Int, sensorType: DataType, faulty: Bool) {
|
||||||
self.sensorID = id
|
self.sensorID = id
|
||||||
@ -18,25 +30,25 @@ public class Sensor {
|
|||||||
self.faulty = false
|
self.faulty = false
|
||||||
}
|
}
|
||||||
|
|
||||||
public func read(_ environment: PhysicalEnvironment) -> PhysicalData {
|
public func read(_ environment: PhysicalEnvironment) async -> PhysicalData {
|
||||||
|
|
||||||
let datum = self._read(environment)
|
let datum = await self._read(environment)
|
||||||
|
|
||||||
return !self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
|
return !self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func _read(_ environment: PhysicalEnvironment) -> PhysicalData{
|
internal func _read(_ environment: PhysicalEnvironment) async -> PhysicalData {
|
||||||
let datum : PhysicalData
|
let datum: PhysicalData
|
||||||
|
|
||||||
do {
|
do {
|
||||||
datum = try environment.getPhysicalData(self.sensorType)
|
datum = try await environment.getPhysicalData(self.sensorType)
|
||||||
} catch CoreError.NoPhysicalDataAvailable {
|
} catch CoreError.NoPhysicalDataAvailable {
|
||||||
datum = self._defaultReadValue()
|
datum = self._defaultReadValue()
|
||||||
} catch {
|
} catch {
|
||||||
print(
|
print(
|
||||||
"This is really a weird problem, to be investigated, but here we are...\n" +
|
"This is really a weird problem, to be investigated, but here we are...\n"
|
||||||
"nonetheless, the error will be handled silently, sowwy :3"
|
+ "nonetheless, the error will be handled silently, sowwy :3"
|
||||||
)
|
)
|
||||||
datum = self._defaultReadValue()
|
datum = self._defaultReadValue()
|
||||||
}
|
}
|
||||||
@ -44,19 +56,28 @@ public class Sensor {
|
|||||||
return datum
|
return datum
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func _defaultReadValue() -> PhysicalData{
|
internal func _defaultReadValue() -> PhysicalData {
|
||||||
return PhysicalData(self.sensorType, 0)
|
return PhysicalData(self.sensorType, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func setFaulty(value: Bool) async {
|
||||||
|
self.faulty = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public func toggleFaulty() async {
|
||||||
|
self.faulty = !self.faulty
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RealSensor: Sensor {
|
public actor RealSensor: @preconcurrency Sensor {
|
||||||
|
public let sensorID: Int
|
||||||
|
public let sensorType: DataType
|
||||||
|
public private(set) var faulty: Bool
|
||||||
|
|
||||||
public var meanNoise: Float { get{ return _meanNoise}}
|
public var meanNoise: Float { return _meanNoise }
|
||||||
public var stdNoise: Float { get{ return _stdNoise}}
|
public var stdNoise: Float { return _stdNoise }
|
||||||
public var quantizationBits: Int { get{ return _quantizationBits}}
|
public var quantizationBits: Int { return _quantizationBits }
|
||||||
|
|
||||||
private var _meanNoise: Float
|
private var _meanNoise: Float
|
||||||
private var _stdNoise: Float
|
private var _stdNoise: Float
|
||||||
@ -72,14 +93,16 @@ public class RealSensor: Sensor {
|
|||||||
stdNoise: Float,
|
stdNoise: Float,
|
||||||
quantizationBits: Int
|
quantizationBits: Int
|
||||||
) {
|
) {
|
||||||
|
self.sensorID = sensorID
|
||||||
|
self.sensorType = sensorType
|
||||||
|
self.faulty = faulty
|
||||||
self._meanNoise = meanNoise
|
self._meanNoise = meanNoise
|
||||||
self._stdNoise = stdNoise
|
self._stdNoise = stdNoise
|
||||||
self._quantizationBits = quantizationBits
|
self._quantizationBits = quantizationBits
|
||||||
self._gaussianNoise = GaussianRNG(self._meanNoise, self._stdNoise)
|
self._gaussianNoise = GaussianRNG(self._meanNoise, self._stdNoise)
|
||||||
super.init(id: sensorID, sensorType: sensorType, faulty: faulty)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
convenience public init(
|
public init(
|
||||||
sensorID: Int,
|
sensorID: Int,
|
||||||
sensorType: DataType,
|
sensorType: DataType,
|
||||||
meanNoise: Float,
|
meanNoise: Float,
|
||||||
@ -96,9 +119,44 @@ public class RealSensor: Sensor {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func read(_ environment: PhysicalEnvironment) -> PhysicalData {
|
public func read(_ environment: PhysicalEnvironment) async -> PhysicalData {
|
||||||
let value: PhysicalData = super.read(environment)
|
|
||||||
|
let datum = await self._read(environment)
|
||||||
|
|
||||||
|
let value = !self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
|
||||||
|
|
||||||
return value + self._gaussianNoise.generate()
|
return value + self._gaussianNoise.generate()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal func _read(_ environment: PhysicalEnvironment) async -> PhysicalData {
|
||||||
|
let datum: PhysicalData
|
||||||
|
|
||||||
|
do {
|
||||||
|
datum = try await environment.getPhysicalData(self.sensorType)
|
||||||
|
} catch CoreError.NoPhysicalDataAvailable {
|
||||||
|
datum = self._defaultReadValue()
|
||||||
|
} catch {
|
||||||
|
print(
|
||||||
|
"This is really a weird problem, to be investigated, but here we are...\n"
|
||||||
|
+ "nonetheless, the error will be handled silently, sowwy :3"
|
||||||
|
)
|
||||||
|
datum = self._defaultReadValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
return datum
|
||||||
|
}
|
||||||
|
|
||||||
|
internal func _defaultReadValue() -> PhysicalData {
|
||||||
|
return PhysicalData(self.sensorType, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func setFaulty(value: Bool) async {
|
||||||
|
self.faulty = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public func toggleFaulty() async {
|
||||||
|
self.faulty = !self.faulty
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public class PhysicalEnvironment: @unchecked Sendable {
|
public actor PhysicalEnvironment {
|
||||||
|
|
||||||
private var physicalEnvironment: [DataType: PhysicalData]
|
private var physicalEnvironment: [DataType: PhysicalData]
|
||||||
public let location: String
|
public let location: String
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
public class PhysicalData {
|
public final class PhysicalData: Sendable {
|
||||||
|
|
||||||
public let type: DataType
|
public let type: DataType
|
||||||
public let value: Float
|
public let value: Float
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
public enum DataType : String {
|
public enum DataType : String, Sendable {
|
||||||
case Temperature
|
case Temperature
|
||||||
case Humidity
|
case Humidity
|
||||||
case Scan
|
case Scan
|
||||||
|
|||||||
@ -7,11 +7,10 @@ public actor DeviceFactory {
|
|||||||
// TODO: Make it possible to set this
|
// TODO: Make it possible to set this
|
||||||
private static var availableIDs: Set<UInt128> = Set(0..<999)
|
private static var availableIDs: Set<UInt128> = Set(0..<999)
|
||||||
|
|
||||||
@MainActor
|
|
||||||
public static func createEdgeDevice(
|
public static func createEdgeDevice(
|
||||||
deviceID: UInt128,
|
deviceID: sending UInt128,
|
||||||
dataType: DataType,
|
dataType: sending DataType,
|
||||||
privateKey: P256.Signing.PrivateKey
|
privateKey: sending P256.Signing.PrivateKey
|
||||||
) throws -> EdgeDevice {
|
) throws -> EdgeDevice {
|
||||||
return try DeviceFactory.createEdgeDevice(
|
return try DeviceFactory.createEdgeDevice(
|
||||||
deviceID: deviceID,
|
deviceID: deviceID,
|
||||||
@ -21,12 +20,11 @@ public actor DeviceFactory {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
public static func createEdgeDevice(
|
public static func createEdgeDevice(
|
||||||
deviceID: UInt128,
|
deviceID: sending UInt128,
|
||||||
dataType: DataType,
|
dataType: sending DataType,
|
||||||
privateKey: P256.Signing.PrivateKey,
|
privateKey: sending P256.Signing.PrivateKey,
|
||||||
realSensor: Bool
|
realSensor: sending Bool
|
||||||
) throws -> EdgeDevice {
|
) throws -> EdgeDevice {
|
||||||
|
|
||||||
return try DeviceFactory.createEdgeDevice(
|
return try DeviceFactory.createEdgeDevice(
|
||||||
@ -43,14 +41,13 @@ public actor DeviceFactory {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
public static func createEdgeDevice(
|
public static func createEdgeDevice(
|
||||||
deviceID: UInt128,
|
deviceID: sending UInt128,
|
||||||
dataType: DataType,
|
dataType: sending DataType,
|
||||||
location: Location,
|
location: sending Location,
|
||||||
dutyCicle: UInt,
|
dutyCicle: sending UInt,
|
||||||
privateKey: P256.Signing.PrivateKey,
|
privateKey: sending P256.Signing.PrivateKey,
|
||||||
realSensor: Bool
|
realSensor: sending Bool
|
||||||
) throws -> EdgeDevice {
|
) throws -> EdgeDevice {
|
||||||
|
|
||||||
if !DeviceFactory.availableIDs.contains(deviceID) {
|
if !DeviceFactory.availableIDs.contains(deviceID) {
|
||||||
@ -64,9 +61,9 @@ public actor DeviceFactory {
|
|||||||
|
|
||||||
if !realSensor {
|
if !realSensor {
|
||||||
sensors = [
|
sensors = [
|
||||||
0: Sensor(id: 0, sensorType: dataType),
|
0: IdealSensor(id: 0, sensorType: dataType),
|
||||||
1: Sensor(id: 1, sensorType: dataType),
|
1: IdealSensor(id: 1, sensorType: dataType),
|
||||||
2: Sensor(id: 2, sensorType: dataType),
|
2: IdealSensor(id: 2, sensorType: dataType),
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
sensors = [
|
sensors = [
|
||||||
@ -89,7 +86,8 @@ public actor DeviceFactory {
|
|||||||
location: location,
|
location: location,
|
||||||
dutyCicle: dutyCicle,
|
dutyCicle: dutyCicle,
|
||||||
sensors: sensors,
|
sensors: sensors,
|
||||||
privateKey: privateKey)
|
privateKey: privateKey
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func getUnusedID() throws -> UInt128 {
|
public static func getUnusedID() throws -> UInt128 {
|
||||||
|
|||||||
@ -3,12 +3,13 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public actor IoTSimulatorCore {
|
@MainActor
|
||||||
|
public class IoTSimulatorCore {
|
||||||
|
|
||||||
internal static var enviroments: [String: PhysicalEnvironment] = Dictionary()
|
@MainActor internal static var enviroments: [String: PhysicalEnvironment] = Dictionary()
|
||||||
internal static var devices: [String: EdgeDeviceP] = Dictionary()
|
@MainActor internal static var devices: [String: EdgeDeviceP] = Dictionary()
|
||||||
internal static var env_dev: [String: Set<String>] = Dictionary()
|
@MainActor internal static var env_dev: [String: Set<String>] = Dictionary()
|
||||||
internal static var dev_tasks: [String: Task<(), Never>] = Dictionary()
|
@MainActor internal static var dev_tasks: [String: Task<(), Never>] = Dictionary()
|
||||||
|
|
||||||
public static func addEnv(environment: PhysicalEnvironment) {
|
public static func addEnv(environment: PhysicalEnvironment) {
|
||||||
IoTSimulatorCore.enviroments[environment.location] = environment
|
IoTSimulatorCore.enviroments[environment.location] = environment
|
||||||
@ -31,7 +32,7 @@ public actor IoTSimulatorCore {
|
|||||||
|
|
||||||
// schedule work
|
// schedule work
|
||||||
let task = IoTSimulatorCore.schedule(
|
let task = IoTSimulatorCore.schedule(
|
||||||
envID: environment.location, deviceID: device.deviceID, success: success,
|
environment: environment, device: device as! EdgeDevice, success: success,
|
||||||
failure: failure)
|
failure: failure)
|
||||||
IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task
|
IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task
|
||||||
|
|
||||||
@ -45,12 +46,6 @@ public actor IoTSimulatorCore {
|
|||||||
return IoTSimulatorCore.enviroments[name]
|
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? {
|
public static func getDev(devID: UInt128) -> EdgeDeviceP? {
|
||||||
return IoTSimulatorCore.devices["\(devID)"]
|
return IoTSimulatorCore.devices["\(devID)"]
|
||||||
}
|
}
|
||||||
@ -73,49 +68,44 @@ public actor IoTSimulatorCore {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
public static func toggleSensor(devID: UInt, sensorID: Int) async -> Bool {
|
||||||
public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool {
|
|
||||||
|
|
||||||
// UGLY: Should throw
|
let a = IoTSimulatorCore.getDev(devID: UInt128(devID))!
|
||||||
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
|
let device = IoTSimulatorCore.getDev(devID: UInt128(devID))! as! EdgeDevice
|
||||||
|
|
||||||
// UGLY: It should throw
|
await device.setSensorFaulty(id: sensorID)
|
||||||
if device.sensors.count <= sensorID {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
device.sensors[sensorID]!.faulty = !device.sensors[sensorID]!.faulty
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static func addPhysicalData(environmentName: String, physicalData: PhysicalData)
|
||||||
|
async throws
|
||||||
|
{
|
||||||
|
guard let environment = getEnv(name: environmentName) else {
|
||||||
|
throw CoreError.NoEnvironment
|
||||||
|
}
|
||||||
|
|
||||||
|
await environment.setPhysicalData(physicalData.type, physicalData)
|
||||||
|
}
|
||||||
|
|
||||||
private static func schedule(
|
private static func schedule(
|
||||||
envID: sending String,
|
environment: PhysicalEnvironment,
|
||||||
deviceID: UInt128,
|
device: EdgeDevice,
|
||||||
success: sending @escaping (_ msg: Data) async throws -> Void,
|
success: sending @escaping (_ msg: Data) async throws -> Void,
|
||||||
failure: sending @escaping () -> Void
|
failure: sending @escaping () -> Void
|
||||||
) -> Task<(), Never>{
|
) -> Task<(), Never> {
|
||||||
let _devID: String = "\(deviceID)"
|
|
||||||
return Task {
|
return Task {
|
||||||
|
|
||||||
var notCancelled: Bool = true
|
var notCancelled: Bool = true
|
||||||
|
|
||||||
let dev = IoTSimulatorCore.getDev(devID: _devID)!
|
|
||||||
let env = IoTSimulatorCore.getEnv(name:envID)!
|
|
||||||
|
|
||||||
while notCancelled {
|
while notCancelled {
|
||||||
// print("Device: \(dev.deviceID)")
|
// print("Device: \(dev.deviceID)")
|
||||||
do {
|
do {
|
||||||
let message = try dev.work(envrionment: env)
|
let message = try await device.work(envrionment: environment)
|
||||||
|
|
||||||
try await success(message)
|
try await success(message)
|
||||||
} catch {
|
} catch {
|
||||||
@ -123,14 +113,14 @@ public actor IoTSimulatorCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try await Task.sleep(nanoseconds: UInt64(dev.dutyCicle) * UInt64(1E6))
|
try await Task.sleep(nanoseconds: UInt64(device.dutyCicle) * UInt64(1E6))
|
||||||
} catch {
|
} catch {
|
||||||
notCancelled = false
|
notCancelled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Bye, Bye, \(dev.deviceID)\n\n")
|
print("Bye, Bye, \(device.deviceID)\n\n")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,13 +7,19 @@ public protocol EdgeDeviceP : Sendable{
|
|||||||
var deviceID : UInt128 {get}
|
var deviceID : UInt128 {get}
|
||||||
var deviceType : DeviceType {get}
|
var deviceType : DeviceType {get}
|
||||||
var dataType : DataType {get}
|
var dataType : DataType {get}
|
||||||
var location: Location{get set}
|
var location: Location{get }
|
||||||
var disconnected : Bool {get set}
|
var disconnected : Bool {get }
|
||||||
var compromised : Bool {get set}
|
var compromised : Bool {get }
|
||||||
var dutyCicle : UInt {get set}
|
var dutyCicle : UInt {get }
|
||||||
var privateKey: P256.Signing.PrivateKey {get}
|
var privateKey: P256.Signing.PrivateKey {get}
|
||||||
var publicKey: P256.Signing.PublicKey {get}
|
var publicKey: P256.Signing.PublicKey {get}
|
||||||
|
|
||||||
func work(envrionment: PhysicalEnvironment) throws -> Data
|
func work(envrionment: PhysicalEnvironment) async throws -> Data
|
||||||
|
|
||||||
|
func setDisconnected(disconnected: Bool) async
|
||||||
|
func setCompromised(compromised: Bool) async
|
||||||
|
func dutyCycle(time: UInt) async
|
||||||
|
func setLocation(location: Location) async
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -11,10 +11,10 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature)
|
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature)
|
||||||
let data = sensor.read(env)
|
let data = await sensor.read(env)
|
||||||
|
|
||||||
|
|
||||||
#expect(data.value == truth.value, "If values match, we are cool")
|
#expect(data.value == truth.value, "If values match, we are cool")
|
||||||
@ -27,10 +27,10 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature, faulty: true)
|
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature, faulty: true)
|
||||||
let data = sensor.read(env)
|
let data = await sensor.read(env)
|
||||||
|
|
||||||
|
|
||||||
#expect(data.value != truth.value, "If these match, something is not working")
|
#expect(data.value != truth.value, "If these match, something is not working")
|
||||||
@ -43,10 +43,10 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
||||||
let data = sensor.read(env)
|
let data = await sensor.read(env)
|
||||||
|
|
||||||
|
|
||||||
#expect(data.value - truth.value < 10, "If these match, we are cool")
|
#expect(data.value - truth.value < 10, "If these match, we are cool")
|
||||||
@ -61,10 +61,10 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: true, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: true, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
|
||||||
let data = sensor.read(env)
|
let data = await sensor.read(env)
|
||||||
|
|
||||||
|
|
||||||
#expect(data.value - truth.value > 10, "If these match, something is not working")
|
#expect(data.value - truth.value > 10, "If these match, something is not working")
|
||||||
@ -79,22 +79,22 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
||||||
|
|
||||||
let dev: EdgeDevice = await EdgeDevice(
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: 1,
|
deviceID: 1,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
location: Location(x: 20, y: 10, z: 0),
|
location: Location(x: 20, y: 10, z: 0),
|
||||||
dutyCicle: 100098,
|
dutyCicle: 100098,
|
||||||
sensors: [
|
sensors: [
|
||||||
0: Sensor(id: 0, sensorType: DataType.Temperature),
|
0: IdealSensor(id: 0, sensorType: DataType.Temperature),
|
||||||
1: Sensor(id: 0, sensorType: DataType.Temperature),
|
1: IdealSensor(id: 0, sensorType: DataType.Temperature),
|
||||||
2: Sensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
2: IdealSensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
||||||
],
|
],
|
||||||
privateKey: try pem2_P256key(filePath: signKeyPath)
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
@ -121,13 +121,13 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
||||||
|
|
||||||
let dev: EdgeDevice = await EdgeDevice(
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: 1,
|
deviceID: 1,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
|
|||||||
@ -10,9 +10,9 @@ import MessageUtils
|
|||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
IoTSimulatorCore.addEnv(environment: env)
|
await IoTSimulatorCore.addEnv(environment: env)
|
||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ import MessageUtils
|
|||||||
privateKey: try pem2_P256key(filePath: signKeyPath)
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
let dev2: EdgeDevice = await EdgeDevice(
|
let dev2: EdgeDevice = EdgeDevice(
|
||||||
deviceID: 2,
|
deviceID: 2,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -56,7 +56,7 @@ import MessageUtils
|
|||||||
privateKey: try pem2_P256key(filePath: signKeyPath)
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
||||||
print(msg)
|
print(msg)
|
||||||
let _signedMessage = try! deserializeV1(serializedData: msg)
|
let _signedMessage = try! deserializeV1(serializedData: msg)
|
||||||
print(_signedMessage.toString())
|
print(_signedMessage.toString())
|
||||||
@ -64,7 +64,7 @@ import MessageUtils
|
|||||||
failure: {
|
failure: {
|
||||||
print("Something went wrong")
|
print("Something went wrong")
|
||||||
})
|
})
|
||||||
try IoTSimulatorCore.addDevice(location: "Delta", device: dev2, success: { msg in
|
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev2, success: { msg in
|
||||||
print(msg)
|
print(msg)
|
||||||
let _signedMessage = try! deserializeV1(serializedData: msg)
|
let _signedMessage = try! deserializeV1(serializedData: msg)
|
||||||
print(_signedMessage.toString())
|
print(_signedMessage.toString())
|
||||||
@ -79,14 +79,14 @@ import MessageUtils
|
|||||||
|
|
||||||
@Test func stressLoop1() async throws {
|
@Test func stressLoop1() async throws {
|
||||||
|
|
||||||
let devices: UInt128 = 5000
|
let devices: UInt128 = 1000000
|
||||||
|
|
||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
IoTSimulatorCore.addEnv(environment: env)
|
await IoTSimulatorCore.addEnv(environment: env)
|
||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
@ -94,6 +94,8 @@ import MessageUtils
|
|||||||
|
|
||||||
for i: UInt128 in 0..<devices {
|
for i: UInt128 in 0..<devices {
|
||||||
|
|
||||||
|
print("\n\nCreating dev \(i)\n\n")
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: i,
|
deviceID: i,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
@ -114,7 +116,7 @@ import MessageUtils
|
|||||||
privateKey: try pem2_P256key(filePath: signKeyPath)
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(
|
try await IoTSimulatorCore.addDevice(
|
||||||
location: "Delta",
|
location: "Delta",
|
||||||
device: dev,
|
device: dev,
|
||||||
success: { msg in
|
success: { msg in
|
||||||
@ -136,21 +138,21 @@ import MessageUtils
|
|||||||
sleep(1)
|
sleep(1)
|
||||||
}
|
}
|
||||||
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
||||||
IoTSimulatorCore.nukeAll()
|
await IoTSimulatorCore.nukeAll()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func stressLoop2() async throws {
|
@Test func stressLoop2() async throws {
|
||||||
|
|
||||||
let devices: UInt128 = 1
|
let devices: UInt128 = 10
|
||||||
|
|
||||||
let env = PhysicalEnvironment("Delta")
|
let env = PhysicalEnvironment("Delta")
|
||||||
let truth = PhysicalData(.Temperature, 22)
|
let truth = PhysicalData(.Temperature, 22)
|
||||||
|
|
||||||
env.setPhysicalData(DataType.Temperature, truth)
|
await env.setPhysicalData(DataType.Temperature, truth)
|
||||||
|
|
||||||
IoTSimulatorCore.addEnv(environment: env)
|
await IoTSimulatorCore.addEnv(environment: env)
|
||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
@ -178,7 +180,7 @@ import MessageUtils
|
|||||||
privateKey: try pem2_P256key(filePath: signKeyPath)
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
||||||
print(msg)
|
print(msg)
|
||||||
let _signedMessage = try deserializeV1(serializedData: msg)
|
let _signedMessage = try deserializeV1(serializedData: msg)
|
||||||
print(_signedMessage.toString())
|
print(_signedMessage.toString())
|
||||||
@ -193,12 +195,11 @@ import MessageUtils
|
|||||||
for i in 0..<_sleep {
|
for i in 0..<_sleep {
|
||||||
print("Hi, at \(i)s\n\n")
|
print("Hi, at \(i)s\n\n")
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
await IoTSimulatorCore.toggleSensor(devID: UInt.random(in: 0..<UInt(devices)), sensorID: 0)
|
||||||
await IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
||||||
IoTSimulatorCore.nukeAll()
|
await IoTSimulatorCore.nukeAll()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user