github.com/simpleiot/simpleiot@v0.18.3/node/modbus-io-node.go (about) 1 package node 2 3 import ( 4 "errors" 5 6 "github.com/simpleiot/simpleiot/data" 7 ) 8 9 // ModbusIONode describes a modbus IO db node 10 type ModbusIONode struct { 11 nodeID string 12 description string 13 id int 14 address int 15 modbusIOType string 16 modbusDataType string 17 readOnly bool 18 scale float64 19 offset float64 20 value float64 21 valueSet float64 22 disabled bool 23 errorCount int 24 errorCountCRC int 25 errorCountEOF int 26 errorCountReset bool 27 errorCountCRCReset bool 28 errorCountEOFReset bool 29 } 30 31 // NewModbusIONode Convert node to modbus IO node 32 func NewModbusIONode(busType string, node *data.NodeEdge) (*ModbusIONode, error) { 33 ret := ModbusIONode{ 34 nodeID: node.ID, 35 } 36 37 var ok bool 38 39 ret.id, ok = node.Points.ValueInt(data.PointTypeID, "") 40 if busType == data.PointValueClient && !ok { 41 if busType == data.PointValueServer { 42 return nil, errors.New("Must define modbus ID") 43 } 44 } 45 46 ret.description, _ = node.Points.Text(data.PointTypeDescription, "") 47 48 ret.address, ok = node.Points.ValueInt(data.PointTypeAddress, "") 49 if !ok { 50 return nil, errors.New("Must define modbus address") 51 } 52 53 ret.modbusIOType, ok = node.Points.Text(data.PointTypeModbusIOType, "") 54 if !ok { 55 return nil, errors.New("Must define modbus IO type") 56 } 57 58 ret.readOnly, _ = node.Points.ValueBool(data.PointTypeReadOnly, "") 59 60 if ret.modbusIOType == data.PointValueModbusInputRegister || 61 ret.modbusIOType == data.PointValueModbusHoldingRegister { 62 ret.modbusDataType, ok = node.Points.Text(data.PointTypeDataFormat, "") 63 if !ok { 64 return nil, errors.New("Data format must be specified") 65 } 66 ret.scale, ok = node.Points.Value(data.PointTypeScale, "") 67 if !ok { 68 return nil, errors.New("Must define modbus scale") 69 } 70 ret.offset, ok = node.Points.Value(data.PointTypeOffset, "") 71 if !ok { 72 return nil, errors.New("Must define modbus offset") 73 } 74 } 75 76 ret.value, _ = node.Points.Value(data.PointTypeValue, "") 77 ret.valueSet, _ = node.Points.Value(data.PointTypeValueSet, "") 78 ret.disabled, _ = node.Points.ValueBool(data.PointTypeDisabled, "") 79 ret.errorCount, _ = node.Points.ValueInt(data.PointTypeErrorCount, "") 80 ret.errorCountCRC, _ = node.Points.ValueInt(data.PointTypeErrorCountCRC, "") 81 ret.errorCountEOF, _ = node.Points.ValueInt(data.PointTypeErrorCountEOF, "") 82 ret.errorCountReset, _ = node.Points.ValueBool(data.PointTypeErrorCountReset, "") 83 ret.errorCountCRCReset, _ = node.Points.ValueBool(data.PointTypeErrorCountCRCReset, "") 84 ret.errorCountEOFReset, _ = node.Points.ValueBool(data.PointTypeErrorCountEOFReset, "") 85 86 return &ret, nil 87 } 88 89 // Changed returns true if the config of the IO has changed 90 // FIXME, we should not need this once we get NATS wired 91 func (io *ModbusIONode) Changed(newIO *ModbusIONode) bool { 92 if io.id != newIO.id || 93 io.address != newIO.address || 94 io.modbusIOType != newIO.modbusIOType || 95 io.modbusDataType != newIO.modbusDataType || 96 io.scale != newIO.scale || 97 io.offset != newIO.offset || 98 io.value != newIO.value || 99 io.valueSet != newIO.valueSet || 100 io.errorCountReset != newIO.errorCountReset || 101 io.errorCountCRCReset != newIO.errorCountCRCReset || 102 io.errorCountEOFReset != newIO.errorCountEOFReset { 103 return true 104 } 105 106 return false 107 }