Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d05a2c3767 | ||
|
|
2ef8cc868b | ||
|
|
811bfcd441 | ||
|
|
06a5dd9c4a | ||
|
|
e0921f4ca4 | ||
|
|
52d5cef335 | ||
|
|
9dbde70406 | ||
|
|
8c3b36967f |
@@ -1,24 +1,23 @@
|
|||||||
import Foundation
|
|
||||||
import Crypto
|
import Crypto
|
||||||
import DataAcquisition
|
import DataAcquisition
|
||||||
|
import Foundation
|
||||||
import MessageUtils
|
import MessageUtils
|
||||||
|
|
||||||
public class EdgeDevice: EdgeDeviceP {
|
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 {
|
||||||
get{
|
return self.privateKey.publicKey
|
||||||
return self.privateKey.publicKey
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var numberOfSensors: Int {
|
private var numberOfSensors: Int {
|
||||||
@@ -43,17 +42,54 @@ public class EdgeDevice: EdgeDeviceP {
|
|||||||
self.dutyCicle = dutyCicle
|
self.dutyCicle = dutyCicle
|
||||||
self.sensors = sensors
|
self.sensors = sensors
|
||||||
self.privateKey = privateKey
|
self.privateKey = privateKey
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func addSensor(sensor: Sensor) {
|
public func addSensor(sensor: Sensor) async {
|
||||||
self.sensors[numberOfSensors + 1] = sensor
|
self.sensors[numberOfSensors + 1] = sensor
|
||||||
}
|
}
|
||||||
|
|
||||||
public func removeSensor(id: Int) {
|
public func removeSensor(id: Int) async {
|
||||||
self.sensors.removeValue(forKey: id)
|
self.sensors.removeValue(forKey: id)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
@@ -70,7 +106,7 @@ public class EdgeDevice: EdgeDeviceP {
|
|||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +116,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,7 +131,8 @@ 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))
|
||||||
|
|
||||||
|
|||||||
@@ -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,50 +30,59 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
private let _quantizationBits: Int
|
private let _quantizationBits: Int
|
||||||
// TODO: add a generator of GaussianRNG
|
// TODO: add a generator of GaussianRNG
|
||||||
private var _gaussianNoise: GaussianRNG
|
private var _gaussianNoise: GaussianRNG
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
@@ -70,35 +91,72 @@ public class RealSensor: Sensor {
|
|||||||
faulty: Bool,
|
faulty: Bool,
|
||||||
meanNoise: Float,
|
meanNoise: Float,
|
||||||
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,
|
||||||
stdNoise: Float,
|
stdNoise: Float,
|
||||||
quantizationBits: Int
|
quantizationBits: Int
|
||||||
) {
|
) {
|
||||||
self.init(
|
self.init(
|
||||||
sensorID: sensorID,
|
sensorID: sensorID,
|
||||||
sensorType: sensorType,
|
sensorType: sensorType,
|
||||||
faulty: false,
|
faulty: false,
|
||||||
meanNoise: meanNoise,
|
meanNoise: meanNoise,
|
||||||
stdNoise: stdNoise,
|
stdNoise: stdNoise,
|
||||||
quantizationBits: quantizationBits
|
quantizationBits: quantizationBits
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
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,11 +1,15 @@
|
|||||||
public class PhysicalEnvironment {
|
import Foundation
|
||||||
|
|
||||||
|
public actor PhysicalEnvironment {
|
||||||
|
|
||||||
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,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,48 +7,47 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
dataType: dataType,
|
dataType: dataType,
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
realSensor: false
|
realSensor: false
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
||||||
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
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@@ -58,19 +57,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: 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 = [
|
||||||
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),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,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 {
|
||||||
@@ -92,7 +98,7 @@ public actor DeviceFactory {
|
|||||||
throw CoreError.FinishedIDs
|
throw CoreError.FinishedIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
return DeviceFactory.availableIDs.removeFirst()
|
return DeviceFactory.availableIDs.first!
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -17,7 +18,7 @@ public actor IoTSimulatorCore {
|
|||||||
public static func addDevice(
|
public static func addDevice(
|
||||||
location: String,
|
location: String,
|
||||||
device: EdgeDeviceP,
|
device: EdgeDeviceP,
|
||||||
success: sending @escaping (_ msg: Data) throws -> Void,
|
success: sending @escaping (_ msg: Data) async throws -> Void,
|
||||||
failure: sending @escaping () -> Void
|
failure: sending @escaping () -> Void
|
||||||
) throws {
|
) throws {
|
||||||
if let environment = IoTSimulatorCore.enviroments[location] {
|
if let environment = IoTSimulatorCore.enviroments[location] {
|
||||||
@@ -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,64 +68,60 @@ public actor IoTSimulatorCore {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool {
|
public static func toggleSensor(devID: UInt, sensorID: Int) async -> Bool {
|
||||||
|
|
||||||
|
let a = IoTSimulatorCore.getDev(devID: UInt128(devID))!
|
||||||
|
|
||||||
|
|
||||||
// UGLY: Should throw
|
let device = IoTSimulatorCore.getDev(devID: UInt128(devID))! as! EdgeDevice
|
||||||
if IoTSimulatorCore.getDev(devID: devID) == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: Should it throw?
|
await device.setSensorFaulty(id: sensorID)
|
||||||
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
|
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) 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.devices["\(_devID)"]!
|
|
||||||
let env = IoTSimulatorCore.enviroments[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 success(message)
|
try await success(message)
|
||||||
} catch {
|
} catch {
|
||||||
failure()
|
failure()
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,24 @@ 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}
|
||||||
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,27 +79,27 @@ 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 = 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: 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)
|
||||||
|
|
||||||
@@ -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 = EdgeDevice(
|
let dev: EdgeDevice = 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)
|
||||||
|
|
||||||
|
|||||||
@@ -10,14 +10,12 @@ 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"
|
||||||
|
|
||||||
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 = EdgeDevice(
|
||||||
deviceID: 2,
|
deviceID: 2,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@@ -55,10 +53,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)
|
||||||
)
|
)
|
||||||
|
|
||||||
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())
|
||||||
@@ -66,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())
|
||||||
@@ -81,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"
|
||||||
|
|
||||||
@@ -96,7 +94,9 @@ import MessageUtils
|
|||||||
|
|
||||||
for i: UInt128 in 0..<devices {
|
for i: UInt128 in 0..<devices {
|
||||||
|
|
||||||
let dev: EdgeDevice = EdgeDevice(
|
print("\n\nCreating dev \(i)\n\n")
|
||||||
|
|
||||||
|
let dev: EdgeDevice = EdgeDevice(
|
||||||
deviceID: i,
|
deviceID: i,
|
||||||
dataType: .Temperature,
|
dataType: .Temperature,
|
||||||
disconnected: false,
|
disconnected: false,
|
||||||
@@ -113,10 +113,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)
|
||||||
)
|
)
|
||||||
|
|
||||||
try IoTSimulatorCore.addDevice(
|
try await IoTSimulatorCore.addDevice(
|
||||||
location: "Delta",
|
location: "Delta",
|
||||||
device: dev,
|
device: dev,
|
||||||
success: { msg in
|
success: { msg in
|
||||||
@@ -128,30 +128,31 @@ 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")
|
||||||
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"
|
||||||
|
|
||||||
@@ -159,7 +160,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 +177,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 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())
|
||||||
},
|
},
|
||||||
failure: {
|
failure: {
|
||||||
@@ -194,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)
|
||||||
IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
|
||||||
IoTSimulatorCore.nukeAll()
|
await IoTSimulatorCore.nukeAll()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user