diff options
| -rw-r--r-- | cache.go | 2 | ||||
| -rw-r--r-- | config.go | 23 | ||||
| -rw-r--r-- | configs/default.json | 2 | ||||
| -rw-r--r-- | homematic.go | 22 | ||||
| -rw-r--r-- | main.go | 6 | ||||
| -rw-r--r-- | mqtt.go | 18 |
6 files changed, 37 insertions, 36 deletions
@@ -33,7 +33,7 @@ func (c *Cache) Update(states States) { for id, state := range states { cached, known := c.States[id] if !known || cached != state { - topic := fmt.Sprintf("%s/contact/%s/state", TOPIC_PREFIX, id) + topic := fmt.Sprintf("contact/%s/state", id) var payload []byte if state == true { payload = []byte("open") @@ -9,17 +9,20 @@ import ( //go:embed configs/default.json var defaultConfig []byte -type StartupConfig struct { - MQTT struct { - Broker string `json:"broker"` - ClientID string `json:"client-id"` - TopicPrefix string `json:"topic-prefix"` - } `json:"mqtt"` +type MQTTConfig struct { + Broker string `json:"broker"` + ClientID string `json:"client-id"` + TopicPrefix string `json:"topic-prefix"` +} - Homematic struct { - CCU string `json:"ccu"` - PollingPeriodMilliseconds int `json:"polling-period-milliseconds"` - } `json:"homematic"` +type HomematicConfig struct { + CCU string `json:"ccu"` + PollingPeriod string `json:"polling-period"` +} + +type StartupConfig struct { + MQTT MQTTConfig `json:"mqtt"` + Homematic HomematicConfig `json:"homematic"` } func (sc *StartupConfig) FromJSON(data []byte) error { diff --git a/configs/default.json b/configs/default.json index 9d3fb07..b291185 100644 --- a/configs/default.json +++ b/configs/default.json @@ -6,6 +6,6 @@ }, "homematic": { "ccu": "http://127.0.0.1:8080", - "polling-period-milliseconds": 50 + "polling-period": "50ms" } } diff --git a/homematic.go b/homematic.go index 7ccc1ad..4bae0ca 100644 --- a/homematic.go +++ b/homematic.go @@ -8,19 +8,19 @@ import ( "xengineering.eu/homematic-go/homematic" ) -const ( - OPENCCU = `http://127.0.0.1:8080` - POLLING_PERIOD = 50 * time.Millisecond -) - -func HomematicRun(tx chan MQTTMessage) { - req, inventory, err := Start() +func HomematicRun(config HomematicConfig, tx chan MQTTMessage) { + req, inventory, err := Start(config.CCU) if err != nil { log.Fatalf("Failed startup process: %v", err) } cache := NewCache(tx) + pollingPeriod, err := time.ParseDuration(config.PollingPeriod) + if err != nil { + log.Fatalf("Failed to parse Homematic polling period: %v", err) + } + for { start := time.Now() @@ -28,18 +28,18 @@ func HomematicRun(tx chan MQTTMessage) { cache.Update(states) - WaitUntil(start.Add(POLLING_PERIOD)) + WaitUntil(start.Add(pollingPeriod)) } } -func Start() (homematic.Requester, homematic.Devices, error) { +func Start(ccu string) (homematic.Requester, homematic.Devices, error) { var req homematic.Requester var inventory homematic.Devices var err error - req = homematic.NewRequester(OPENCCU) - log.Printf("Created Homematic requester (%s).", OPENCCU) + req = homematic.NewRequester(ccu) + log.Printf("Created Homematic requester (%s).", ccu) inventory, err = req.ListDevices() if err != nil { @@ -11,10 +11,12 @@ func main() { log.Println("+++ Started Sia server +++") defer log.Println("--- Stopped Sia server ---") + config := GetStartupConfig() + tx := make(chan MQTTMessage) - go MQTTRun(tx) - go HomematicRun(tx) + go MQTTRun(config.MQTT, tx) + go HomematicRun(config.Homematic, tx) Await(syscall.SIGTERM, syscall.SIGINT) } @@ -9,9 +9,6 @@ import ( ) const ( - BROKER = `tcp://127.0.0.1:1883` - CLIENT_ID = `siaserver` - TOPIC_PREFIX = `sia` QOS = byte(1) RETAINED = true MQTT_CONNECT_TIMEOUT = 1 * time.Second @@ -28,14 +25,12 @@ type MQTTMessage struct { Payload []byte } -func init() { - mqttServerHealthTopic = fmt.Sprintf("%s/server/health", TOPIC_PREFIX) -} +func MQTTRun(config MQTTConfig, tx chan MQTTMessage) { + mqttServerHealthTopic = fmt.Sprintf("%s/server/health", config.TopicPrefix) -func MQTTRun(tx chan MQTTMessage) { opts := mqtt.NewClientOptions() - opts.AddBroker(BROKER) - opts.SetClientID(CLIENT_ID) + opts.AddBroker(config.Broker) + opts.SetClientID(config.ClientID) opts.SetCleanSession(true) opts.SetOnConnectHandler(MQTTOnConnectHandler) opts.SetConnectionLostHandler(MQTTConnectionLostHandler) @@ -56,12 +51,13 @@ func MQTTRun(tx chan MQTTMessage) { defer client.Disconnect(MQTT_DISCONNECT_TIMEOUT_US) for message := range tx { - client.Publish(message.Topic, QOS, RETAINED, message.Payload) + topic := fmt.Sprintf("%s/%s", config.TopicPrefix, message.Topic) + client.Publish(topic, QOS, RETAINED, message.Payload) } } func MQTTOnConnectHandler(c mqtt.Client) { - log.Printf("Connected to MQTT broker (%s)", BROKER) + log.Printf("Connected to MQTT broker.") c.Publish(mqttServerHealthTopic, QOS, true, []byte(`good`)) } |
