package device import ( pb "FRMS/internal/pkg/grpc" "fmt" "sync" "github.com/spf13/viper" ) type SubManager interface { Start() error Exit() error String() string // printing info about the sub manager // for config bs GetDefaultName() string } type NameChan struct { Ch chan string } // base device manager type DeviceManager struct { SubManager `mapstructure:",squash"` // across controllers/sensors Address int `mapstructure:"address"` infoMu sync.Mutex *Name Name string `mapstructure:"name"` Status string // easier to remember Config *viper.Viper ConfigPrefix string //sync.RWMutex pb.UnimplementedDeviceInfoServer } func NewDeviceManager(addr int32, config *viper.Viper, cPre string) (*DeviceManager, error) { s, err := NewSubManager(addr) dm := &DeviceManager{ SubManager: s, Config: config, Address: int(addr), ConfigPrefix: cPre, } return dm, err } func (m *DeviceManager) LoadConfig() error { // setting default name mainKey := fmt.Sprintf("%s.devices.%d", m.ConfigPrefix, m.Address) nameKey := fmt.Sprintf("%s.name", mainKey) if !m.Config.IsSet(nameKey) { m.Config.Set(nameKey, m.SubManager.GetDefaultName()) } m.Config.UnmarshalKey(mainKey, m) return nil } // dev info grpc handlers func (m *DeviceManager) Name(stream pb.DeviceInfo_StatusServer) error { // this might be scuffed } // I think I can use grpc streams to do better // func (m *DeviceManger) GetName(ctx context.Context, req *pb.DeviceName) (*pb.DeviceName, error) { // // gets name // var name string // var err error // // quick lock // m.infoMu.RLock() // name = m.Name // m.infoMu.RUnlock() // if name == "" { // name = m.GetDefaultName() // err = m.UpdateName(name) // } // response := &pb.DeviceName{Address: int32(m.Address), Name: name} // return response, err // } // func (m *DeviceManger) SetName(ctx context.Context, req *pb.DeviceName) (*pb.DeviceName, error) { // // handles incoming req // response := &pb.DeviceName{Address: int32(m.Address)} // err := m.UpdateName() // return response, err // } // func (m *DeviceManager) UpdateName(name string) error { // // updates name/config // m.infoMu.Lock() // defer m.infoMu.Unlock() // nameKey := fmt.Sprintf("%s.devices.%d.name", m.ConfigPrefix, m.Address) // m.Config.SetKey(nameKey, name) // return nil // } // func (m *DeviceManger) GetStatus(ctx context.Context, req *pb.DeviceStatus) (*pb.DeviceStatus, error) { // } // func (m *DeviceManger) SetStatus(ctx context.Context, req *pb.DeviceStatus) (*pb.DeviceStatus, error) { // } // monitoring grpc func (m *DeviceManager) GetDevice() (*pb.Device, error) { // turns info into pb.Device for monitoring grpc } func (m *DeviceManger) UpdateDevice(*pb.Device) error { // unpacks pb.Device to update itself }