func (g *Gadget) SendCommand(cmd string) error { m := gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "quimby", Type: gogadgets.COMMAND, Body: cmd, } return g.UpdateMessage(m) }
func (g *Gadget) Method(steps []string) error { m := gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "quimby", Type: gogadgets.METHOD, Method: gogadgets.Method{ Steps: steps, }, } return g.UpdateMessage(m) }
func (g *Gadget) createGadget(tx *bolt.Tx) error { g.Id = gogadgets.GetUUID() b, err := tx.CreateBucket([]byte(g.Id)) if err != nil { return err } if _, err := b.CreateBucket(_notes); err != nil { return err } _, err = b.CreateBucket(_stats) return err }
func connectThermometer(t *accessory.Thermometer, g Gadget, k string) { ch := make(chan gogadgets.Message) uuid := gogadgets.GetUUID() Clients.Add(g.Host, uuid, ch) go func(ch chan gogadgets.Message, k string, t *accessory.Thermometer) { for { msg := <-ch key := fmt.Sprintf("%s %s", msg.Location, msg.Name) if key == k { temp := (msg.Value.Value.(float64) - 32.0) / 1.8 t.TempSensor.CurrentTemperature.SetValue(temp) } } }(ch, k, t) }
func (h *HLT) sendUpdate() { h.out <- gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "hlt volume", Location: "hlt", Name: "volume", Type: "update", Timestamp: time.Now().UTC(), Value: gogadgets.Value{ Value: h.volumes["hlt"], Units: "gallons", }, Info: gogadgets.Info{ Direction: "input", }, } }
func (b *Boiler) sendUpdate() { b.out <- gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "boiler volume", Location: "boiler", Name: "volume", Type: "update", Timestamp: time.Now().UTC(), Value: gogadgets.Value{ Value: b.volumes["boiler"], Units: "gallons", }, Info: gogadgets.Info{ Direction: "input", }, } }
func (c *Carboy) sendUpdate() { c.out <- gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "carboy volume", Location: "carboy", Name: "volume", Type: "update", Timestamp: time.Now().UTC(), Value: gogadgets.Value{ Value: c.volumes["carboy"], Units: "gallons", }, Info: gogadgets.Info{ Direction: "input", }, } }
func (t *Tun) sendUpdate() { msg := gogadgets.Message{ UUID: gogadgets.GetUUID(), Sender: "tun volume", Location: "tun", Name: "volume", Type: "update", Timestamp: time.Now().UTC(), Value: gogadgets.Value{ Value: t.volumes["tun"] * ML_TO_GALLONS, Units: "gallons", }, Info: gogadgets.Info{ Direction: "input", }, } t.out <- msg }
func connectSwitch(s *accessory.Switch, g Gadget, k string) { s.Switch.On.OnValueRemoteUpdate(func(on bool) { if on == true { g.SendCommand(fmt.Sprintf("turn on %s", k)) } else { g.SendCommand(fmt.Sprintf("turn off %s", k)) } }) ch := make(chan gogadgets.Message) uuid := gogadgets.GetUUID() Clients.Add(g.Host, uuid, ch) go func(ch chan gogadgets.Message, k string, s *accessory.Switch) { for { msg := <-ch key := fmt.Sprintf("%s %s", msg.Location, msg.Name) if key == k { s.Switch.On.SetValue(msg.Value.Value.(bool)) } } }(ch, k, s) }
//Registers with a gogadget instance and starts up //a websocket. It pushes new messages from the //instance to the websocket and vice versa. func Connect(w http.ResponseWriter, req *http.Request) { args := GetArgs(req) if err := quimby.Register(*args.Gadget); err != nil { context.Set(req, "error", err) return } ws := make(chan gogadgets.Message) q := make(chan bool) ch := make(chan gogadgets.Message) uuid := gogadgets.GetUUID() quimby.Clients.Add(args.Gadget.Host, uuid, ch) upgrader := websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { return true }, } conn, err := upgrader.Upgrade(w, req, nil) if err != nil { context.Set(req, "error", err) return // err } go listen(conn, ws, q) for { select { case msg := <-ws: args.Gadget.UpdateMessage(msg) case msg := <-ch: sendSocketMessage(conn, msg) case <-q: quimby.Clients.Delete(args.Gadget.Host, uuid) return } } }
"/websocket", ) h := http.Header{"Origin": {u}, "Authorization": {token}} var err error ws, _, err = dialer.Dial(u, h) Expect(err).To(BeNil()) }) AfterEach(func() { ws.Close() }) It("allows the sending of a message with a websocket", func() { uuid := gogadgets.GetUUID() msg := gogadgets.Message{ Type: gogadgets.COMMAND, Body: "turn on back yard sprinklers", UUID: uuid, Sender: "cli", } d, _ := json.Marshal(msg) err := ws.WriteMessage(websocket.TextMessage, d) Expect(err).To(BeNil()) Eventually(func() string { d, err := ioutil.ReadFile("/tmp/sprinklers.txt") Expect(err).To(BeNil()) return string(d) }).Should(Equal("1"))