package device import ( "time" "src.dualinventive.com/go/cp3000-interface/internal/statusupdate" "src.dualinventive.com/go/dinet/ditime" "src.dualinventive.com/go/dinet/rpc" "src.dualinventive.com/go/lib/cp3000" ) // CrtmSensor is a ZKL 3000 RC device type CrtmSensor struct { *Base } func (z *CrtmSensor) construct() { z.decoder = statusupdate.NewUpdateDecoder(z.dev.Logger()) z.dev.Subscribe(rpc.ClassMethodSensorInfo, rpc.MsgTypeRequest, z.sensorInfo) z.dev.AddConfig(rpc.ConfigEndpoint, z.configGetEndpoint, z.configSetEndpoint, nil) } // nolint: dupl func (z *CrtmSensor) init() { } // Statusupdate is a callback for the $STAT message // Suppress the linter because statusUpdateCmd needs to satisfy the interface //nolint: unparam func (z *CrtmSensor) statusUpdateCmd(r cp3000.Router, args []string) error { old := z.decoder.StatusUpdate() err := z.decoder.Decode(args) if err != nil { return err } s := z.decoder.StatusUpdate() data := []*rpc.ResultValueItem{} data = old.CheckBattery1(data, s) data = old.CheckBattery1State(data, s) data = old.CheckRSSI(data, s) data = old.CheckBER(data, s) data = old.CheckCRTMTemperature1(data, s) data = old.CheckCRTMTemperature2(data, s) data = old.CheckRailContact(data, s) data = old.CheckRailContactSleep(data, s) data = old.CheckTilt(data, s) if len(data) > 0 { z.dev.Logger().WithField("count", len(data)).Debug("send sensor data") err = z.dev.PublishSensorData(ditime.Now(), data) } if pErr := z.dev.SendDeviceData(); pErr != nil { return pErr } return err } // Versions returns the firmware and hardware versions func (z *CrtmSensor) Versions() (rpc.VersionMap, rpc.ErrorCode) { mcu, rpcerr := z.requestVersion("FW-MCU") if rpcerr != rpc.Ok { return nil, rpcerr } return rpc.VersionMap{ rpc.VersionKeyFwCRTM: mcu, rpc.VersionKeyHwCRTM: z.hwVersion, }, rpc.Ok } // TimeoutDuration returns when the device should be considered offline func (z *CrtmSensor) TimeoutDuration() time.Duration { return time.Minute } // Transport returns the transportation type func (z *CrtmSensor) Transport() (interface{}, rpc.ErrorCode) { return &rpc.ConnectionInfoXBee{}, rpc.Ok } // sensorInfo returns all the sensor information func (z *CrtmSensor) sensorInfo(req *rpc.Msg) *rpc.Msg { rep := req.CreateReply() rep.Result = rpc.NewResult([]rpc.ResultInfoItem{ rpc.SensorBattery1Voltage.Info(), rpc.SensorBattery1State.Info(), rpc.SensorRSSI.Info(), rpc.SensorBER.Info(), rpc.CRTMSensorTemperature1.Info(), rpc.CRTMSensorTemperature2.Info(), rpc.CRTMSensorRailContact.Info(), rpc.CRTMSensorRailContactSleep.Info(), rpc.CRTMSensorAcceleration.Info(), }) return rep } // RegisterCallbacks registers additional callbacks for CRTM 3000 devices once they authenticated func (z *CrtmSensor) RegisterCallbacks() error { z.client.Register(cp3000.CommandUpdate, z.statusUpdateCmd) return nil }