You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
package reactor
|
|
|
|
import (
|
|
"FRMS/internal/pkg/device"
|
|
pb "FRMS/internal/pkg/grpc"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type DeviceManager interface {
|
|
Start() error
|
|
Exit() error
|
|
GetName() string
|
|
GetStatus() pb.Status
|
|
GetDevice() *pb.Device // monitoring info
|
|
LoadConfig(string) error
|
|
}
|
|
|
|
func NewDeviceManager(addr int, config *viper.Viper) (DeviceManager, error) {
|
|
return device.NewDeviceManager(&pb.Device{Addr: int32(addr)}, config)
|
|
}
|
|
|
|
type DeviceCoordinator struct {
|
|
Config *viper.Viper
|
|
Managers map[int]DeviceManager
|
|
sync.RWMutex
|
|
}
|
|
|
|
func NewDeviceCoordinator(config *viper.Viper) *DeviceCoordinator {
|
|
dm := &DeviceCoordinator{Config: config}
|
|
dm.Managers = make(map[int]DeviceManager)
|
|
return dm
|
|
}
|
|
|
|
func (c *DeviceCoordinator) UpdateDevices(config *viper.Viper, i2c I2CClient, active map[int]bool) error {
|
|
// update dev status, add new ones
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
|
|
var err error
|
|
|
|
for addr, _ := range active {
|
|
// loop over devs
|
|
if _, ok := c.Managers[addr]; !ok {
|
|
// no device
|
|
if c.Managers[addr], err = NewDeviceManager(addr, c.Config); err != nil {
|
|
return err
|
|
}
|
|
// loading config
|
|
c.Managers[addr].LoadConfig("reactor")
|
|
// check for config name
|
|
// nameKey := fmt.Sprintf("device.%d.name", addr)
|
|
// if !config.IsSet(nameKey) {
|
|
// // no config name, get default
|
|
// config.Set(nameKey, c.Managers[addr].GetName())
|
|
// }
|
|
// // setting it
|
|
// if err = c.Managers[addr].SetName(config.Get(nameKey).(string)); err != nil {
|
|
// return err
|
|
// }
|
|
// // atlas delays
|
|
// delayKey := fmt.Sprintf("device.%d.delays", addr)
|
|
// if !config.IsSet(delayKey) {
|
|
// // set empty delays
|
|
// config.Set(fmt.Sprintf("device.%d.delays.read", addr), 0)
|
|
// config.Set(fmt.Sprintf("device.%d.delays.cal", addr), 0)
|
|
// }
|
|
// c.Managers[addr].GetDelay(config, delayKey)
|
|
}
|
|
}
|
|
// all devs accounted for
|
|
for addr, dm := range c.Managers {
|
|
if active[addr] {
|
|
// active
|
|
if dm.GetStatus() != 1 {
|
|
err = dm.Start()
|
|
}
|
|
} else {
|
|
if dm.GetStatus() != 0 {
|
|
err = dm.Exit()
|
|
}
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *DeviceCoordinator) GetDevices() ([]*pb.Device, error) {
|
|
c.RLock()
|
|
defer c.RUnlock()
|
|
|
|
var err error
|
|
var devices []*pb.Device
|
|
var dev *pb.Device
|
|
|
|
for _, dm := range c.Managers {
|
|
dev = dm.GetDevice()
|
|
fmt.Println(dev)
|
|
devices = append(devices, dev)
|
|
}
|
|
|
|
return devices, err
|
|
}
|