Help with BSC Schema

10 replies [Last post]
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011

I'm afraid that I haven't yet got the idea of the BSC schema so I'd be grateful for any help with the following problem:
I'd like to create a BSC endpoint that will allow me to change a file which shows the state of my boiler boost function.
The idea is to switch it between 1 and 0 depending on the state of the boost.

This is what I have so far but I can't get it to actually change the file.

This is an applet which I was hoping would do the work:

module(...,package.seeall)


require "xap"
require "xap.bsc"

info={
version="1.0", description="Boiler Boost Controller"
}

function boostCmd(endpoint)
file = io.open("/etc/plugboard/boosttemp","w+")
if endpoint.state == "on" then
file:write("1")
else
file:write("0")
end
file:close()
end

function boilerboostInfoEvent(endpoint, clazz)
endpoint.displaytext = "Boost is " .. endpoint.state
return true -- Return TRUE if the INFO/EVENT should be emitted / FALSE otherwise
end


function init()
boilerBSC = bsc.Endpoint{source="dbzoo.livebox.boilerboost:state", direction=bsc.OUTPUT, type=bsc.BINARY, cmdCB=boostCmd, infoEventCB=boilerboostInfoEvent}
end

*******************************


This is a script just to test the endpoint

require "xap"
require "xap.bsc"

state = 0


function init()
print("starting")
end




function flipflop()

if state == 0 then
state=1
else
state=0
end

if state==1 then
sendBscCmd("dbzoo.livebox.boilerboost:state","text=on")
else
sendBscCmd("dbzoo.livebox.boilerboost:state","text=off")
end

end

function sendBscCmd(target,body)
xap.sendShort(string.format([[xap-header
{
target=%s
class=xAPBSC.cmd
}
output.state.1
{
id=*
%s
}]], target, body))
end


xap.init("dbzoo.lua.boostendpointtest","FF005D00" )

xap.Timer(flipflop,30):start()

xap.process()

******************

xfx viewer shows the xap commands being sent to switch the state and the info seems to change but I don't understand well enough to know why the actual change is not being made to the file.

What am I doing wrong?

 

 

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
It's all about filters

Just having a quick glance at your applet, you don't seen to have a filter setup that will trigger your boostCmd.  so in you init function you would need some code similiar to that shown below, which creates a filter for any bsc packets with the  "dbzoo.livebox.boilerboost:state" endpoint, if such a packet is seen it calls the boostCmd function, the one that updates your file :-

 

-- Create a filter, the conditions needed to call our "action" routine
  filter  = xap.Filter()

-- We want a state change to trigger our routine
  filter:add("xap-header","source", "dbzoo.livebox.boilerboost:state")

-- This is the function we call when a match is made to the filters
  filter:callback(boostCmd)

 

I haven't tested your code, or the snipit above, but I think you get the idea.  Read here, http://www.dbzoo.com/livebox/hah_plugboard_v2#filter, for more info.

 

UPDATE: try the attached.

Karl

AttachmentSize
testboost.lua 814 bytes
boostApplet.lua 1.12 KB
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Thanks

Karl

Thanks for these I'll try them out and post back.

Still struggling with no landline or broadband at the moment so a bit difficult to do things.

I'll let you know.

 

Allan

Thanks for these I

brett
Offline
Providence, United States
Joined: 9 Jan 2010
This applet works just fine -

This applet works just fine - there is nothing wrong with it.  I've taken the liberty of condensing the code a little.

module(...,package.seeall)

require "xap"
require "xap.bsc"
require "pl"

info={
version="1.0", description="Boiler Boost Controller"
}

function boostCmd(endpoint)
--  pretty.dump(endpoint)
  file.write("/etc/plugboard/boosttemp",utils.choose(endpoint.state == "on","1","0"))
end

function boilerboostInfoEvent(endpoint, clazz)
--  pretty.dump(endpoint)
  endpoint.displayText = "Boost is "..endpoint.state
  return true
end


function init()
  bsc.Endpoint{source="dbzoo.livebox.boiler:boost",
               direction=bsc.OUTPUT, type=bsc.BINARY, cmdCB=boostCmd,infoEventCB=boilerboostInfoEvent}
end


I started it up and then send it the following message from xfXViewer

xap-header
{
v=12
hop=1
uid=FF00D800
class=xAPBSC.cmd
target=dbzoo.livebox.boiler:boost
source=dbzoo.acme.test
}
output.state.1
{
id=*
state=on
}

I'm not going to speak to your test program ... I just wanted to point out that your BSC applet is basically right.  You can uncomment the pretty.dump() command to display the endpoint data to help you debug.

NOTE: Karl when using a BSC endpoints you don't need to worry about filters this is ALL handled internally for you by the BSC class.  This is what makes them so attractive, they do all the GRUNT work for you.

Brett

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Thanks for your help

Brett

 

Thanks again for your help with this. I now seem to have a working version which will allow SWMBO to hit a boost button on her iPhone and switch on the central heating for an hour.

I guess I was getting my "text" and "state" mixed up :-p Oooh it can be so difficult sometimes.

