JavaScript Query Interface
JavaScript Query Interface
The aim of the javascript library is to provide an abstraction to the web technologies used to communicate with the controller and to provide an api which will not change. This allows us to change the way we communicate with the controller without braking compatibility with existing custom web pages.
Usage
To use the javascript library include this line in the head of the html file.
<script src="/default/js/query.js"></script>
The Query object will then exist and queries can then be made by calling the approriate method eg
Query.get_current_time(callback) callback is a function that is called when the server responds to the request. This is required as all the requests made to the controller are asynchronous. The response sent by the controller is given as the first argument of the callback function and is in JSON format.
API
This is documented along with the rest of the API
here
.
Authentication
If an API call is made that the current user does not have sufficient privileges to make, the user will be redirected to the default web interface's login page. If, however, you are using .htaccess files within the custom web interface, the user will be redirected to the page defined by the AuthFormLoginRequiredLocation directive in the main .htaccess
file.
Your custom login page will then need to receive the username and password from the user and send that data back to the controller's web server.
From your custom login page, you will need to POST the username and password to /authorise as either form data or
JSON. The data must be user and password.
Form:
<form action="/authorise" method="POST">
<input type="text" id="user" name="user">
<input type="password" id="password" name="password">
<button type="submit">Submit</button>
</form>
JSON:
{
- 299 -
Pharos Designer User Manual
"user": "Pharos",
"password": "password123"
}
Examples
Using Push_to_web()
The Lua Script has the function push_to_web(name,value).
This allows you to send some data to the web interface as a JSON packet push_to_web(name,value) sends {name:value} to the interface.
Within the web interface, use the following Javascript to get the information:
Query.subscribe_lua(function(incoming) { if (incoming.hasOwnProperty('speed'))
{ let speed = incoming.speed;
// do something with speed, e.g. display the value
$('#speed-display').text(speed);
} else if (incoming.hasOwnProperty('direction'))
{ let direction = incoming.direction;
// do something with direction, e.g. display the value
$('#direction-display').text(direction);
}
});
In Lua, you would send a speed of 5 with push_to_web("speed", 5) or a direction of 240 with push_to_web("direction", 240)
Fire A Trigger With Multiple Variables
If you have a trigger which is expecting multiple variables, you can add them to the fire_trigger() function as a string:
Query.fire_trigger(1, '"var1", 2, 3.5, "20:100"')
- 300 -
JavaScript Query Interface
All values passed into the variables argument will be converted to trigger variables. Integers passed without a range
(i.e. without using the colon syntax, where e.g. 20:100 resolves to 0.2, or 20%) will be assumed to be in a range of 0-
255 (so a variable of value 128 will be converted to a trigger variable of 50%). If the action of the trigger expects a percentage, e.g. Set Intensity, you can either use a floating point number between 0.0 and 1.0, an integer between 0 and 255, or any integer range, e.g. 20:100.
- 301 -