2.19 node.js stuff

deeproot
Posts: 2
Joined: Fri Mar 06, 2015 11:25 pm

2.19 node.js stuff

Postby deeproot » Fri Oct 16, 2015 11:52 pm

I just put this at the bottom of my /opt/local_control/aau/http.js and reboot the wink hub
This uses no authenication so be careful. Of course the aprontest command is for my light and you need to change it to your -m and -t

I have a light that has value ON and value OFF so I can use curl to toggle this light again.
curl http://xxx.xxx.xxx.xxx:8282/?ON
curl http://xxx.xxx.xxx.xxx:8282/?OFF

var http = require('http');
var url = require("url");
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
var queryObject = url.parse(request.url).query;

response.write(queryObject);
var cp = require("child_process");
cp.exec("aprontest -u -m1 -t1 -v "+queryObject)
response.end("light" +queryObject);

});

// Listen on port 8282, IP defaults to 127.0.0.1
server.listen(8282);

bdruth
Posts: 1
Joined: Wed Dec 30, 2015 2:52 pm

Re: 2.19 node.js stuff

Postby bdruth » Wed Dec 30, 2015 2:57 pm

I took this as a base (thank you!!) and made some adjustments to give it more flexibility.

Code: Select all

var http, server, url;

http = require('http');

url = require('url');

server = http.createServer(function(request, response) {
  var attr, attr_id, cmd_line, command, cp, id, parsed_url, target, target_switch, url_args;
  response.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  parsed_url = url.parse(request.url);
  url_args = parsed_url.pathname.substring(1).split('/');
  target = url_args[0], id = url_args[1], attr = url_args[2], command = url_args[3];
  // master IDs are -m, group IDs are -x
  target_switch = target === 'light' ? 'm' : 'x';
  // On_Off attribute is ID 1, Level is 2 (for dimming)
  attr_id = attr === 'On_Off' ? 1 : 2;
  response.write(target + ": " + id + "\n");
  response.write("Attribute: " + attr + "[" + attr_id + "]\n");
  response.write("Command: " + command + "\n");
  cmd_line = "aprontest -u -" + target_switch + id + " -t" + attr_id + " -v " + command;
  response.write(cmd_line + "\n");
  cp = require('child_process');
  cp.exec(cmd_line, function(error, stdin, stdout) {});
  response.end(target + "=>" + command);
});

server.listen(8282);


You can use this in a script like so:

Code: Select all

#!/bin/bash
LOGFILE=/tmp/controlLightApron.log
function log() {
  echo $@ >> $LOGFILE
}
log "#######################"
log "Beginning execution ..."
log "#######################"
log "Args:"
log $@

while [ $# -ge 1 ]; do
    case "$1" in
        -G)
           log "Received Group Command"
            TARGET="group"
            ;;
        -L)
            TARGET="light"
            ;;
        -p)
            attribute="On_Off"
            command="$2"
            shift
            ;;
        -b)
            attribute="Level"
            command="$2"
            shift
            ;;
        -i)
            id="$2"
            shift
            ;;
    esac

    shift

done
curl "http://wink:8282/$TARGET/$id/$attribute/$command" >> $LOGFILE
log 


And from an OpenHAB rule like this:

Individual light

Code: Select all

rule "Deck Light Control"
when Item Dimmer_Outdoor_Deck received command
then
   var Number percent = 0
   if(Dimmer_Outdoor_Deck.state instanceof DecimalType) percent = Dimmer_Outdoor_Deck.state as DecimalType
   if(receivedCommand==INCREASE) {percent = percent + 0.05}
   else if(receivedCommand==DECREASE) {percent = percent - 0.05}
   else {percent = (receivedCommand) as DecimalType / 100}
   if(percent<=0) percent = 0.1
   if(percent>1) percent = 1
   executeCommandLine("configurations/scripts/controlLightApron.sh -L -b "+ (percent*255).intValue +" -i "+ deck_id)
   postUpdate(Dimmer_Outdoor_Deck, percent*100);
end

rule "Deck Light Control"
when Item Switch_Outdoor_Deck received command
then
    var Number percent = 0
    if(Dimmer_Outdoor_Deck.state instanceof DecimalType) percent = Dimmer_Outdoor_Deck.state as DecimalType
    executeCommandLine("configurations/scripts/controlLightApron.sh -L -p "+receivedCommand+" -i "+ deck_id)
    postUpdate(Switch_Outdoor_Deck, receivedCommand.toString);
    postUpdate(Dimmer_Outdoor_Deck, percent);
end


Group of lights

Code: Select all

rule "Outdoor Front Light Control"
when Item Dimmer_Outdoor_Front received command
then
   var Number percent = 0
   if(Dimmer_Outdoor_Front.state instanceof DecimalType) percent = Dimmer_Outdoor_Front.state as DecimalType
   if(receivedCommand==INCREASE) {percent = percent + 0.05}
   else if(receivedCommand==DECREASE) {percent = percent - 0.05}
   else {percent = (receivedCommand) as DecimalType / 100}
   if(percent<=0) percent = 0.1
   if(percent>1) percent = 1
   executeCommandLine("configurations/scripts/controlLightApron.sh -G -b "+ (percent*255).intValue +" -i "+ outdoorall_id)
   postUpdate(Dimmer_Outdoor_Front, percent*100);
   postUpdate(Dimmer_Outdoor_Frontdoor, percent*100);
   postUpdate(Dimmer_Outdoor_GarageLeft, percent*100);
   postUpdate(Dimmer_Outdoor_GarageRight, percent*100);
   postUpdate(Dimmer_Outdoor_Deck, percent*100);
end

rule "Outdoor Front Light Control"
when Item Switch_Outdoor_Front received command
then
    executeCommandLine("configurations/scripts/controlLightApron.sh -G -p "+receivedCommand+" -i "+ outdoorall_id)
    postUpdate(Switch_Outdoor_Front, receivedCommand.toString);
    postUpdate(Switch_Outdoor_Frontdoor, receivedCommand.toString);
    postUpdate(Switch_Outdoor_GarageRight, receivedCommand.toString);
    postUpdate(Switch_Outdoor_GarageLeft, receivedCommand.toString);
    postUpdate(Switch_Outdoor_Deck, receivedCommand.toString);
end


Here's some sample log output from hitting the endpoint ..

#######################
Beginning execution ...
#######################
Args:
-G -b 25 -i 11
Received Group Command
group: 11
Attribute: Level[2]
Command: 25
aprontest -u -x11 -t2 -v 25
group=>25
#######################
Beginning execution ...
#######################
Args:
-G -p OFF -i 11
Received Group Command
group: 11
Attribute: On_Off[1]
Command: OFF
aprontest -u -x11 -t1 -v OFF
group=>OFF


Hope this helps someone along ... enjoy!


Return to “General / Other”

Who is online

Users browsing this forum: No registered users and 1 guest