From 901ed32bb01cceda293aa200b206e17e83285ed4 Mon Sep 17 00:00:00 2001 From: Ingo Date: Thu, 15 Feb 2024 09:21:09 +0100 Subject: [PATCH] add 'Since' to connection state --- lib/controller/connectionstate.go | 5 +++-- lib/controller/mongodb.go | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/controller/connectionstate.go b/lib/controller/connectionstate.go index a738a8c..88584df 100644 --- a/lib/controller/connectionstate.go +++ b/lib/controller/connectionstate.go @@ -19,6 +19,7 @@ package controller import ( "github.com/SENERGY-Platform/connection-log-worker/lib/model" "gopkg.in/mgo.v2/bson" + "time" ) func (this *Controller) setHubState(gatewayLog model.HubLog) (update bool, err error) { @@ -30,7 +31,7 @@ func (this *Controller) setHubState(gatewayLog model.HubLog) (update bool, err e } update = count == 0 if update { - _, err = collection.Upsert(bson.M{"gateway": gatewayLog.Id}, HubState{Gateway: gatewayLog.Id, Online: gatewayLog.Connected}) + _, err = collection.Upsert(bson.M{"gateway": gatewayLog.Id}, HubState{Gateway: gatewayLog.Id, Online: gatewayLog.Connected, Since: time.Now().Unix()}) } return } @@ -44,7 +45,7 @@ func (this *Controller) setDeviceState(deviceLog model.DeviceLog) (update bool, } update = count == 0 if update { - _, err = collection.Upsert(bson.M{"device": deviceLog.Id}, DeviceState{Device: deviceLog.Id, Online: deviceLog.Connected}) + _, err = collection.Upsert(bson.M{"device": deviceLog.Id}, DeviceState{Device: deviceLog.Id, Online: deviceLog.Connected, Since: time.Now().Unix()}) } return } diff --git a/lib/controller/mongodb.go b/lib/controller/mongodb.go index e873b29..acccdd5 100644 --- a/lib/controller/mongodb.go +++ b/lib/controller/mongodb.go @@ -57,9 +57,11 @@ func (this *Controller) getHubStateCollection() (session *mgo.Session, collectio type DeviceState struct { Device string `json:"device,omitempty" bson:"device,omitempty"` Online bool `json:"online" bson:"online"` + Since int64 `json:"since" bson:"since"` } type HubState struct { Gateway string `json:"gateway,omitempty" bson:"gateway,omitempty"` Online bool `json:"online" bson:"online"` + Since int64 `json:"since" bson:"since"` }