Files
2026-02-17 09:29:34 -06:00

145 lines
4.3 KiB
JavaScript

//In v6, we introduced the following key addition to the Common API.
//- Addition of Tags to IoT Gateway
// - Config API
// The following codes are usable only for 6.1 and above
//Function for event Log.
function readEventLog() {
$.ajax({
type: 'GET',
url: 'http://' + inputServer + '/config/v1/event_log?limit=1000',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data) {
//We send HTTP request at 1s rate. In one min, there will be 100 security only event
var tempArray = [];
for (var i = 0; i < data.length; i++) {
if (data[i].event != 'Security') {
tempArray.push(data[i]);
}
}
if (eventArray.toString() != tempArray.toString()) {
eventArray = tempArray;
console.log("EventArray Changed");
eventArrayChanged = true;
}
},
error: function(data) {
updateLog("Error retrieving KSE log");
}
});
}
function createRestServer(serverName, port) {
port = parseInt(port);
var tempdata = {
"common.ALLTYPES_NAME": serverName,
"iot_gateway.AGENTTYPES_TYPE": "REST Server",
"iot_gateway.REST_SERVER_ALLOW_ANONYMOUS_LOGIN": true,
"iot_gateway.REST_SERVER_CORS_ALLOWED_ORIGINS": "*",
"iot_gateway.REST_SERVER_ENABLE_WRITE_ENDPOINT": true,
"iot_gateway.REST_SERVER_NETWORK_ADAPTER": "Localhost only",
"iot_gateway.REST_SERVER_PORT_NUMBER": port,
"iot_gateway.REST_SERVER_USE_HTTPS": false
};
tempdata = JSON.stringify(tempdata);
console.log(tempdata);
$.ajax({
type: 'POST',
url: 'http://' + inputServer + '/config/v1/project/_iot_gateway/rest_servers',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
data: tempdata,
success: function() {
updateLog(serverName + " has been created");
},
error: function(data, status, xhr) {
console.log(data.responseJSON.message);
updateLog(data.responseJSON.message);
}
});
}
function deleteRestServer(serverName) {
$.ajax({
type: 'DELETE',
url: 'http://' + inputServer + '/config/v1/project/_iot_gateway/rest_servers/' + serverName,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function() {
updateLog(serverName + "deleted");
},
error: function(e) {
updateLog("Error adding item");
console.log(e);
}
});
}
function addRestTag(serverName, tagaddr) {
var tempdata = {
"iot_gateway.IOT_ITEM_SERVER_TAG": tagaddr,
};
tempdata = JSON.stringify(tempdata);
console.log(tempdata);
$.ajax({
type: 'POST',
url: 'http://' + inputServer + '/config/v1/project/_iot_gateway/rest_servers/' + serverName + '/iot_items',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
data: tempdata,
success: function() {
updateLog(tag + " added");
},
error: function(e) {
updateLog("Error adding item");
console.log(e);
}
});
}
function readTag(tagaddr, port) {
$.ajax({
type: 'GET',
url: 'http://127.0.0.1:' + port + '/iotgateway/read?ids=' + tagaddr,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data) {
console.log(data.readResults[0].v);
$("#tagValue").html("<p>" + data.readResults[0].v + "</p>");
},
error: function(data) {
updateLog(data.responseJSON.message);
}
});
}