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

226
pkg/telldus/telldus.go Normal file
View File

@@ -0,0 +1,226 @@
package telldus
/*
#cgo CFLAGS: -I/usr/local/include
#cgo LDFLAGS: -L/usr/local/lib -ltelldus-core
#include <telldus-core.h>
#include <stdlib.h>
// Extern declarations for exported Go callback functions
extern void goDeviceEvent(int deviceId, int method, char *data, int callbackId, void *context);
extern void goDeviceChangeEvent(int deviceId, int changeEvent, int changeType, int callbackId, void *context);
extern void goRawDeviceEvent(char *data, int controllerId, int callbackId, void *context);
extern void goSensorEvent(char *protocol, char *model, int id, int dataType, char *value, int timestamp, int callbackId, void *context);
extern void goControllerEvent(int controllerId, int changeEvent, int changeType, char *newValue, int callbackId, void *context);
*/
import "C"
import "unsafe"
// Callback function types
type DeviceEventFunc func(deviceId, method int, data string, callbackId int)
type DeviceChangeEventFunc func(deviceId, changeEvent, changeType, callbackId int)
type RawDeviceEventFunc func(data string, controllerId, callbackId int)
type SensorEventFunc func(protocol, model string, id, dataType int, value string, timestamp, callbackId int)
type ControllerEventFunc func(controllerId, changeEvent, changeType int, newValue string, callbackId int)
// Global callback variables
var deviceEventCallback DeviceEventFunc
var deviceChangeEventCallback DeviceChangeEventFunc
var rawDeviceEventCallback RawDeviceEventFunc
var sensorEventCallback SensorEventFunc
var controllerEventCallback ControllerEventFunc
//export goDeviceEvent
func goDeviceEvent(deviceId C.int, method C.int, data *C.char, callbackId C.int, context unsafe.Pointer) {
if deviceEventCallback != nil {
deviceEventCallback(int(deviceId), int(method), C.GoString(data), int(callbackId))
}
}
//export goDeviceChangeEvent
func goDeviceChangeEvent(deviceId C.int, changeEvent C.int, changeType C.int, callbackId C.int, context unsafe.Pointer) {
if deviceChangeEventCallback != nil {
deviceChangeEventCallback(int(deviceId), int(changeEvent), int(changeType), int(callbackId))
}
}
//export goRawDeviceEvent
func goRawDeviceEvent(data *C.char, controllerId C.int, callbackId C.int, context unsafe.Pointer) {
if rawDeviceEventCallback != nil {
rawDeviceEventCallback(C.GoString(data), int(controllerId), int(callbackId))
}
}
//export goSensorEvent
func goSensorEvent(protocol *C.char, model *C.char, id C.int, dataType C.int, value *C.char, timestamp C.int, callbackId C.int, context unsafe.Pointer) {
if sensorEventCallback != nil {
sensorEventCallback(C.GoString(protocol), C.GoString(model), int(id), int(dataType), C.GoString(value), int(timestamp), int(callbackId))
}
}
//export goControllerEvent
func goControllerEvent(controllerId C.int, changeEvent C.int, changeType C.int, newValue *C.char, callbackId C.int, context unsafe.Pointer) {
if controllerEventCallback != nil {
controllerEventCallback(int(controllerId), int(changeEvent), int(changeType), C.GoString(newValue), int(callbackId))
}
}
// Init initializes the Telldus library
func Init() {
C.tdInit()
}
// Close closes the Telldus library
func Close() {
C.tdClose()
}
// TurnOn turns on a device
func TurnOn(deviceId int) int {
return int(C.tdTurnOn(C.int(deviceId)))
}
// TurnOff turns off a device
func TurnOff(deviceId int) int {
return int(C.tdTurnOff(C.int(deviceId)))
}
// Learns a device
func Learn(deviceId int) int {
return int(C.tdLearn(C.int(deviceId)))
}
// Dim dims a device to a level (0-255)
func Dim(deviceId int, level int) int {
return int(C.tdDim(C.int(deviceId), C.uchar(level)))
}
// Bell rings the bell on a device
func Bell(deviceId int) int {
return int(C.tdBell(C.int(deviceId)))
}
// GetNumberOfDevices returns the number of devices
func GetNumberOfDevices() int {
return int(C.tdGetNumberOfDevices())
}
// GetDeviceId returns the device ID at the given index
func GetDeviceId(index int) int {
return int(C.tdGetDeviceId(C.int(index)))
}
// GetName returns the name of a device
func GetName(deviceId int) string {
cstr := C.tdGetName(C.int(deviceId))
defer C.tdReleaseString(cstr)
return C.GoString(cstr)
}
// GetProtocol returns the protocol of a device
func GetProtocol(deviceId int) string {
cstr := C.tdGetProtocol(C.int(deviceId))
defer C.tdReleaseString(cstr)
return C.GoString(cstr)
}
// GetModel returns the model of a device
func GetModel(deviceId int) string {
cstr := C.tdGetModel(C.int(deviceId))
defer C.tdReleaseString(cstr)
return C.GoString(cstr)
}
// Methods returns the supported methods for a device
func Methods(deviceId int, methodsSupported int) int {
return int(C.tdMethods(C.int(deviceId), C.int(methodsSupported)))
}
// Sensor retrieves sensor data
func Sensor(protocol *string, model *string, id *int, dataTypes *int) int {
var cProtocol [20]C.char
var cModel [20]C.char
var cId C.int
var cDataTypes C.int
ret := int(C.tdSensor(&cProtocol[0], 20, &cModel[0], 20, &cId, &cDataTypes))
if ret == 0 {
*protocol = C.GoString(&cProtocol[0])
*model = C.GoString(&cModel[0])
*id = int(cId)
*dataTypes = int(cDataTypes)
}
return ret
}
// SensorValue retrieves a specific sensor value
func SensorValue(protocol, model string, id, dataType int) (string, int, int) {
var value [20]C.char
var timestamp C.int
ret := int(C.tdSensorValue(C.CString(protocol), C.CString(model), C.int(id), C.int(dataType), &value[0], 20, &timestamp))
if ret == 0 {
return C.GoString(&value[0]), int(timestamp), ret
}
return "", 0, ret
}
// GetErrorString returns the error string for an error code
func GetErrorString(errorNo int) string {
cstr := C.tdGetErrorString(C.int(errorNo))
defer C.tdReleaseString(cstr)
return C.GoString(cstr)
}
// RegisterDeviceEvent registers a callback for device events
func RegisterDeviceEvent(cb DeviceEventFunc) int {
deviceEventCallback = cb
return int(C.tdRegisterDeviceEvent(C.TDDeviceEvent(C.goDeviceEvent), nil))
}
// RegisterDeviceChangeEvent registers a callback for device change events
func RegisterDeviceChangeEvent(cb DeviceChangeEventFunc) int {
deviceChangeEventCallback = cb
return int(C.tdRegisterDeviceChangeEvent(C.TDDeviceChangeEvent(C.goDeviceChangeEvent), nil))
}
// RegisterRawDeviceEvent registers a callback for raw device events
func RegisterRawDeviceEvent(cb RawDeviceEventFunc) int {
rawDeviceEventCallback = cb
return int(C.tdRegisterRawDeviceEvent(C.TDRawDeviceEvent(C.goRawDeviceEvent), nil))
}
// RegisterSensorEvent registers a callback for sensor events
func RegisterSensorEvent(cb SensorEventFunc) int {
sensorEventCallback = cb
return int(C.tdRegisterSensorEvent(C.TDSensorEvent(C.goSensorEvent), nil))
}
// RegisterControllerEvent registers a callback for controller events
func RegisterControllerEvent(cb ControllerEventFunc) int {
controllerEventCallback = cb
return int(C.tdRegisterControllerEvent(C.TDControllerEvent(C.goControllerEvent), nil))
}
// UnregisterCallback unregisters a callback
func UnregisterCallback(callbackId int) int {
return int(C.tdUnregisterCallback(C.int(callbackId)))
}
// Constants for methods
const (
MethodTurnOn = 1
MethodTurnOff = 2
MethodBell = 4
MethodDim = 16
MethodLearn = 32
)
// Constants for sensor data types
const (
DataTypeTemperature = 1
DataTypeHumidity = 2
DataTypeRainRate = 4
DataTypeRainTotal = 8
DataTypeWindDirection = 16
DataTypeWindAverage = 32
DataTypeWindGust = 64
)