Added sensors support and implemented other classes
This commit is contained in:
parent
b21f633bb4
commit
0c82f284e8
1
.vscode/settings.json
vendored
Normal file
1
.vscode/settings.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
92
Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift
Normal file
92
Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift
Normal 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
|
||||
|
||||
}
|
||||
11
Sources/IoT-Simulator-Core/Classes/Utils/Field.swift
Normal file
11
Sources/IoT-Simulator-Core/Classes/Utils/Field.swift
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
12
Sources/IoT-Simulator-Core/Classes/Utils/Location3D.swift
Normal file
12
Sources/IoT-Simulator-Core/Classes/Utils/Location3D.swift
Normal 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
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
7
Sources/IoT-Simulator-Core/Enums/MessageType.swift
Normal file
7
Sources/IoT-Simulator-Core/Enums/MessageType.swift
Normal file
@ -0,0 +1,7 @@
|
||||
public enum MessageType {
|
||||
case KeepAlive
|
||||
case Data
|
||||
case Info
|
||||
case Warning
|
||||
case Critical
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user