This commit is contained in:
Mats Tornberg
2025-11-22 17:35:24 +01:00
parent 0596fe60fa
commit 87660c7d1d
12 changed files with 34 additions and 39 deletions

View File

@@ -35,7 +35,8 @@ COPY go.sum .
RUN go mod download RUN go mod download
COPY main.go . COPY main.go .
COPY telldus ./telldus COPY pkg ./pkg
# Build the Go application # Build the Go application
RUN go build main.go RUN go build main.go

Binary file not shown.

16
main.go
View File

@@ -1,11 +1,11 @@
package main package main
import ( import (
"app/datastore" "app/pkg/datastore"
"app/devices" "app/pkg/devices"
"app/mqtt" "app/pkg/mqtt"
"app/telldus" "app/pkg/telldus"
daemon "app/telldus-daemon" daemon "app/pkg/telldus-daemon"
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
@@ -25,8 +25,6 @@ var eventMgr *devices.EventManager
const maxEvents = 1000 const maxEvents = 1000
func main() { func main() {
// Initialize daemon manager // Initialize daemon manager
daemonMgr = daemon.New() daemonMgr = daemon.New()
@@ -140,8 +138,6 @@ func main() {
log.Fatal(http.ListenAndServe(httpPort, setupRoutes())) log.Fatal(http.ListenAndServe(httpPort, setupRoutes()))
} }
func getRawEvents(w http.ResponseWriter, r *http.Request) { func getRawEvents(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(eventMgr.GetRawEvents()) json.NewEncoder(w).Encode(eventMgr.GetRawEvents())
@@ -355,5 +351,3 @@ func getSensor(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(sensor) json.NewEncoder(w).Encode(sensor)
} }

View File

@@ -1,9 +1,9 @@
package devices package devices
import ( import (
"app/datastore" "app/pkg/datastore"
"app/mqtt" "app/pkg/mqtt"
"app/telldus" "app/pkg/telldus"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
@@ -52,7 +52,7 @@ func (em *EventManager) GetSensorEvents() []datastore.SensorEvent {
// HandleDeviceEvent handles device state change events // HandleDeviceEvent handles device state change events
func (em *EventManager) HandleDeviceEvent(deviceID, method int, data string, callbackID int) { func (em *EventManager) HandleDeviceEvent(deviceID, method int, data string, callbackID int) {
fmt.Printf("Device event: ID=%d, Method=%d, Data=%s\n", deviceID, method, data) fmt.Printf("Device event: ID=%d, Method=%d, Data=%s\n", deviceID, method, data)
var state string var state string
switch method { switch method {
case telldus.MethodTurnOn: case telldus.MethodTurnOn:
@@ -60,7 +60,7 @@ func (em *EventManager) HandleDeviceEvent(deviceID, method int, data string, cal
case telldus.MethodTurnOff: case telldus.MethodTurnOff:
state = "OFF" state = "OFF"
} }
if state != "" { if state != "" {
em.mqttClient.PublishDeviceState(deviceID, state) em.mqttClient.PublishDeviceState(deviceID, state)
} }
@@ -70,10 +70,10 @@ func (em *EventManager) HandleDeviceEvent(deviceID, method int, data string, cal
func (em *EventManager) HandleSensorEvent(protocol, model string, id, dataType int, value string, timestamp, callbackID int) { func (em *EventManager) HandleSensorEvent(protocol, model string, id, dataType int, value string, timestamp, callbackID int) {
fmt.Printf("Sensor event: Protocol=%s, Model=%s, ID=%d, Type=%d, Value=%s, Timestamp=%d\n", fmt.Printf("Sensor event: Protocol=%s, Model=%s, ID=%d, Type=%d, Value=%s, Timestamp=%d\n",
protocol, model, id, dataType, value, timestamp) protocol, model, id, dataType, value, timestamp)
// Publish to MQTT // Publish to MQTT
em.mqttClient.PublishSensorValue(protocol, model, id, dataType, value) em.mqttClient.PublishSensorValue(protocol, model, id, dataType, value)
// Store in history // Store in history
em.mu.Lock() em.mu.Lock()
em.sensorEvents = append(em.sensorEvents, datastore.SensorEvent{ em.sensorEvents = append(em.sensorEvents, datastore.SensorEvent{
@@ -88,7 +88,7 @@ func (em *EventManager) HandleSensorEvent(protocol, model string, id, dataType i
em.sensorEvents = em.sensorEvents[1:] em.sensorEvents = em.sensorEvents[1:]
} }
em.mu.Unlock() em.mu.Unlock()
// Update last value in DB // Update last value in DB
if err := em.store.UpdateSensorValue(protocol, model, id, dataType, value); err != nil { if err := em.store.UpdateSensorValue(protocol, model, id, dataType, value); err != nil {
log.Printf("Error updating sensor %s %s %d: %v", protocol, model, id, err) log.Printf("Error updating sensor %s %s %d: %v", protocol, model, id, err)
@@ -98,7 +98,7 @@ func (em *EventManager) HandleSensorEvent(protocol, model string, id, dataType i
// HandleRawDeviceEvent handles raw device detection events // HandleRawDeviceEvent handles raw device detection events
func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callbackID int) { func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callbackID int) {
fmt.Printf("Raw device event: ControllerID=%d, Data=%s\n", controllerID, data) fmt.Printf("Raw device event: ControllerID=%d, Data=%s\n", controllerID, data)
// Parse data // Parse data
fields := strings.Split(data, ";") fields := strings.Split(data, ";")
var class, protocol, model, deviceID string var class, protocol, model, deviceID string
@@ -118,7 +118,7 @@ func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callback
} }
} }
} }
// Store in potential_devices // Store in potential_devices
potentialDev := &datastore.PotentialDevice{ potentialDev := &datastore.PotentialDevice{
Class: class, Class: class,
@@ -131,7 +131,7 @@ func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callback
if err := em.store.UpsertPotentialDevice(potentialDev); err != nil { if err := em.store.UpsertPotentialDevice(potentialDev); err != nil {
log.Printf("Error storing potential device: %v", err) log.Printf("Error storing potential device: %v", err)
} }
// If sensor, ensure in sensors table // If sensor, ensure in sensors table
if class == "sensor" { if class == "sensor" {
idInt, _ := strconv.Atoi(deviceID) idInt, _ := strconv.Atoi(deviceID)
@@ -147,7 +147,7 @@ func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callback
log.Printf("Error inserting sensor from raw: %v", err) log.Printf("Error inserting sensor from raw: %v", err)
} }
} }
// Log the raw event data // Log the raw event data
em.mu.Lock() em.mu.Lock()
em.rawEvents = append(em.rawEvents, datastore.RawEvent{ em.rawEvents = append(em.rawEvents, datastore.RawEvent{

View File

@@ -1,8 +1,8 @@
package devices package devices
import ( import (
"app/datastore" "app/pkg/datastore"
"app/telldus" "app/pkg/telldus"
"fmt" "fmt"
"log" "log"
) )
@@ -40,7 +40,7 @@ func (s *Syncer) SyncSensors() error {
var protocol, model string var protocol, model string
var id, dataTypes int var id, dataTypes int
ret := telldus.Sensor(&protocol, &model, &id, &dataTypes) ret := telldus.Sensor(&protocol, &model, &id, &dataTypes)
for ret == 0 { for ret == 0 {
sensor := &datastore.Sensor{ sensor := &datastore.Sensor{
Protocol: protocol, Protocol: protocol,

View File

@@ -1,8 +1,8 @@
package mqtt package mqtt
import ( import (
"app/datastore" "app/pkg/datastore"
"app/telldus" "app/pkg/telldus"
"fmt" "fmt"
"log" "log"
) )
@@ -66,7 +66,7 @@ func (c *Client) PublishDeviceDiscovery(deviceID int) error {
"manufacturer": "Telldus" "manufacturer": "Telldus"
} }
}`, device.Name, deviceID, deviceID, device.UniqueID, deviceID, device.Name) }`, device.Name, deviceID, deviceID, device.UniqueID, deviceID, device.Name)
c.client.Publish(topic, 0, true, payload) c.client.Publish(topic, 0, true, payload)
return nil return nil
} }
@@ -76,7 +76,7 @@ func (c *Client) publishSensorDiscovery() error {
var protocol, model string var protocol, model string
var id, dataTypes int var id, dataTypes int
ret := telldus.Sensor(&protocol, &model, &id, &dataTypes) ret := telldus.Sensor(&protocol, &model, &id, &dataTypes)
for ret == 0 { for ret == 0 {
sensor, err := c.store.GetSensorByIdentity(protocol, model, id) sensor, err := c.store.GetSensorByIdentity(protocol, model, id)
if err != nil || sensor.Hidden { if err != nil || sensor.Hidden {
@@ -105,7 +105,7 @@ func (c *Client) PublishSensorDiscovery(protocol, model string, id int) error {
var p, m string var p, m string
var sensorID, dataTypes int var sensorID, dataTypes int
ret := telldus.Sensor(&p, &m, &sensorID, &dataTypes) ret := telldus.Sensor(&p, &m, &sensorID, &dataTypes)
// Find matching sensor // Find matching sensor
for ret == 0 { for ret == 0 {
if p == protocol && m == model && sensorID == id { if p == protocol && m == model && sensorID == id {

View File

@@ -1,8 +1,8 @@
package mqtt package mqtt
import ( import (
"app/datastore" "app/pkg/datastore"
"app/telldus" "app/pkg/telldus"
"fmt" "fmt"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
@@ -27,7 +27,7 @@ func New(cfg Config, store *datastore.DataStore) (*Client, error) {
opts := mqtt.NewClientOptions().AddBroker(cfg.BrokerURL) opts := mqtt.NewClientOptions().AddBroker(cfg.BrokerURL)
opts.SetUsername(cfg.Username) opts.SetUsername(cfg.Username)
opts.SetPassword(cfg.Password) opts.SetPassword(cfg.Password)
client := mqtt.NewClient(opts) client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil { if token := client.Connect(); token.Wait() && token.Error() != nil {
return nil, token.Error() return nil, token.Error()
@@ -78,12 +78,12 @@ func (c *Client) UnsubscribeFromDeviceCommands() {
func (c *Client) SubscribeToDeviceCommands() error { func (c *Client) SubscribeToDeviceCommands() error {
// Unsubscribe from existing subscriptions first // Unsubscribe from existing subscriptions first
c.UnsubscribeFromDeviceCommands() c.UnsubscribeFromDeviceCommands()
numDevices := telldus.GetNumberOfDevices() numDevices := telldus.GetNumberOfDevices()
for i := 0; i < numDevices; i++ { for i := 0; i < numDevices; i++ {
deviceID := telldus.GetDeviceId(i) deviceID := telldus.GetDeviceId(i)
topic := fmt.Sprintf("telldus/device/%d/set", deviceID) topic := fmt.Sprintf("telldus/device/%d/set", deviceID)
// Capture deviceID in closure // Capture deviceID in closure
id := deviceID id := deviceID
c.client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) { c.client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
@@ -94,7 +94,7 @@ func (c *Client) SubscribeToDeviceCommands() error {
telldus.TurnOff(id) telldus.TurnOff(id)
} }
}) })
// Track subscription // Track subscription
c.subscriptions = append(c.subscriptions, topic) c.subscriptions = append(c.subscriptions, topic)
} }

View File

@@ -9,7 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
"app/telldus" "app/pkg/telldus"
) )
// Manager handles the telldusd daemon lifecycle // Manager handles the telldusd daemon lifecycle