github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/pkg/hueemulator/lights.go (about) 1 package hueemulator 2 3 import ( 4 "io/ioutil" 5 "log" 6 "strconv" 7 8 "github.com/gin-gonic/gin" 9 ) 10 11 type light struct { 12 State struct { 13 On bool `json:"on"` 14 Bri uint8 `json:"bri"` 15 Hue int `json:"hue,omitempty"` 16 Sat int `json:"sat,omitempty"` 17 Effect string `json:"effect,omitempty"` 18 Ct int `json:"ct,omitempty"` 19 Alert string `json:"alert,omitempty"` 20 Colormode string `json:"colormode,omitempty"` 21 Reachable bool `json:"reachable"` 22 XY []float64 `json:"xy,omitempty"` 23 } `json:"state"` 24 Type string `json:"type"` 25 Name string `json:"name"` 26 ModelId string `json:"modelid"` 27 ManufacturerName string `json:"manufacturername"` 28 UniqueId string `json:"uniqueid"` 29 SwVersion string `json:"swversion"` 30 } 31 32 type lights struct { 33 Lights map[string]light `json:"lights"` 34 } 35 36 func initLight(id int, name string) *light { 37 l := &light{ 38 Type: "Dimmable light", 39 ModelId: "LWB014", 40 SwVersion: "1.15.0_r18729", 41 ManufacturerName: "Philips", 42 Name: name, 43 UniqueId: strconv.Itoa(id), 44 } 45 l.State.Reachable = true 46 return l 47 } 48 49 func enumerateLights() map[string]*light { 50 lightList := make(map[string]*light) 51 handlerMapLock.Lock() 52 for _, hstate := range handlerMap { 53 lightList[strconv.Itoa(hstate.Id)] = hstate.Light 54 } 55 handlerMapLock.Unlock() 56 return lightList 57 } 58 59 func getLightsList(c *gin.Context) { 60 c.JSON(200, enumerateLights()) 61 } 62 63 type request struct { 64 On bool `json:"on"` 65 Brightness uint8 `json:"bri"` 66 Hue uint16 `json:"hue"` 67 Sat uint8 `json:"sat"` 68 } 69 70 func setLightState(c *gin.Context) { 71 req := &request{} 72 err := c.BindJSON(req) 73 if err != nil { 74 log.Println(err) 75 defer c.Request.Body.Close() 76 body, err := ioutil.ReadAll(c.Request.Body) 77 if err != nil { 78 log.Println(err) 79 } 80 log.Println("Request body was:", string(body)) 81 return 82 } 83 84 lightId := c.Param("lightId") 85 86 log.Println("[DEVICE]", c.Param("userId"), "requested state:", req.On, "requested brightness: ", req.Brightness) 87 88 //if hstate, ok := handlerMap[lightId]; ok { 89 if hstate := getHueStateById(lightId); hstate != nil { 90 hstate.Handler(Request{ 91 UserId: c.Param("userId"), 92 //RequestedOnState: req.On, 93 Request: req, 94 RemoteAddr: c.Request.RemoteAddr, 95 }) 96 hstate.Light.State.On = req.On 97 hstate.Light.State.Bri = req.Brightness 98 log.Println("[DEVICE] handler replied with state:", req.On) 99 //handlerMap[lightId] = hstate 100 c.Writer.Write([]byte("[{\"success\":{\"/lights/" + lightId + "/state/on\":" + strconv.FormatBool(req.On) + "}}]")) 101 } 102 } 103 104 func getHueStateById(id string) *huestate { 105 for _, h := range handlerMap { 106 if strconv.Itoa(h.Id) == id { 107 return h 108 } 109 } 110 return nil 111 } 112 113 func getLightInfo(c *gin.Context) { 114 id := c.Param("lightId") 115 hs := getHueStateById(id) 116 c.JSON(200, hs.Light) 117 }