Skip to content

Commit aec6799

Browse files
committed
Rename hksonoffd into more generic hkswitchd
1 parent 71387fa commit aec6799

File tree

4 files changed

+30
-48
lines changed

4 files changed

+30
-48
lines changed

config.json-example

Lines changed: 0 additions & 26 deletions
This file was deleted.

hklightd.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ func main() {
6565
value := "off"
6666
if on {
6767
value = "on"
68-
} else {
69-
value = "off"
7068
}
7169
log.Printf("Switching %s", value)
7270
err = cli.Publish(&client.PublishOptions{

hksonoffd.go renamed to hkswitchd.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"github.com/brutella/hc"
67
"github.com/brutella/hc/accessory"
78
"github.com/brutella/log"
@@ -14,7 +15,7 @@ func main() {
1415
log.Verbose = true
1516
log.Info = true
1617

17-
file, err := ioutil.ReadFile("./config.json")
18+
file, err := ioutil.ReadFile("./hkswitchd.json")
1819
if err != nil {
1920
panic(err)
2021
}
@@ -55,35 +56,31 @@ func main() {
5556
Name: accessory_config["name"].(string),
5657
Manufacturer: accessory_config["manufacturer"].(string),
5758
}
59+
control_topic := fmt.Sprintf("switch_%s/control", accessory_config["machine_id"].(string))
60+
state_topic := fmt.Sprintf("switch_%s/state", accessory_config["machine_id"].(string))
5861

5962
acc := accessory.NewSwitch(info)
6063

6164
acc.Switch.On.OnValueRemoteUpdate(func(on bool) {
62-
if on == true {
63-
err = cli.Publish(&client.PublishOptions{
64-
QoS: mqtt.QoS0,
65-
TopicName: []byte(accessory_config["control_topic"].(string)),
66-
Message: []byte("on"),
67-
})
68-
if err != nil {
69-
panic(err)
70-
}
71-
} else {
72-
err = cli.Publish(&client.PublishOptions{
73-
QoS: mqtt.QoS0,
74-
TopicName: []byte(accessory_config["control_topic"].(string)),
75-
Message: []byte("off"),
76-
})
77-
if err != nil {
78-
panic(err)
79-
}
65+
value := "off"
66+
if on {
67+
value = "on"
68+
}
69+
log.Printf("Switching %s", value)
70+
err = cli.Publish(&client.PublishOptions{
71+
QoS: mqtt.QoS0,
72+
TopicName: []byte(control_topic),
73+
Message: []byte(fmt.Sprintf("power:%s", value)),
74+
})
75+
if err != nil {
76+
panic(err)
8077
}
8178
})
8279

8380
err = cli.Subscribe(&client.SubscribeOptions{
8481
SubReqs: []*client.SubReq{
8582
&client.SubReq{
86-
TopicFilter: []byte(accessory_config["state_topic"].(string)),
83+
TopicFilter: []byte(state_topic),
8784
QoS: mqtt.QoS0,
8885
// Define the processing of the message handler.
8986
Handler: func(topicName, message []byte) {

hkswitchd.json-example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"broker": "127.0.0.1:1883",
3+
"client_id": "hkswitchd",
4+
"pin": "12345678",
5+
"storage_path": "./db_hkswitchd",
6+
"accessories": [
7+
{
8+
"name": "Sonoff 01",
9+
"manufacturer": "ITEAD",
10+
"machine_id": "01020304"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)