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