And thanks also to Karl for your input.

 

Allan

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Glad its working.

I've also raised issue 40 - there is a need for some BSC API's to allow the sending of control messages without people having to write these each time using the xap.sendShort() primitive.  Perhaps there are some others...

Brett

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Hand holding, please

Hi Brett

Please take a look at the attached lua.....its a hodge-podge of several other peoples code in an attempt for me to learn lua scripting.  I've adapted elements from various sources and am struggling a bit..... I've (I think) created several endpoints on the Plugboard branch (is this OK - or should they be on their own branch) and most of the time they appear to work fine - then it all goes wrong and the Pachube heart-beat disappears and nothing gets posted but the end-points still update fine.....

It must be me - can you have a look, please....ta!

AttachmentSize
CCumElectricApplet.lua 2.27 KB
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
When I said iPod Touch

That was assuming that the iPod Touch application xAP Viewer would actually send a BSC command. It doesn't seem to work for me. Has anyone else used this app?

 

http://itunes.apple.com/gb/app/xap-viewer/id458152134?mt=8

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I can't really follow your

I can't really follow your programming logic - it might be best to describe what you are trying to do.  It is rather a hodge podge as you say.

If the pachube heartbeat is disappearing perhaps the xap-pachube process is crashing out trying to handle the service update being sent to it.  If this is the case then there is a bug in xap-pachube that needs fixing and something you need to examine in a little more detail.  Check /var/log for something, run  xap-pachube in debug mode whilst your lua scripts are active and see what the last thing is it emit before it dies (if indeed it is dying)

I will look at this later on at the close of my day - I have a day job that I need to get back to..

Brett

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Right - here goes

I have four lua scripts running....all based on the one above.....

CConsumptionApplet.lua reads CurrentCost:ch1 and ch2, averages them and publishes the result to Plugboard:ConsumedW (on uid FF00D801) which has been created as an endpoint thusly >>     bscConsumedW = bsc.Endpoint{source="dbzoo.livebox.Plugboard:ConsumedW", direction=bsc.INPUT, type=bsc.STREAM} <<

CCumElectricApplet.lua reads Plugboard:ConsumedW and calculates a time dependent value which it then publishes to Plugboard:TUSEkWh (on uid FF00D803) endpoint def >>     bscCurrentKwH = bsc.Endpoint{source="dbzoo.livebox.Plugboard:TUSEkWh", direction=bsc.INPUT, type=bsc.STREAM} <<

CCumBoilerApplet.lua reads ch.3 and calculates a time dependent value which it then publishes to Plugboard:TBOILkWh (on uid FF00D802) endpoint def >>     bscCurrentKwH = bsc.Endpoint{source="dbzoo.livebox.Plugboard:TBOILkWh", direction=bsc.INPUT, type=bsc.STREAM} <<

CCumGENApplet.lua reads ch.1 and calculates a time dependent value which it then publishes to Plugboard:TGENkWh (on uid FF00D804) endpoint def >>     bscCurrentKwH = bsc.Endpoint{source="dbzoo.livebox.Plugboard:TGENkWh", direction=bsc.INPUT, type=bsc.STREAM} <<

I've been unable to run xap-pachube in debug mode - I use PuTTY, log onto the HUB type in xap-pachube -d 7 and it errors out saying 'd' not recognised!

I did find this in the pachube log once (normally it dies with an empty file)......

Pachube Connector for xAP v12
Copyright (C) DBzoo 2009-2010

[err][pachulib.c:196:read_data] errno 22 (Invalid argument)
[err][pachulib.c:196:read_data] Err!! desconexion en recv
[err][pachulib.c:196:read_data] errno 22 (Invalid argument)
[err][pachulib.c:196:read_data] Err!! desconexion en recv
[err][pachulib.c:196:read_data] errno 22 (Invalid argument)
[err][pachulib.c:196:read_data] Err!! desconexion en recv
[err][pachulib.c:196:read_data] errno 22 (Invalid argument)
[err][pachulib.c:196:read_data] Err!! desconexion en recv
[err][pachulib.c:196:read_data] errno 22 (Invalid argument)
[err][pachulib.c:196:read_data] Err!! desconexion en recv

Hope this helps.....EJ

 

AFTER REMOVING ALL LUA AND IMPLEMENTING THEM SINGLY - IT APPEARS THAT IT MIGHT BE DUE TO PULLING DATA FROM AN INSTANCED HUB AND PUSHING TO A FEED ON AN INSTANCED HUB..... I've put the code sample below - it fails every time and I guess the reason I wasn't seeing any error log on my local Hub was because it was the instanced Hub's Pachube that was failing?  Hope this clarifies the situation.....EJ

AttachmentSize
CCXXApplet.lua 2.79 KB
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
I have no luck with xap viewer either. Try mote

Allan.  I use an iPhone to send xap messages via the mote app. Pain to set up but really powerfull and has nice customisable GUI screens. Try it out and give me a shout if you need help setting if up

 

Cheers

 

Garru

Hardware Info