Rename ShroomStatus to ShroomState

This commit is contained in:
Kelvin Ly 2023-05-16 10:24:50 -04:00
parent 5bb945e875
commit af7627a15a
2 changed files with 50 additions and 36 deletions

View File

@ -18,7 +18,8 @@ func newlinePos(s []byte) int {
} }
type StatusJson struct { type StatusJson struct {
HumOn *bool `json:"humidifier"` HumOn *bool `json:"humidifier"`
ManualMode *bool `json:"manual_mode"`
} }
type DataJson struct { type DataJson struct {
@ -28,36 +29,53 @@ type DataJson struct {
HumidifierVolts *float32 `json:"hv"` HumidifierVolts *float32 `json:"hv"`
} }
type ParamJson struct {
Name string `json:"name"`
Value float32 `json:"string"`
}
type ShroomPacket struct { type ShroomPacket struct {
Data *DataJson `json:"data"` Data *DataJson `json:"data"`
Status *StatusJson `json:"status"` Status *StatusJson `json:"status"`
Param *ParamJson `json:"param"`
} }
type ShroomStatus struct { type ShroomState struct {
sync.RWMutex sync.RWMutex
HumidifierOn bool HumidifierOn bool
NumConnections int NumConnections int
Wait chan struct{}
StatusWait chan struct{} Params map[string]float32
Wait chan struct{}
StatusWait chan struct{}
Commands chan []byte Commands chan []byte
} }
func (s *ShroomStatus) Update() { func NewShroomState() ShroomState {
return ShroomState{
Params: make(map[string]float32),
Wait: make(chan struct{}),
StatusWait: make(chan struct{}),
Commands: make(chan []byte),
}
}
func (s *ShroomState) Update() {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
close(s.Wait) close(s.Wait)
s.Wait = make(chan struct{}) s.Wait = make(chan struct{})
} }
func (s *ShroomStatus) StatusUpdate() { func (s *ShroomState) StatusUpdate() {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
close(s.StatusWait) close(s.StatusWait)
s.StatusWait = make(chan struct{}) s.StatusWait = make(chan struct{})
} }
func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) { func parseMsg(line []byte, db *sql.DB, state *ShroomState) {
packet := ShroomPacket{} packet := ShroomPacket{}
err := json.Unmarshal(line, &packet) err := json.Unmarshal(line, &packet)
if err != nil { if err != nil {
@ -74,24 +92,24 @@ func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) {
log.Println("unable to write to database: ", err) log.Println("unable to write to database: ", err)
} }
// we got a data packet // we got a data packet
status.Update() state.Update()
} }
} else if packet.Status != nil { } else if packet.Status != nil {
//log.Println("received status ", data.Status) //log.Println("received status ", data.Status)
status.Lock() state.Lock()
// TODO change to have more detailed data // TODO change to have more detailed data
if packet.Status.HumOn != nil { if packet.Status.HumOn != nil {
status.HumidifierOn = *packet.Status.HumOn state.HumidifierOn = *packet.Status.HumOn
} }
status.Unlock() state.Unlock()
status.StatusUpdate() state.StatusUpdate()
} else { } else {
log.Println("unknown packet: ", line, string(line)) log.Println("unknown packet: ", line, string(line))
} }
} }
func InitTcpServer(db *sql.DB, status *ShroomStatus) { func InitTcpServer(db *sql.DB, state *ShroomState) {
// start TCP server for the pipe from the raspberry pi // start TCP server for the pipe from the raspberry pi
ln, err := net.Listen("tcp", ":9876") ln, err := net.Listen("tcp", ":9876")
if err != nil { if err != nil {
@ -111,16 +129,16 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) {
// wrapping in a func() so that I can use defer // wrapping in a func() so that I can use defer
// to automatically decrement the number of connections // to automatically decrement the number of connections
func() { func() {
status.Lock() state.Lock()
status.NumConnections += 1 state.NumConnections += 1
status.Unlock() state.Unlock()
status.StatusUpdate() state.StatusUpdate()
defer func() { defer func() {
status.Lock() state.Lock()
status.NumConnections -= 1 state.NumConnections -= 1
status.Unlock() state.Unlock()
status.StatusUpdate() state.StatusUpdate()
log.Println("connection disconnected") log.Println("connection disconnected")
}() }()
@ -131,7 +149,7 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) {
go func() { go func() {
for { for {
select { select {
case v, ok := <-status.Commands: case v, ok := <-state.Commands:
if !ok { if !ok {
return return
} }
@ -185,7 +203,7 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) {
continue continue
} }
parseMsg(line, db, status) parseMsg(line, db, state)
} }
// shift the remaining data back to the start of the buffer // shift the remaining data back to the start of the buffer
copy(buf[:len(unread)], unread) copy(buf[:len(unread)], unread)

View File

@ -72,12 +72,8 @@ func main() {
log.Fatal("unable to create table ", err) log.Fatal("unable to create table ", err)
} }
status := s.ShroomStatus{ state := s.NewShroomState()
Wait: make(chan struct{}), s.InitTcpServer(db, &state)
StatusWait: make(chan struct{}),
Commands: make(chan []byte),
}
s.InitTcpServer(db, &status)
contentSub, err := fs.Sub(content, "static") contentSub, err := fs.Sub(content, "static")
if err != nil { if err != nil {
@ -101,10 +97,10 @@ func main() {
} }
getStatus := func(w http.ResponseWriter, _req *http.Request) { getStatus := func(w http.ResponseWriter, _req *http.Request) {
status.RLock() state.RLock()
num_connections := status.NumConnections num_connections := state.NumConnections
humidifier := status.HumidifierOn humidifier := state.HumidifierOn
status.RUnlock() state.RUnlock()
s := statusJson{ s := statusJson{
Connected: num_connections > 0, Connected: num_connections > 0,
Humidifier: humidifier, Humidifier: humidifier,
@ -157,7 +153,7 @@ func main() {
return return
} }
select { select {
case status.Commands <- inner_msg: case state.Commands <- inner_msg:
w.Write([]byte("ok")) w.Write([]byte("ok"))
default: default:
w.WriteHeader(503) w.WriteHeader(503)
@ -168,14 +164,14 @@ func main() {
updateHandler := func(w http.ResponseWriter, req *http.Request) { updateHandler := func(w http.ResponseWriter, req *http.Request) {
stillopen := true stillopen := true
for stillopen { for stillopen {
_, stillopen = <-status.Wait _, stillopen = <-state.Wait
} }
w.Write([]byte("ok")) w.Write([]byte("ok"))
} }
statusUpdateHandler := func(w http.ResponseWriter, req *http.Request) { statusUpdateHandler := func(w http.ResponseWriter, req *http.Request) {
stillopen := true stillopen := true
for stillopen { for stillopen {
_, stillopen = <-status.StatusWait _, stillopen = <-state.StatusWait
} }
w.Write([]byte("ok")) w.Write([]byte("ok"))
} }