-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from fa21-collaborative-drone-interactions/feat…
…ure/sensors-endpoint Modify interface for ApodiniDeploy
- Loading branch information
Showing
10 changed files
with
189 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Apodini | ||
import Foundation | ||
|
||
|
||
struct ConductivitySensor: SensorHandler { | ||
static let dirPath = "data" | ||
static let sensorType: SensorType = .CONDUCTIVITY | ||
static let converter = getMeasurementConverterInstance(sensorType: .CONDUCTIVITY) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
let mountDir = URL(fileURLWithPath: "/buoy") | ||
|
||
func readJSONFromFile<T>(_ type: T.Type, filePath: String) -> T? where T: Decodable { | ||
readJSONFromFile(type, fileURL: mountDir.appendingPathComponent(filePath)) | ||
} | ||
|
||
func readJSONFromFile<T>(_ type: T.Type, fileURL: URL) -> T? where T: Decodable { | ||
guard let data = try? Data(contentsOf: fileURL) else { | ||
return nil | ||
} | ||
return try? JSONDecoder().decode(T.self, from: data) | ||
} | ||
|
||
func readJSONDirectory<T>(_ type: T.Type, dirPath: String) -> [T] where T: Decodable { | ||
guard let files = try? FileManager.default.contentsOfDirectory(at: mountDir.appendingPathComponent(dirPath), | ||
includingPropertiesForKeys: nil) | ||
else { | ||
return [] | ||
} | ||
return files.compactMap {fileURL in | ||
readJSONFromFile(T.self, fileURL: fileURL) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Apodini | ||
|
||
|
||
protocol MeasurementConverter { | ||
func convert(rawValue: Double) -> Double | ||
} | ||
|
||
func getMeasurementConverterInstance(sensorType: SensorType) -> MeasurementConverter { | ||
switch sensorType { | ||
case TemperatureSensor.sensorType: | ||
return PolynomialMeasurementConverter(configFilePath: "sensorconfig/TemperatureSensor.json") | ||
case ConductivitySensor.sensorType: | ||
return PolynomialMeasurementConverter(configFilePath: "sensorconfig/ConductivitySensor.json") | ||
case PhSensor.sensorType: | ||
return PolynomialMeasurementConverter(configFilePath: "sensorconfig/PhSensor.json") | ||
default: | ||
return NoopConverter() | ||
} | ||
} | ||
|
||
struct NoopConverter: MeasurementConverter { | ||
func convert(rawValue: Double) -> Double { | ||
rawValue | ||
} | ||
} | ||
|
||
struct PolynomialMeasurementConverter: MeasurementConverter { | ||
var polynomialCoefficients: [Double] | ||
|
||
|
||
init(configFilePath: String) { | ||
self.polynomialCoefficients = readJSONFromFile([Double].self, filePath: configFilePath) ?? [] | ||
} | ||
|
||
func convert(rawValue: Double) -> Double { | ||
polynomialCoefficients.reduce(0) {result, coeff in | ||
result * rawValue + coeff | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
import Apodini | ||
import Foundation | ||
|
||
|
||
struct Sensor: Handler { | ||
func handle() -> String { | ||
""" | ||
List of sensors: | ||
pH | ||
""" | ||
} | ||
|
||
var content: some Component { | ||
Group("ph") { | ||
PhSensor() | ||
} | ||
func handle() -> [Int] { | ||
readJSONFromFile([Int].self, filePath: "available_sensors.json") ?? [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
import Apodini | ||
import Foundation | ||
|
||
struct MeasurementItem: Content, Decodable { | ||
var sensorID: Int | ||
var sensorType: Int | ||
var measurement: Double | ||
} | ||
|
||
struct SensorDump: Content, Decodable { | ||
var buoyId: Int | ||
var date: String | ||
var location: Location | ||
var measurements: [MeasurementItem] | ||
} | ||
|
||
struct Location: Content, Decodable { | ||
var latitude: Double | ||
var longitude: Double | ||
} | ||
|
||
struct SensorData: Handler { | ||
static let dirPath = "data" | ||
|
||
func handle() -> [SensorDump] { | ||
let dirPath = "/buoy/data" | ||
guard let files = try? FileManager.default.contentsOfDirectory(atPath: dirPath) else { | ||
return [] | ||
} | ||
return files.compactMap {fileName in | ||
guard let data = try? Data(contentsOf: URL(fileURLWithPath: dirPath + "/" + fileName)) else { | ||
return nil | ||
readJSONDirectory(SensorDump.self, dirPath: Self.dirPath) | ||
.map { | ||
SensorDump( | ||
buoyId: $0.buoyId, | ||
date: $0.date, | ||
location: $0.location, | ||
measurements: $0.measurements.map { item in | ||
MeasurementItem( | ||
sensorID: item.sensorID, | ||
sensorType: item.sensorType, | ||
measurement: getMeasurementConverterInstance(sensorType: item.sensorType).convert(rawValue: item.measurement)) | ||
} | ||
) | ||
} | ||
return try? JSONDecoder().decode(SensorDump.self, from: data) | ||
} | ||
|
||
var content: some Component { | ||
Group(TemperatureSensor.sensorType.description) { | ||
TemperatureSensor() | ||
} | ||
|
||
Group(ConductivitySensor.sensorType.description) { | ||
ConductivitySensor() | ||
} | ||
|
||
Group(PhSensor.sensorType.description) { | ||
PhSensor() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Apodini | ||
|
||
struct SensorDump: Content, Decodable { | ||
var buoyId: Int | ||
var date: String | ||
var location: Location | ||
var measurements: [MeasurementItem] | ||
|
||
func filteredBySensorType(_ sensorType: SensorType) -> SensorDump { | ||
SensorDump( | ||
buoyId: self.buoyId, | ||
date: self.date, | ||
location: self.location, | ||
measurements: self.measurements.filter { $0.sensorType == sensorType } | ||
) | ||
} | ||
|
||
func convertMeasurements(_ converter: MeasurementConverter) -> SensorDump { | ||
SensorDump( | ||
buoyId: self.buoyId, | ||
date: self.date, | ||
location: self.location, | ||
measurements: self.measurements.map { item in | ||
MeasurementItem(sensorID: item.sensorID, sensorType: item.sensorType, measurement: converter.convert(rawValue: item.measurement)) | ||
} | ||
) | ||
} | ||
} | ||
|
||
struct Location: Content, Decodable { | ||
var latitude: Double | ||
var longitude: Double | ||
} | ||
|
||
struct MeasurementItem: Content, Decodable { | ||
var sensorID: Int | ||
var sensorType: SensorType | ||
var measurement: Double | ||
} | ||
|
||
enum SensorType: Int, Content, Decodable, CustomStringConvertible { | ||
case TEMPERATURE | ||
case CONDUCTIVITY | ||
case POTENTIAHYDROGENII | ||
|
||
var description: String { | ||
switch self { | ||
case .TEMPERATURE: return "temperature" | ||
case .CONDUCTIVITY: return "conductivity" | ||
case .POTENTIAHYDROGENII: return "ph" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Apodini | ||
|
||
|
||
protocol SensorHandler: Handler { | ||
static var dirPath: String { get } | ||
static var sensorType: SensorType { get } | ||
static var converter: MeasurementConverter { get } | ||
|
||
func handle() -> [SensorDump] | ||
} | ||
|
||
extension SensorHandler { | ||
func handle() -> [SensorDump] { | ||
readJSONDirectory(SensorDump.self, dirPath: Self.dirPath) | ||
.map { $0.filteredBySensorType(Self.sensorType).convertMeasurements(Self.converter) } | ||
.filter { !$0.measurements.isEmpty } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Apodini | ||
import Foundation | ||
|
||
|
||
struct TemperatureSensor: SensorHandler { | ||
static let dirPath = "data" | ||
static let sensorType: SensorType = .TEMPERATURE | ||
static let converter = getMeasurementConverterInstance(sensorType: .TEMPERATURE) | ||
} |
This file was deleted.
Oops, something went wrong.