Skip to content

Instantly share code, notes, and snippets.

@jarrodbell
Last active December 21, 2015 03:59
Show Gist options
  • Save jarrodbell/6246150 to your computer and use it in GitHub Desktop.
Save jarrodbell/6246150 to your computer and use it in GitHub Desktop.
How to parse data using JavaScript in iViewer, step by step guide.
// To listen to feedback in JS, you need to use CF.watch within the userMain function, like this:
// Note: "Incoming Alarm Data" is the name of the feedback item in guiDesigner, replace these with whatever name you are using in your GUI.
// The regex you use in your feedback item determines WHEN the FeedbackMatchedEvent is triggered, but has no affect on the data sent to JavaScript.
// Instead your script will be sent the full string of data that was received from your external system, so long as it matches the regex.
// So if you wanted ALL data to be processed in JavaScript, you could use a "catch all" RegEx as simple as: (.*)
CF.userMain = function() {
CF.watch(CF.FeedbackMatchedEvent, "System Name", "Incoming Alarm Data", function(feedbackName, matchedString) {
// Code for using the matched data goes here...
});
}
// The above example uses an anonymous callback function, but you could also call a defined function like this:
function LOG_DATA(feedbackName, matchedString) {
// Code for using the matched data goes here...
}
CF.userMain = function() {
CF.watch(CF.FeedbackMatchedEvent, "Incoming Alarm Data", LOG_DATA);
}
// Both ways work fine, just depends on your coding style, code maintenance, etc.
// So now that we have the data being sent to JavaScript, we can perform more complex logic to parse it.
// In this example, we are expecting data to come back like so:
// 1EAS000000000111111100000000000F
// 1EAS000000000111111100000000000E
// And we want to get a specific byte of the data, then use it to determine the value of a text object in our GUI.
// Easy way to get specific data is using RegEx, but you could also just use string functions like substr, split, indexOf, etc.
function LOG_DATA(feedbackName, matchedString) {
var matches = matchedString.match(/1EAS00000000(\d)11111110000000000/);
// First check that a match was actually found (definitely will be if we use the same regex in the feedback item in guiDesigner, but not so if using a catch all regex)
if (matches !== null) {
// The matches array will contain the entire string in index 0, and each capture group as indexes above that.
// So the byte we want to use from our regex is in the matches array index 1. So lets compare it using a switch statement:
var finalValue = ""; // Create a variable to store the text data we want to apply. Best to do this so that if you change the join later, its only one CF.setJoin call that you need to change.
switch (matches[1]) {
case "0":
// the byte is a 0, so lets set the data to whatever value that should represent in our GUI.
finalValue = "Not Ready To Arm";
break; // Exit the switch case
case "1":
finalValue = "Ready To Arm";
break;
case "2":
finalValue = "Ready To Arm, but a zone is violated and can be Force Armed.";
break;
case "3":
finalValue = "Armed with Exit Timer working";
break;
case "4":
finalValue = "Armed Fully";
break;
case "5":
finalValue = "Force Armed with a force arm zone violated";
break;
case "6":
finalValue = "Armed with a bypass";
break;
}
// In this example, we are using a text object on serial join 10 that we want to show the data in.
// Change 's10' to suit whatever join you are using in your GUI for your text object.
CF.setJoin("s10", finalValue);
// Do a log call for good luck...
CF.log("Final Value: " + finalValue);
}
}
// One last comment: This is not a complete script, but an example containing various code examples you can use.
// Make sure your scripts only have ONE CF.userMain function defined!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment