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.

93 lines
1.8 KiB
Go

package server
import (
"sync"
_ "fmt"
)
// package will create and maintain a concurrent system structure
// allows for multiple readers/writers
type System struct {
sync.Mutex
Reactors map[uint32]*Reactor
}
type Reactor struct {
Status bool
Devices map[int]*Device
sync.Mutex
}
type Device struct {
Type string
Status string
Data string
}
type ReactorInfo struct {
Id uint32
Status bool
}
type DeviceInfo struct {
Addr int
Type string
Status string
Data string
}
func NewSystemStruct() *System {
s := &System{}
r := make(map[uint32]*Reactor)
s.Reactors = r
return s
}
func (s *System) UpdateReactor(id uint32, status bool) {
s.Lock()
defer s.Unlock()
if _, exists := s.Reactors[id]; exists {
s.Reactors[id].Status = status
} else {
m := make(map[int]*Device)
s.Reactors[id] = &Reactor{Status:status,Devices:m}
}
}
func (s *System) UpdateReactorDevice(id uint32, addr int, t, st, d string) {
dev := &Device{Type:t, Status:st, Data:d}
go s.Reactors[id].UpdateDevice(addr, dev)
}
func (r *Reactor) UpdateDevice(addr int, d *Device) {
r.Lock()
defer r.Unlock()
r.Devices[addr] = d
}
func (s *System) GetReactors() []*ReactorInfo {
s.Lock()
defer s.Unlock()
rinfo := []*ReactorInfo{}
for id, r := range s.Reactors {
rinfo = append(rinfo,&ReactorInfo{Id:id,Status:r.Status})
}
return rinfo
}
func (s *System) GetReactorDevices(id uint32) []*DeviceInfo {
return s.Reactors[id].GetDevices()
}
func (r *Reactor) GetDevices() []*DeviceInfo {
//
r.Lock()
defer r.Unlock()
di := []*DeviceInfo{}
for a,v := range r.Devices {
di = append(di, &DeviceInfo{Addr:a,Status:v.Status,Type:v.Type,Data:v.Data})
}
return di
}