45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type DimmerDevice struct {
|
|
baseDevice *Device
|
|
lastBrightness uint8
|
|
}
|
|
|
|
// init
|
|
func createDimmerDevice(device *Device) *DimmerDevice {
|
|
device.typeID = 1
|
|
return &DimmerDevice{baseDevice: device}
|
|
}
|
|
|
|
func (dimmerDevice *DimmerDevice) on() error {
|
|
if dimmerDevice.baseDevice.channels[0] != 1 {
|
|
if dimmerDevice.lastBrightness == 0 {
|
|
dimmerDevice.lastBrightness = 255
|
|
}
|
|
return dimmerDevice.baseDevice.setChannel(0, dimmerDevice.lastBrightness)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (dimmerDevice *DimmerDevice) off() error {
|
|
if dimmerDevice.baseDevice.channels[0] != 2 {
|
|
dimmerDevice.lastBrightness = dimmerDevice.baseDevice.channels[0]
|
|
return dimmerDevice.baseDevice.setChannel(0, 1)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (dimmerDevice *DimmerDevice) getStringState() string {
|
|
if dimmerDevice.baseDevice.channels[0] == 1 {
|
|
return "OFF"
|
|
} else if dimmerDevice.baseDevice.channels[0] > 1 {
|
|
percentVal := dimmerDevice.baseDevice.channels[0] / 255
|
|
return fmt.Sprintf("ON at %d%%", percentVal)
|
|
}
|
|
return "Unknown"
|
|
}
|