Added sensors support and implemented other classes

This commit is contained in:
Christian Risi 2024-11-29 19:07:37 +00:00
parent b21f633bb4
commit 0c82f284e8
9 changed files with 151 additions and 1 deletions

1
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,92 @@
public class Sensor {
public let sensorID: Int
public let sensorType: DataType
public var faulty: Bool
public init(id: Int, sensorType: DataType, faulty: Bool) {
self.sensorID = id
self.sensorType = sensorType
self.faulty = faulty
}
public init(id: Int, sensorType: DataType) {
self.sensorID = id
self.sensorType = sensorType
self.faulty = false
}
public func read(_ environment: PhysicalEnvironment) -> PhysicalData {
let datum = self._read(environment)
return self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
}
internal func _read(_ environment: PhysicalEnvironment) -> PhysicalData{
let datum : PhysicalData
do {
datum = try 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 class RealSensor: Sensor {
public var meanNoise: Float { get{ return _meanNoise}}
public var stdNoise: Float { get{ return _stdNoise}}
public var quantizationBits: Int { get{ return _quantizationBits}}
private var _meanNoise: Float
private var _stdNoise: Float
private let _quantizationBits: Int
public init(
sensorID: Int,
sensorType: DataType,
faulty: Bool,
meanNoise: Float,
stdNoise: Float,
quantizationBits: Int
) {
self._meanNoise = meanNoise
self._stdNoise = stdNoise
self._quantizationBits = quantizationBits
super.init(id: sensorID, sensorType: sensorType, faulty: faulty)
}
public init(
sensorID: Int,
sensorType: DataType,
meanNoise: Float,
stdNoise: Float,
quantizationBits: Int
) {
self._meanNoise = meanNoise
self._stdNoise = stdNoise
self._quantizationBits = quantizationBits
super.init(id: sensorID, sensorType: sensorType)
}
// TODO: Override and implement read with Vincenzo lib here
}

View File

@ -0,0 +1,11 @@
public class Field {
public let key: String
public let value: String
public init(key: String, value: String) {
self.key = key
self.value = value
}
}

View File

@ -0,0 +1,12 @@
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
}
}

View File

@ -1,3 +1,26 @@
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 let signature : [UInt8]
public init(
msgType: MessageType,
timestamp: Date,
deviceID: String,
location: Location3D,
fields: [Field],
signature: [UInt8]
) {
self.messageType = msgType
self.timestamp = timestamp
self.deviceID = deviceID
self.location = location
self.fields = fields
self.signature = signature
}
}

View File

@ -3,11 +3,15 @@
public class PhysicalData {
public let type: DataType
public let value: Float
public let value: Float
public init(_ dataType: DataType, _ value: Float) {
self.type = dataType
self.value = value
}
public static func + (left: PhysicalData, right: Float) -> PhysicalData {
return PhysicalData(left.type, left.value + right)
}
}

View File

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