From af7627a15a7a61e0bd4a9cac2e65e6c06722642b Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Tue, 16 May 2023 10:24:50 -0400 Subject: [PATCH] Rename ShroomStatus to ShroomState --- shroom_internals/tcp_server.go | 64 ++++++++++++++++++++++------------ shroom_server.go | 22 +++++------- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/shroom_internals/tcp_server.go b/shroom_internals/tcp_server.go index 27d22cb..ae01a5d 100644 --- a/shroom_internals/tcp_server.go +++ b/shroom_internals/tcp_server.go @@ -18,7 +18,8 @@ func newlinePos(s []byte) int { } type StatusJson struct { - HumOn *bool `json:"humidifier"` + HumOn *bool `json:"humidifier"` + ManualMode *bool `json:"manual_mode"` } type DataJson struct { @@ -28,36 +29,53 @@ type DataJson struct { HumidifierVolts *float32 `json:"hv"` } +type ParamJson struct { + Name string `json:"name"` + Value float32 `json:"string"` +} + type ShroomPacket struct { Data *DataJson `json:"data"` Status *StatusJson `json:"status"` + Param *ParamJson `json:"param"` } -type ShroomStatus struct { +type ShroomState struct { sync.RWMutex HumidifierOn bool NumConnections int - Wait chan struct{} - StatusWait chan struct{} + + Params map[string]float32 + Wait chan struct{} + StatusWait chan struct{} 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() defer s.Unlock() close(s.Wait) s.Wait = make(chan struct{}) } -func (s *ShroomStatus) StatusUpdate() { +func (s *ShroomState) StatusUpdate() { s.Lock() defer s.Unlock() close(s.StatusWait) 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{} err := json.Unmarshal(line, &packet) if err != nil { @@ -74,24 +92,24 @@ func parseMsg(line []byte, db *sql.DB, status *ShroomStatus) { log.Println("unable to write to database: ", err) } // we got a data packet - status.Update() + state.Update() } } else if packet.Status != nil { //log.Println("received status ", data.Status) - status.Lock() + state.Lock() // TODO change to have more detailed data if packet.Status.HumOn != nil { - status.HumidifierOn = *packet.Status.HumOn + state.HumidifierOn = *packet.Status.HumOn } - status.Unlock() - status.StatusUpdate() + state.Unlock() + state.StatusUpdate() } else { 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 ln, err := net.Listen("tcp", ":9876") if err != nil { @@ -111,16 +129,16 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) { // wrapping in a func() so that I can use defer // to automatically decrement the number of connections func() { - status.Lock() - status.NumConnections += 1 - status.Unlock() - status.StatusUpdate() + state.Lock() + state.NumConnections += 1 + state.Unlock() + state.StatusUpdate() defer func() { - status.Lock() - status.NumConnections -= 1 - status.Unlock() - status.StatusUpdate() + state.Lock() + state.NumConnections -= 1 + state.Unlock() + state.StatusUpdate() log.Println("connection disconnected") }() @@ -131,7 +149,7 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) { go func() { for { select { - case v, ok := <-status.Commands: + case v, ok := <-state.Commands: if !ok { return } @@ -185,7 +203,7 @@ func InitTcpServer(db *sql.DB, status *ShroomStatus) { continue } - parseMsg(line, db, status) + parseMsg(line, db, state) } // shift the remaining data back to the start of the buffer copy(buf[:len(unread)], unread) diff --git a/shroom_server.go b/shroom_server.go index 0b194fb..d5a4ae4 100644 --- a/shroom_server.go +++ b/shroom_server.go @@ -72,12 +72,8 @@ func main() { log.Fatal("unable to create table ", err) } - status := s.ShroomStatus{ - Wait: make(chan struct{}), - StatusWait: make(chan struct{}), - Commands: make(chan []byte), - } - s.InitTcpServer(db, &status) + state := s.NewShroomState() + s.InitTcpServer(db, &state) contentSub, err := fs.Sub(content, "static") if err != nil { @@ -101,10 +97,10 @@ func main() { } getStatus := func(w http.ResponseWriter, _req *http.Request) { - status.RLock() - num_connections := status.NumConnections - humidifier := status.HumidifierOn - status.RUnlock() + state.RLock() + num_connections := state.NumConnections + humidifier := state.HumidifierOn + state.RUnlock() s := statusJson{ Connected: num_connections > 0, Humidifier: humidifier, @@ -157,7 +153,7 @@ func main() { return } select { - case status.Commands <- inner_msg: + case state.Commands <- inner_msg: w.Write([]byte("ok")) default: w.WriteHeader(503) @@ -168,14 +164,14 @@ func main() { updateHandler := func(w http.ResponseWriter, req *http.Request) { stillopen := true for stillopen { - _, stillopen = <-status.Wait + _, stillopen = <-state.Wait } w.Write([]byte("ok")) } statusUpdateHandler := func(w http.ResponseWriter, req *http.Request) { stillopen := true for stillopen { - _, stillopen = <-status.StatusWait + _, stillopen = <-state.StatusWait } w.Write([]byte("ok")) }