[C API] Fetching battery level

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
tobiasn
Posts: 2
Joined: 17 Nov 2014 22:31

[C API] Fetching battery level

Post by tobiasn »

Hi,

I'm just getting started with the RazBerry and the C API, in combination with a Vision ZD2102 door/window sensor.

I've got the callback for status updates working fine, so when the sensor is triggered I get notified properly. What I'm not sure about is how to properly get the battery status using the zway_cc_battery_get function.

My code is (in a simple form):

Code: Select all

zway_cc_battery_get(zway, 3, 0, batteryStatusCallback, NULL, NULL);

void batteryStatusCallback(const ZWay zway, ZWBYTE functionId, void* arg)
{
    zway_data_acquire_lock(zway);

    ZDataHolder data = zway_find_device_instance_cc_data(zway, 3, 0, 0x80, "last");

    int batteryStatus;
    zway_data_get_integer(zway, data, &batteryStatus);
    // batteryStatus is now updated

    zway_data_release_lock(zway);
}
The part I'm not sure about is the content of the callback function: should I really be using zway_find_device_instance_cc_data(..), or is there some other function I simply missed?

Also, what is the purpose of the functionId? I've failed to find any function in the API that takes a function ID as a parameter, so I'm not sure what I'm supposed to do with it.

Thanks in advance!
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: [C API] Fetching battery level

Post by pofs »

Return value (battery level) is not really available when Set callback is being called. Callback only indicates command was successfully sent to device, but device may reply with delay.

You should set a callback to value change. This is how you get updates regardless if they were sent unsolicited or as a reply.

For Z-Way 1.x:

Code: Select all

ZDataHolder batteryLevel = zway_find_device_instance_cc_data(zway, 3, 0, 0x80, "last");
zway_data_add_callback(zway, batteryLevel, batteryLevelCallback, FALSE, NULL);

void batteryLevelCallback(ZWay zway, ZWDataChangeType type, ZDataHolder data, void* arg)
{
    // no lock required in callback
    int batteryStatus;
    zway_data_get_integer(zway, data, &batteryStatus);
}
For Z-Way 2.x:

Code: Select all

ZDataHolder batteryLevel = zway_find_device_instance_cc_data(zway, 3, 0, 0x80, "last");
zdata_add_callback(batteryLevel, batteryLevelCallback, FALSE, NULL);

void batteryLevelCallback(const ZDataRootObject root, ZWDataChangeType type, ZDataHolder data, void* arg)
{
    // no lock required in callback
    int batteryStatus;
    zdata_get_integer(data, &batteryStatus);
}
That way any battery level change will be reported.
tobiasn
Posts: 2
Joined: 17 Nov 2014 22:31

Re: [C API] Fetching battery level

Post by tobiasn »

pofs wrote:Return value (battery level) is not really available when Set callback is being called. Callback only indicates command was successfully sent to device, but device may reply with delay.
Yes, that was also what I was seeing. The callback wasn't called until ~5-10 minutes after the call to zway_cc_battery_get.
pofs wrote:You should set a callback to value change. This is how you get updates regardless if they were sent unsolicited or as a reply.

For Z-Way 1.x:

Code: Select all

ZDataHolder batteryLevel = zway_find_device_instance_cc_data(zway, 3, 0, 0x80, "last");
zway_data_add_callback(zway, batteryLevel, batteryLevelCallback, FALSE, NULL);

void batteryLevelCallback(ZWay zway, ZWDataChangeType type, ZDataHolder data, void* arg)
{
    // no lock required in callback
    int batteryStatus;
    zway_data_get_integer(zway, data, &batteryStatus);
}
For Z-Way 2.x:

Code: Select all

ZDataHolder batteryLevel = zway_find_device_instance_cc_data(zway, 3, 0, 0x80, "last");
zdata_add_callback(batteryLevel, batteryLevelCallback, FALSE, NULL);

void batteryLevelCallback(const ZDataRootObject root, ZWDataChangeType type, ZDataHolder data, void* arg)
{
    // no lock required in callback
    int batteryStatus;
    zdata_get_integer(data, &batteryStatus);
}
That way any battery level change will be reported.
Ok, so just what I did for the sensor level.

Now it's just a matter of waiting until the battery level degrades so I can check my callback is doing what it's supposed to :) Thanks!
Post Reply