|
|
@ -3,6 +3,10 @@ package reactor
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb-client-go/v2"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
//"log"
|
|
|
|
//"log"
|
|
|
|
//"fmt"
|
|
|
|
//"fmt"
|
|
|
|
//"net"
|
|
|
|
//"net"
|
|
|
@ -28,7 +32,9 @@ func (c *Coordinator) DevStatus(ch chan *DeviceStatus, a int, dm DeviceManager)
|
|
|
|
ch <-d
|
|
|
|
ch <-d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) GetStatus() []*pb.Device {
|
|
|
|
func (c *Coordinator) GetStatus(client influxdb2.Client) []*pb.Device {
|
|
|
|
|
|
|
|
// db stuff
|
|
|
|
|
|
|
|
api := client.WriteAPIBlocking(c.Org,c.Bucket)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
devs := []*pb.Device{}
|
|
|
|
devs := []*pb.Device{}
|
|
|
|
statusChan := make(chan *DeviceStatus)
|
|
|
|
statusChan := make(chan *DeviceStatus)
|
|
|
@ -47,6 +53,24 @@ func (c *Coordinator) GetStatus() []*pb.Device {
|
|
|
|
select{
|
|
|
|
select{
|
|
|
|
case s:= <-statusChan:
|
|
|
|
case s:= <-statusChan:
|
|
|
|
//fmt.Printf("%v is %v\n",s.Type,s.Status)
|
|
|
|
//fmt.Printf("%v is %v\n",s.Type,s.Status)
|
|
|
|
|
|
|
|
data := strings.Split(s.Data,",") // T:10C,H:102% -> T:10C H:10%
|
|
|
|
|
|
|
|
for _, m := range data {
|
|
|
|
|
|
|
|
var meas string
|
|
|
|
|
|
|
|
splt := strings.Split(m,":") // T 10C or H 10%
|
|
|
|
|
|
|
|
if splt[0] == "T" {
|
|
|
|
|
|
|
|
meas = "Temperature"
|
|
|
|
|
|
|
|
} else if splt[0] == "H" {
|
|
|
|
|
|
|
|
meas = "Humidity"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.ParseFloat(strings.Trim(splt[1]," %C\n"), 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
p := influxdb2.NewPoint("measurements",map[string]string{"type":meas},map[string]interface{}{"val":val},time.Now())
|
|
|
|
|
|
|
|
if err := api.WritePoint(context.Background(), p); err != nil {
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
devs = append(devs,&pb.Device{Addr:int32(s.Addr),Type:s.Type,Status:s.Status,Data:s.Data})
|
|
|
|
devs = append(devs,&pb.Device{Addr:int32(s.Addr),Type:s.Type,Status:s.Status,Data:s.Data})
|
|
|
|
wg.Done()
|
|
|
|
wg.Done()
|
|
|
|
case <-allDone:
|
|
|
|
case <-allDone:
|
|
|
@ -56,9 +80,9 @@ func (c *Coordinator) GetStatus() []*pb.Device {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// grpc status update handler
|
|
|
|
// grpc status update handler
|
|
|
|
func (c *Coordinator) Ping() {
|
|
|
|
func (c *Coordinator) Ping(client influxdb2.Client) {
|
|
|
|
// sends all device status to central coordinator
|
|
|
|
// sends all device status to central coordinator
|
|
|
|
devs := c.GetStatus()
|
|
|
|
devs := c.GetStatus(client)
|
|
|
|
req := &pb.ReactorStatusPing{Id:c.Id,Devices:devs}
|
|
|
|
req := &pb.ReactorStatusPing{Id:c.Id,Devices:devs}
|
|
|
|
_, err := c.MonitoringClient.ReactorStatusHandler(context.Background(),req)
|
|
|
|
_, err := c.MonitoringClient.ReactorStatusHandler(context.Background(),req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|