github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-deconz/models/light.go (about)

     1  package models
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models/devices"
     7  )
     8  
     9  // Lights is a list of Light's
    10  type Lights map[string]Light
    11  
    12  func NewLights() Lights {
    13  	return make(map[string]Light)
    14  }
    15  
    16  // Light is a deconz RESP api light
    17  type Light struct {
    18  	Etag         string `json:"etag"`
    19  	Hascolor     bool   `json:"hascolor"`
    20  	Manufacturer string `json:"manufacturer"`
    21  	Modelid      string `json:"modelid"`
    22  	Name         string `json:"name"`
    23  	State        devices.State
    24  	//Pointsymbol  struct {
    25  	//} `json:"pointsymbol"`
    26  	//State struct {
    27  	//Alert     string    `json:"alert"`
    28  	//Bri       int       `json:"bri"`
    29  	//Colormode string    `json:"colormode"`
    30  	//Ct        int       `json:"ct"`
    31  	//Effect    string    `json:"effect"`
    32  	//Hue       int       `json:"hue"`
    33  	//On        bool      `json:"on"`
    34  	//Reachable bool      `json:"reachable"`
    35  	//Sat       int       `json:"sat"`
    36  	//Xy        []float64 `json:"xy"`
    37  	//} `json:"state"`
    38  	Swversion string `json:"swversion"`
    39  	Type      string `json:"type"`
    40  	Uniqueid  string `json:"uniqueid"`
    41  }
    42  
    43  func LightToDeviceState(lightState, state devices.State) bool {
    44  
    45  	changes := 0
    46  	lightState.Float("bri", func(f float64) {
    47  		if f != state["brightness"] {
    48  			changes++
    49  		}
    50  		state["brightness"] = f / 255
    51  	})
    52  
    53  	lightState.Bool("on", func(v bool) {
    54  		if v != state["on"] {
    55  			changes++
    56  		}
    57  		state["on"] = v
    58  	})
    59  
    60  	lightState.Float("ct", func(v float64) {
    61  		v = (500 + 153) - v // invert value
    62  		temp := float64(math.Round(((v - 153) / (500 - 153) * (6500 - 2000)) + 2000))
    63  		if temp != state["temperature"] {
    64  			changes++
    65  		}
    66  		state["temperature"] = temp
    67  	})
    68  
    69  	return changes > 0
    70  }
    71  
    72  func (l Light) GenerateDevice(id string) *devices.Device {
    73  
    74  	online := false
    75  	l.State.Bool("reachable", func(v bool) { online = v })
    76  	state := devices.State{}
    77  	LightToDeviceState(l.State, state)
    78  
    79  	dev := &devices.Device{
    80  		Type: l.GetType(),
    81  		ID: devices.ID{
    82  			ID: id,
    83  		},
    84  		Name:   l.Name,
    85  		Online: online,
    86  		State:  state,
    87  		Traits: l.GetTraits(),
    88  	}
    89  	return dev
    90  }
    91  
    92  // GetTraits converts deconz types to stampzilla devices types
    93  func (l Light) GetTraits() []string {
    94  	var traits []string
    95  	//traits :=
    96  	switch l.Type {
    97  	case "On/Off plug-in unit":
    98  		traits = []string{
    99  			"OnOff",
   100  		}
   101  	case "Color temperature light":
   102  		traits = []string{
   103  			"OnOff",
   104  			"ColorSetting",
   105  			"Brightness",
   106  		}
   107  	}
   108  
   109  	return traits
   110  }
   111  
   112  // GetType converts deconz types to stampzilla devices types
   113  func (l Light) GetType() string {
   114  	switch l.Type {
   115  	case "On/Off plug-in unit":
   116  		return "switch"
   117  	case "Color temperature light":
   118  		return "light"
   119  	}
   120  
   121  	return "light"
   122  }