From 87660c7d1d157a322ea3cdb9d46aa184fe2bef1a Mon Sep 17 00:00:00 2001 From: Mats Tornberg Date: Sat, 22 Nov 2025 17:35:24 +0100 Subject: [PATCH] update --- Dockerfile | 3 ++- db/telldus.db | Bin 65536 -> 69632 bytes main.go | 16 ++++-------- {datastore => pkg/datastore}/datastore.go | 0 {datastore => pkg/datastore}/types.go | 0 {devices => pkg/devices}/manager.go | 24 +++++++++--------- {devices => pkg/devices}/sync.go | 6 ++--- {mqtt => pkg/mqtt}/discovery.go | 10 ++++---- {mqtt => pkg/mqtt}/mqtt.go | 12 ++++----- .../telldus-daemon}/daemon.go | 2 +- .../telldus-daemon}/watcher.go | 0 {telldus => pkg/telldus}/telldus.go | 0 12 files changed, 34 insertions(+), 39 deletions(-) rename {datastore => pkg/datastore}/datastore.go (100%) rename {datastore => pkg/datastore}/types.go (100%) rename {devices => pkg/devices}/manager.go (98%) rename {devices => pkg/devices}/sync.go (98%) rename {mqtt => pkg/mqtt}/discovery.go (99%) rename {mqtt => pkg/mqtt}/mqtt.go (98%) rename {telldus-daemon => pkg/telldus-daemon}/daemon.go (99%) rename {telldus-daemon => pkg/telldus-daemon}/watcher.go (100%) rename {telldus => pkg/telldus}/telldus.go (100%) diff --git a/Dockerfile b/Dockerfile index e88d973..d998bab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,8 @@ COPY go.sum . RUN go mod download COPY main.go . -COPY telldus ./telldus +COPY pkg ./pkg + # Build the Go application RUN go build main.go diff --git a/db/telldus.db b/db/telldus.db index c6143a2645d139f9963203e3f209bd847ab35b6e..7a86f42fa14036adc64d847640466c10d45ec90e 100644 GIT binary patch delta 551 zcmZo@U};#uGC_)sS%86oL2#mi9TT&_#soGyc4HIEOvR_~Cg<86V>h%g%T#=3G1>t!cSWQ?~vnVmoWENm*Vf@Wl#PE_KVPc~kOCxI&>*TG>ieK@>Pwj zjrDkC>tM3x=+4Xo>aE2i%h6Z^atH_1As(9#e0AXB;{^sCGhY$|e>vYfzNF294k3Ij hjT}uJNN#>+0dzAv%w+W7d8P!EWdq5IVhOxwN&uX3vcUiV delta 159 zcmZozz|zpbGC_)s=>h`-gTO=uJ0_+J8xz><*bPn0GZjy9P0qDD#%^F?mZ^9yW3rX~ z9xfweJ#%9tAh&Fj!%cZ-hOc6b%=%0P`6a1&C7Fpi@hPcgnaQce48MWq@iG>t<`w4` z71y%@c^v##fFy$e|8M?p{2%$>@IT{!z<-PX%4V5@9sHZGF@rW8);& K%?G|ZZ~*{(Z#9ho diff --git a/main.go b/main.go index d8f1849..58583bf 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,11 @@ package main import ( - "app/datastore" - "app/devices" - "app/mqtt" - "app/telldus" - daemon "app/telldus-daemon" + "app/pkg/datastore" + "app/pkg/devices" + "app/pkg/mqtt" + "app/pkg/telldus" + daemon "app/pkg/telldus-daemon" "encoding/json" "log" "net/http" @@ -25,8 +25,6 @@ var eventMgr *devices.EventManager const maxEvents = 1000 - - func main() { // Initialize daemon manager daemonMgr = daemon.New() @@ -140,8 +138,6 @@ func main() { log.Fatal(http.ListenAndServe(httpPort, setupRoutes())) } - - func getRawEvents(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") 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") json.NewEncoder(w).Encode(sensor) } - - diff --git a/datastore/datastore.go b/pkg/datastore/datastore.go similarity index 100% rename from datastore/datastore.go rename to pkg/datastore/datastore.go diff --git a/datastore/types.go b/pkg/datastore/types.go similarity index 100% rename from datastore/types.go rename to pkg/datastore/types.go diff --git a/devices/manager.go b/pkg/devices/manager.go similarity index 98% rename from devices/manager.go rename to pkg/devices/manager.go index 3199428..2ded7ea 100644 --- a/devices/manager.go +++ b/pkg/devices/manager.go @@ -1,9 +1,9 @@ package devices import ( - "app/datastore" - "app/mqtt" - "app/telldus" + "app/pkg/datastore" + "app/pkg/mqtt" + "app/pkg/telldus" "fmt" "log" "strconv" @@ -52,7 +52,7 @@ func (em *EventManager) GetSensorEvents() []datastore.SensorEvent { // HandleDeviceEvent handles device state change events 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) - + var state string switch method { case telldus.MethodTurnOn: @@ -60,7 +60,7 @@ func (em *EventManager) HandleDeviceEvent(deviceID, method int, data string, cal case telldus.MethodTurnOff: state = "OFF" } - + if 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) { fmt.Printf("Sensor event: Protocol=%s, Model=%s, ID=%d, Type=%d, Value=%s, Timestamp=%d\n", protocol, model, id, dataType, value, timestamp) - + // Publish to MQTT em.mqttClient.PublishSensorValue(protocol, model, id, dataType, value) - + // Store in history em.mu.Lock() 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.mu.Unlock() - + // Update last value in DB 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) @@ -98,7 +98,7 @@ func (em *EventManager) HandleSensorEvent(protocol, model string, id, dataType i // HandleRawDeviceEvent handles raw device detection events func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callbackID int) { fmt.Printf("Raw device event: ControllerID=%d, Data=%s\n", controllerID, data) - + // Parse data fields := strings.Split(data, ";") var class, protocol, model, deviceID string @@ -118,7 +118,7 @@ func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callback } } } - + // Store in potential_devices potentialDev := &datastore.PotentialDevice{ Class: class, @@ -131,7 +131,7 @@ func (em *EventManager) HandleRawDeviceEvent(data string, controllerID, callback if err := em.store.UpsertPotentialDevice(potentialDev); err != nil { log.Printf("Error storing potential device: %v", err) } - + // If sensor, ensure in sensors table if class == "sensor" { 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 the raw event data em.mu.Lock() em.rawEvents = append(em.rawEvents, datastore.RawEvent{ diff --git a/devices/sync.go b/pkg/devices/sync.go similarity index 98% rename from devices/sync.go rename to pkg/devices/sync.go index 8f41fec..c16887d 100644 --- a/devices/sync.go +++ b/pkg/devices/sync.go @@ -1,8 +1,8 @@ package devices import ( - "app/datastore" - "app/telldus" + "app/pkg/datastore" + "app/pkg/telldus" "fmt" "log" ) @@ -40,7 +40,7 @@ func (s *Syncer) SyncSensors() error { var protocol, model string var id, dataTypes int ret := telldus.Sensor(&protocol, &model, &id, &dataTypes) - + for ret == 0 { sensor := &datastore.Sensor{ Protocol: protocol, diff --git a/mqtt/discovery.go b/pkg/mqtt/discovery.go similarity index 99% rename from mqtt/discovery.go rename to pkg/mqtt/discovery.go index 2499ca0..249cd39 100644 --- a/mqtt/discovery.go +++ b/pkg/mqtt/discovery.go @@ -1,8 +1,8 @@ package mqtt import ( - "app/datastore" - "app/telldus" + "app/pkg/datastore" + "app/pkg/telldus" "fmt" "log" ) @@ -66,7 +66,7 @@ func (c *Client) PublishDeviceDiscovery(deviceID int) error { "manufacturer": "Telldus" } }`, device.Name, deviceID, deviceID, device.UniqueID, deviceID, device.Name) - + c.client.Publish(topic, 0, true, payload) return nil } @@ -76,7 +76,7 @@ func (c *Client) publishSensorDiscovery() error { var protocol, model string var id, dataTypes int ret := telldus.Sensor(&protocol, &model, &id, &dataTypes) - + for ret == 0 { sensor, err := c.store.GetSensorByIdentity(protocol, model, id) if err != nil || sensor.Hidden { @@ -105,7 +105,7 @@ func (c *Client) PublishSensorDiscovery(protocol, model string, id int) error { var p, m string var sensorID, dataTypes int ret := telldus.Sensor(&p, &m, &sensorID, &dataTypes) - + // Find matching sensor for ret == 0 { if p == protocol && m == model && sensorID == id { diff --git a/mqtt/mqtt.go b/pkg/mqtt/mqtt.go similarity index 98% rename from mqtt/mqtt.go rename to pkg/mqtt/mqtt.go index be83cbf..5fedba9 100644 --- a/mqtt/mqtt.go +++ b/pkg/mqtt/mqtt.go @@ -1,8 +1,8 @@ package mqtt import ( - "app/datastore" - "app/telldus" + "app/pkg/datastore" + "app/pkg/telldus" "fmt" 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.SetUsername(cfg.Username) opts.SetPassword(cfg.Password) - + client := mqtt.NewClient(opts) if token := client.Connect(); token.Wait() && token.Error() != nil { return nil, token.Error() @@ -78,12 +78,12 @@ func (c *Client) UnsubscribeFromDeviceCommands() { func (c *Client) SubscribeToDeviceCommands() error { // Unsubscribe from existing subscriptions first c.UnsubscribeFromDeviceCommands() - + numDevices := telldus.GetNumberOfDevices() for i := 0; i < numDevices; i++ { deviceID := telldus.GetDeviceId(i) topic := fmt.Sprintf("telldus/device/%d/set", deviceID) - + // Capture deviceID in closure id := deviceID c.client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) { @@ -94,7 +94,7 @@ func (c *Client) SubscribeToDeviceCommands() error { telldus.TurnOff(id) } }) - + // Track subscription c.subscriptions = append(c.subscriptions, topic) } diff --git a/telldus-daemon/daemon.go b/pkg/telldus-daemon/daemon.go similarity index 99% rename from telldus-daemon/daemon.go rename to pkg/telldus-daemon/daemon.go index d66b258..f0c553d 100644 --- a/telldus-daemon/daemon.go +++ b/pkg/telldus-daemon/daemon.go @@ -9,7 +9,7 @@ import ( "syscall" "time" - "app/telldus" + "app/pkg/telldus" ) // Manager handles the telldusd daemon lifecycle diff --git a/telldus-daemon/watcher.go b/pkg/telldus-daemon/watcher.go similarity index 100% rename from telldus-daemon/watcher.go rename to pkg/telldus-daemon/watcher.go diff --git a/telldus/telldus.go b/pkg/telldus/telldus.go similarity index 100% rename from telldus/telldus.go rename to pkg/telldus/telldus.go