Files
controls-web/controls-classic/test/kepware/JS/v6CommonAPI.js
2026-02-17 09:29:34 -06:00

401 lines
16 KiB
JavaScript

//DISCLAIMER HERE//
//This sample demonstrates the calls for each of the end point for Create, Delete, Modify Operations
//Note that this code is not recommended for production due to the security reasons. For production, please use HTTPS with the relevant CA certs as well as CORS setup accordingly. Do consider the use of our OPC UA Tunnel together with a DMZ PC for systems that are exposed to the internet. Consult your IT team for more information.
//CREATE OPERATIONS - v6
//The create channel function takes in the inputServer (IP address), Channel Type and Channel Name to create the channel
function createChannel(inputServer, inputChannelType, inputChannel) {
for (var i = 0; i < channelList.length; i++) {
if (inputChannel == channelList[i]) {
updateLog("Channel of the same name already exist.");
}
}
updateLog("Creating a " + inputChannelType + " named " + inputChannel);
$.ajax({
type: 'POST',
url: 'http://' + inputServer + '/config/v1/project/channels/',
data: '{"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "' + inputChannelType + '","common.ALLTYPES_NAME":"' + inputChannel + '"}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function() {
updateLog("Channel " + inputChannel + " created.", status);
funcdone = 1;
},
error: function() {
updateLog("Channel " + inputChannel + " failed.", status);
funcdone = 0;
}
});
}
//The create Device function allows you to take in the inputServer (IP address), inputChannel (Channel Name), input DevID (Device ID), inputDevice (Device Name), inputDriver (Driver Type) to create a tag
function createDevice(inputServer, inputChannel, inputDevID, inputDevice, inputDriver) {
updateLog("Creating " + inputDevice + " with ID " + inputDevID + " for driver " + inputDriver);
$.ajax({
type: 'POST',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + '/devices',
data: '{"common.ALLTYPES_NAME":"' + inputDevice + '","servermain.DEVICE_MODEL":0,"servermain.DEVICE_ID_STRING": "' + inputDevID + '","servermain.DEVICE_CHANNEL_ASSIGNMENT":"' + inputChannel + '","servermain.MULTIPLE_TYPES_DEVICE_DRIVER":"' + inputDriver + '"}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, status) {
updateLog(inputDevice + " created under " + inputChannel, status);
funcdone = 2;
},
error: function(JSON, status) {
updateLog("Creation of " + inputDevice + " failed.", status);
funcdone = 0;
}
});
}
//The create Tag function allows you to take in the inputServer (IP address), inputChannel (Channel Name), input Device (Device Name), inputTag (Tag Name), inputTagAddr (Tag Adress) to create a device
function createTag(inputServer, inputChannel, inputDevice, inputTag, inputTagAddr) {
console.log(inputDevice);
console.log(inputTag + inputTagAddr);
updateLog("Creating " + inputTag + " with address " + inputTagAddr);
$.ajax({
type: 'POST',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + '/devices/' + inputDevice + '/tags',
data: '{"common.ALLTYPES_NAME":"' + inputTag + '","servermain.TAG_ADDRESS":"' + inputTagAddr + '","servermain.TAG_DATA_TYPE":' + inputTagType + '}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, status, xhr) {
updateLog(inputTag + " created under " + inputDevice, status);
updateTags(dselected, cselected);
//console.log(d);
},
error: function(JSON, status, xhr) {
updateLog("Creation of " + inputTag + " failed.", status);
//console.log(d);
}
});
}
//DELETE OPERATIONS - v6
//Deletes all channel. We update he log and loop through the global variable to delete all the channels one by one
function delAllChannels() {
var oldList = [];
oldList = channelList;
console.log(oldList);
updateLog("Deleting everything...");
for (var i = 0; i < oldList.length; i++) {
$.ajax({
type: 'DELETE',
url: 'http://' + inputServer + '/config/v1/project/channels/' + oldList[i].text,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data, status) {},
error: function(data, status) {}
});
}
}
//Deletes a single channel
//Takes in the channel name and deletes it
function delChannel(channelName) {
updateLog("Deleting " + channelName);
$.ajax({
type: 'DELETE',
url: 'http://' + inputServer + '/config/v1/project/channels/' + channelName,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data, status) {
updateLog("Delete Channel Success", status);
},
error: function(data, status) {
updateLog("Delete Channel Fail", status);
}
});
}
//Delete an device. We take in the device Name and channel Name to delete it
function delDevice(deviceName, channelName) {
updateLog("Deleting " + device + "under Channel Name" + channelName);
$.ajax({
type: 'DELETE',
url: 'http://' + inputServer + '/config/v1/project/channels/' + channelName + '/devices/' + deviceName,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data, status) {
updateLog("Delete Device Success", status);
},
error: function(data, status) {
updateLog("Delete Device Fail", status);
}
});
}
//Deletes a tag. We take in the device Name, Channel Name, Tag Name to delete.
function delTag(deviceName, channelName, tagName) {
updateLog("Deleting Tag " + tagName + " from Device " + deviceName + " under " + channelName);
$.ajax({
type: 'DELETE',
url: 'http://' + inputServer + '/config/v1/project/channels/' + channelName + '/devices/' + deviceName + '/tags/' + tagName,
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(data, status) {
updateLog("Delete Tag Success", status);
updateTags(dselected, cselected);
},
error: function(data, status) {
updateLog("Delete Tag Fail", status);
}
});
}
//MODIFYING OPERATIONS - v6
//Detects changes made in the text field and updates the existing object defined in the Asset tab accordingly
//Stores existing object data
function modChannel(cselected) {
inputChannelOld = cselected;
inputChannel = document.getElementById("channel").value;
if (inputChannelOld != inputChannel) {
updateLog('Updating Channel name to ' + inputChannel); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannelOld,
data: '{"common.ALLTYPES_NAME":"' + inputChannel + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
treeChanged = true;
console.log(d);
updateLog("Channel name changed to " + inputChannel);
},
error: function(JSON, xhr, d) {
console.log(d);
updateLog("Channel name update failed, check your settings.");
}
});
} else {
updateLog("Channel updates failed, remember to select the Channel you wish to modify from the tree panel on the left");
}
}
//Detects changes made in the text field and updates the existing object defined in the Asset tab accordingly
//Stores existing object data
function modDevice(dselected) {
inputDeviceOld = dselected;
inputDevice = document.getElementById("device").value;
inputDevID = document.getElementById("deviceId").value;
inputChannel = document.getElementById("channel").value;
if (inputDeviceOld != inputDevice && inputDevIDOld != inputDevID) {
updateLog('Error - please only update one property at a time.'); //debug alert
} else {
if (inputDeviceOld != inputDevice) {
updateLog('Updating Device name to ' + inputDevice); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + "/devices/" + inputDeviceOld,
data: '{"common.ALLTYPES_NAME":"' + inputDevice + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
treeChanged = true;
console.log(d);
updateLog("Device name changed to " + inputDevice);
},
error: function(JSON, xhr, d) {
console.log(d);
updateLog("Device updates failed, remember to select the Device you wish to modify from the tree panel on the left");
}
});
}
if (inputDevIDOld != inputDevID) {
updateLog('Updating Device ID to ' + inputDevID); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + "/devices/" + inputDeviceOld,
data: '{"servermain.DEVICE_ID_STRING":"' + inputDevID + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
treeChanged = true;
console.log(d);
updateLog("Device ID changed to " + inputDevID);
},
error: function(JSON, xhr, d) {
console.log(d);
updateLog("Device updates failed, remember to select the Device you wish to modify from the tree panel on the left");
}
});
}
}
}
//Detects changes made in the text field and updates the existing object defined in the Asset tab accordingly
//Stores existing object data
function modTag(tselected, taddrselected) {
inputTagOld = tselected; //Tag name to modfiy
inputTag = document.getElementById("tag").value; //Get new Tag
inputTagAddrOld = taddrselected;
inputTagAddr = document.getElementById("tagAddr").value; //Tag address to modfiy
inputTagType = document.getElementById("tagType").value; //Tag data type to modfiy
inputDevice = document.getElementById("device").value;
inputChannel = document.getElementById("channel").value;
if (inputTagOld != inputTag) {
updateLog('Updating Tag name to ' + inputTag); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + "/devices/" + inputDevice + "/tags/" + inputTagOld,
data: '{"common.ALLTYPES_NAME":"' + inputTag + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
updateLog("Tag name changed to " + inputTag);
updateTags(dselected, cselected);
},
error: function(JSON, xhr, d) {
console.log(d);
//updateLog("Tag updates failed, remember to select the Tag you wish to modify from the tag panel on the right");
}
});
}
if (inputTagAddrOld != inputTagAddr) {
updateLog('Updating Tag Address to ' + inputTagAddr); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + "/devices/" + inputDevice + "/tags/" + inputTagOld,
data: '{"servermain.TAG_ADDRESS":"' + inputTagAddr + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
updateLog("Tag Address changed to " + inputTagAddr);
updateTags(dselected, cselected);
},
error: function(JSON, xhr, d) {
console.log(d);
//updateLog("Tag updates failed, remember to select the Tag you wish to modify from the tag panel on the right");
}
});
}
if (inputTagTypeOld != inputTagType) {
updateLog('Updating Tag Data Type to ' + inputTagType); //debug alert
$.ajax({
type: 'PUT',
url: 'http://' + inputServer + '/config/v1/project/channels/' + inputChannel + "/devices/" + inputDevice + "/tags/" + inputTagOld,
data: '{"servermain.TAG_DATA_TYPE":"' + inputTagType + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(JSON, xhr, d) {
treeChanged = true;
console.log(d);
updateLog("Tag Data Type changed to " + inputTagType);
},
error: function(JSON, xhr, d) {
console.log(d);
updateLog("Check your tag settings, ensure this data type exists");
// updateLog("Tag updates failed, remember to select the Tag you wish to modify from the tag panel on the right");
}
});
} else {
updateLog("Tag updates failed, remember to select the Tag you wish to modify from the tag panel on the right");
}
}
//MISC FUNCITONS BUT RELEVANT TO V6
//Function updates and writes JSON object into Server to load the preconfigured JSON
function setJson(jsonObj) {
$.ajax({
type: 'PUT',
url: inputServer + '/config/v1/project/channels/' + inputChannel,
//Removed .value as we have a global varibale storing it
data: '{"common.ALLTYPES_NAME":"' + jsonObj['common.ALLTYPES_NAME'] + '","Force_Update": true}',
contentType: 'application/json',
xhrFields: {
withCredentials: false
},
headers: {
'Authorization': 'Basic ' + encodeAuth
},
success: function(d) {
console.log(d);
},
error: function() {
console.log(d);
}
});
}