|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/brutella/hc" |
| 7 | + "github.com/brutella/hc/accessory" |
| 8 | + "github.com/brutella/log" |
| 9 | + "github.com/yosssi/gmq/mqtt" |
| 10 | + "github.com/yosssi/gmq/mqtt/client" |
| 11 | + "io/ioutil" |
| 12 | + "strconv" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + log.Verbose = true |
| 17 | + log.Info = true |
| 18 | + |
| 19 | + file, err := ioutil.ReadFile("./hkthermostatd.json") |
| 20 | + if err != nil { |
| 21 | + panic(err) |
| 22 | + } |
| 23 | + |
| 24 | + var config map[string]interface{} |
| 25 | + if err := json.Unmarshal(file, &config); err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + |
| 29 | + cli := client.New(&client.Options{ |
| 30 | + // Define the processing of the error handler. |
| 31 | + ErrorHandler: func(err error) { |
| 32 | + log.Println(err) |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + defer cli.Terminate() |
| 37 | + |
| 38 | + err = cli.Connect(&client.ConnectOptions{ |
| 39 | + Network: "tcp", |
| 40 | + Address: config["broker"].(string), |
| 41 | + ClientID: []byte(config["client_id"].(string)), |
| 42 | + }) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + var accessories []*accessory.Accessory |
| 48 | + |
| 49 | + configArr, ok := config["accessories"].([]interface{}) |
| 50 | + if !ok { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + for _, accessory_config := range configArr { |
| 55 | + accessory_config := accessory_config.(map[string]interface{}) |
| 56 | + info := accessory.Info{ |
| 57 | + Name: accessory_config["name"].(string), |
| 58 | + Manufacturer: config["manufacturer"].(string), |
| 59 | + } |
| 60 | + state_topic := fmt.Sprintf("sensor/temperature/%s", accessory_config["id"].(string)) |
| 61 | + |
| 62 | + acc := accessory.NewTemperatureSensor(info, 0, -20, 40, 1.0) |
| 63 | + |
| 64 | + err = cli.Subscribe(&client.SubscribeOptions{ |
| 65 | + SubReqs: []*client.SubReq{ |
| 66 | + &client.SubReq{ |
| 67 | + TopicFilter: []byte(state_topic), |
| 68 | + QoS: mqtt.QoS0, |
| 69 | + // Define the processing of the message handler. |
| 70 | + Handler: func(topicName, message []byte) { |
| 71 | + new_value, err := strconv.ParseFloat(string(message), 64) |
| 72 | + if err != nil { |
| 73 | + panic(err) |
| 74 | + } |
| 75 | + log.Printf("%s value externally changed to %f\n", string(topicName), new_value) |
| 76 | + acc.TempSensor.CurrentTemperature.SetValue(new_value) |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + |
| 85 | + accessories = append(accessories, acc.Accessory) |
| 86 | + } |
| 87 | + |
| 88 | + transport_config := hc.Config{Pin: config["pin"].(string), StoragePath: config["storage_path"].(string)} |
| 89 | + t, err := hc.NewIPTransport(transport_config, accessories[0], accessories[1:]...) |
| 90 | + if err != nil { |
| 91 | + log.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + hc.OnTermination(func() { |
| 95 | + t.Stop() |
| 96 | + }) |
| 97 | + |
| 98 | + t.Start() |
| 99 | +} |
0 commit comments