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.
77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package device
|
|
|
|
import (
|
|
"FRMS/internal/pkg/manager"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// base device manager
|
|
|
|
type Manager interface {
|
|
// core manager
|
|
Start() error
|
|
Stop() error
|
|
IsActive() manager.Status
|
|
HeartBeat(chan struct{}, int, int, time.Duration)
|
|
}
|
|
|
|
func NewManager() (Manager, error) {
|
|
// no timeouts needed
|
|
return manager.New(0)
|
|
}
|
|
|
|
type DeviceManager struct {
|
|
// for device agnostic fields/methods
|
|
Address int `mapstructure:"address"`
|
|
Bus int // i2c bus
|
|
// mutable
|
|
infoMu sync.RWMutex
|
|
Name string `mapstructure:"name"`
|
|
defaultName string
|
|
// base manager
|
|
Manager
|
|
// config
|
|
Config *viper.Viper
|
|
}
|
|
|
|
func NewDeviceManager(
|
|
bus, addr int,
|
|
config *viper.Viper,
|
|
defaultName string,
|
|
) (*DeviceManager, error) {
|
|
// new base dm
|
|
m, err := NewManager()
|
|
if err != nil {
|
|
return &DeviceManager{}, err
|
|
}
|
|
|
|
return &DeviceManager{
|
|
Address: addr,
|
|
Bus: bus,
|
|
defaultName: defaultName,
|
|
Manager: m,
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (m *DeviceManager) LoadConfig() error {
|
|
|
|
// setting default name
|
|
mainKey := fmt.Sprintf("devices.%d", m.Address)
|
|
nameKey := fmt.Sprintf("%s.name", mainKey)
|
|
|
|
if !m.Config.IsSet(nameKey) {
|
|
m.Config.Set(nameKey, m.defaultName)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *DeviceManager) Start() error {
|
|
// start
|
|
return m.Manager.Start()
|
|
}
|