V 0.7.3 Arroyo Toad
Swept a bit of dust from code to make it more concurrent safe
This commit is contained in:
parent
9dbde70406
commit
52d5cef335
@ -1,26 +1,24 @@
|
|||||||
import Foundation
|
|
||||||
import Crypto
|
import Crypto
|
||||||
import DataAcquisition
|
import DataAcquisition
|
||||||
|
import Foundation
|
||||||
import MessageUtils
|
import MessageUtils
|
||||||
|
|
||||||
public class EdgeDevice: EdgeDeviceP {
|
public class EdgeDevice: EdgeDeviceP, @unchecked Sendable {
|
||||||
|
|
||||||
|
|
||||||
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 var disconnected: Bool
|
||||||
public var compromised: Bool
|
public var compromised: Bool
|
||||||
public var location: Location
|
public var location: Location
|
||||||
public var dutyCicle: UInt
|
public var dutyCicle: UInt
|
||||||
public var sensors: [Int: Sensor]
|
public var sensors: [Int: Sensor]
|
||||||
public var privateKey: P256.Signing.PrivateKey
|
public var privateKey: P256.Signing.PrivateKey
|
||||||
public var publicKey: P256.Signing.PublicKey {
|
public var publicKey: P256.Signing.PublicKey {
|
||||||
get{
|
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
|
||||||
}
|
}
|
||||||
@ -43,18 +41,25 @@ public class EdgeDevice: EdgeDeviceP {
|
|||||||
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) {
|
||||||
|
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) {
|
||||||
|
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 work(envrionment: PhysicalEnvironment) 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
|
||||||
|
|
||||||
@ -80,7 +85,7 @@ public class EdgeDevice: EdgeDeviceP {
|
|||||||
// UGLY: Supposing everytihing is fine
|
// UGLY: Supposing everytihing is fine
|
||||||
let msg: Message = Message(
|
let msg: Message = Message(
|
||||||
version: 1,
|
version: 1,
|
||||||
messageType: MessageType.DATA,
|
messageType: MessageType.DATA,
|
||||||
devType: self.deviceType,
|
devType: self.deviceType,
|
||||||
RESERVED: 0,
|
RESERVED: 0,
|
||||||
signType: SignType.P256,
|
signType: SignType.P256,
|
||||||
@ -95,11 +100,12 @@ public class EdgeDevice: EdgeDeviceP {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var msgData = serializeV1(msg: msg)
|
var msgData = serializeV1(msg: msg)
|
||||||
let signature = try signMessage(msgData: msgData, signType: msg.signType, key: self.privateKey)
|
let signature = try signMessage(
|
||||||
|
msgData: msgData, signType: msg.signType, key: self.privateKey)
|
||||||
|
|
||||||
msgData.append(Data(signature))
|
msgData.append(Data(signature))
|
||||||
|
|
||||||
|
self.lock.unlock()
|
||||||
|
|
||||||
return msgData
|
return msgData
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,15 @@
|
|||||||
public class PhysicalEnvironment {
|
import Foundation
|
||||||
|
|
||||||
|
public class PhysicalEnvironment: @unchecked Sendable {
|
||||||
|
|
||||||
private var physicalEnvironment: [DataType: PhysicalData]
|
private var physicalEnvironment: [DataType: PhysicalData]
|
||||||
public let location: String
|
public let location: String
|
||||||
|
private let lock: NSLock
|
||||||
|
|
||||||
public init(_ location: String) {
|
public init(_ location: String) {
|
||||||
self.physicalEnvironment = Dictionary()
|
self.physicalEnvironment = Dictionary()
|
||||||
self.location = location
|
self.location = location
|
||||||
|
self.lock = NSLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets speficied datum from environment
|
/// Gets speficied datum from environment
|
||||||
@ -13,19 +17,25 @@ public class PhysicalEnvironment {
|
|||||||
/// - Throws: NoPhysicalDataAvailable
|
/// - Throws: NoPhysicalDataAvailable
|
||||||
/// - Returns: The datum asked for
|
/// - Returns: The datum asked for
|
||||||
public func getPhysicalData(_ dataType: DataType) throws -> PhysicalData {
|
public func getPhysicalData(_ dataType: DataType) throws -> PhysicalData {
|
||||||
|
self.lock.lock()
|
||||||
guard let result: PhysicalData = self.physicalEnvironment[dataType] else {
|
guard let result: PhysicalData = self.physicalEnvironment[dataType] else {
|
||||||
throw CoreError.NoPhysicalDataAvailable(dataType: "\(dataType)")
|
throw CoreError.NoPhysicalDataAvailable(dataType: "\(dataType)")
|
||||||
}
|
}
|
||||||
|
self.lock.unlock()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setPhysicalData(_ dataType: DataType, _ value: PhysicalData) {
|
public func setPhysicalData(_ dataType: DataType, _ value: PhysicalData) {
|
||||||
|
self.lock.lock()
|
||||||
self.physicalEnvironment[dataType] = value
|
self.physicalEnvironment[dataType] = value
|
||||||
|
self.lock.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removePhysicalData(_ dataType: DataType) -> PhysicalData? {
|
public func removePhysicalData(_ dataType: DataType) -> PhysicalData? {
|
||||||
return self.physicalEnvironment.removeValue(forKey: dataType)
|
self.lock.lock()
|
||||||
|
let data = self.physicalEnvironment.removeValue(forKey: dataType)
|
||||||
|
self.lock.unlock()
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
public enum DataType : String{
|
public enum DataType : String {
|
||||||
case Temperature
|
case Temperature
|
||||||
case Humidity
|
case Humidity
|
||||||
case Scan
|
case Scan
|
||||||
|
|||||||
@ -7,41 +7,43 @@ 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: UInt128,
|
||||||
dataType: DataType,
|
dataType: DataType,
|
||||||
privateKey: P256.Signing.PrivateKey
|
privateKey: P256.Signing.PrivateKey
|
||||||
) throws -> EdgeDevice{
|
) throws -> EdgeDevice {
|
||||||
return try DeviceFactory.createEdgeDevice(
|
return try DeviceFactory.createEdgeDevice(
|
||||||
deviceID: deviceID,
|
deviceID: deviceID,
|
||||||
dataType: dataType,
|
dataType: dataType,
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
realSensor: false
|
realSensor: false
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
public static func createEdgeDevice(
|
public static func createEdgeDevice(
|
||||||
deviceID: UInt128,
|
deviceID: UInt128,
|
||||||
dataType: DataType,
|
dataType: DataType,
|
||||||
privateKey: P256.Signing.PrivateKey,
|
privateKey: P256.Signing.PrivateKey,
|
||||||
realSensor: Bool
|
realSensor: Bool
|
||||||
) throws -> EdgeDevice{
|
) throws -> EdgeDevice {
|
||||||
|
|
||||||
return try DeviceFactory.createEdgeDevice(
|
return try DeviceFactory.createEdgeDevice(
|
||||||
deviceID: deviceID,
|
deviceID: deviceID,
|
||||||
dataType: dataType,
|
dataType: dataType,
|
||||||
location: Location(
|
location: Location(
|
||||||
x: UInt64.random(in: 0...10000),
|
x: UInt64.random(in: 0...10000),
|
||||||
y: UInt64.random(in: 0...10000),
|
y: UInt64.random(in: 0...10000),
|
||||||
z: UInt64.random(in: 0...10000)
|
z: UInt64.random(in: 0...10000)
|
||||||
),
|
),
|
||||||
dutyCicle: UInt.random(in: 0...5000),
|
dutyCicle: UInt.random(in: 0...5000),
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
realSensor: realSensor
|
realSensor: realSensor
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
public static func createEdgeDevice(
|
public static func createEdgeDevice(
|
||||||
deviceID: UInt128,
|
deviceID: UInt128,
|
||||||
dataType: DataType,
|
dataType: DataType,
|
||||||
@ -58,19 +60,25 @@ public actor DeviceFactory {
|
|||||||
DeviceFactory.availableIDs.remove(deviceID)
|
DeviceFactory.availableIDs.remove(deviceID)
|
||||||
|
|
||||||
// Create sensors here
|
// Create sensors here
|
||||||
let sensors : [Int: Sensor]
|
let sensors: [Int: Sensor]
|
||||||
|
|
||||||
if !realSensor {
|
if !realSensor {
|
||||||
sensors = [
|
sensors = [
|
||||||
0: Sensor(id: 0, sensorType: dataType),
|
0: Sensor(id: 0, sensorType: dataType),
|
||||||
1: Sensor(id: 1, sensorType: dataType),
|
1: Sensor(id: 1, sensorType: dataType),
|
||||||
2: Sensor(id: 2, sensorType: dataType)
|
2: Sensor(id: 2, sensorType: dataType),
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
sensors = [
|
sensors = [
|
||||||
0: RealSensor(sensorID: 0, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8),
|
0: RealSensor(
|
||||||
1: RealSensor(sensorID: 1, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8),
|
sensorID: 0, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
|
||||||
2: RealSensor(sensorID: 2, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8)
|
quantizationBits: 8),
|
||||||
|
1: RealSensor(
|
||||||
|
sensorID: 1, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
|
||||||
|
quantizationBits: 8),
|
||||||
|
2: RealSensor(
|
||||||
|
sensorID: 2, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
|
||||||
|
quantizationBits: 8),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -73,6 +73,7 @@ public actor IoTSimulatorCore {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool {
|
public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool {
|
||||||
|
|
||||||
// UGLY: Should throw
|
// UGLY: Should throw
|
||||||
@ -108,11 +109,11 @@ public actor IoTSimulatorCore {
|
|||||||
|
|
||||||
var notCancelled: Bool = true
|
var notCancelled: Bool = true
|
||||||
|
|
||||||
let dev = IoTSimulatorCore.devices["\(_devID)"]!
|
let dev = IoTSimulatorCore.getDev(devID: _devID)!
|
||||||
let env = IoTSimulatorCore.enviroments[envID]!
|
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 dev.work(envrionment: env)
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import Foundation
|
|||||||
import Crypto
|
import Crypto
|
||||||
import MessageUtils
|
import MessageUtils
|
||||||
|
|
||||||
public protocol EdgeDeviceP {
|
public protocol EdgeDeviceP : Sendable{
|
||||||
|
|
||||||
var deviceID : UInt128 {get}
|
var deviceID : UInt128 {get}
|
||||||
var deviceType : DeviceType {get}
|
var deviceType : DeviceType {get}
|
||||||
|
|||||||
@ -85,7 +85,7 @@ import MessageUtils
|
|||||||
|
|
||||||
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = await EdgeDevice(
|
||||||
deviceID: 1,
|
deviceID: 1,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -96,10 +96,10 @@ import MessageUtils
|
|||||||
1: Sensor(id: 0, sensorType: DataType.Temperature),
|
1: Sensor(id: 0, sensorType: DataType.Temperature),
|
||||||
2: Sensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
2: Sensor(id: 0, sensorType: DataType.Temperature, faulty: true)
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
let message = try dev.work(envrionment: env)
|
let message = try await dev.work(envrionment: env)
|
||||||
|
|
||||||
let signedMessage = try deserializeV1(serializedData: message)
|
let signedMessage = try deserializeV1(serializedData: message)
|
||||||
|
|
||||||
@ -127,7 +127,7 @@ import MessageUtils
|
|||||||
|
|
||||||
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = await EdgeDevice(
|
||||||
deviceID: 1,
|
deviceID: 1,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -138,10 +138,10 @@ import MessageUtils
|
|||||||
1: RealSensor(sensorID: 1, 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),
|
2: RealSensor(sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
let message = try dev.work(envrionment: env)
|
let message = try await dev.work(envrionment: env)
|
||||||
|
|
||||||
let signedMessage = try deserializeV1(serializedData: message)
|
let signedMessage = try deserializeV1(serializedData: message)
|
||||||
|
|
||||||
|
|||||||
@ -16,8 +16,6 @@ import MessageUtils
|
|||||||
|
|
||||||
let signKeyPath = "./Private/privateKey.pem"
|
let signKeyPath = "./Private/privateKey.pem"
|
||||||
|
|
||||||
let privateKey = try pem2_P256key(filePath: signKeyPath)
|
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: 1,
|
deviceID: 1,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
@ -35,10 +33,10 @@ import MessageUtils
|
|||||||
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
||||||
quantizationBits: 3),
|
quantizationBits: 3),
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
let dev2: EdgeDevice = EdgeDevice(
|
let dev2: EdgeDevice = await EdgeDevice(
|
||||||
deviceID: 2,
|
deviceID: 2,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -55,7 +53,7 @@ import MessageUtils
|
|||||||
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
||||||
quantizationBits: 3),
|
quantizationBits: 3),
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
||||||
@ -96,7 +94,7 @@ import MessageUtils
|
|||||||
|
|
||||||
for i: UInt128 in 0..<devices {
|
for i: UInt128 in 0..<devices {
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: i,
|
deviceID: i,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -113,7 +111,7 @@ import MessageUtils
|
|||||||
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
||||||
quantizationBits: 3),
|
quantizationBits: 3),
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(
|
try IoTSimulatorCore.addDevice(
|
||||||
@ -128,9 +126,10 @@ import MessageUtils
|
|||||||
print("Something went wrong")
|
print("Something went wrong")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let _sleep = 15
|
let _sleep = 60
|
||||||
|
|
||||||
for i in 0..<_sleep {
|
for i in 0..<_sleep {
|
||||||
print("Hi, at \(i)s\n\n")
|
print("Hi, at \(i)s\n\n")
|
||||||
@ -159,7 +158,7 @@ import MessageUtils
|
|||||||
|
|
||||||
for i: UInt128 in 0..<devices {
|
for i: UInt128 in 0..<devices {
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
let dev: EdgeDevice = await EdgeDevice(
|
||||||
deviceID: i,
|
deviceID: i,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@ -176,12 +175,12 @@ import MessageUtils
|
|||||||
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
|
||||||
quantizationBits: 3),
|
quantizationBits: 3),
|
||||||
],
|
],
|
||||||
privateKey: privateKey
|
privateKey: try pem2_P256key(filePath: signKeyPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
|
try 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())
|
||||||
},
|
},
|
||||||
failure: {
|
failure: {
|
||||||
@ -195,7 +194,7 @@ import MessageUtils
|
|||||||
print("Hi, at \(i)s\n\n")
|
print("Hi, at \(i)s\n\n")
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
|
||||||
IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
|
await IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
|
||||||
|
|
||||||
}
|
}
|
||||||
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user