diff --git a/deployment/instances/udi/default/config.json b/deployment/instances/udi/default/config.json index bb0fc59..00ebd30 100644 --- a/deployment/instances/udi/default/config.json +++ b/deployment/instances/udi/default/config.json @@ -4,6 +4,15 @@ "tlsEnable": "false" }, "topicMappings": [ + { + "topics": [ "snmp" ], + "handler": "SNMP", + "id": "SNMP", + "config": { + "attributes": { + } + } + }, { "topics": [ "dt1/ai/periodic/1" ], "handler": "DT1T", diff --git a/queries/hottis.sql b/queries/hottis.sql index a1dc7e0..b83447b 100644 --- a/queries/hottis.sql +++ b/queries/hottis.sql @@ -70,12 +70,15 @@ create or replace view soil_v as create or replace view co2_v as select time, - cast(values->'CO2concentration'->>'value' as float) as co2concentration, - cast(values->'Humidity'->>'value' as float) as humidity, - cast(values->'Temperature'->>'value' as float) as temperature, - device - from measurements - where application = 'de-hottis-app01' and attributes->>'DeviceType' = 'hottis-scd30'; + cast(m.values->'CO2concentration'->>'value' as float) as co2concentration, + cast(m.values->'Humidity'->>'value' as float) as humidity, + cast(m.values->'Temperature'->>'value' as float) as temperature, + m.device as device, + d.attributes->>'Label' as label + from measurements m, devices d + where m.application = 'de-hottis-app01' and + m.attributes->>'DeviceType' = 'hottis-scd30' and + m.device = d.label; create or replace view locative_v as select time, diff --git a/src/udi/dispatcher/dispatcher.go b/src/udi/dispatcher/dispatcher.go index b55dddb..d8e570e 100644 --- a/src/udi/dispatcher/dispatcher.go +++ b/src/udi/dispatcher/dispatcher.go @@ -17,6 +17,7 @@ import "udi/handlers/sver" import "udi/handlers/svej" import "udi/handlers/dt1t" import "udi/handlers/locative" +import "udi/handlers/snmp" var handlerMap map[string]handler.Handler = make(map[string]handler.Handler) @@ -47,6 +48,8 @@ func InitDispatcher() { factory = dt1t.New case "Locative": factory = locative.New + case "SNMP": + factory = snmp.New default: factory = nil log.Printf("No handler %s found, ignore mapping", mapping.Handler) diff --git a/src/udi/handlers/snmp/snmp.go b/src/udi/handlers/snmp/snmp.go new file mode 100644 index 0000000..8511020 --- /dev/null +++ b/src/udi/handlers/snmp/snmp.go @@ -0,0 +1,74 @@ +package snmp + +import ( + "time" + "log" + "encoding/json" + "udi/config" + "udi/handlers/handler" + "udi/database" +) + + +type SnmpHandler struct { + handler.CommonHandler + dbh *database.DatabaseHandle +} + +type endpoint_t struct { + Label string `json:"label"` + Variable string `json:"variable"` + Value string `json:"value"` + DiffValue string `json:"diffValue"` +} + +type observation_t struct { + Device string `json:"device"` + Label string `json:"label"` + Variables map[string]endpoint_t `json:"variables"` +} + + +func New(id string, config config.HandlerConfigT) handler.Handler { + t := &SnmpHandler { + } + t.Id = id + t.dbh = database.NewDatabaseHandle() + return t +} + +func (self *SnmpHandler) Handle(message handler.MessageT) { + log.Printf("Handler SNMP %d processing %s -> %s", self.Id, message.Topic, message.Payload) + + var observation observation_t + err := json.Unmarshal([]byte(message.Payload), &observation) + if err != nil { + self.Lost("Unable to parse payload into Observation struct", err, message) + return + } + + var measurement database.Measurement + measurement.Time = time.Now() + + measurement.Application = "SNMP" + measurement.Device = observation.Device + + measurement.Attributes = make(map[string]interface{}) + + measurement.Values = make(map[string]database.VariableType) + for k, v := range observation.Variables { + measurement.Values[k] = database.VariableType { + Label: v.Label, + Variable: v.Variable, + Unit: "", + Value: v.Value, + } + } + + log.Printf("Prepared measurement item: %s", measurement) + + self.dbh.StoreMeasurement(&measurement) + self.S() +} + +