package devices import ( "app/datastore" "app/telldus" "fmt" "log" ) // Syncer handles synchronization of devices and sensors to the database type Syncer struct { store *datastore.DataStore } // NewSyncer creates a new device/sensor syncer func NewSyncer(store *datastore.DataStore) *Syncer { return &Syncer{store: store} } // SyncDevices synchronizes all telldus devices to the database func (s *Syncer) SyncDevices() error { numDevices := telldus.GetNumberOfDevices() for i := 0; i < numDevices; i++ { deviceID := telldus.GetDeviceId(i) name := telldus.GetName(deviceID) device := &datastore.Device{ ID: deviceID, Name: name, UniqueID: fmt.Sprintf("telldus_device_%d", deviceID), } if err := s.store.UpsertDevice(device); err != nil { log.Printf("Error upserting device %d: %v", deviceID, err) } } return nil } // SyncSensors synchronizes all telldus sensors to the database 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, Model: model, ID: id, Name: fmt.Sprintf("%s %s %d", protocol, model, id), } if dataTypes&telldus.DataTypeTemperature != 0 { sensor.TemperatureUniqueID = fmt.Sprintf("telldus_sensor_%s_%s_%d_temperature", protocol, model, id) } if dataTypes&telldus.DataTypeHumidity != 0 { sensor.HumidityUniqueID = fmt.Sprintf("telldus_sensor_%s_%s_%d_humidity", protocol, model, id) } if err := s.store.UpsertSensor(sensor); err != nil { log.Printf("Error upserting sensor %s %s %d: %v", protocol, model, id, err) } ret = telldus.Sensor(&protocol, &model, &id, &dataTypes) } return nil } // ListDevices prints all devices to stdout func (s *Syncer) ListDevices() { numDevices := telldus.GetNumberOfDevices() if numDevices < 0 { errStr := telldus.GetErrorString(numDevices) fmt.Printf("Error fetching devices: %s\n", errStr) return } fmt.Printf("Number of devices: %d\n", numDevices) for i := 0; i < numDevices; i++ { deviceID := telldus.GetDeviceId(i) name := telldus.GetName(deviceID) protocol := telldus.GetProtocol(deviceID) model := telldus.GetModel(deviceID) fmt.Printf("%d\t%s\tProtocol: %s\tModel: %s\n", deviceID, name, protocol, model) } } // ListSensors prints all sensors to stdout func (s *Syncer) ListSensors() { fmt.Println("\nSENSORS:") var protocol, model string var id, dataTypes int ret := telldus.Sensor(&protocol, &model, &id, &dataTypes) if ret == 0 { for { fmt.Printf("Protocol: %s, Model: %s, ID: %d, DataTypes: %d\n", protocol, model, id, dataTypes) // Fetch values if available if dataTypes&telldus.DataTypeTemperature != 0 { value, timestamp, _ := telldus.SensorValue(protocol, model, id, telldus.DataTypeTemperature) fmt.Printf(" Temperature: %s°C at %d\n", value, timestamp) } if dataTypes&telldus.DataTypeHumidity != 0 { value, timestamp, _ := telldus.SensorValue(protocol, model, id, telldus.DataTypeHumidity) fmt.Printf(" Humidity: %s%% at %d\n", value, timestamp) } ret = telldus.Sensor(&protocol, &model, &id, &dataTypes) if ret != 0 { break } } } else if ret != -6 { // Assuming -6 is TELLSTICK_ERROR_DEVICE_NOT_FOUND errStr := telldus.GetErrorString(ret) fmt.Printf("Error fetching sensors: %s\n", errStr) } }