/* eslint-disable no-loop-func */ import Base64 from 'crypto-js/enc-base64'; import Utf8 from 'crypto-js/enc-utf8'; import sha256 from 'crypto-js/sha256'; var port = ''; var devInfoUrl = ''; var devcap = ''; var deviceInfoXml = ''; export function discoverDevice() { var scan_port = 11100; var scan_port_start = 11100; var scan_port_end = 11120; // Note check if the device is still connected to earlier discovered port. /*var discoveredPort = window.localStorage.getItem('rd_device_port'); if(discoveredPort){ scan_port_start = discoveredPort; scan_port_end = discoveredPort; }*/ for (scan_port = scan_port_start; scan_port <= scan_port_end; scan_port++) { getJSON_rdser(scan_port, function (err, data, prt) { if (err !== null && err !== 0 && err !== 12029) { alert('Something went wrong: ' + err); } else { if ( String(data) !== '' && !String(data).includes('NOTREADY') && String(data).includes('READY') ) { var parser = new DOMParser(); var xmlDoc = parser.parseFromString(String(data), 'text/xml'); var cells = xmlDoc.getElementsByTagName('Interface'); for (var i = 0; i < cells.length; i++) { var id = cells[i].getAttribute('id'); if (id === 'CAPTURE') devcap = cells[i].getAttribute('path'); else if (id === 'DEVICEINFO') devInfoUrl = cells[i].getAttribute('path'); } port = prt; window.localStorage.setItem('rd_device_port', port); window.localStorage.setItem('rd_device_url', devInfoUrl); getJSON_info(); } } }); } function getJSON_rdser(prt, callback) { var url = 'https://localhost:' + prt; var xhr; var ua = window.navigator.userAgent; var msie = ua.indexOf('MSIE '); if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\\:11\./)) { // IE browser xhr = new window.ActiveXObject('Microsoft.XMLHTTP'); // check } else { // other browser xhr = new XMLHttpRequest(); } xhr.open('RDSERVICE', url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var status = xhr.status; if (status === 200) { callback(null, xhr.responseText, prt); } } }; xhr.send(); } var getJSON_info = function () { var xhr; var ua = window.navigator.userAgent; var msie = ua.indexOf('MSIE '); var devicePort = window.localStorage.getItem('rd_device_port'); var deviceUrl = window.localStorage.getItem('rd_device_url'); var url = 'https://localhost:' + devicePort + deviceUrl; if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\\:11\./)) { // IE browser xhr = new window.ActiveXObject('Microsoft.XMLHTTP'); } else { // other browser xhr = new XMLHttpRequest(); } xhr.open('DEVICEINFO', url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var status = xhr.status; if (status === 200) { deviceInfoXml = xhr.response; } else { // msgElement.setAttribute('color', 'error'); // msgElement.value.innerHTML = // 'Error while connecting to RD device. Please ensure that device is connected.'; // document.getElementById('discoverDevice').style.display = 'block'; } } }; xhr.send(); }; return deviceInfoXml; } export let getJSONCapture = function (dev) { var deviceType = 'F'; var devicePort = window.localStorage.getItem('rd_device_port'); dev = deviceInfoXml; if ( dev.includes('EPI1000') || dev.includes('IRITECH') || dev.includes('MIS100V2') || dev.includes('MFS100') ) { deviceType = 'I'; } else { deviceType = 'F'; } if (dev.includes('MFS100')) { deviceType = 'F'; } var ra = deviceType; var rc = 'Y'; var lr = 'Y'; var de = 'N'; var pfr = 'N'; var text = '2.5' + ra + rc + lr + de + pfr; var wadh = Base64.stringify(sha256(text)); //Convert to SHA-256; and then to base64 var url = 'https://localhost:' + devicePort + devcap; //very important variable var InputXml = '' + '' + ''; var xhr; var ua = window.navigator.userAgent; var msie = ua.indexOf('MSIE '); if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv:11\./)) { // If Internet Explorer, return version number // IE browser xhr = new window.ActiveXObject('Microsoft.XMLHTTP'); } else { // other browser xhr = new XMLHttpRequest(); } xhr.open('CAPTURE', url, false); xhr.send(InputXml); if (xhr.status === 200) { return Base64.stringify(Utf8.parse(xhr.responseText)); } else { return 'error'; } };