Last active
December 29, 2015 05:59
-
-
Save jarrodbell/7625906 to your computer and use it in GitHub Desktop.
JavaScript example for using CommandFusion iViewer to process data returned from a HTTP Request to a Z-Wave MiCasaVerde Vera Lite system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myJSON, baseURL = "http://192.168.1.90:3480/data_request?id=lu_status2&DataVersion=1&DeviceNum="; | |
function requestData(deviceNum) { | |
CF.request(baseURL + deviceNum, function(status, headers, body) { | |
if (status == 200) { | |
// OK response received, now grab the JSON string from body and turn it into an object | |
myJSON = JSON.parse(body); | |
// Now assign one of the known state variables to a serial join (if it will always be at a certain index in the state array) | |
CF.setJoin("s100", myJSON["Device_Num_"+deviceNum].states[1].value); | |
// Or lookup a variable by name from a specific device name if multiple devices are returned | |
CF.setJoin("s101", getDeviceVariable("Device_Num_"+deviceNum,"LoadLevelStatus")); | |
// Or lookup a variable by name on its own if you don't care what device it is coming from | |
CF.setJoin("s102", getVariable("LoadLevelStatus")); | |
} | |
}); | |
} | |
// Get the value of a variable from a specific device | |
function getDeviceVariable(deviceName, variableName) { | |
// First check that the device name exists | |
if (!myJSON.hasOwnProperty(deviceName)) { | |
CF.log("Device name could not be found: " + deviceName); | |
return; | |
} | |
// Get the number of state variables in the device | |
var i = 0, n = myJSON[deviceName].states.length; | |
// Loop through all the state variables | |
for (; i < n; i++) { | |
// Find the variable name that matches the one requested | |
if (myJSON[deviceName].states[i].variable == variableName) { | |
// Send the value of the variable back to the caller | |
return myJSON[deviceName].states[i].variable; | |
} | |
} | |
// If we get to here, the variable was not found in any of the devices. | |
// Do some error logging and send back an empty string as the value | |
CF.log("State variable not found: " + variableName); | |
return ""; | |
} | |
// Get the value of a variable from the first device that contains it | |
function getVariable(variableName) { | |
// Loop through all device objects in the returned data (eg. Device_Num_3) | |
for (var device in myJSON) { | |
// Get the number of state variables in the device | |
var i = 0, n = myJSON[device].states.length; | |
// Loop through all the state variables | |
for (; i < n; i++) { | |
// Find the variable name that matches the one requested | |
if (myJSON[device].states[i].variable == variableName) { | |
// Send the value of the variable back to the caller | |
return myJSON[device].states[i].variable; | |
} | |
} | |
} | |
// If we get to here, the variable was not found in any of the devices. | |
// Do some error logging and send back null value | |
CF.log("State variable not found: " + variableName); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment