Hi guys, I’ve written a little connector that should help you integrate webCoRE. As you probably know, webCoRE now supports multiple installed instances, making the listing of pistons a little more complicated. The connector is (hopefully) handling that right.
/*************************************************************************/
/* webCoRE Connector v0.2 */
/*************************************************************************/
/* Copyright 2016 Adrian Caramaliu <ady624(at)gmail.com> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>.*/
/*************************************************************************/
/* Initialize the connector in your initialize() method using */
/* webCoRE_init() */
/* Optionally, pass the string name of a method to call when a piston */
/* is executed: */
/* webCoRE_init('pistonExecutedMethod') */
/*************************************************************************/
/* List all available pistons by using one of the following: */
/* webCoRE_list() - returns the list of id/name pairs */
/* webCoRE_list('id') - returns the list of piston IDs */
/* webCoRE_list('name') - returns the list of piston names */
/*************************************************************************/
/* Execute a piston by using the following: */
/* webCoRE_execute(pistonIdOrName) */
/* The execute method accepts either an id or the name of a */
/* piston, previously retrieved by webCoRE_list() */
/*************************************************************************/
private webCoRE_handle(){return'webCoRE'}
private webCoRE_init(pistonExecutedCbk){state.webCoRE=(state.webCoRE instanceof Map?state.webCoRE:[:])+(pistonExecutedCbk?[cbk:pistonExecutedCbk]:[:]);subscribe(location,"${webCoRE_handle()}.pistonList",webCoRE_handler);if(pistonExecutedCbk)subscribe(location,"${webCoRE_handle()}.pistonExecuted",webCoRE_handler);webCoRE_poll();}
private webCoRE_poll(){sendLocationEvent([name: webCoRE_handle(),value:'poll',isStateChange:true,displayed:false])}
public webCoRE_execute(pistonIdOrName,Map data=[:]){def i=(state.webCoRE?.pistons?:[]).find{(it.name==pistonIdOrName)||(it.id==pistonIdOrName)}?.id;if(i){sendLocationEvent([name:i,value:app.label,isStateChange:true,displayed:false,data:data])}}
public webCoRE_list(mode){def p=state.webCoRE?.pistons;if(p)p.collect{mode=='id'?it.id:(mode=='name'?it.name:[id:it.id,name:it.name])}}
public webCoRE_handler(evt){switch(evt.value){case 'pistonList':List p=state.webCoRE?.pistons?:[];Map d=evt.jsonData?:[:];if(d.id&&d.pistons&&(d.pistons instanceof List)){p.removeAll{it.iid==d.id};p+=d.pistons.collect{[iid:d.id]+it}.sort{it.name};state.webCoRE = [updated:now(),pistons:p];};break;case 'pistonExecuted':def cbk=state.webCoRE?.cbk;if(cbk&&evt.jsonData)"$cbk"(evt.jsonData);break;}}
You will need webCoRE v0.2.0bb at a minimum for this to work. Please be aware that providing a callback function during the webCoRE_init() is fairly expensive, as your SmartApp will run on every single piston execution, so don’t provide a callback function if you don’t need to know when pistons executed.
If using a parent-child SmartApp, use this in the parent SmartApp and make the webCoRE_list and webCoRE_execute public, so that you can call them from the child as parent.webCoRE_list and parent.webCoRE_execute
You can pass data to a piston while executing it, simply provide a Map in the data parameter:
webCoRE_execute('test piston', [param1: 'a', param2: 'b'])
I strongly recommend using the IDs when storing information about a piston or trying to execute one, as names may change.
This is the initial release and may change as webCoRE gets closer to RC status. I will update the code here, if it changes.