This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Param struct {
|
||||
@@ -221,6 +222,17 @@ func (p Params) DeviceState(prefix string) models.NewDeviceState {
|
||||
}
|
||||
}
|
||||
|
||||
func (p Params) SceneAssignment(prefix string) *models.DeviceSceneAssignment {
|
||||
if p.Get(prefix+"scene") == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &models.DeviceSceneAssignment{
|
||||
SceneID: p.Get(prefix+"scene").IntOr(-1),
|
||||
Group: p.Get(prefix+"scene.group").StringOr(time.Now().Format(time.RFC3339)),
|
||||
DurationMS: int64(p.Get(prefix+"scene.duration").IntOr(0)),
|
||||
}
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
|
||||
@@ -99,7 +99,7 @@ func handlerCmd(
|
||||
func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandler {
|
||||
// Remove keys
|
||||
for _, elem := range cmd.Params.Strings(1) {
|
||||
if elem[0] != '-' {
|
||||
if !strings.HasPrefix(elem, "-") {
|
||||
continue
|
||||
}
|
||||
keyToRemove := elem[1:]
|
||||
@@ -128,6 +128,10 @@ func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandl
|
||||
model.From = models.Never
|
||||
case "to":
|
||||
model.To = models.Never
|
||||
case "set-scene":
|
||||
model.Actions.SetScene = nil
|
||||
case "push-scene":
|
||||
model.Actions.PushScene = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,5 +177,9 @@ func applyCmdToHandler(model models.EventHandler, cmd Command) models.EventHandl
|
||||
}
|
||||
}
|
||||
|
||||
// Scenes
|
||||
model.Actions.SetScene = cmd.Params.SceneAssignment("set-")
|
||||
model.Actions.PushScene = cmd.Params.SceneAssignment("push-")
|
||||
|
||||
return model
|
||||
}
|
||||
|
||||
+16
-2
@@ -79,7 +79,7 @@ func sceneCmd(
|
||||
}
|
||||
}
|
||||
|
||||
case "assign":
|
||||
case "push", "assign":
|
||||
{
|
||||
fetch := cmd.Params.Get(0).String()
|
||||
id := cmd.Params.Get(1).Int()
|
||||
@@ -87,7 +87,7 @@ func sceneCmd(
|
||||
log.Println("Usage: lucy scene assign <fetch> <id> <group=S> <duration=I>")
|
||||
}
|
||||
|
||||
devices, err := c.AssignDevice(ctx, *fetch, models.DeviceSceneAssignment{
|
||||
devices, err := c.AssignDevice(ctx, *fetch, cmd.Name == "push", models.DeviceSceneAssignment{
|
||||
SceneID: *id,
|
||||
Group: cmd.Params.Get("group").StringOr(*fetch),
|
||||
DurationMS: int64(cmd.Params.Get("duration").IntOr(0)),
|
||||
@@ -99,6 +99,20 @@ func sceneCmd(
|
||||
|
||||
WriteDeviceInfoTable(os.Stdout, devices)
|
||||
}
|
||||
|
||||
case "clear":
|
||||
fetch := cmd.Params.Get(0).String()
|
||||
if fetch == nil {
|
||||
log.Println("Usage: lucy scene clear <fetch>")
|
||||
}
|
||||
|
||||
devices, err := c.ClearDevice(ctx, *fetch)
|
||||
if err != nil {
|
||||
log.Println("Could not clear devices:", err)
|
||||
return
|
||||
}
|
||||
|
||||
WriteDeviceInfoTable(os.Stdout, devices)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+34
-3
@@ -14,16 +14,32 @@ func WriteDeviceStateTable(w io.Writer, devices []models.Device) {
|
||||
table.SetHeader([]string{"ID", "NAME", "POWER", "COLOR", "INTENSITY", "TEMPERATURE"})
|
||||
|
||||
table.SetReflowDuringAutoWrap(true)
|
||||
table.SetColumnAlignment([]int{
|
||||
tablewriter.ALIGN_RIGHT,
|
||||
tablewriter.ALIGN_LEFT,
|
||||
tablewriter.ALIGN_RIGHT,
|
||||
tablewriter.ALIGN_LEFT,
|
||||
tablewriter.ALIGN_RIGHT,
|
||||
tablewriter.ALIGN_RIGHT,
|
||||
})
|
||||
|
||||
for _, v := range devices {
|
||||
powerStr := ""
|
||||
if v.HasCapability(models.DCPower) {
|
||||
powerStr = strconv.FormatBool(v.State.Power)
|
||||
if v.SceneState != nil && v.SceneState.Power != v.State.Power {
|
||||
powerStr = strconv.FormatBool(v.SceneState.Power) + "*"
|
||||
} else {
|
||||
powerStr = strconv.FormatBool(v.State.Power)
|
||||
}
|
||||
}
|
||||
|
||||
colorStr := ""
|
||||
if v.HasCapability(models.DCColorHSK, models.DCColorHS, models.DCColorKelvin) {
|
||||
colorStr = v.State.Color.String()
|
||||
if v.SceneState != nil && v.SceneState.Color.String() != v.State.Color.String() {
|
||||
colorStr = v.SceneState.Color.String() + "*"
|
||||
} else {
|
||||
colorStr = v.State.Color.String()
|
||||
}
|
||||
}
|
||||
|
||||
temperatureString := ""
|
||||
@@ -33,7 +49,11 @@ func WriteDeviceStateTable(w io.Writer, devices []models.Device) {
|
||||
|
||||
intensityString := ""
|
||||
if v.HasCapability(models.DCIntensity) {
|
||||
intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64)
|
||||
if v.SceneState != nil && v.SceneState.Intensity != v.State.Intensity {
|
||||
intensityString = strconv.FormatFloat(v.SceneState.Intensity, 'g', 2, 64) + "*"
|
||||
} else {
|
||||
intensityString = strconv.FormatFloat(v.State.Intensity, 'f', -1, 64)
|
||||
}
|
||||
}
|
||||
|
||||
table.Append([]string{
|
||||
@@ -104,6 +124,17 @@ func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
|
||||
if h.Actions.FireEvent != nil {
|
||||
actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
|
||||
}
|
||||
if h.Actions.FireEvent != nil {
|
||||
actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
|
||||
}
|
||||
if h.Actions.SetScene != nil {
|
||||
s := h.Actions.SetScene
|
||||
actionStr += fmt.Sprintf("setScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
|
||||
}
|
||||
if h.Actions.PushScene != nil {
|
||||
s := h.Actions.PushScene
|
||||
actionStr += fmt.Sprintf("pushScene=(id=%d,group=\"%s\",duration=%d) ", s.SceneID, s.Group, s.DurationMS)
|
||||
}
|
||||
|
||||
eventName := h.EventName
|
||||
if h.OneShot {
|
||||
|
||||
Reference in New Issue
Block a user