Issues with native python bindings

Moderator: Telldus

Post Reply
heisenberg
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

Issues with native python bindings

Post by heisenberg »

Hi,

I am trying to use the native python bindings (from http://git.telldus.com/telldus.git) with a Duo on an Excito B3. I ran into problems with the provided callback.py example. I am monitoring a single temperature sensor. The problem appears to be related to the return type of the callback function. When pasted into a python interpreter window, the following code (simplified example based on callback.py) will segfault after it receives and prints one single sensor reading.

Code: Select all

# This code segfaults upon the first sensor event                                                         
import telldus
telldus.tdInit()

def event(*args):
    print('event:', args)
    return True

telldus.tdRegisterRawDeviceEvent(event)
I figured the callback probably needs to return a ctypes based type, and so after some modifications the following code will keep handling sensor events without any segfault.

Code: Select all

# This code runs ok                                                             
import ctypes
import telldus
telldus.tdInit()

def event(*args):
    print('event:', args)
    return ctypes.c_int(0)

telldus.tdRegisterRawDeviceEvent(event)
I also had some problems with the poll.py code example, but perhaps that has to do with my tellstick.conf, not quite sure how to properly register a sensor there. tdSensor() will return the appropriate sensor, but tdSensorValue() does not generate a tuple. However this has no impact on the earlier code example for raw device events.

It is kind of hard to figure out how to use the bindings when the example is broken and documentation is sparse. I am thinking ideally the python native bindings should be wrapped in a small layer which prevents the programmer from having to deal at all with ctypes, at least for provided callback functions.

I know there are other python APIs available, but I wanted to also try this one as an "officially supported" API.
phelicks
Posts: 1
Joined: Fri Mar 17, 2023 9:45 am

Re: Issues with native python bindings

Post by phelicks »

Hi,

I was having the same problem, and given that I haven't even touched Python before, debugging this using my raspberry pi seemed a bit daunting.

This post helped me to proceed, replacing "return True" with "return ctypes.c_bool(True) in callback and rawcallback in the callback.py-example. Thanks alot!

Code: Select all

# callback.py

import threading
import telldus
import time
import ctypes    # added

telldus.tdInit()

def turnOn():
        print "turning on"
        telldus.tdTurnOn(1)

def turnOff():
        print "turning off"
        telldus.tdTurnOff(1)

def callback(deviceId, method, value, callbackId):
        print "callback"
        print "DeviceId: %i Method: %i Value: %s" % (deviceId, method, value)
        return ctypes.c_bool(True)    # modified

#function to be called when device event occurs, even for unregistered devices
def rawcallback(data, controllerId, callbackId):
        print "raw callback"
        print "Data: %s ControllerId: %i" % (data, controllerId)
        return ctypes.c_bool(True)     # modified

callbackid = telldus.tdRegisterDeviceEvent(callback)
rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback)

print callbackid, rawcallbackid

try:
        while(1):
                time.sleep(0.5) #don't exit
except KeyboardInterrupt:
        print "Exiting"
        telldus.tdUnregisterCallback(callbackid)
        telldus.tdUnregisterCallback(rawcallbackid)
        telldus.tdClose()
Post Reply