Created
July 25, 2017 23:34
-
-
Save jarrodbell/47390519edf2e849c875d3b0ec6386b9 to your computer and use it in GitHub Desktop.
Integrate IP-Symcon with CommandFusion iViewer
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
// Change these variables to suit your setup | |
var username = "admin"; | |
var password = "password"; | |
var ipAddress = "192.168.0.100"; | |
// Call this function from your button press like this: | |
// sendToIPSymcon("SetValue", [12345, 42]); | |
// sendToIPSymcon("SetValue", [12346, false]); | |
function sendToIPSymcon(method, params) { | |
CF.request("http://" + username + ":" + password + "@" + ipAddress + ":3777/api/", "POST", {"Content-Type": "application/json"}, {"jsonrpc": "2.0", "id": "0", "method": method, "params": params}, function (status, headers, body) { | |
if (status == 200) { | |
CF.log(method + " SUCCESS!"); | |
CF.log(body); | |
// Check if response has a return value | |
if (method == "GetValue") { | |
// Convert the JSON response into a JS object | |
var data = JSON.parse(body); | |
// Now we will check the data type being returned. | |
// And set the join number matching the command param | |
// For this to work, the first params array index must be a number | |
if (typeof params[0] != "number") { | |
CF.log("Could not apply result to join because first param is not a number!"); | |
CF.log("Param: " + params[0]); | |
return; | |
} | |
if (typeof data.response == "number") { | |
// Data type is a number, set the analog join value and serial join value | |
// You would probably want to scale the analog values (as all setJoin calls to analog joins need to be in range 0-65535) | |
// Otherwise the join change on a slider/gauge might not even be noticable | |
CF.setJoins([ | |
{join: "s" + params[0], value: data.result}, | |
{join: "a" + params[0], value: data.result} | |
]); | |
} else if (typeof data.response == "boolean") { | |
// Data type is boolean, so just set the digital join state | |
CF.setJoin("d" + params[0], data.response); | |
} else if (typeof data.response == "string") { | |
// Data type is string, so just set the serial join | |
CF.setJoin("s" + params[0], data.response); | |
} | |
} | |
} else { | |
CF.log(method + "ERROR!"); | |
CF.log(body); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment