src.dualinventive.com/go/dinet/rpc/objects_sensor.go

202 lines
8.4 KiB
Go

package rpc
import (
"fmt"
"src.dualinventive.com/go/dinet/ditime"
)
// Sensor is a DI-Net sensor
type Sensor struct {
uid uint16
label string
typeName string
enum map[string]int
}
// GPSSensorData is the structure for sensor-type 'gps'
type GPSSensorData struct {
HDOP float64 `json:"hdop" msgpack:"hdop"`
Latitude float64 `json:"latitude" msgpack:"latitude"`
Longitude float64 `json:"longitude" msgpack:"longitude"`
}
// RTSDataKey returns the realtime status sensor:data key
func (s *Sensor) RTSDataKey() string {
return fmt.Sprintf("%s:%d:data", ClassSensor, s.UID())
}
// RTSInfoKey returns the realtime status sensor:info key
func (s *Sensor) RTSInfoKey() string {
return fmt.Sprintf("%s:%d:info", ClassSensor, s.UID())
}
// UID returns the unique identifier of a Sensor
func (s *Sensor) UID() uint16 {
return s.uid
}
// Label returns the label for a sensor
func (s *Sensor) Label() string {
return s.label
}
// Data returns a value object, optionally an alternative time can be provided
func (s *Sensor) Data(val interface{}, t ...ditime.Time) *ResultValueItem {
tm := ditime.Now()
if len(t) > 0 {
tm = t[0]
}
return &ResultValueItem{
UID: s.uid,
Time: tm,
Value: val,
}
}
// Info returns a info object
func (s *Sensor) Info() ResultInfoItem {
r := ResultInfoItem{
UID: s.uid,
Label: s.label,
Type: s.typeName,
}
if len(s.enum) > 0 {
r.Enum = s.enum
}
return r
}
// BatteryState is a battery state type
type BatteryState int
const (
// BatteryStateRemoved indicates that the battery is removed
BatteryStateRemoved BatteryState = 0
// BatteryStateEmpty indicates that the battery is empty
BatteryStateEmpty BatteryState = 1
// BatteryStateCritical indicates that the battery is critical
BatteryStateCritical BatteryState = 2
// BatteryStateLow indicates that the battery is low
BatteryStateLow BatteryState = 3
// BatteryStateHalf indicates that the battery is half full
BatteryStateHalf BatteryState = 4
// BatteryStateFull indicates that the battery is full
BatteryStateFull BatteryState = 5
)
//nolint: gochecknoglobals
var batteryEnum = map[string]int{
"removed": int(BatteryStateRemoved),
"empty": int(BatteryStateEmpty),
"crit": int(BatteryStateCritical),
"low": int(BatteryStateLow),
"half": int(BatteryStateHalf),
"full": int(BatteryStateFull),
}
// KeyswitchState is a keyswitch state type
type KeyswitchState int
const (
// KeyswitchStateUnknown indicates that the keyswitch is in an unknown position
KeyswitchStateUnknown KeyswitchState = 0
// KeyswitchStateOperational indicates that the keyswitch is in the operational position
KeyswitchStateOperational KeyswitchState = 1
// KeyswitchStateOn indicates that the keyswitch is in the on position
KeyswitchStateOn KeyswitchState = 2
// KeyswitchStateOff indicates that the keyswitch is in the off position
KeyswitchStateOff KeyswitchState = 3
)
//nolint: gochecknoglobals
var keyswitchEnum = map[string]int{
"unknown": int(KeyswitchStateUnknown),
"operational": int(KeyswitchStateOperational),
"on": int(KeyswitchStateOn),
"off": int(KeyswitchStateOff),
}
// ChargerState is a charger state type
type ChargerState int
const (
// ChargerStateDisconnected indicates that the charger is disconnected
ChargerStateDisconnected ChargerState = 0
// ChargerStateConnected indicates that the charger is connected, but not charging
ChargerStateConnected ChargerState = 1
// ChargerStateCharging indicates that the charger is connected and charging
ChargerStateCharging ChargerState = 2
// ChargerStateError indicates that there is a charger error
ChargerStateError ChargerState = 3
// ChargerStateUnknown indicates that the charger is unknown
ChargerStateUnknown ChargerState = 255
)
//nolint: gochecknoglobals
var chargerEnum = map[string]int{
"disconnected": int(ChargerStateDisconnected),
"connected": int(ChargerStateConnected),
"charging": int(ChargerStateCharging),
"error": int(ChargerStateError),
"unknown": int(ChargerStateUnknown),
}
// SectionsShort is the struct for ZKLRCSensorSectionsShort and ZKLRCSensorSectionsBattery
type SectionsShort struct {
// State is true when all the sections are true
State bool `json:"state" msgpack:"state"`
Section1 bool `json:"section_1" msgpack:"section_1"`
Section2 bool `json:"section_2" msgpack:"section_2"`
Section3 bool `json:"section_3" msgpack:"section_3"`
Section4 bool `json:"section_4" msgpack:"section_4"`
}
// nolint: golint, gochecknoglobals
var (
SensorBattery1Voltage = Sensor{uid: 1, label: "bat1-voltage", typeName: "number"}
SensorBattery1State = Sensor{uid: 2, label: "bat1-state", typeName: "enum", enum: batteryEnum}
SensorBattery2Voltage = Sensor{uid: 3, label: "bat2-voltage", typeName: "number"}
SensorBattery2State = Sensor{uid: 4, label: "bat2-state", typeName: "enum", enum: batteryEnum}
SensorBattery3Voltage = Sensor{uid: 5, label: "bat3-voltage", typeName: "number"}
SensorBattery3State = Sensor{uid: 6, label: "bat3-state", typeName: "enum", enum: batteryEnum}
SensorBattery4Voltage = Sensor{uid: 7, label: "bat4-voltage", typeName: "number"}
SensorBattery4State = Sensor{uid: 8, label: "bat4-state", typeName: "enum", enum: batteryEnum}
SensorCharger1Voltage = Sensor{uid: 9, label: "charger1-voltage", typeName: "number"}
SensorCharger1State = Sensor{uid: 10, label: "charger1-state", typeName: "enum", enum: chargerEnum}
SensorCharger2Voltage = Sensor{uid: 11, label: "charger2-voltage", typeName: "number"}
SensorCharger2State = Sensor{uid: 12, label: "charger2-state", typeName: "enum", enum: chargerEnum}
SensorGPS = Sensor{uid: 13, label: "gps", typeName: "gps"}
SensorRSSI = Sensor{uid: 14, label: "rssi", typeName: "number"}
SensorBER = Sensor{uid: 15, label: "ber", typeName: "number"}
GatewaySensorTemperature1 = Sensor{uid: 100, label: "temperature1", typeName: "number"}
CRMSensorTemperature1 = Sensor{uid: 100, label: "temperature1", typeName: "number"}
CRMSensorTemperature2 = Sensor{uid: 101, label: "temperature2", typeName: "number"}
CRMSensorAcceleration = Sensor{uid: 102, label: "acceleration", typeName: "struct"}
CRMSensorCapTouch = Sensor{uid: 103, label: "cap-touch", typeName: "number"}
CRTMSensorTemperature1 = Sensor{uid: 100, label: "temperature1", typeName: "number"}
CRTMSensorTemperature2 = Sensor{uid: 101, label: "temperature2", typeName: "number"}
CRTMSensorRailContact = Sensor{uid: 102, label: "rail-contact", typeName: "bool"}
CRTMSensorRailContactSleep = Sensor{uid: 103, label: "rail-contact-sleep", typeName: "bool"}
CRTMSensorAcceleration = Sensor{uid: 104, label: "acceleration", typeName: "struct"}
DNCMSensorTemperature = Sensor{uid: 100, label: "dncm-temp", typeName: "number"}
DUMSensorButton = Sensor{uid: 100, label: "du-manual", typeName: "number"}
DUUSensorUltrasonic = Sensor{uid: 100, label: "du-ultrasonic", typeName: "number"}
DUUSensorPosTilt = Sensor{uid: 101, label: "duu-pos-tilt", typeName: "struct"}
WUMSensorAlarmButton = Sensor{uid: 100, label: "wu-alarm-button", typeName: "number"}
ZKLSensorDetectionQuality = Sensor{uid: 100, label: "detection-quality", typeName: "number"}
ZKLSensorDetection = Sensor{uid: 101, label: "detection-status", typeName: "bool"}
ZKLSensorMeasurement = Sensor{uid: 102, label: "measurement", typeName: "bool"}
ZKLSensorBa = Sensor{uid: 103, label: "ba", typeName: "number"}
ZKLSensorFrequency = Sensor{uid: 104, label: "frequency", typeName: "number"}
ZKLSensorRMS = Sensor{uid: 107, label: "rms", typeName: "number"}
ZKLSensorBAAutocal = Sensor{uid: 108, label: "ba-autocal", typeName: "number"}
ZKLSensorTempOnboard = Sensor{uid: 109, label: "temp-onboard", typeName: "number"}
ZKLSensorTempNTC = Sensor{uid: 110, label: "temp-ntc", typeName: "number"}
ZKLRCSensorSectionsShort = Sensor{uid: 105, label: "sw-short", typeName: "struct"}
ZKLRCSensorSectionsBattery = Sensor{uid: 106, label: "sw-battery", typeName: "struct"}
ZKLRCSensorSwitchShort = Sensor{uid: 150, label: "switch-state", typeName: "bool"}
ZKLRCSensorKeySwitch = Sensor{uid: 151, label: "keyswitch", typeName: "enum", enum: keyswitchEnum}
ReedSensorContactClosed = Sensor{uid: 102, label: "contact-closed", typeName: "bool"}
)