|
|
|
@ -16,22 +16,23 @@ import (
|
|
|
|
|
pb "FRMS/internal/pkg/grpc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Hardware interface {
|
|
|
|
|
GetIp() string
|
|
|
|
|
GetId() uint32
|
|
|
|
|
GetBus() int
|
|
|
|
|
GetModel() string
|
|
|
|
|
GetPort() int
|
|
|
|
|
type Hardware struct {
|
|
|
|
|
Id uint32
|
|
|
|
|
Ip string
|
|
|
|
|
Bus int
|
|
|
|
|
Model string
|
|
|
|
|
Port int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type I2CMonitor interface {
|
|
|
|
|
Connected() []int
|
|
|
|
|
type HWinfo interface {
|
|
|
|
|
Get() (*Hardware, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Coordinator struct {
|
|
|
|
|
Server string
|
|
|
|
|
HW Hardware // object to keep track of reactor hw info
|
|
|
|
|
I2C I2CMonitor // I2C monitor to keep track of I2C devices
|
|
|
|
|
HW Hardware
|
|
|
|
|
Connected <-chan int
|
|
|
|
|
Disconnected <-chan int
|
|
|
|
|
Sensors map[int]SensorManager
|
|
|
|
|
Err chan error
|
|
|
|
|
mu sync.Mutex
|
|
|
|
@ -49,44 +50,76 @@ func NewSensorManager(addr int) SensorManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCoordinator(s string,ch chan error) *Coordinator {
|
|
|
|
|
return &Coordinator{Server:s,Err:ch}
|
|
|
|
|
c := make(Coordinator{Server:s,Err:ch}
|
|
|
|
|
c.Connected = make(<-chan int)
|
|
|
|
|
c.Disconnected = make(<-chan int)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewI2CMonitor(b int) I2CMonitor {
|
|
|
|
|
func NewI2CMonitor(c, dc <-chan int, b int) error {
|
|
|
|
|
return I2C.NewMonitor(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHWMonitor() Hardware {
|
|
|
|
|
func GetHWInfo() Hwinfo {
|
|
|
|
|
return system.NewHWMonitor()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) Start() error {
|
|
|
|
|
// should discover hwinfo and sensors on its own
|
|
|
|
|
// now setting up sensor managers
|
|
|
|
|
c.HW = NewHWMonitor()
|
|
|
|
|
fmt.Printf("Reactor at IP addr %v using I2C bus %v\n",c.HW.GetIp(),c.HW.GetBus())
|
|
|
|
|
response := c.Connect()
|
|
|
|
|
c.HW, err = GetHWInfo()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = NewI2CMonitor(c.Connect,c.Disconnect,c.Bus)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Reactor at IP addr %v using I2C bus %v\n",c.Ip,c.Bus)
|
|
|
|
|
response := c.EstablishConnection()
|
|
|
|
|
for response != nil {
|
|
|
|
|
fmt.Println("Connection failed!")
|
|
|
|
|
response = c.Connect()
|
|
|
|
|
fmt.Println("Connection failed!, Retrying")
|
|
|
|
|
response = c.EstablishConnection()
|
|
|
|
|
}
|
|
|
|
|
c.Register()
|
|
|
|
|
c.mu.Lock() // lock to finish setting up devices
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) Monitor() {
|
|
|
|
|
// function to automatically create and destroy sm
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case addr := <-c.Connect:
|
|
|
|
|
//check if sm exists and add sm for addr
|
|
|
|
|
c.Connected(addr)
|
|
|
|
|
case addr := <-c.Disconnect:
|
|
|
|
|
// kill sm for addr
|
|
|
|
|
c.Disconnected(addr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) Connected(addr int) {
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
c.I2C = NewI2CMonitor(c.HW.GetBus())
|
|
|
|
|
devs := c.I2C.Connected()
|
|
|
|
|
c.Sensors = make(map[int]SensorManager)
|
|
|
|
|
for _,d := range devs {
|
|
|
|
|
// create a goroutine for each active sensor
|
|
|
|
|
sm := NewSensorManager(d)
|
|
|
|
|
c.Sensors[d] = sm
|
|
|
|
|
sm, exists := c.Sensors[addr]
|
|
|
|
|
if !exists {
|
|
|
|
|
sm := NewSensorManager(addr)
|
|
|
|
|
c.Sensors[addr] = sm
|
|
|
|
|
go sm.Start()
|
|
|
|
|
fmt.Printf("Sensor Manager for addr %v Started!\n",d)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
} // ignoring case of existing sm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) Connect() error {
|
|
|
|
|
func (c *Coordinator) Disconnected(addr int) {
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
sm, exists := c.Sensors[addr]
|
|
|
|
|
if exists {
|
|
|
|
|
c.Sensors = delete(c.Sensors,addr)
|
|
|
|
|
sm.Kill()
|
|
|
|
|
} // not dealing with kill requests for not existing sm
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) EstablishConnection() error {
|
|
|
|
|
// function connects to central server and passes hwinfo
|
|
|
|
|
var opts []grpc.DialOption
|
|
|
|
|
opts = append(opts,grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
@ -96,7 +129,7 @@ func (c *Coordinator) Connect() error {
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
client := pb.NewHandshakeClient(conn)
|
|
|
|
|
req := &pb.ReactorDiscoveryRequest{Id:c.HW.GetId(),Ip:c.HW.GetIp(),Port:int32(2000),Model:c.HW.GetModel()}
|
|
|
|
|
req := &pb.ReactorDiscoveryRequest{Id:c.Id,Ip:c.Ip,Port:c.Port,Model:c.Model}
|
|
|
|
|
resp, err := client.ReactorDiscoveryHandler(context.Background(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -110,8 +143,8 @@ func (c *Coordinator) Connect() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Coordinator) Register() error {
|
|
|
|
|
fmt.Printf("Listing for pings on %v:%v\n",c.HW.GetIp(),c.HW.GetPort())
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf("%v:%v",c.HW.GetIp(),c.HW.GetPort()))
|
|
|
|
|
fmt.Printf("Listing for pings on %v:%v\n",c.Ip,c.Port)
|
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf("%v:%v",c.Ip(),c.Port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -125,7 +158,7 @@ func (c *Coordinator) SensorStatusHandler(ctx context.Context, ping *pb.SensorSt
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
var sensors []*pb.SensorStatus
|
|
|
|
|
resp := &pb.SensorStatusResponse{Id:c.HW.GetId(),Sensors:sensors}
|
|
|
|
|
resp := &pb.SensorStatusResponse{Id:c.Id,Sensors:sensors}
|
|
|
|
|
for a, s := range c.Sensors {
|
|
|
|
|
resp.Sensors = append(resp.Sensors,&pb.SensorStatus{Addr:int32(a), Type:s.GetType(), Status:s.GetStatus()})
|
|
|
|
|
}
|
|
|
|
|