Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //исправлено Антоном для привязки пользователя к городу,v13
- "use strict";
- let hulla = new hullabaloo();
- let wb = new wbdialog();
- let scrollX=true;
- let pagesize=15;
- let get = parseQuery(window.location.search);
- let address = null;
- let commands = null;
- let parProfiles = null;
- let deviceNames = null;
- let parents = {};
- let editmode = false;
- const filterstoragekey = "filter"+window.location.pathname;
- moment.locale(environment.language || window.navigator.userLanguage || window.navigator.language);
- function capitalizeFirstLetter(string) {
- return string.charAt(0).toUpperCase() + string.slice(1);
- }
- $(async function() {
- $('#notificationBell').hide();
- $('#loader').show();
- let town;
- if (environment.login_name.indexOf("_") !== -1) {
- town = '&town='+capitalizeFirstLetter(environment.login_name.split('_')[0])
- }
- else{
- town = ''
- }
- let previousTotal = localStorage.getItem('previousTotal') ? parseInt(localStorage.getItem('previousTotal') - 1) : 0;
- let soundEnabled = false;
- function updateEventData() {
- $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=last_statistic'+town,
- type: 'GET',
- dataType: "json",
- success: function(data) {
- let eventData = data.rows[0]
- var eventList = $('#eventList');
- const now = new Date();
- const formattedDateTime = now.toLocaleString();
- const total = eventData.PowerFailureCount + eventData.EquipmentFailureCount + eventData.SipFailureCount + eventData.IpFailureCount + eventData.TimeAlertFailureCount + eventData.ControllerFailureCount + eventData.DoorOpenCount + eventData.SyncFailureCount;
- if (total !== previousTotal) {
- eventList.empty();
- eventList.append('<li class="datetime">Time: ' + formattedDateTime + '</li>');
- eventList.append('<li>' + i18next.t("Power Failure") + ': ' + eventData.PowerFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("Object Failure") + ': ' + eventData.EquipmentFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("SIP failure") + ': ' + eventData.SipFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("IP failure") + ': ' + eventData.IpFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("Clock error") + ': ' + eventData.TimeAlertFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("Controller error") + ': ' + eventData.ControllerFailureCount + '</li>');
- eventList.append('<li>' + i18next.t("Door open") + ': ' + eventData.DoorOpenCount + '</li>');
- eventList.append('<li>' + i18next.t("Out of sync") + ': ' + eventData.SyncFailureCount + '</li>');
- $('#eventCount').text(total)
- } else {
- eventList.find('li.datetime').text('Time: ' + formattedDateTime + '')
- }
- $('#loader').hide();
- $('#notificationBell').show();
- if(total > 100){
- $('.notification-container').addClass('large')
- }
- if (total > previousTotal && soundEnabled) {
- $('#notificationSound')[0].play();
- }
- previousTotal = total;
- localStorage.setItem('previousTotal', previousTotal);
- }
- });
- }
- setInterval(updateEventData, 1000);
- $('#eventCount').click(function() {
- $('#eventDropdown').toggle();
- });
- $('#notificationBell').click(function() {
- soundEnabled = true;
- $('#notificationBell').removeClass('disabled').addClass('active');
- });
- $(document).click(function(event) {
- if (!$(event.target).closest('#eventCount, #eventDropdown').length) {
- if ($('#eventDropdown').is(":visible")) {
- $('#eventDropdown').hide();
- }
- }
- });
- });
- $(function() {
- $('[data-toggle="tooltip"]').tooltip();
- scrollX = !($('#descktop-visible').is(':visible'));
- if(scrollX)
- pagesize=10;
- onload();
- });
- async function onload() {
- let result;
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=address',
- type: 'GET',
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- $("#content").html("<h2>Address info's was not loaded</h2>");
- return;
- }
- address = result;
- // привязка к городу
- //$('#Town_id').find('option').remove();
- let towns = address.towns.rows;
- //Привязка пользователей к городу
- // if (environment.login_name.indexOf("_") !== -1) {
- if (environment.login_name.indexOf("_") !== -1 || environment.login_name === 'автодор') {
- for (let key in towns) {
- // if (environment.login_name.split('_')[0].toLowerCase() === towns[key].Name.toLowerCase()) {
- if ((environment.login_name.split('_')[0].toLowerCase() === towns[key].Name.toLowerCase()) || (environment.login_name === 'автодор' && towns[key].Name.toLowerCase().includes('орстрой'))) {
- $("#Town_id").append($("<option></option>")
- .attr("value", towns[key].id)
- .prop("selected", true)
- .text(towns[key].Name));
- }
- }
- } else {
- $("#Town_id").append($("<option></option>")
- .attr("value", "")
- .prop("selected", true)
- .text(i18next.t("Any city")));
- for (let key in towns) {
- $("#Town_id").append($("<option></option>")
- .attr("value", towns[key].id)
- .text(towns[key].Name));
- }
- }
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=ServerCommand&type=1,3',
- type: 'GET',
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- $("#content").html("<h2>Commands was not loaded</h2>");
- return;
- }
- commands = result.rows;
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=dimprofiles&filter=true',
- type: 'GET',
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- $("#content").html("<h2>Profiles was not loaded</h2>");
- return;
- }
- parProfiles = result.rows;
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=devicenames&filter=true',
- type: 'GET',
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- $("#content").html("<h2>Names was not loaded</h2>");
- return;
- }
- deviceNames = result.rows;
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=parents',
- type: 'GET',
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- $("#content").html("<h2>Parents was not loaded</h2>");
- return;
- }
- parents.stations = result.stations.rows;
- parents.boxes = result.boxes.rows;
- addNames();
- addProfiles();
- selTypeMenu("#seltype",0,"obj");
- add2Address();
- modeMenu("#mode",0);
- powerMenu("#power");
- condMenu("#cond");
- comMenu("#com");
- onoffMenu("#onoff");
- //привязка к городу
- if($('#map').length > 0) {
- goTown();
- }
- //addParents();
- setTimeout(function () {
- if($('#Town_id').val().length > 0) {
- add2Address()
- }
- },500);
- if( get['device']==undefined ) {
- loadflt();
- }
- let initflt = {show:false};
- Object.assign(initflt, JSON.parse(localStorage.getItem(filterstoragekey)) || {} );
- if( get['device']!==undefined ) {
- initflt = {show:initflt.show};
- }
- if(initflt.show)
- tougleOpt();
- setTimeout(renew,100);
- }
- function add2Address() {
- addAddress("District", address.districts.rows, i18next.t("Any district"));
- addAddress("Location", address.locations.rows, i18next.t("Any street"));
- addParents();
- $("#Location").selectpicker('refresh');
- }
- function addAddress(sel, obj, anystr="Any") {
- let town = $('#Town_id').val();
- $("#"+sel).find('option').remove();
- $("#"+sel).append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t(anystr)));
- let names = new Set( obj.filter((x)=> town == "" || x.Town_id == town).map((x) => x.Name) );
- for (let name of names) {
- $("#"+sel).append($("<option></option>")
- .attr("value",name)
- .text(name));
- }
- }
- function addProfiles() {
- $("#DimProfile_id").find('option').remove();
- $("#DimProfile_id").append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t("Any shedule")));
- for (let pard of parProfiles) {
- $("#DimProfile_id").append($("<option></option>")
- .attr("value",`{"DimProfile_id":${pard.id}}`)
- .text(pard.Name));
- }
- }
- function addNames() {
- $("#Name").find('option').remove();
- $("#Name").append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t("Any name")));
- for (let name of deviceNames) {
- $("#Name").append($("<option></option>")
- .attr("value",name.Name)
- .text(name.Name));
- }
- }
- function addParents() {
- let town = $("#Town_id").val();
- let typeval = $("#seltype").val(), type = 0;
- if(typeval)
- type = JSON.parse(typeval).Type;
- $("#Station").find('option').remove();
- $("#Station").append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t("Any connection")));
- if(type==0 || type==2) {
- let boxes = parents.boxes.filter(x => town == "" || x.Town_id == town);
- for (let box of boxes) {
- $("#Station").append($("<option></option>")
- .attr("value",`{"box":${box.id}}`)
- .text(`№${box.CustomNum} ${box.Name}`));
- }
- }
- let stations;
- if(type==0 || type==1)
- stations = new Set( parents.stations.filter((x)=> x.Station && (town == "" || x.Town_id == town)).map((x) => x.Station) );
- console.log(stations);
- if(type==0 && stations.size)
- $("#Station").append($("<option></option>")
- .prop("disabled",true)
- .text(" "+i18next.t("Station")+":"));
- if(type==0 || type==1) {
- for (let station of stations) {
- $("#Station").append($("<option></option>")
- .attr("value",station)
- .text(station));
- }
- }
- $("#Name").find('option').remove();
- $("#Name").append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t("Any name")));
- let names = deviceNames.filter(x => town == "" || x.Town_id == town);
- for (let name of names) {
- $("#Name").append($("<option></option>")
- .attr("value",name.Name)
- .text(name.Name));
- }
- $("#DimProfile_id").find('option').remove();
- $("#DimProfile_id").append($("<option></option>")
- .attr("value","")
- .prop("selected",true)
- .text(i18next.t("Any shedule")));
- let pars = parProfiles.filter(x => town == "" || x.Town_id == town);
- for (let pard of pars) {
- $("#DimProfile_id").append($("<option></option>")
- .attr("value",`{"DimProfile_id":${pard.id}}`)
- .text(pard.Name));
- }
- }
- function addcmd(devtype) {
- $("#cmd").find('option').remove();
- for (let key in commands) {
- if(commands[key].DeviceType == devtype && commands[key].Letter!="F") {
- $("#cmd").append($("<option></option>")
- .attr("value",commands[key].id)
- .text(i18next.t(commands[key].Name)));
- }
- }
- }
- async function runCmd() {
- let ids = seldev.slice(0);
- //cmdclear();
- let srvcmd = {};
- srvcmd.cmd = parseInt($("#cmd").val());
- let selcmd = commands.find(x => x.id == srvcmd.cmd);
- let cmdtext=i18next.t(selcmd.Name);
- for(let i=0; i<ids.length;i++) ids[i] = parseInt(ids[i]);
- srvcmd.devs = ids;
- let result = null;
- try {
- result = await $.ajax({
- url: environment.base_url+"/"+'?do=api&fn=makecalls',
- data: JSON.stringify(srvcmd),
- type: 'PUT',
- contentType: "application/json; charset=utf-8",
- dataType: "json"
- });
- } catch (error) {
- console.error(error);
- hulla.send(i18next.t("Device control error"));
- return false;
- }
- if(result.success) {
- switch (result.queued) {
- case 0:
- hulla.send(i18next.t("Command not queued"));
- break;
- case 1:
- hulla.send(`${i18next.t("Command")} "${cmdtext}" ${i18next.t("queued")}`,"success");
- break;
- default:
- hulla.send(`${cmdtext} - ${i18next.t("Command queued for multiple devices")} (${result.queued})`,"success");
- break;
- }
- } else {
- hulla.send(i18next.t("An error occurred while adding commands to the queue"));
- console.log(result);
- }
- return false;
- }
- function tougleOpt() {
- let flt = JSON.parse(localStorage.getItem(filterstoragekey)) || {};
- if($("#opt-form").hasClass("d-none")) {
- $("#opt-form").removeClass("d-none");
- $("#hdcap").removeClass("fa-angle-double-down");
- $("#hdcap").addClass("fa-angle-double-up");
- $(".address").removeClass("d-none");
- flt.show = true;
- }
- else {
- $("#hdcap").removeClass("fa-angle-double-up");
- $("#hdcap").addClass("fa-angle-double-down");
- $("#opt-form").addClass("d-none");
- $(".address").addClass("d-none");
- flt.show = false;
- }
- localStorage.setItem(filterstoragekey,JSON.stringify(flt));
- }
- const simplefld = ['Town_id','District','Location','seltype','power','mode','cond','com','onoff', 'DimProfile_id', 'Name', 'Station'];
- function changeflt() {
- let saveflt = JSON.parse(localStorage.getItem(filterstoragekey)) || {} ;
- for(let sel of simplefld) {
- let val = $("#"+sel).val();
- if(val==0 || val==null || val=="") {
- if(saveflt.hasOwnProperty(sel))
- delete saveflt[sel];
- continue;
- }
- saveflt[sel]=val;
- }
- if( get['device']==undefined )
- localStorage.setItem( filterstoragekey, JSON.stringify(saveflt) );
- }
- function loadflt() {
- let saveflt = JSON.parse(localStorage.getItem(filterstoragekey)) || {} ;
- for(let sel of simplefld) {
- if(saveflt.hasOwnProperty(sel) && saveflt[sel]!==null && saveflt[sel]!=="" && saveflt[sel]!==0) {
- $("#"+sel).val(saveflt[sel]);
- if(sel=="Location")
- $("#Location").selectpicker('refresh');
- }
- }
- }
- function makeflt() {
- let flt = {};
- for(let sel of simplefld) {
- let val = $("#"+sel).val();
- if(val==0 || val==null || val=="")
- continue;
- if( val[0]=="{" || val[0]=="[" )
- val=JSON.parse(val);
- if( val !== "") {
- if(Array.isArray(val))
- flt[sel] = val.join(",");
- else if(typeof val === "object") {
- for(let key in val) {
- let prop = val[key];
- if(Array.isArray(prop))
- flt[key]=prop.join(",");
- else
- flt[key]=prop;
- }
- }
- else {
- flt[sel]=val;
- }
- }
- }
- return flt;
- }
- let saveprocessing = false;
- async function seteditmode(mode=false, save=false) {
- if(save) {
- if(saveprocessing)
- return;
- saveprocessing=true;
- if(movedObj.size == 0)
- hulla.send(i18next.t("Objects have not been moved"), "info");
- else {
- let url=environment.base_url+"/"+'?do=api&fn=devicebulkupdate';
- try {
- await $.ajax({
- url: url,
- type: 'POST',
- contentType: "application/json; charset=utf-8",
- data: JSON.stringify( Object.fromEntries(movedObj) ),
- dataType: "json"
- });
- } catch (error) {
- console.log(error);
- hulla.send(`${i18next.t("Failed to save data")} - ${i18next.t("Error occurred")}`);
- saveprocessing=false;
- return;
- }
- hulla.send(i18next.t("Objects moved") + ": " + movedObj.size, "success");
- }
- saveprocessing=false;
- }
- editmode = mode;
- if(editmode) {
- $("#dbtnEdit, #dbtnTougleFilter").addClass("d-none");
- $("#dbtnSave, #dbtnCancel").removeClass("d-none");
- } else {
- $("#dbtnEdit, #dbtnTougleFilter").removeClass("d-none");
- $("#dbtnSave, #dbtnCancel").addClass("d-none");
- }
- setTimeout(markers, 500);
- cmdpanel();
- movedObj.clear();
- }
- function devicon(dev) {
- let rnd=[];
- let iconName = 'lamp.svg';
- if(dev.Type==1)
- iconName = 'box.svg';
- iconName +="?ver=" + config.version;
- if(dev.Smart == 0)
- rnd.push('unmanaged');
- if(dev.Sip==0 || dev.Ip==0)
- rnd.push('nocon');
- if((dev.Power < 7 && dev.Type==1) || dev.Power==0)
- rnd.push('nopower');
- if(dev.Mode == 4)
- rnd.push('manual');
- else
- rnd.push('auto');
- if(dev.Trouble > 0)
- rnd.push('trouble');
- if(dev.State)
- rnd.push('on');
- else
- rnd.push('off');
- if(dev.Timeallert)
- rnd.push('timeallert');
- if(!dev.Controller)
- rnd.push('controlallert');
- if(!dev.Door)
- rnd.push('open');
- if(config.ous_pulse && dev.Ous)
- rnd.push('ous');
- if (dev.num_contactors >= 2 && dev.State > 0) {
- rnd.push('multiple=true');
- }
- if (dev.Door === 0) rnd.push("open=1");
- if (dev.Type == 1)
- rnd.push('state=' + (dev.State ?? 0));
- iconName += "&"+rnd.join("&");
- return environment.base_url+"/images/"+iconName;
- }
- function selection_fire(clear=false) {
- let atxt=[];
- let acls=[];
- let aspn=[];
- if(seldev.length == 0) {
- if(clear)
- cmdclear();
- return;
- }
- let id1 = seldev.slice(0, 1)[0];
- let ind1 = ddata.map[id1];
- let dt1 = ddata.rows[ind1].Type;
- let mix = false;
- for(let id of seldev) {
- let ind = ddata.map[id];
- let dev = ddata.rows[ind];
- if(dt1 !== dev.Type) {
- mix = true;
- break;
- }
- acls.push("."+"cli"+dev.id);
- aspn.push(`<div data-toggle="tooltip" data-placement="right" data-html="true" id="ctrl_${dev.id}" class="devctrl" onclick="goDevice(${dev.id},false,true)">${dev.CustomNum}</div>`)
- }
- if(mix) {
- hulla.send(i18next.t("Mixed types selection are not allowed"));
- cmdclear();
- return;
- }
- $("#devlist").html(aspn.join(" "));
- $(".cli").removeClass("border border-primary");
- $(acls.join(",")).addClass("border border-primary");
- addcmd(dt1);
- cmdpanel();
- }
- function removeFilter() {
- $("#District, #Location, #power, #mode, #cond, #com, #onoff, #DimProfile_id, #Name, #Station").prop('selectedIndex', 0).trigger("change");
- $("#seltype").prop('selectedIndex', 2).trigger("change");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement