const CHANNELS = ["channels\\.INPUT", "channels\\.OUTPUT", "channels\\.DIAGNOSTICS", "channels\\.SETTINGS"] function getObjectFromFields() { let map = {}; if ($(`#channels\\.INPUT\\.port`).val() !== undefined) { for (let channel of CHANNELS) { let channel0 = channel.split("\\").join(''); map[`${channel0}.port`] = $(`#${channel}\\.port`).val(); map[`${channel0}.enabled`] = $(`#${channel}\\.enabled`).prop('checked').toString(); } } return { "name": $("#nameIO").val(), "controllerClass": $("#classIO").val(), "enabled": $('#enabledIO').prop('checked'), "ipAddress": $("#ipIO").val(), "port": $('#portIO').val(), "description": $('#desIO').val(), "extraFields": map }; } function createController() { console.log("start createController"); let obj = getObjectFromFields(); $.ajax({ type: "POST", url: '/api/controllers', data: JSON.stringify(obj), contentType: "application/json; charset=utf-8", dataType: "json", complete: function () { tableIO.ajax.reload(); customHistory.back(); }, error: function(errMsg) { console.log(errMsg); } }); } function updateController(id) { console.log("START updateController"); let obj = getObjectFromFields(); $.ajax({ type: "PUT", url: '/api/controllers/' + id + '/', data: JSON.stringify(obj), contentType: "application/json; charset=utf-8", dataType: "json", success: $('#btn-create-controller1').empty().append( ''), complete: function () { tableIO.ajax.reload(); customHistory.back(); }, error: function(errMsg) { console.log(errMsg); } }); } function newFormDeviceIO() { $('#io-form').empty().append('

Новое устройство ввода/вывода:



\n' + '
\n' + ''); $('#btn-create-controller1').empty().append( ''); pageselect('io-add-page'); } function fillFormIO(val) { if (val === 'TibboDriver') { $('#io-form').empty().append('

Новое устройство ввода/вывода:



\n' + '
\n' + '

\n' + '\n' + '

\n' + '\n' + '

\n' + '\n' + '

' + '
' + '

\n' + '
' + '

\n' + '
' + '

\n' + '
' + '

' + '' + '' + '' + '' + '

Устройство включено

' + '' ); } else { $('#io-form').empty().append('

Новое устройство ввода/вывода:



\n' + '
\n' + '

\n' + '\n' + '

\n' + '\n' + '

\n' + '\n' + '

' + '\n' + '

' + '
' + '
' + '
' ); } $(".validateIO").change(function() { let isIncorrectIp = false; let isIncorrectPort = false let all = $(".validateIO").map(function() { if (this.disabled === false) { if (this.id === 'ipIO') {isIncorrectIp = validateIpAddressIO($("#ipIO"));} if (this.id === 'portIO') {isIncorrectPort = validatePortIO($("#portIO"));} return this.id; } }).get(); for (let i = 0; i < all.length; i++) { let elem = $("#" + all[i]).val(); if (elem === null || elem === "" || isIncorrectIp || isIncorrectPort) { $("#btn-save-controller").prop('disabled', true); return; } } $("#btn-save-controller").prop('disabled', false); }); } function editController(id) { $.ajax({ type: "GET", url: '/api/controllers/' + id + '/', contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { fillUpdateFormIO(data); }, error: function(errMsg) { console.log(errMsg); } }); pageselect('io-add-page'); } function fillUpdateFormIO(data) { if (data.controllerClass === 'ru.kservice.ksm.line.driver.TibboDriver' || data.controllerClass === 'ru.kservice.ksm.line.driver.mock.MockDriver') { let arr = data.controllerClass.split('.'); let shortName = arr[arr.length - 1]; $('#io-form').empty().append('

Новое устройство ввода/вывода:



\n' + '
\n' + '

\n' + '\n' + '

\n' + '\n' + '

\n' + '\n' + '

\n' + '
' + '

\n' + '
' + '

\n' + '
' + '

\n' + '
' + '

\n' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' + '
' ); for (let channel of CHANNELS) { let key = `${channel}.port`.split("\\").join(''); let value = data.extraFields[`${key}`]; $(`#${channel}\\.port`).val(value); let channel0 = channel.split("\\").join(''); let bool= data.extraFields[`${channel0}.enabled`] == "true"; $(`#${channel}\\.enabled`).prop('checked', bool); } $('#desIO').val(data.description); $('#nameIO').val(data.name); $('#enabledIO').prop('checked', data.enabled); $('#btn-create-controller1').empty().append( ''); } else { let arr = data.controllerClass.split('.'); let shortName = arr[arr.length - 1]; $('#io-form').empty().append('

Новое устройство ввода/вывода:



\n' + '
\n' + '

\n' + '\n' + '

\n' + '\n' + '

\n' + '\n' + '

' + '\n' + '

' + '' + '
' + '
' ); $('#desIO').val(data.description); $('#enabledIO').prop('checked', data.enabled); $('#nameIO').val(data.name); $('#btn-create-controller1').empty().append( ''); } $(".validateIO").change(function() { let isIncorrectIp = false; let isIncorrectPort = false let all = $(".validateIO").map(function() { if (this.disabled === false) { if (this.id === 'ipIO') {isIncorrectIp = validateIpAddressIO($("#ipIO"));} if (this.id === 'portIO') {isIncorrectPort = validatePortIO($("#portIO"));} return this.id; } }).get(); for (let i = 0; i < all.length; i++) { let elem = $("#" + all[i]).val(); if (elem === null || elem === "" || isIncorrectIp || isIncorrectPort) { $("#btn-save-controller").prop('disabled', true); return; } } $("#btn-save-controller").prop('disabled', false); }); } function validateIpAddressIO(ipaddress) { if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress.val())) { ipaddress.css('color', 'black'); return false; } ipaddress.css('color', 'red'); return true; } function validatePortIO(port) { if (/^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/.test(port.val())) { port.css('color', 'black'); return false; } port.css('color', 'red'); return true; }