@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/app/client"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func handlerCmd(
|
||||
ctx context.Context,
|
||||
c client.Client,
|
||||
) {
|
||||
cmd := parseCommand(os.Args[2:])
|
||||
|
||||
switch cmd.Name {
|
||||
case "list":
|
||||
handlers, err := c.GetHandlers(ctx, cmd.Params.Get(0).StringOr("all"))
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
WriteHandlerInfoTable(os.Stdout, handlers)
|
||||
default:
|
||||
if cmd.Name != "help" {
|
||||
log.Println("Unknown command:", cmd.Name)
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintln(os.Stderr, helpString[1:])
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ EXAMPLES
|
||||
lucy run SetProfile name=evening
|
||||
|
||||
DEVICE COMMANDS
|
||||
handler list <search>
|
||||
list <search>
|
||||
set <search> <power=B> <color=C> <intensity=F> <temperature=N>
|
||||
tag <search> <[+/-]tag-name>
|
||||
|
||||
+10
-5
@@ -17,6 +17,8 @@ func main() {
|
||||
cfg = &Config{Remote: "http://localhost:8000"}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
c := client.Client{APIRoot: cfg.Remote}
|
||||
cmd := parseCommand(os.Args[1:])
|
||||
|
||||
@@ -30,7 +32,7 @@ func main() {
|
||||
fetchStr = &s
|
||||
}
|
||||
|
||||
devices, err := c.GetDevices(context.Background(), *fetchStr)
|
||||
devices, err := c.GetDevices(ctx, *fetchStr)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
@@ -39,7 +41,7 @@ func main() {
|
||||
}
|
||||
case "set":
|
||||
{
|
||||
devices, err := c.PutDeviceState(context.Background(), cmd.Params.Get(0).StringOr("all"), models.NewDeviceState{
|
||||
devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), models.NewDeviceState{
|
||||
Power: cmd.Params.Get("power").Bool(),
|
||||
Color: cmd.Params.Get("color").String(),
|
||||
Intensity: cmd.Params.Get("intensity").Float(),
|
||||
@@ -67,7 +69,7 @@ func main() {
|
||||
}
|
||||
|
||||
devices, err := c.PutDeviceTags(
|
||||
context.Background(),
|
||||
ctx,
|
||||
cmd.Params.Get(0).StringOr("all"),
|
||||
addTags,
|
||||
removeTags,
|
||||
@@ -80,7 +82,7 @@ func main() {
|
||||
}
|
||||
case "update":
|
||||
{
|
||||
devices, err := c.PutDevice(context.Background(), cmd.Params.Get(0).StringOr("all"), models.DeviceUpdate{
|
||||
devices, err := c.PutDevice(ctx, cmd.Params.Get(0).StringOr("all"), models.DeviceUpdate{
|
||||
Name: cmd.Params.Get("name").String(),
|
||||
Icon: cmd.Params.Get("icon").String(),
|
||||
UserProperties: cmd.Params.Subset("prop.").StringPtrMap(),
|
||||
@@ -92,6 +94,9 @@ func main() {
|
||||
WriteDeviceInfoTable(os.Stdout, devices)
|
||||
}
|
||||
|
||||
case "handler":
|
||||
handlerCmd(ctx, c)
|
||||
|
||||
// EVENT
|
||||
case "run":
|
||||
{
|
||||
@@ -105,7 +110,7 @@ func main() {
|
||||
Payload: cmd.Params.StringMap(),
|
||||
}
|
||||
|
||||
err := c.FireEvent(context.Background(), event)
|
||||
err := c.FireEvent(ctx, event)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"io"
|
||||
@@ -72,3 +73,45 @@ func WriteDeviceInfoTable(w io.Writer, devices []models.Device) {
|
||||
|
||||
table.Render()
|
||||
}
|
||||
|
||||
func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
|
||||
table := tablewriter.NewWriter(w)
|
||||
table.SetHeader([]string{"ID", "EVENT NAME", "ONE SHOT", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"})
|
||||
table.SetReflowDuringAutoWrap(false)
|
||||
|
||||
for _, h := range handlers {
|
||||
condStr := ""
|
||||
for key, value := range h.Conditions {
|
||||
condStr += fmt.Sprintf("%s=%s ", key, value)
|
||||
}
|
||||
|
||||
actionStr := ""
|
||||
if h.Actions.SetPower != nil {
|
||||
actionStr += fmt.Sprintf("setPower=%t ", *h.Actions.SetPower)
|
||||
}
|
||||
if h.Actions.SetColor != nil {
|
||||
actionStr += fmt.Sprintf("setColor=%s ", *h.Actions.SetColor)
|
||||
}
|
||||
if h.Actions.SetIntensity != nil {
|
||||
actionStr += fmt.Sprintf("setIntensity=%.02f ", *h.Actions.SetIntensity)
|
||||
}
|
||||
if h.Actions.AddIntensity != nil {
|
||||
actionStr += fmt.Sprintf("addIntensity=%.02f ", *h.Actions.AddIntensity)
|
||||
}
|
||||
if h.Actions.FireEvent != nil {
|
||||
actionStr += fmt.Sprintf("fireEvent=%s ", (*h.Actions.FireEvent).Name)
|
||||
}
|
||||
|
||||
table.Append([]string{
|
||||
strconv.Itoa(h.ID),
|
||||
h.EventName,
|
||||
strconv.FormatBool(h.OneShot),
|
||||
strconv.Itoa(h.Priority),
|
||||
condStr,
|
||||
fmt.Sprintf("%s:%s", h.TargetKind, h.TargetValue),
|
||||
actionStr,
|
||||
})
|
||||
}
|
||||
|
||||
table.Render()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user