You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.9 KiB
144 lines
3.9 KiB
package hue2
|
|
|
|
import (
|
|
"fmt"
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type DeviceData struct {
|
|
ID string `json:"id"`
|
|
LegacyID string `json:"id_v1"`
|
|
Metadata DeviceMetadata `json:"metadata"`
|
|
Type string `json:"type"`
|
|
|
|
ProductData DeviceProductData `json:"product_data"`
|
|
Services []ResourceLink `json:"services"`
|
|
}
|
|
|
|
type DeviceMetadata struct {
|
|
Archetype string `json:"archetype"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type DeviceProductData struct {
|
|
Certified bool `json:"certified"`
|
|
ManufacturerName string `json:"manufacturer_name"`
|
|
ModelID string `json:"model_id"`
|
|
ProductArchetype string `json:"product_archetype"`
|
|
ProductName string `json:"product_name"`
|
|
SoftwareVersion string `json:"software_version"`
|
|
}
|
|
|
|
type ResourceData struct {
|
|
ID string `json:"id"`
|
|
LegacyID string `json:"id_v1"`
|
|
Metadata DeviceMetadata `json:"metadata"`
|
|
Type string `json:"type"`
|
|
|
|
Mode *string `json:"mode"`
|
|
|
|
Owner *ResourceLink `json:"owner"`
|
|
ProductData *DeviceProductData `json:"product_data"`
|
|
Services []ResourceLink `json:"services"`
|
|
Power *LightPower `json:"on"`
|
|
Color *LightColor `json:"color"`
|
|
ColorTemperature *LightCT `json:"color_temperature"`
|
|
Dimming *LightDimming `json:"dimming"`
|
|
Dynamics *LightDynamics `json:"dynamics"`
|
|
Alert *LightAlert `json:"alert"`
|
|
PowerState *PowerState `json:"power_state"`
|
|
Status *string `json:"status"`
|
|
}
|
|
|
|
func (res *ResourceData) ServiceID(kind string) *string {
|
|
for _, ptr := range res.Services {
|
|
if ptr.Kind == kind {
|
|
return &ptr.ID
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type PowerState struct {
|
|
BatteryState string `json:"battery_state"`
|
|
BatteryLevel float64 `json:"battery_level"`
|
|
}
|
|
|
|
type LightPower struct {
|
|
On bool `json:"on"`
|
|
}
|
|
|
|
type LightDimming struct {
|
|
Brightness float64 `json:"brightness"`
|
|
}
|
|
|
|
type LightColor struct {
|
|
Gamut models.ColorGamut `json:"gamut"`
|
|
GamutType string `json:"gamut_type"`
|
|
XY models.ColorXY `json:"xy"`
|
|
}
|
|
|
|
type LightCT struct {
|
|
Mirek *int `json:"mirek"`
|
|
MirekSchema LightCTMirekSchema `json:"mirek_schema"`
|
|
MirekValid bool `json:"mirek_valid"`
|
|
}
|
|
|
|
type LightCTMirekSchema struct {
|
|
MirekMaximum int `json:"mirek_maximum"`
|
|
MirekMinimum int `json:"mirek_minimum"`
|
|
}
|
|
|
|
type LightDynamics struct {
|
|
Speed float64 `json:"speed"`
|
|
SpeedValid bool `json:"speed_valid"`
|
|
Status string `json:"status"`
|
|
StatusValues []string `json:"status_values"`
|
|
}
|
|
|
|
type LightAlert struct {
|
|
ActionValues []string `json:"action_values"`
|
|
}
|
|
|
|
type ResourceUpdate struct {
|
|
Power *bool
|
|
ColorXY *models.ColorXY
|
|
Brightness *float64
|
|
Mirek *int
|
|
TransitionDuration *time.Duration
|
|
}
|
|
|
|
func (r ResourceUpdate) MarshalJSON() ([]byte, error) {
|
|
chunks := make([]string, 0, 4)
|
|
if r.Power != nil {
|
|
chunks = append(chunks, fmt.Sprintf(`"on":{"on":%v}`, *r.Power))
|
|
}
|
|
if r.ColorXY != nil {
|
|
chunks = append(chunks, fmt.Sprintf(`"color":{"xy":{"x":%f,"y":%f}}`, r.ColorXY.X, r.ColorXY.Y))
|
|
}
|
|
if r.Brightness != nil {
|
|
chunks = append(chunks, fmt.Sprintf(`"dimming":{"brightness":%f}`, *r.Brightness))
|
|
}
|
|
if r.Mirek != nil {
|
|
chunks = append(chunks, fmt.Sprintf(`"color_temperature":{"mirek":%d}`, *r.Mirek))
|
|
}
|
|
if r.TransitionDuration != nil {
|
|
chunks = append(chunks, fmt.Sprintf(`"dynamics":{"duration":%d}`, r.TransitionDuration.Truncate(time.Millisecond*100).Milliseconds()))
|
|
}
|
|
|
|
fmt.Println(fmt.Sprintf("{%s}", strings.Join(chunks, ",")))
|
|
|
|
return []byte(fmt.Sprintf("{%s}", strings.Join(chunks, ","))), nil
|
|
}
|
|
|
|
type ResourceLink struct {
|
|
ID string `json:"rid"`
|
|
Kind string `json:"rtype"`
|
|
}
|
|
|
|
func (rl *ResourceLink) Path() string {
|
|
return fmt.Sprintf("/clip/v2/resource/%s/%s", rl.Kind, rl.ID)
|
|
}
|