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.
 
 
 
 

131 lines
2.5 KiB

package main
import (
"context"
"fmt"
"git.aiterp.net/lucifer/new-server/app/client"
"git.aiterp.net/lucifer/new-server/models"
"log"
"os"
"strings"
)
func main() {
cfg, err := loadConfig()
if err != nil {
log.Println("Config at $HOME/.config/lucy.yaml failed:", err)
cfg = &Config{Remote: "http://localhost:8000"}
}
ctx := context.Background()
c := client.Client{APIRoot: cfg.Remote}
cmd := parseCommand(os.Args[1:])
switch cmd.Name {
// DEVICE
case "list":
{
fetchStr := cmd.Params.Get(0).String()
if fetchStr == nil {
s := "all"
fetchStr = &s
}
devices, err := c.GetDevices(ctx, *fetchStr)
if err != nil {
log.Fatalln(err)
}
if info := cmd.Params.Get("info").String(); info != nil {
WriteDeviceInfo(os.Stdout, devices, strings.Split(*info, ","))
} else {
WriteDeviceStateTable(os.Stdout, devices)
}
}
case "set":
{
devices, err := c.PutDeviceState(ctx, cmd.Params.Get(0).StringOr("all"), cmd.Params.DeviceState(""))
if err != nil {
log.Fatalln(err)
}
WriteDeviceStateTable(os.Stdout, devices)
}
case "tag":
{
var addTags []string
var removeTags []string
for _, tag := range cmd.Params.Strings(1) {
if strings.HasPrefix(tag, "+") {
addTags = append(addTags, tag[1:])
} else if strings.HasPrefix(tag, "-") {
removeTags = append(removeTags, tag[1:])
} else {
addTags = append(addTags, tag)
}
}
devices, err := c.PutDeviceTags(
ctx,
cmd.Params.Get(0).StringOr("all"),
addTags,
removeTags,
)
if err != nil {
log.Fatalln(err)
}
WriteDeviceInfoTable(os.Stdout, devices)
}
case "update":
{
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(),
})
if err != nil {
log.Fatalln(err)
}
WriteDeviceInfoTable(os.Stdout, devices)
}
case "factory":
factoryCmd(ctx, c)
case "handler":
handlerCmd(ctx, c)
case "scene":
sceneCmd(ctx, c)
// EVENT
case "run":
{
name := cmd.Params.Get(0).String()
if name == nil {
return
}
event := models.Event{
Name: *name,
Payload: cmd.Params.StringMap(),
}
err := c.FireEvent(ctx, event)
if err != nil {
log.Fatalln(err)
}
}
default:
if cmd.Name != "help" {
log.Println("Unknown command:", cmd.Name)
}
_, _ = fmt.Fprintln(os.Stderr, helpString[1:])
}
}