V0.7.0 Arroyo Toad

Fixed support integrating MessageUtils Library
This commit is contained in:
Christian Risi
2024-12-09 12:58:26 +00:00
parent 2ebad7f68e
commit 5b358b8bef
16 changed files with 116 additions and 211 deletions

View File

@@ -1,34 +1,36 @@
import Foundation
import Crypto
import DataAcquisition
import Foundation
import MessageUtils
public class EdgeDevice: EdgeDeviceP {
public let deviceID: String
public let deviceID: UInt128
public let deviceType: DeviceType
public let dataType: DataType
public var disconnected: Bool
public var compromised: Bool
public var location: Location3D
public var location: Location
public var dutyCicle: UInt
public var sensors: [Int: Sensor]
public var privateKey: P521.Signing.PrivateKey
public var privateKey: P256.Signing.PrivateKey
private var numberOfSensors: Int {
return sensors.count
}
public init(
deviceID: String,
deviceID: UInt128,
dataType: DataType,
disconnected: Bool,
location: Location3D,
location: Location,
dutyCicle: UInt,
sensors: [Int: Sensor],
privateKey: P521.Signing.PrivateKey
privateKey: P256.Signing.PrivateKey
) {
self.deviceID = deviceID
self.deviceType = DeviceType.EdgeDevice
self.deviceType = DeviceType.EDGE_SENSOR
self.dataType = dataType
self.disconnected = disconnected
self.compromised = false
@@ -46,7 +48,7 @@ public class EdgeDevice: EdgeDeviceP {
self.sensors.removeValue(forKey: id)
}
public func work(envrionment: PhysicalEnvironment) -> Message {
public func work(envrionment: PhysicalEnvironment) throws -> Data {
// UGLY: Declaring here some variables manually, remove them if you want to offere flexibility
let numberOfSamples: Int = 10
@@ -70,11 +72,15 @@ public class EdgeDevice: EdgeDeviceP {
let metrics: Metrics = getMetrics(
rowPointer, Int32(self.sensors.count), Int32(numberOfSamples))
// Todo: END Add Vincenzo's implementation
// UGLY: Supposing everytihing is fine
let msg: Message = Message(
msgType: MessageType.Data,
version: 1,
messageType: MessageType.DATA,
devType: self.deviceType,
RESERVED: 0,
signType: SignType.P256,
timestamp: Date(),
deviceID: self.deviceID,
devID: self.deviceID,
location: self.location,
fields: [
Field(key: "data_type", value: "\(self.dataType)"),
@@ -83,9 +89,14 @@ public class EdgeDevice: EdgeDeviceP {
]
)
msg.signMessage(key: self.privateKey)
var msgData = serializeV1(msg: msg)
let signature = try signMessage(msgData: msgData, signType: msg.signType, key: self.privateKey)
return msg
msgData.append(Data(signature))
return msgData
}
}

View File

@@ -1,16 +0,0 @@
public class Field : Codable{
public let key: String
public let value: String
public init(key: String, value: String) {
self.key = key
self.value = value
}
public var description: String {
return "\(self.key): \t\(self.value)"
}
}

View File

@@ -1,16 +0,0 @@
public class Location3D {
public let x : Float
public let y : Float
public let z : Float
public init(_ x: Float, _ y: Float, _ z : Float) {
self.x = x
self.y = y
self.z = z
}
public var description: String {
return "X: \(self.x)\tY: \(self.y)\tZ: \(self.z)"
}
}

View File

@@ -1,68 +0,0 @@
import Crypto
import Foundation
public class Message {
public let messageType: MessageType
public let timestamp: Date
public let deviceID: String
public let location: Location3D
public let fields: [Field]
public var signature: String {
return self._signature != nil ? self._signature! : "##INVALID"
}
private var _signature: String? = nil
public init(
msgType: MessageType,
timestamp: Date,
deviceID: String,
location: Location3D,
fields: [Field]
) {
self.messageType = msgType
self.timestamp = timestamp
self.deviceID = deviceID
self.location = location
self.fields = fields
}
public func toDataCompatibleString() -> String {
var string: String = "\(self.messageType)"
string += "\(self.timestamp)"
string += "\(self.deviceID)"
string += "\(self.location.x)"
string += "\(self.location.y)"
string += "\(self.location.z)"
for field in self.fields {
string += "\(field.key)\(field.value)"
}
return string
}
public func signMessage(key: P521.Signing.PrivateKey) {
do {
self._signature = try sign(string: self.toDataCompatibleString(), key: key)
} catch {
// Do nothing
}
}
public var description: String {
var string: String = "MessageType: \t\(self.messageType)\n"
string += "Timestamp: \t\(self.timestamp)\n"
string += "DeviceID: \t\(self.deviceID)\n"
string += "Location: \t\(self.location.description)\n"
for field in self.fields {
string += "\(field.description)\n"
}
return string
}
}

View File

@@ -1,4 +0,0 @@
public enum DeviceType : Sendable{
case AsyncEdgeDevice
case EdgeDevice
}

View File

@@ -1,7 +0,0 @@
public enum MessageType {
case KeepAlive
case Data
case Info
case Warning
case Critical
}

View File

@@ -7,7 +7,7 @@ public actor IoTSimulatorCore {
private static var enviroments: [String: PhysicalEnvironment] = Dictionary()
private static var devices: [String: EdgeDeviceP] = Dictionary()
private static var env_dev: [String: Set<String>] = Dictionary()
private static var env_dev: [String: Set<UInt128>] = Dictionary()
private static var dev_tasks: [String: Task<(), Never>] = Dictionary()
public static func addEnv(environment: PhysicalEnvironment) {
@@ -16,7 +16,7 @@ public actor IoTSimulatorCore {
public static func addDevice(location: String, device: EdgeDeviceP) throws {
if let environment = IoTSimulatorCore.enviroments[location] {
IoTSimulatorCore.devices[device.deviceID] = device
IoTSimulatorCore.devices["\(device.deviceID)"] = device
if IoTSimulatorCore.env_dev[location] == nil {
IoTSimulatorCore.env_dev[location] = Set()
@@ -26,12 +26,11 @@ public actor IoTSimulatorCore {
// schedule work
let task = IoTSimulatorCore.schedule(envID: environment.location, deviceID: device.deviceID) { msg in
print("\(msg.description)\n\n")
print("\(msg)\n\n")
} failure: {
print("Something is wrong")
}
IoTSimulatorCore.dev_tasks[device.deviceID] = task
IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task
} else {
throw CoreError.NoEnvironment
@@ -43,6 +42,10 @@ public actor IoTSimulatorCore {
return IoTSimulatorCore.enviroments[name]
}
public static func getDev(devID: UInt128) -> EdgeDeviceP? {
return IoTSimulatorCore.devices["\(devID)"]
}
public static func getDev(devID: String) -> EdgeDeviceP? {
return IoTSimulatorCore.devices[devID]
}
@@ -53,23 +56,27 @@ public actor IoTSimulatorCore {
}
}
private static func schedule(
envID: sending String,
deviceID: sending String,
success: sending @escaping (_ msg: Message) throws -> Void,
deviceID: UInt128,
success: sending @escaping (_ msg: Data) throws -> Void,
failure: sending @escaping () -> Void
) -> Task<(), Never> {
let _devID : String = "\(deviceID)"
return Task {
var notCancelled: Bool = true
let dev = IoTSimulatorCore.getDev(devID: deviceID)!
let dev = IoTSimulatorCore.getDev(devID: _devID)!
let env = IoTSimulatorCore.getEnv(name: envID)!
while notCancelled {
let message = dev.work(envrionment: env)
print("Device: \(dev.deviceID)")
do {
let message = try dev.work(envrionment: env)
try success(message)
} catch {
failure()

View File

@@ -1,16 +1,18 @@
import Crypto
import Foundation
import Crypto
import MessageUtils
public protocol EdgeDeviceP {
var deviceID : String {get}
var deviceID : UInt128 {get}
var deviceType : DeviceType {get}
var dataType : DataType {get}
var location: Location3D {get set}
var location: Location{get set}
var disconnected : Bool {get set}
var compromised : Bool {get set}
var dutyCicle : UInt {get set}
var privateKey: P521.Signing.PrivateKey {get}
var privateKey: P256.Signing.PrivateKey {get}
func work(envrionment: PhysicalEnvironment) -> Message
func work(envrionment: PhysicalEnvironment) throws -> Data
}

View File

@@ -0,0 +1,16 @@
import MessageUtils
extension Field {
public init(
key: String,
value: String
) {
self.init(
key: Array(key.utf8),
value: Array(value.utf8)
)
}
}