var deviceSubscriptions = {}
function clearDeviceListContainer() {
$("#device-list-page > .config").empty();
}
function requestDeviceList() {
$.ajax({
async: false,
dataType: 'json',
url: '/manage/device',
success: function (jsondata) {
fillDeviceList(jsondata);
resetPagingPageNumber("#device-list-page");
nextView("#device-list-page");
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("ERROR : ", jqXHR.responseText);
}
});
}
function fillDeviceList(deviceList) {
clearDeviceListContainer();
for (let i = 0; i < deviceList.length; i++) {
let device = deviceList[i];
$("#device-list-page > .config").append(renderDeviceRow(i, device));
}
}
function renderDeviceRow(index, device) {
let testButtonText = "Настройки";
let testButtonIcon = "glyphicon-wrench";
switch (device.type) {
case "PRINTER":
testButtonIcon = "glyphicon-print";
break;
case "CONTROL":
case "OTHER":
testButtonText = "Тест";
testButtonIcon = "glyphicon-bullhorn";
break;
}
let testButtonPattern = `
`;
return `
${device.localizedName}
${device.testAvailable ? testButtonPattern : ''}
${device.localizedStatus}
`;
}
function deviceTest() {
let deviceName = $(this).data("deviceName");
let deviceType = $(this).data("deviceType");
let deviceLocalizedName = $(this).data("localizedName");
switch (deviceType) {
case "PRINTER":
openPrinterTest(deviceName, deviceLocalizedName);
break;
case "CONTROL":
case "LIGHT":
subscribeOnce(`/app/device/${deviceName}/test`);
alert(`Запущено тестирование устройства ${deviceName}`);
break;
case "ENCODER":
$("#encoder-device-name").val(deviceName);
pageselect("encoder-test-page");
break;
case "OTHER":
$("#device-name").val(deviceName);
pageselect("device-test-page");
break;
default:
alert(`Неизвестное устройство. Тип: ${deviceType}, имя: ${deviceName}`);
}
}
function startDeviceOutput(deviceName, handler) {
subscribeOnce(`/app/device/${deviceName}/output/start`)
.then(message => {
handler(message);
deviceSubscriptions[deviceName] = stompClient.subscribe(`/topic/device/${deviceName}/output`, handler);
});
}
function stopDeviceOutput(deviceName, handler) {
subscribeOnce(`/app/device/${deviceName}/output/stop`).then(message => {
if (deviceSubscriptions[deviceName]) {
deviceSubscriptions[deviceName].unsubscribe();
}
handler(message);
});
}