update
This commit is contained in:
@@ -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
|
||||
|
||||
BIN
db/telldus.db
BIN
db/telldus.db
Binary file not shown.
16
main.go
16
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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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{
|
||||
@@ -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,
|
||||
@@ -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 {
|
||||
@@ -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)
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"app/telldus"
|
||||
"app/pkg/telldus"
|
||||
)
|
||||
|
||||
// Manager handles the telldusd daemon lifecycle
|
||||
Reference in New Issue
Block a user