-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelfhydroState.go
51 lines (45 loc) · 1.64 KB
/
SelfhydroState.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
mqtt "github.com/selfhydro/selfhydro/mqtt"
"github.com/selfhydro/selfhydro/sensors"
)
type SelfHydroState struct {
WaterTemperatureSensor sensors.MQTTTopic
AmbientTemperatureSensor sensors.MQTTTopic
AmbientHumiditySensor sensors.MQTTTopic
ElectricalConductivitySensor sensors.MQTTTopic
}
type state struct {
WaterTemperature float64
AmbientTemperature float64
AmbientHumidity float64
ElectricalConductivity float64
Time string
DeviceId string
}
func NewSelfHydroState(mqtt mqtt.MQTTComms) *SelfHydroState {
waterTemp := SetupSensor(&sensors.WaterTemperature{}, mqtt)
ambientTemperature := SetupSensor(&sensors.AmbientTemperature{}, mqtt)
ambientHumidity := SetupSensor(&sensors.AmbientHumidity{}, mqtt)
electricalConductivity := SetupSensor(&sensors.WaterElectricalConductivity{}, mqtt)
selfHydroState := &SelfHydroState{
WaterTemperatureSensor: waterTemp,
AmbientTemperatureSensor: ambientTemperature,
AmbientHumiditySensor: ambientHumidity,
ElectricalConductivitySensor: electricalConductivity,
}
return selfHydroState
}
func SetupSensor(sensor sensors.MQTTTopic, mqtt mqtt.MQTTComms) sensors.MQTTTopic {
sensor.Subscribe(mqtt)
return sensor
}
func (s SelfHydroState) GetLatestDeviceState() state {
return state{
WaterTemperature: s.WaterTemperatureSensor.GetLatestData(),
AmbientTemperature: s.AmbientTemperatureSensor.GetLatestData(),
AmbientHumidity: s.AmbientHumiditySensor.GetLatestData(),
ElectricalConductivity: s.ElectricalConductivitySensor.GetLatestData(),
DeviceId: "selfhydro",
}
}