summaryrefslogtreecommitdiff
path: root/shelly.go
diff options
context:
space:
mode:
Diffstat (limited to 'shelly.go')
-rw-r--r--shelly.go35
1 files changed, 34 insertions, 1 deletions
diff --git a/shelly.go b/shelly.go
index 5f6f2ad..508b393 100644
--- a/shelly.go
+++ b/shelly.go
@@ -5,6 +5,8 @@ import (
"log"
"net"
"strings"
+
+ "github.com/gorilla/websocket"
)
func ShellyRun(config ShellyConfigs, route Route) {
@@ -15,7 +17,10 @@ func ShellyRun(config ShellyConfigs, route Route) {
continue
}
- log.Printf("Send '%s' to '%s'.", command, ip)
+ err = shellySendCommand(ip, command)
+ if err != nil {
+ log.Printf("Could not send command '%s' to %v: %v", command, ip, err)
+ }
}
}
@@ -55,3 +60,31 @@ func parseMessage(config ShellyConfigs, m MQTTMessage) (ip *net.IP, command stri
return nil, "", fmt.Errorf("Got message for unknown cover '%s'", id)
}
+
+func shellySendCommand(ip *net.IP, command string) error {
+ template := `
+{
+ "jsonrpc":"2.0",
+ "id": 1,
+ "src":"user_1",
+ "method":"%s",
+ "params": {
+ "id":0
+ }
+}
+`
+ message := fmt.Appendf([]byte{}, template, command)
+
+ c, _, err := websocket.DefaultDialer.Dial("ws://" + ip.String() + "/rpc", nil)
+ if err != nil {
+ return fmt.Errorf("Could not connect to Shelly: %w", err)
+ }
+ defer c.Close()
+
+ err = c.WriteMessage(websocket.TextMessage, message)
+ if err != nil {
+ return fmt.Errorf("Failed writing websocket message to Shelly: %w", err)
+ }
+
+ return nil
+}