2022-12-30 06:51:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
|
|
|
|
package hue
|
|
|
|
|
|
|
|
import (
|
2023-01-02 06:54:23 +00:00
|
|
|
"fmt"
|
2022-12-30 06:51:51 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.rob.mx/nidito/chinampa/pkg/command"
|
|
|
|
"git.rob.mx/nidito/puerta/internal/door"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-04-16 21:17:36 +00:00
|
|
|
var SetupHueCommand = &command.Command{
|
2022-12-30 06:51:51 +00:00
|
|
|
Path: []string{"hue", "setup"},
|
|
|
|
Summary: "Creates a local hue user and finds out available plugs",
|
|
|
|
Description: "",
|
|
|
|
Arguments: command.Arguments{
|
|
|
|
{
|
|
|
|
Name: "ip",
|
|
|
|
Description: "The ip address of the bridge",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "domain",
|
|
|
|
Description: "the domain or application name to use when registering",
|
|
|
|
Default: "puerta.nidi.to",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cmd *command.Command) error {
|
|
|
|
ip := cmd.Arguments[0].ToValue().(string)
|
|
|
|
domain := cmd.Arguments[1].ToValue().(string)
|
|
|
|
|
|
|
|
logrus.Infof("Setting up with bridge at %s, app %s", ip, domain)
|
2023-01-02 06:54:23 +00:00
|
|
|
doorI, err := door.NewHue(map[string]any{
|
2022-12-30 06:51:51 +00:00
|
|
|
"ip": ip,
|
|
|
|
"username": "",
|
|
|
|
"device": -1,
|
2023-01-02 06:54:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not connect to door: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter := doorI.(*door.Hue)
|
2022-12-30 06:51:51 +00:00
|
|
|
|
2023-01-02 06:54:23 +00:00
|
|
|
return adapter.Setup(os.Args[2])
|
2022-12-30 06:51:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-16 21:17:36 +00:00
|
|
|
var TestHueCommand = &command.Command{
|
2022-12-30 06:51:51 +00:00
|
|
|
Path: []string{"hue", "test"},
|
|
|
|
Summary: "Uses a given configuration to open door",
|
|
|
|
Description: "",
|
|
|
|
Arguments: command.Arguments{
|
|
|
|
{
|
|
|
|
Name: "ip",
|
|
|
|
Description: "The ip address of the bridge",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "username",
|
|
|
|
Description: "An existing bridge username",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "device",
|
|
|
|
Description: "The device ID to test",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cmd *command.Command) error {
|
|
|
|
ip := cmd.Arguments[0].ToValue().(string)
|
|
|
|
username := cmd.Arguments[1].ToValue().(string)
|
|
|
|
device := cmd.Arguments[2].ToValue().(string)
|
|
|
|
|
|
|
|
logrus.Infof("Testing bridge at %s, username %s, device %s", ip, username, device)
|
2023-01-02 06:54:23 +00:00
|
|
|
|
|
|
|
err := door.Connect(map[string]any{
|
|
|
|
"adapter": "hue",
|
2022-12-30 06:51:51 +00:00
|
|
|
"ip": ip,
|
|
|
|
"username": username,
|
|
|
|
"device": device,
|
|
|
|
})
|
2023-01-02 06:54:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not connect to door: %s", err)
|
|
|
|
}
|
|
|
|
return door.RequestToEnter("test")
|
2022-12-30 06:51:51 +00:00
|
|
|
},
|
|
|
|
}
|