snmp
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2024-01-25 15:13:36 +01:00
parent 8cfc92c226
commit d1bbbeaccf
4 changed files with 95 additions and 6 deletions

View File

@ -4,6 +4,15 @@
"tlsEnable": "false"
},
"topicMappings": [
{
"topics": [ "snmp" ],
"handler": "SNMP",
"id": "SNMP",
"config": {
"attributes": {
}
}
},
{
"topics": [ "dt1/ai/periodic/1" ],
"handler": "DT1T",

View File

@ -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,

View File

@ -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)

View File

@ -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()
}