How to integrate My Bus code to OpenHab Framework

289 views
Skip to first unread message

v detez

unread,
Dec 2, 2012, 12:43:36 PM12/2/12
to ope...@googlegroups.com
Hello,

Started to develop my Velbus Binding.  First some words to explain what I had before :

My existing code

- my java code using a Jvelbus library.  it's using Rxtxcom.jar, rxtxparalell.dll and rxtxserial.dll to interface with Serial/USB port.
- code :
SerialBus bus = new SerialBus("COM1");
Worker worker = new Worker(bus);
Thread t = new Thread(worker);        
 t.start();

where Worker class implements Runnable, PacketSentListener, PacketReceivedListener
with public void packetReceivedEvent(PacketReceivedEvent event) which get read packet from the BUS on the serial port
with public void packetSentEvent(PacketSentEvent event) which implements message sent to the BUS
public void run() {
            this.bus.addPacketReceivedListener(this);
            this.bus.addPacketSentListener(this);
            this.bus.open();
            while (running) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
...

So with Worker class, implementing PacketReceivedListener, I get message/packets from Bus and I decrypt it to reflect it in my current gui.

For action on the Bus, I am using a bus.sendBlocking(Packet) or bus.send(Packet) methods.

The Bus is synchronized to share this object correctly between thread.

This java api si a low level api, so I have to work with Hexadecimal packet and interpret command/received message.


How to integrate my existing code with openhab... but smartly
- First I would like open the bus and start worker thread via the Activator.
- Second for I will create a Provider for SwitchItem with command velbus={">[ON:switchrelay:moduleaddress:channel] >[OFF:switchrelay:moduleaddress:channel]}
- Now for the binding itself, could you give me someadvices :
1 Where can I implement my asynchronous calls to the bus? in which method?
2 Where can I implement the update of ItemState? messages coming from the bus? in which method?

By the way I hope to open my code as rest WS.  Do I have to think something special now in order to develop it?

Hoping with your advices to have fast a skeleton of code that I could test.

Thks for your time

Valery


Kai Kreuzer

unread,
Dec 2, 2012, 3:44:53 PM12/2/12
to ope...@googlegroups.com
Hi,

> - First I would like open the bus and start worker thread via the Activator.

You should only start your worker thread if your binding is used in some item - if there is no item associated to your binding, it should behave silently and not do anything.

> 1 Where can I implement my asynchronous calls to the bus? in which method?

Use the service "EventPublisher", which provides methods "sendCommand", "postCommand" and "postUpdate" to send events to the openHAB event bus.

> 2 Where can I implement the update of ItemState? messages coming from the bus? in which method?

Your binding should extend AbstractEventSubscriberBinding, in which you can then implement methods "receiveCommand" and "receiveUpdate" to be informed about events on the openHAB event bus.

> By the way I hope to open my code as rest WS. Do I have to think something special now in order to develop it?

Not sure what you have in mind here, but it sounds to me like a major architecture breach, so be clear on what you want to do there exactly.

Regards,
Kai

Thomas Eichstädt-Engelen

unread,
Dec 3, 2012, 4:21:20 AM12/3/12
to ope...@googlegroups.com
Hi,

>> - First I would like open the bus and start worker thread via the Activator.
> You should only start your worker thread if your binding is used in some item - if there is no item associated to your binding, it should behave silently and not do anything.

right, please see AbstractActiveBinding.start() as example how you
could implement this.

>> 2 Where can I implement the update of ItemState? messages coming from the bus? in which method?
> Your binding should extend AbstractEventSubscriberBinding, in which you can then implement methods "receiveCommand" and "receiveUpdate" to be informed about events on the openHAB event bus.

When extending AbtractEventSubscriberBinding you will have to
implement "internalReceiveCommand" and "internalReceiveUpdate" which
are internally called by "receiveCommand" or "receiveUpdate" but only
when there are existing bindings.

Regards,

Thomas E.-E.

v detez

unread,
Dec 4, 2012, 5:09:11 PM12/4/12
to ope...@googlegroups.com
Hi Thomas, Hi Kai,

Thanks to your advices, It's starting to sound good!

- If I well understood your advice Kai (and I have implemented it like that), I pass my EventPublish to my class listening my own bus and use this EP to notify the OpenHab bus.  Correct?

class VelbusBinding extends AbstractEventSubscriberBinding{
private EventPublisher ep;
public activate().
{
ListenToMybusClass ltb = new ListenToMybusClass (ep)
}
}

class ListenToMybusClass extends Thread{
public ListenToMybusClass (EventPublisher ep){} //constructor
public run()
{
EACH TIME THAT RECEIVE A MESSAGE FROM MY BYS I NOTIFY OPENHAB BUS via EventPublisher
}
}

- what's the difference between  "sendCommand", "postCommand" and "postUpdate" methods? When use them?

- so I suppose that I have implemented "internalReceiveCommand" and "internalReceiveUpdate" rather than "receiveCommand" and "receiveUpdate", correct?

thks

Valery

Thomas Eichstädt-Engelen

unread,
Dec 4, 2012, 5:58:23 PM12/4/12
to ope...@googlegroups.com
Hi Valery,

- If I well understood your advice Kai (and I have implemented it like that), I pass my EventPublish to my class listening my own bus and use this EP to notify the OpenHab bus.  Correct?

yes

- what's the difference between  "sendCommand", "postCommand" and "postUpdate" methods? When use them?

The main difference between Commands and Updates is explained here http://code.google.com/p/openhab/wiki/Architecture. sendCommand sends the given coammdn synchronously to the openHAB event bus whereas postCommand sends it asynchronously (see http://www.osgi.org/javadoc/r4v42/org/osgi/service/event/EventAdmin.html for more information),

- so I suppose that I have implemented "internalReceiveCommand" and "internalReceiveUpdate" rather than "receiveCommand" and "receiveUpdate", correct?

yes.

Regards,

Thomas E.-E.



--
You received this message because you are subscribed to the Google Groups "openhab" group.
To view this discussion on the web visit https://groups.google.com/d/msg/openhab/-/AfcBeGclwE0J.
To post to this group, send email to ope...@googlegroups.com.
To unsubscribe from this group, send email to openhab+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/openhab?hl=en.

v detez

unread,
Jan 10, 2013, 9:35:30 AM1/10/13
to ope...@googlegroups.com
Hello Thomas (and Co)

First Happy new year for everybody..... and a lot of Success for Openhab this year!

I am still investigating the code and I am wondering when the "internalReceiveUpdate" method is called.

I thought that the postUpdate() will call it before the update the status (via a post on the bus) but no. (postUpdate -> internalReceiveUpdate -> bus -> change status)

By the way I don't understand the AutoUpdateBinding concept.  I already read some topics in this forum but I probably missed some details.

Could you give me the light please?

Thks

Valery

Thomas Eichstädt-Engelen

unread,
Jan 10, 2013, 5:43:52 PM1/10/13
to ope...@googlegroups.com
Hi Valery,

> First Happy new year for everybody..... and a lot of Success for Openhab this year!

thanks :-)

> I am still investigating the code and I am wondering when the "internalReceiveUpdate" method is called.
> I thought that the postUpdate() will call it before the update the status (via a post on the bus) but no. (postUpdate -> internalReceiveUpdate -> bus -> change status)

postUpdate() is called by the eventPublisher and sends an event to the openHAB event bus. After that all registered Event-Subscribers receive that event asynchronously. There is no direct call of receiveUpdate(). See class AbstractEventSubscriberBinding (and it's parents) which implements the EventHandler (OSGi) Interface. internalReceiveUpdate() is just a convenience method which is called by receiveUpdate() but only if there are bindings available (see AbstractEventSubscriberBinding as well).

> By the way I don't understand the AutoUpdateBinding concept. I already read some topics in this forum but I probably missed some details.

Generally receiving a command to change a state and the effective change of the state need not happen at the same time. In KNX e.g. you have a GA which receives the command ON and there is a second GA which sends out the feedback that a given lamp has been switched ON. BUT there is hardware which doesn't support such active feedback. In such cases the AutoUpdateBinding comes into play and updates the state to the received command accordingly . It implements the assumption that the given command is processed correctly by the hardware.

Hope this helps,

Thomas E.-E.

Reply all
Reply to author
Forward
0 new messages