You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
Version 1
Current »
This section consists of the following:
Get REST API Request Examples
Get request for a single datapoint: GET /iap/devs/*+name==Sensor1/if/LightSensor/0/nvoLuxLevel/value Response [ { "id":449092, "local":"2019-07-18 16:25:43.265 PDT", "utc":"2019-07-18 23:25:43.265 UTC", "deviceId":"17q2d7k.2", "deviceName":"Sensor1", "blockName":"LightSensor", "blockIndex":"0", "datapointName":"nvoLuxLevel", "deviceState":"provisioned", "deviceHealth":"normal", "dpQualifier":"2/LightSensor/0/nvoLuxLevel", "value":43 } ]
Get request for multiple datapoints for a single Web page (using tag.value): GET /iap/devs/*/if/*/*/*+tag==xyzzjd_floor1&tag.value==1/value?pg=1&sz=30
Get request for multiple datapoints for a single Web page (using qualifier): GET /iap/devs/*/if/*/*/*+qualifier=-17q3awh/lon/F5/Lamp/0/nviValue,17q3awh/lon/5/Lamp/F1/nvoValueFb/value?max_age=0&noxs=true |
Note: Special characters, depending where they are located in the request, may need to be encoded before sending the request to the SmartServer.
Simple Read Web Page Example
The simpleRead.html file displays the following web page:
SimpleRead.html Source Code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9 IE=10" />
<title>Simple Get</title>
<style>
//body { background-color:grey}
span {
display: inline-block;
padding-left:10px;
padding-right:10px;
width:70px
}
</style>
<script>
function getVersion() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
json = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = json.value;
}
else if(xhr.readyState == 4 && xhr.status == 401)
{
login();
}
else if(xhr.readyState == 4)
{
/* alert(xhr.status + ", " + xhr.statusText); */
}
};
xhr.open("GET", "https://" + window.location.hostname + "/iap/version", true);
xhr.send();
}
function login() {
var loginUrl = "https://" + window.location.hostname + "/user/login.html?next=";
loginUrl += document.location;
window.open(loginUrl,'_self',false);
}
function logout() {
var logoutUrl = "https://" + window.location.hostname + "/iap/auth/logout";
//send POST request to logout -- TBD
login();
}
</script>
</head>
<body>
<button style="float: right;" onclick="logout()">Logout</button>
<br><br>
SmartServer IOT Version:<span id="demo" >---</span><button type="button" style="font-size:large;" onclick="getVersion()">Get Version</button>
</body>
</html>
Simple Read/Write Web Page Example
Simple Read/Write Source Code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9 IE=10" />
<title>Simple Write</title>
<style>
//body { background-color:grey}
span {
display: inline-block;
padding-left:10px;
padding-right:10px;
margin-left:5px;
margin-right:5px;
width:150px
}
input {
display: inline-block;
padding-left:10px;
padding-right:10px;
margin-left:5px;
margin-right:5px;
width:150px;
}
button {
font-size:large;
margin-left:5px;
}
</style>
<script>
function readValue() {
var xhr = new XMLHttpRequest();
var currentValue;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
json = JSON.parse(this.responseText);
currentValue = JSON.stringify(json[0].value);
currentValue = currentValue.replace(/{|}/g,"");
document.getElementById("currentValue").innerHTML = currentValue;
document.getElementById("switchOut1").value = currentValue;
}
else if(xhr.readyState == 4 && xhr.status == 401)
{
login();
}
else if(xhr.readyState == 4)
{
alert(xhr.status + ", " + xhr.statusText);
}
};
xhr.open("GET", "https://" + window.location.hostname + "/iap/devs/*+name==Sensor1/if/*+xifName==Lamp/0/*+xifName==nvoValueFb/value", true);
xhr.send();
}
function writeValue() {
var xhr = new XMLHttpRequest();
var payload = document.getElementById("switchOut1").value;
payload = "{" + payload + "}";
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 204) {
}
else if(xhr.readyState == 4 && xhr.status == 401)
{
login();
}
else if(xhr.readyState == 4)
{
alert(xhr.status + ", " + xhr.statusText);
}
};
xhr.open("PUT", "https://" + window.location.hostname + "/iap/devs/*+name==Sensor1/if/*+xifName==Lamp/0/*+xifName==nviValue/value", true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.send(payload);
}
function login() {
var loginUrl = "https://" + window.location.hostname + "/user/login.html?next=";
loginUrl += document.URL; //document.location;
window.open(loginUrl,'_self',false);
}
function logout() {
var logoutUrl = "https://" + window.location.hostname + "/user/login.html?next=";
// send POST request to logout -- TBD
login();
}
</script>
</head>
<body>
Sensor1 Lamp (SNVT_switch)
<button style="float: right;" onclick="logout()"> Logout</button>
<br><br>
Current Value:<span id="currentValue" >---</span><button type="button" onclick="readValue()">Read</button>
<br><br>
Write Value (0 or 1)<input id="switchOut1" placeholder="Click Read Button"><button type="button" onclick="writeValue()">Write</button>
</body>
</html>