|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
_ "fmt"
|
|
|
|
_ "FRMS/internal/pkg/I2C"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
Addr int
|
|
|
|
Sensor *SensorInfo
|
|
|
|
I2CMonitor I2Cmonitor
|
|
|
|
Kill chan<- bool
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementing sensor skeleton
|
|
|
|
|
|
|
|
type SensorStatus uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
READY SensorStatus = iota
|
|
|
|
ALIVE
|
|
|
|
SENDING
|
|
|
|
KILLED
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s SensorStatus) String() string {
|
|
|
|
return [...]string{"Ready","Active","Sending","Disabled"}[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
type SensorInfo struct {
|
|
|
|
Status SensorStatus
|
|
|
|
Type string
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorInfo) GetStatus() uint32{
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return uint32(s.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorInfo) GetType() string{
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// i2c stuff
|
|
|
|
type I2Cmonitor interface {
|
|
|
|
GetStatus(int) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSensorManager(addr int,i I2Cmonitor) (*Manager,error) {
|
|
|
|
m := &Manager{Addr:addr,I2CMonitor:i}
|
|
|
|
go m.Start()
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Start() {
|
|
|
|
types := map[int]string{97:"DO Sensor",99:"pH Sensor",102:"RTD Sensor"}
|
|
|
|
m.Sensor = &SensorInfo{Type:types[m.Addr]}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetStatus() uint32 {
|
|
|
|
b := m.I2CMonitor.GetStatus(m.Addr)
|
|
|
|
m.Sensor.Update(b)
|
|
|
|
return m.Sensor.GetStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetType() string {
|
|
|
|
return m.Sensor.GetType()
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2C interface stuff
|
|
|
|
|
|
|
|
func (s *SensorInfo) Update(b bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if b {
|
|
|
|
s.Status = ALIVE
|
|
|
|
} else {
|
|
|
|
s.Status = KILLED
|
|
|
|
}
|
|
|
|
}
|