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

     1  package models
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models/devices"
     7  )
     8  
     9  /*
    10  {
    11      "config": {
    12        "battery": 100,
    13        "offset": 0,
    14        "on": true,
    15        "reachable": true
    16      },
    17      "ep": 1,
    18      "etag": "1f0d135e4c7bc074090166ea9fbac900",
    19      "manufacturername": "LUMI",
    20      "modelid": "lumi.sensor_ht",
    21      "name": "Humidity 4",
    22      "state": {
    23        "humidity": 2056,
    24        "lastupdated": "2019-01-20T20:14:18"
    25      },
    26      "type": "ZHAHumidity",
    27      "uniqueid": "00:15:8d:00:02:3d:26:5e-01-0405"
    28    }
    29  */
    30  // Lights is a list of Light's
    31  type Sensors map[string]Sensor
    32  
    33  func NewSensors() Sensors {
    34  	return make(Sensors)
    35  }
    36  
    37  type Sensor struct {
    38  	Config           devices.State `json:"config"`
    39  	Ep               int           `json:"ep"`
    40  	Manufacturername string        `json:"manufacturername"`
    41  	Modelid          string        `json:"modelid"`
    42  	Name             string        `json:"name"`
    43  	State            devices.State `json:"state"`
    44  	Type             string        `json:"type"`
    45  	UniqueID         string        `json:"uniqueid"`
    46  	ID               string        `json:"id"`
    47  }
    48  
    49  //Get ID returns different IDs if its a light or sensor. This is because a single sensor device can be devided in multiple sensors in the API
    50  func (s *Sensor) GetID() string {
    51  	// take first part of uniqueid 00:15:8d:00:02:55:82:0f-01-0402 which is the mac address
    52  	return strings.SplitN(s.UniqueID, "-", 2)[0]
    53  }
    54  
    55  func (s Sensor) GenerateDevice() *devices.Device {
    56  
    57  	online := false
    58  	s.Config.Bool("reachable", func(v bool) { online = v })
    59  	state := devices.State{}
    60  	SensorToDeviceState(s.State, state)
    61  
    62  	s.Config.Float("battery", func(b float64) { state["battery"] = int(b) })
    63  
    64  	dev := &devices.Device{
    65  		Type: "sensor",
    66  		ID: devices.ID{
    67  			ID: s.GetID(),
    68  		},
    69  		Name:   s.Name,
    70  		Online: online,
    71  		State:  state,
    72  		//Traits: s.GetTraits(),
    73  	}
    74  	return dev
    75  }
    76  func SensorToDeviceState(sensorsState, state devices.State) bool {
    77  	//for k, v := range sensorsState {
    78  	//fmt.Printf("%s: %T %v\n", k, v, v)
    79  	//}
    80  	changes := 0
    81  	sensorsState.String("lastupdated", func(s string) {
    82  		state["lastupdated"] = s
    83  	})
    84  
    85  	sensorsState.Float("temperature", func(t float64) {
    86  		tf := t / 100.0
    87  		if state["temperature"] != tf {
    88  			changes++
    89  		}
    90  		state["temperature"] = tf
    91  	})
    92  	sensorsState.Float("humidity", func(h float64) {
    93  		hf := h / 100.0
    94  		if state["temperature"] != hf {
    95  			changes++
    96  		}
    97  		state["humidity"] = hf
    98  	})
    99  
   100  	return changes > 0
   101  }