diff --git a/deployment/instances/udi-pg/default/config.json b/deployment/instances/udi-pg/default/config.json deleted file mode 100644 index 01b2bb7..0000000 --- a/deployment/instances/udi-pg/default/config.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "mqtt": { - "broker": "ssl://eu1.cloud.thethings.network:8883", - "username": "com-passavant-geiger-poc@ttn", - "tlsEnable": "true" - }, - "topicMappings": [ - { - "topics": [ "v3/com-passavant-geiger-poc@ttn/devices/#" ], - "handler": "TTN", - "id": "TTN0", - "config": { - "attributes": { - } - } - } - ], - "archiver": { - "dir": "/archive" - } -} diff --git a/deployment/instances/udi/soil/config.json b/deployment/instances/udi/soil/config.json deleted file mode 100644 index 825fb9d..0000000 --- a/deployment/instances/udi/soil/config.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "mqtt": { - "broker": "ssl://eu1.cloud.thethings.network:8883", - "username": "de-hottis-app01@ttn", - "password": "ENV", - "tlsEnable": "true" - }, - "topicMappings": [ - { - "topics": [ "v3/#" ], - "handler": "TTN", - "id": "TTN0", - "config": { - "attributes": { - } - } - } - ], - "archiver": { - "dir": "/archive" - } -} diff --git a/src/udi/handlers/mbgw3/mbgw3.go b/src/udi/handlers/mbgw3/mbgw3.go index cc15477..a52ce0f 100644 --- a/src/udi/handlers/mbgw3/mbgw3.go +++ b/src/udi/handlers/mbgw3/mbgw3.go @@ -1,106 +1,91 @@ package mbgw3 import ( - "time" - "log" - "strconv" - "encoding/json" - "udi/config" - "udi/handlers/handler" - "udi/database" + "encoding/json" + "log" + "strconv" + "time" + "udi/config" + "udi/database" + "udi/handlers/handler" ) - type Mbgw3Handler struct { - handler.CommonHandler - dbh *database.DatabaseHandle + handler.CommonHandler + dbh *database.DatabaseHandle } type Observation struct { - Status string `json:"Status"` - RequestId string `json:"RequestId"` - Device string `json:"Device"` - Errors string `json:"Errors"` - ErrorRatio string `json:"ErrorRatio"` - Requests string `json:"Requests"` - Values map[string]string + Status string `json:"Status"` + RequestId string `json:"RequestId"` + Device string `json:"Device"` + Errors string `json:"Errors"` + ErrorRatio string `json:"ErrorRatio"` + Requests string `json:"Requests"` + Values map[string]string } - func New(id string, config config.HandlerConfigT) handler.Handler { - t := &Mbgw3Handler { - } - t.Id = id - t.dbh = database.NewDatabaseHandle() - log.Printf("Handler MBGW3 %d initialized", id) - return t + t := &Mbgw3Handler{} + t.Id = id + t.dbh = database.NewDatabaseHandle() + log.Printf("Handler MBGW3 %d initialized", id) + return t } func (self *Mbgw3Handler) Handle(message handler.MessageT) { - //log.Printf("Handler MBGW3 %d processing %s -> %s", self.Id, message.Topic, message.Payload) + //log.Printf("Handler MBGW3 %d processing %s -> %s", self.Id, message.Topic, message.Payload) - var observation Observation - err := json.Unmarshal([]byte(message.Payload), &observation) - if err != nil { - self.Lost("Unable to parse payload into Observation struct", err, message) - return - } + var observation Observation + 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() + var measurement database.Measurement + measurement.Time = time.Now() - if observation.Device == "Gas" { - measurement.Application = "Gas" - measurement.Device = "Gasmeter" - } else { - measurement.Application = "Power" - measurement.Device = observation.Device - } + if observation.Device == "Gas" { + measurement.Application = "Gas" + measurement.Device = "Gasmeter" + } else { + measurement.Application = "Power" + measurement.Device = observation.Device + } - measurement.Attributes = make(map[string]interface{}) - if v, err := strconv.Atoi(observation.RequestId); err == nil { - measurement.Attributes["RequestId"] = v - } - if v, err := strconv.ParseFloat(observation.ErrorRatio, 32); err == nil { - measurement.Attributes["ErrorRatio"] = v - } - if v, err := strconv.Atoi(observation.Errors); err == nil { - measurement.Attributes["Errors"] = v - } - if v, err := strconv.Atoi(observation.Requests); err == nil { - measurement.Attributes["Requests"] = v - } - measurement.Attributes["Status"] = observation.Status + measurement.Attributes = make(map[string]interface{}) + if v, err := strconv.Atoi(observation.RequestId); err == nil { + measurement.Attributes["RequestId"] = v + } + if v, err := strconv.ParseFloat(observation.ErrorRatio, 32); err == nil { + measurement.Attributes["ErrorRatio"] = v + } + if v, err := strconv.Atoi(observation.Errors); err == nil { + measurement.Attributes["Errors"] = v + } + if v, err := strconv.Atoi(observation.Requests); err == nil { + measurement.Attributes["Requests"] = v + } + measurement.Attributes["Status"] = observation.Status - measurement.Values = make(map[string]database.VariableType) - unitMap := map[string]string { "Energy": "Wh", "Power": "W", "Voltage": "V", "Current": "A", "Volume": "m3" } - keyCount := make(map[string]int) - - for k, v := range observation.Values { - unit, exists := unitMap[k] - if ! exists { - unit = "Unmapped Unit" - } - - // Check if key already exists and create unique key if needed - keyCount[k]++ - uniqueKey := k - if keyCount[k] > 1 { - uniqueKey = k + strconv.Itoa(keyCount[k]) - } - - measurement.Values[uniqueKey] = database.VariableType { - Label: "", - Variable: k, - Unit: unit, - Value: v, - } - } + measurement.Values = make(map[string]database.VariableType) + unitMap := map[string]string{"Energy": "Wh", "Power": "W", "Voltage": "V", "Current": "A", "Volume": "m3"} + for k, v := range observation.Values { + unit, exists := unitMap[k] + if !exists { + unit = "Unmapped Unit" + } + measurement.Values[k] = database.VariableType{ + Label: "", + Variable: k, + Unit: unit, + Value: v, + } + } - //log.Printf("Prepared measurement item: %s", measurement) + //log.Printf("Prepared measurement item: %s", measurement) - self.dbh.StoreMeasurement(&measurement) - self.S() + self.dbh.StoreMeasurement(&measurement) + self.S() } - -