adding reactor config parser for devices
parent
1c2868daf8
commit
b1520db055
@ -0,0 +1,5 @@
|
||||
reactor:
|
||||
sensors:
|
||||
address: 112 # decimal
|
||||
name: "DO Sensor"
|
||||
|
@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
// pacakge serves to store/load config files for reactor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ReactorConf struct {
|
||||
Reactor ReactorConfig `mapstructure:"reactor"`
|
||||
Devices map[int]DeviceConfig `mapstructure:"devices"`
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type DeviceConfig struct {
|
||||
Address uint32 `mapstructure:"address"`
|
||||
Interval uint32 `mapstructure:"interval"`
|
||||
Name string `mapstructure:"name"`
|
||||
}
|
||||
|
||||
// loaded in other file
|
||||
func (c *ReactorConf) GetURL() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Reactor.URL, nil
|
||||
}
|
@ -1,114 +1,114 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
_"fmt"
|
||||
"time"
|
||||
"sync"
|
||||
"strings"
|
||||
_ "FRMS/internal/pkg/I2C"
|
||||
"log"
|
||||
_ "FRMS/internal/pkg/I2C"
|
||||
_ "fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
*Dev
|
||||
I2CDevice
|
||||
*Active
|
||||
Hb time.Duration
|
||||
*Dev
|
||||
I2CDevice
|
||||
*Active
|
||||
Hb time.Duration
|
||||
}
|
||||
|
||||
type Active struct {
|
||||
sync.Mutex
|
||||
bool
|
||||
int
|
||||
sync.Mutex
|
||||
bool
|
||||
int
|
||||
}
|
||||
|
||||
type Dev struct {
|
||||
// last known values
|
||||
Addr int
|
||||
Type string
|
||||
Status string // could be more efficient but to hell with it
|
||||
Data string
|
||||
// last known values
|
||||
Addr int
|
||||
Type string
|
||||
Status string // could be more efficient but to hell with it
|
||||
Data string
|
||||
}
|
||||
|
||||
type I2CDevice interface {
|
||||
// basic device info
|
||||
GetAddr() int
|
||||
GetStatus() string
|
||||
GetType() string
|
||||
GetData() string
|
||||
// basic device info
|
||||
GetAddr() int
|
||||
GetStatus() string
|
||||
GetType() string
|
||||
GetData() string
|
||||
}
|
||||
|
||||
func NewDeviceManager(i2c I2CDevice) *Manager {
|
||||
m := &Manager{Hb:time.Duration(1*time.Second)}
|
||||
m.I2CDevice = i2c
|
||||
m.Active = &Active{}
|
||||
m.Dev = &Dev{Addr:i2c.GetAddr(),Type:i2c.GetType(),Status:i2c.GetStatus(),Data:i2c.GetData()}
|
||||
return m
|
||||
func NewDeviceManager(i2c I2CDevice) *Manager {
|
||||
m := &Manager{Hb: time.Duration(1 * time.Second)}
|
||||
m.I2CDevice = i2c
|
||||
m.Active = &Active{}
|
||||
m.Dev = &Dev{Addr: i2c.GetAddr(), Type: i2c.GetType(), Status: i2c.GetStatus(), Data: i2c.GetData()}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Manager) Start() {
|
||||
// goal is to start a long running monitoring routine
|
||||
if !m.Activate() {
|
||||
log.Fatal("Manager already running!")
|
||||
} // atomically activated if this runs
|
||||
// go m.Monitor()
|
||||
// goal is to start a long running monitoring routine
|
||||
if !m.Activate() {
|
||||
log.Fatal("Manager already running!")
|
||||
} // atomically activated if this runs
|
||||
// go m.Monitor()
|
||||
}
|
||||
|
||||
func (m *Manager) Exit() {
|
||||
if !m.Deactivate() {
|
||||
log.Fatal("Manager already exited!")
|
||||
}
|
||||
if !m.Deactivate() {
|
||||
log.Fatal("Manager already exited!")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) GetType() string {
|
||||
return m.Type
|
||||
return m.Type
|
||||
}
|
||||
|
||||
func (m *Manager) GetStatus() string {
|
||||
m.Status = m.I2CDevice.GetStatus()
|
||||
if m.IsActive() && strings.Contains(m.Status,"KILLED") {
|
||||
m.Exit()
|
||||
}
|
||||
return m.Status
|
||||
m.Status = m.I2CDevice.GetStatus()
|
||||
if m.IsActive() && strings.Contains(m.Status, "KILLED") {
|
||||
m.Exit()
|
||||
}
|
||||
return m.Status
|
||||
}
|
||||
|
||||
func (m *Manager) GetData() string {
|
||||
m.Data = m.I2CDevice.GetData()
|
||||
return m.Data
|
||||
m.Data = m.I2CDevice.GetData()
|
||||
return m.Data
|
||||
}
|
||||
|
||||
func (m *Manager) GetAddr() int {
|
||||
return m.Addr
|
||||
return m.Addr
|
||||
}
|
||||
|
||||
// atomic activation and deactivation
|
||||
func (a *Active) Activate() bool {
|
||||
// returns true if success, false otherwise
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
if a.bool { // already active
|
||||
return false
|
||||
} else {
|
||||
a.bool = true
|
||||
a.int = 0
|
||||
return a.bool
|
||||
}
|
||||
// returns true if success, false otherwise
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
if a.bool { // already active
|
||||
return false
|
||||
} else {
|
||||
a.bool = true
|
||||
a.int = 0
|
||||
return a.bool
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Active) Deactivate() bool {
|
||||
// returns true if success false otherise
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
if a.bool {
|
||||
a.bool = false
|
||||
return true
|
||||
} else { // already deactivated
|
||||
return a.bool // false
|
||||
}
|
||||
// returns true if success false otherise
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
if a.bool {
|
||||
a.bool = false
|
||||
return true
|
||||
} else { // already deactivated
|
||||
return a.bool // false
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Active) IsActive() bool {
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
return a.bool
|
||||
a.Lock()
|
||||
defer a.Unlock()
|
||||
return a.bool
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
/*
|
||||
this file serves as a map that the sensor library can use to determine which manager to call
|
||||
*/
|
||||
|
||||
type NewManager interface {
|
||||
// serves as interface to restrict managers can be relocated
|
||||
}
|
||||
|
||||
type DM struct {
|
||||
DeviceManagers map[uint]NewManager
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func NewManagerDirectory() *DM {
|
||||
m := map[uint]NewManager{
|
||||
// map to set functions up
|
||||
112: NewDOManager(),
|
||||
}
|
||||
return &DM{DeviceManagers: m}
|
||||
}
|
||||
|
||||
func (d *DM) GetManager(addr uint) (NewManager, error) {
|
||||
d.Lock()
|
||||
defer d.Unlock()
|
||||
if m, ok := d.DeviceManagers[addr]; ok {
|
||||
return m, nil
|
||||
}
|
||||
return &DM{}, fmt.Errorf("No manager found for address %d!", addr)
|
||||
}
|
Loading…
Reference in New Issue