initial commit (some event stuffs)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package device
|
||||
|
||||
import "math/bits"
|
||||
|
||||
type SupportFlags uint32
|
||||
|
||||
func (f SupportFlags) HasAny(d SupportFlags) bool {
|
||||
return (f & d) != 0
|
||||
}
|
||||
|
||||
func (f SupportFlags) HasAll(d SupportFlags) bool {
|
||||
return bits.OnesCount32(uint32(f&d)) == bits.OnesCount32(uint32(d))
|
||||
}
|
||||
|
||||
const (
|
||||
SFlagPower SupportFlags = 1 << iota
|
||||
SFlagIntensity
|
||||
SFlagColor
|
||||
SFlagTemperature
|
||||
SFlagSensorButtons
|
||||
SFlagSensorTemperature
|
||||
SFlagSensorLightLevel
|
||||
SFlagSensorPresence
|
||||
)
|
||||
|
||||
// ColorFlag is primarily to detect warm-white lights, as XY/RGB/HS/HSK can convert without trouble.
|
||||
type ColorFlag uint32
|
||||
|
||||
const (
|
||||
CFlagXY ColorFlag = 1 << iota
|
||||
CFlagRGB
|
||||
CFlagHS
|
||||
CFlagHSK
|
||||
CFlagKelvin
|
||||
)
|
||||
|
||||
func (f ColorFlag) IsColor() bool {
|
||||
return f.HasAny(CFlagXY | CFlagRGB | CFlagHS | CFlagHSK)
|
||||
}
|
||||
|
||||
func (f ColorFlag) IsWarmWhite() bool {
|
||||
return f&CFlagKelvin == CFlagKelvin
|
||||
}
|
||||
|
||||
func (f ColorFlag) IsColorOnly() bool {
|
||||
return f != 0 && f&CFlagKelvin == 0
|
||||
}
|
||||
|
||||
func (f ColorFlag) IsWarmWhiteOnly() bool {
|
||||
return f == CFlagKelvin
|
||||
}
|
||||
|
||||
func (f ColorFlag) HasAny(d ColorFlag) bool {
|
||||
return (f & d) != 0
|
||||
}
|
||||
|
||||
func (f ColorFlag) HasAll(d ColorFlag) bool {
|
||||
return bits.OnesCount32(uint32(f&d)) == bits.OnesCount32(uint32(d))
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer3/server/internal/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
Power *bool `json:"power"`
|
||||
Temperature *float64 `json:"temperature"`
|
||||
Intensity *float64 `json:"intensity"`
|
||||
Color *color.Color `json:"color"`
|
||||
|
||||
SensedTemperature *float64 `json:"sensedTemperature"`
|
||||
SensedLightLevel *float64 `json:"sensedLightLevel"`
|
||||
SensedPresence *PresenceState `json:"sensedPresence"`
|
||||
SensedButton *int `json:"sensedButton"`
|
||||
}
|
||||
|
||||
func (d State) String() string {
|
||||
parts := make([]string, 0, 4)
|
||||
if d.Power != nil {
|
||||
parts = append(parts, fmt.Sprintf("power:%t", *d.Power))
|
||||
}
|
||||
if d.Temperature != nil {
|
||||
parts = append(parts, fmt.Sprintf("temperature:%f", *d.Temperature))
|
||||
}
|
||||
if d.Intensity != nil {
|
||||
parts = append(parts, fmt.Sprintf("intensity:%.2f", *d.Intensity))
|
||||
}
|
||||
if d.Color != nil {
|
||||
parts = append(parts, fmt.Sprintf("color:%s", d.Color.String()))
|
||||
}
|
||||
|
||||
if d.SensedTemperature != nil {
|
||||
parts = append(parts, fmt.Sprintf("sensedTemperature:%.2f", *d.SensedTemperature))
|
||||
}
|
||||
if d.SensedLightLevel != nil {
|
||||
parts = append(parts, fmt.Sprintf("sensedLightLevel:%.1f", *d.SensedLightLevel))
|
||||
}
|
||||
if d.SensedPresence != nil {
|
||||
parts = append(parts, fmt.Sprintf("sensedPresense:(%t,%.1f)", d.SensedPresence.Present, d.SensedPresence.AbsenceSeconds))
|
||||
}
|
||||
if d.SensedButton != nil {
|
||||
parts = append(parts, fmt.Sprintf("sensedButton:%d", *d.SensedButton))
|
||||
}
|
||||
|
||||
return fmt.Sprint("(", strings.Join(parts, ", "), ")")
|
||||
}
|
||||
|
||||
type PresenceState struct {
|
||||
Present bool `json:"present"`
|
||||
AbsenceSeconds float64 `json:"absenceSeconds,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user