diff --git a/server/SHServ/Controllers/DevicesRESTAPIController.php b/server/SHServ/Controllers/DevicesRESTAPIController.php index 9c39ce3..314e492 100644 --- a/server/SHServ/Controllers/DevicesRESTAPIController.php +++ b/server/SHServ/Controllers/DevicesRESTAPIController.php @@ -251,4 +251,31 @@ "total" => count($devices) ]); } + + public function update_name($device_id, $name) { + $device_id = intval($device_id); + + if($device_id < 1) { + return $this -> utils() -> response_error("invalid_id", ["device_id"]); + } + + $device = new Device($device_id); + + if(!$device) { + return $this -> utils() -> response_error("device_not_found"); + } + + if(!strlen($name)) { + return $this -> utils() -> response_error("empty_field", ["name"]); + } + + if(!$device -> device_api() -> set_device_name($name)) { + return $this -> utils() -> response_error("device_request_fail"); + } + + $device -> name = $name; + $device -> update(); + + return $this -> utils() -> response_success(); + } } \ No newline at end of file diff --git a/server/SHServ/Routes/DevicesRESTAPI_v1.php b/server/SHServ/Routes/DevicesRESTAPI_v1.php index 1da0f64..5eff81a 100644 --- a/server/SHServ/Routes/DevicesRESTAPI_v1.php +++ b/server/SHServ/Routes/DevicesRESTAPI_v1.php @@ -34,6 +34,12 @@ "{$this -> cn}\\DevicesRESTAPIController@place_in_area", "/api/v1/devices/place-in-area" ); + + $this -> router -> post( + [ "device_id", "name" ], + "{$this -> cn}\\DevicesRESTAPIController@update_name", + "/api/v1/devices/update-name" + ); } protected function devices_restapi_get_routes() { diff --git a/webclient/dist/js/main.js b/webclient/dist/js/main.js index 1aa40b9..8836378 100644 --- a/webclient/dist/js/main.js +++ b/webclient/dist/js/main.js @@ -1,4 +1,4 @@ -(()=>{var Oe=Object.defineProperty,Ce=Object.defineProperties;var ke=Object.getOwnPropertyDescriptors;var ee=Object.getOwnPropertySymbols;var Ie=Object.prototype.hasOwnProperty,Re=Object.prototype.propertyIsEnumerable;var te=(a,e,t)=>e in a?Oe(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,v=(a,e)=>{for(var t in e||(e={}))Ie.call(e,t)&&te(a,t,e[t]);if(ee)for(var t of ee(e))Re.call(e,t)&&te(a,t,e[t]);return a},ae=(a,e)=>Ce(a,ke(e));var ne=(a,e,t)=>new Promise((n,s)=>{var r=c=>{try{i(t.next(c))}catch(l){s(l)}},o=c=>{try{i(t.throw(c))}catch(l){s(l)}},i=c=>c.done?n(c.value):Promise.resolve(c.value).then(r,o);i((t=t.apply(a,e)).next())});var h,$,A;function xe(){h.dataset.navState="displayed",h.classList.remove("state-off"),h.classList.add("state-on"),$.classList.add("a-show")}function N(){h.dataset.navState="hidden",h.classList.remove("state-on"),h.classList.add("state-off"),$.classList.add("a-hide"),$.classList.remove("a-show"),setTimeout(()=>{$.classList.remove("a-hide")},300)}function se(){console.log("HUD init"),h=document.querySelector(".hud .nav-toggle"),$=document.querySelector(".hud .navigation"),A=document.querySelector(".hud .reload-screen"),h==null||h.addEventListener("click",a=>{a.currentTarget.dataset.navState!="displayed"?xe():N()}),A==null||A.addEventListener("click",a=>{Screens.reload()})}var M=class{constructor(e,t,n){this.screens={},this.routesMap={},this.currentScreen=null,this.eventsHandlers={switch:[],reload:[],reinit:[],errorScreen:[]},this.screensContainer=document.querySelector(e),this.loader=document.querySelector(t),this.errorScreen=document.querySelector(n),console.log("Screens Init")}add(e,t){if(typeof t!="object")return console.error("Screens: screens must be object");if(typeof(t==null?void 0:t.alias)=="undefined")return console.error("Screens: undefined alias");if(typeof(t==null?void 0:t.renderer)!="function")return console.error("Screens: renderer must be function");this.screens[t.alias]=v({route:e},t),this.routesMap[e]=t.alias}switch(e){var n;if(this.runSwitchHandlers(e),this.hideErrorScreen(),this.showLoader(),(n=this.currentScreen)==null||n.DOMObject.remove(),typeof this.screens[e]=="undefined"){console.error(`Screens: "${e}" not found`);return}this.currentScreen=this.screens[e];let t=document.createElement("div");t.classList.add("screen"),t.id=e,t.dataset.alias=e,t.innerHTML=this.currentScreen.renderer(),this.currentScreen.DOMObject=t,this.screensContainer.append(this.currentScreen.DOMObject),this.currentScreen.initer(this)}reload(){this.currentScreen&&(this.runReloadHandlers(this.currentScreen.alias),this.switch(this.currentScreen.alias))}reinit(){this.currentScreen&&(this.currentScreen.initer(this),this.runReinitHandlers())}routing(){setInterval(()=>{let e=document.location.hash.split("#!")[1];if(typeof e=="undefined"||e=="")return;let t=typeof this.routesMap[e]=="undefined"?"not-found-screen":this.routesMap[e];(!this.currentScreen||this.currentScreen.alias!=t)&&this.switch(t)},50)}ready(){this.currentScreen!=null&&(this.currentScreen.DOMObject||(this.currentScreen.DOMObject=document.getElementsById(this.currentScreen.alias)),this.hideLoader(),this.currentScreen.DOMObject.classList.add("a-show"))}error(e,t){var n;(n=this.currentScreen)==null||n.DOMObject.remove(),this.errorScreen.querySelector(".error-title").innerHTML=e,this.errorScreen.querySelector(".error-text").innerHTML=t,this.showErrorScreen(),this.runErrorScreenHandlers()}hideLoader(){this.loader.classList.remove("a-show")}showLoader(){this.loader.classList.add("a-show")}showErrorScreen(){this.errorScreen.classList.add("a-show")}hideErrorScreen(){this.errorScreen.classList.remove("a-show")}getScreens(){return this.screens}getRoutesMap(){return this.routesMap}onSwitch(e){this.eventsHandlers.switch.push(e)}onReaload(e){this.eventsHandlers.reload.push(e)}onReinit(e){this.eventsHandlers.reinit.push(e)}onErrorScreen(e){this.eventsHandlers.errorScreen.push(e)}runSwitchHandlers(e){let t=Object.keys(this.routesMap).filter(n=>this.routesMap[n]==e);for(let n of this.eventsHandlers.switch)n(this,e,t.length?t[0]:void 0)}runReloadHandlers(e){for(let t of this.eventsHandlers.reload)t(this,e)}runReinitHandlers(){for(let e of this.eventsHandlers.reinit)e(this)}runErrorScreenHandlers(){for(let e of this.eventsHandlers.errorScreen)e(this)}};function Ne(a,e,t,n){return` +(()=>{var Oe=Object.defineProperty,Ce=Object.defineProperties;var ke=Object.getOwnPropertyDescriptors;var ee=Object.getOwnPropertySymbols;var Ie=Object.prototype.hasOwnProperty,Re=Object.prototype.propertyIsEnumerable;var te=(a,e,t)=>e in a?Oe(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,b=(a,e)=>{for(var t in e||(e={}))Ie.call(e,t)&&te(a,t,e[t]);if(ee)for(var t of ee(e))Re.call(e,t)&&te(a,t,e[t]);return a},ae=(a,e)=>Ce(a,ke(e));var ne=(a,e,t)=>new Promise((n,s)=>{var r=c=>{try{i(t.next(c))}catch(l){s(l)}},o=c=>{try{i(t.throw(c))}catch(l){s(l)}},i=c=>c.done?n(c.value):Promise.resolve(c.value).then(r,o);i((t=t.apply(a,e)).next())});var h,$,A;function xe(){h.dataset.navState="displayed",h.classList.remove("state-off"),h.classList.add("state-on"),$.classList.add("a-show")}function N(){h.dataset.navState="hidden",h.classList.remove("state-on"),h.classList.add("state-off"),$.classList.add("a-hide"),$.classList.remove("a-show"),setTimeout(()=>{$.classList.remove("a-hide")},300)}function se(){console.log("HUD init"),h=document.querySelector(".hud .nav-toggle"),$=document.querySelector(".hud .navigation"),A=document.querySelector(".hud .reload-screen"),h==null||h.addEventListener("click",a=>{a.currentTarget.dataset.navState!="displayed"?xe():N()}),A==null||A.addEventListener("click",a=>{Screens.reload()})}var D=class{constructor(e,t,n){this.screens={},this.routesMap={},this.currentScreen=null,this.eventsHandlers={switch:[],reload:[],reinit:[],errorScreen:[]},this.screensContainer=document.querySelector(e),this.loader=document.querySelector(t),this.errorScreen=document.querySelector(n),console.log("Screens Init")}add(e,t){if(typeof t!="object")return console.error("Screens: screens must be object");if(typeof(t==null?void 0:t.alias)=="undefined")return console.error("Screens: undefined alias");if(typeof(t==null?void 0:t.renderer)!="function")return console.error("Screens: renderer must be function");this.screens[t.alias]=b({route:e},t),this.routesMap[e]=t.alias}switch(e){var n;if(this.runSwitchHandlers(e),this.hideErrorScreen(),this.showLoader(),(n=this.currentScreen)==null||n.DOMObject.remove(),typeof this.screens[e]=="undefined"){console.error(`Screens: "${e}" not found`);return}this.currentScreen=this.screens[e];let t=document.createElement("div");t.classList.add("screen"),t.id=e,t.dataset.alias=e,t.innerHTML=this.currentScreen.renderer(),this.currentScreen.DOMObject=t,this.screensContainer.append(this.currentScreen.DOMObject),this.currentScreen.initer(this)}reload(){this.currentScreen&&(this.runReloadHandlers(this.currentScreen.alias),this.switch(this.currentScreen.alias))}reinit(){this.currentScreen&&(this.currentScreen.initer(this),this.runReinitHandlers())}routing(){setInterval(()=>{let e=document.location.hash.split("#!")[1];if(typeof e=="undefined"||e=="")return;let t=typeof this.routesMap[e]=="undefined"?"not-found-screen":this.routesMap[e];(!this.currentScreen||this.currentScreen.alias!=t)&&this.switch(t)},50)}ready(){this.currentScreen!=null&&(this.currentScreen.DOMObject||(this.currentScreen.DOMObject=document.getElementsById(this.currentScreen.alias)),this.hideLoader(),this.currentScreen.DOMObject.classList.add("a-show"))}error(e,t){var n;(n=this.currentScreen)==null||n.DOMObject.remove(),this.errorScreen.querySelector(".error-title").innerHTML=e,this.errorScreen.querySelector(".error-text").innerHTML=t,this.showErrorScreen(),this.runErrorScreenHandlers()}hideLoader(){this.loader.classList.remove("a-show")}showLoader(){this.loader.classList.add("a-show")}showErrorScreen(){this.errorScreen.classList.add("a-show")}hideErrorScreen(){this.errorScreen.classList.remove("a-show")}getScreens(){return this.screens}getRoutesMap(){return this.routesMap}onSwitch(e){this.eventsHandlers.switch.push(e)}onReaload(e){this.eventsHandlers.reload.push(e)}onReinit(e){this.eventsHandlers.reinit.push(e)}onErrorScreen(e){this.eventsHandlers.errorScreen.push(e)}runSwitchHandlers(e){let t=Object.keys(this.routesMap).filter(n=>this.routesMap[n]==e);for(let n of this.eventsHandlers.switch)n(this,e,t.length?t[0]:void 0)}runReloadHandlers(e){for(let t of this.eventsHandlers.reload)t(this,e)}runReinitHandlers(){for(let e of this.eventsHandlers.reinit)e(this)}runErrorScreenHandlers(){for(let e of this.eventsHandlers.errorScreen)e(this)}};function Ne(a,e,t,n){return`
It's empty here yet
- `}var ce={template:{sidebarNav:Be,table:ze,createElement:oe,createAlert:Je,mainTemplate:Ge,connectionStatusBadge:Ke,toogleStateBadge:Qe,circleLoaderHTML:et,emptyHereHTML:tt},unification:{deviceFieldsUnification:Ve,timeAgo:Xe,formatDate:Ze},states:{btnLoadingState:We,cardScriptActionLoadingState:Ye}};var D=class{constructor(e){this.core=e}actions_list(e){return this.core.api_get("/api/v1/scripts/actions/list",e)}scopes_list(e){return this.core.api_get("/api/v1/scripts/scopes/list",e)}regular_list(e){return this.core.api_get("/api/v1/scripts/regular/list",e)}scope_get_by_filename(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/scopes/name/${n}`,t,{})}scope_create(e,t){return this.core.api_post("/api/v1/scripts/scopes/new",e,t)}scope_update(e,t){return this.core.api_post("/api/v1/scripts/scopes/update",e,t)}action_enable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/actions/alias/${n}/enable`,t)}action_disable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/actions/alias/${n}/disable`,t)}regular_enable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/regular/alias/${n}/enable`,t)}regular_disable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/regular/alias/${n}/disable`,t)}scope_enable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/scopes/name/${n}/enable`,t)}scope_disable(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/scopes/name/${n}/disable`,t)}scope_remove(e,t){let n=encodeURIComponent(String(e||""));return this.core.api_get(`/api/v1/scripts/scopes/name/${n}/remove`,t)}run(e,t){return this.core.api_post("/api/v1/scripts/actions/run",e,t)}place_in_area(e,t){return this.core.api_post("/api/v1/scripts/place-in-area",e,t)}unassign_from_area(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/scripts/id/${n}/unassign-from-area`,t)}};var q=class{constructor(e){this.core=e}list(e){return this.core.api_get("/api/v1/devices/list",e)}scanning_setup(e){return this.core.api_get("/api/v1/devices/scanning/setup",e)}scanning_all(e){return this.core.api_get("/api/v1/devices/scanning/all",e)}setup_new_device(e,t){return this.core.api_post("/api/v1/devices/setup/new-device",e,t)}info(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}/info`,t)}get(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}`,t)}status(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}/status`,t)}action(e,t){return this.core.api_post("/api/v1/devices/action",e,t)}remove(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}/remove`,t)}reboot(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}/reboot`,t)}place_in_area(e,t){return this.core.api_post("/api/v1/devices/place-in-area",e,t)}unassign_from_area(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/devices/id/${n}/unassign-from-area`,t)}};var O=class{constructor(e){this.core=e}list(e){return this.core.api_get("/api/v1/areas/list",e)}inner_list(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/list`,t)}new_area(e,t){return this.core.api_post("/api/v1/areas/new-area",e,t)}remove(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/remove`,t)}place_in_area(e,t){return this.core.api_post("/api/v1/areas/place-in-area",e,t)}update_display_name(e,t){return this.core.api_post("/api/v1/areas/update-display-name",e,t)}update_alias(e,t){return this.core.api_post("/api/v1/areas/update-alias",e,t)}devices(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/devices`,t)}scripts(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/scripts`,t)}unassign_from_area(e,t){let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/unassign-from-area`,t)}types_list(e){return this.core.api_get("/api/v1/areas/types/list",e)}reboot_devices(e,t){if(e==null)return this.core.api_get("/api/v1/areas/reboot_devices",t);let n=encodeURIComponent(String(e));return this.core.api_get(`/api/v1/areas/id/${n}/reboot_devices`,t)}};function le(a){if(!a||typeof a!="object")return"";let e=new URLSearchParams;Object.entries(a).forEach(([n,s])=>{s!=null&&e.append(n,String(s))});let t=e.toString();return t?`?${t}`:""}function at(a,e){let t=String(a||"").replace(/\/+$/,""),n=String(e||"").replace(/^\/+/,"");return`${t}/${n}`}function nt(a){try{return{ok:!0,data:JSON.parse(a)}}catch(e){return{ok:!1,error:e}}}var C=class{constructor(e){this.base_url=(e==null?void 0:e.base_url)||"",this.token=(e==null?void 0:e.token)||"",this.timeout_ms=Number.isFinite(e==null?void 0:e.timeout_ms)?e.timeout_ms:15e3,this.default_headers=(e==null?void 0:e.default_headers)||{},this.on_unauthorized=typeof(e==null?void 0:e.on_unauthorized)=="function"?e.on_unauthorized:null,this.proxy_path=(e==null?void 0:e.proxy_path)||"",this.scripts=new D(this),this.devices=new q(this),this.areas=new O(this)}set_base_url(e){this.base_url=e||""}set_token(e){this.token=e||""}set_proxy_path(e){this.proxy_path=e||""}_wrap_path(e,t){if(!this.proxy_path)return t?`${e}${le(t)}`:e;let n=v({path:e},t||{});return`${this.proxy_path}${le(n)}`}request(e,t,n,s,r){let o=typeof s=="function"?s:()=>{},i=at(this.base_url,t),c=new AbortController,l=Number.isFinite(r==null?void 0:r.timeout_ms)?r.timeout_ms:this.timeout_ms,d=setTimeout(()=>c.abort(),l),p=v(v({},this.default_headers),(r==null?void 0:r.headers)||{});this.token&&(p.Authorization=`Bearer ${this.token}`);let m;n!=null&&(p["Content-Type"]="application/json",m=JSON.stringify(n)),fetch(i,{method:e,headers:p,body:m,signal:c.signal}).then(u=>ne(this,null,function*(){clearTimeout(d);let g={url:i,method:e,status_code:u.status,headers:u.headers},b=yield u.text(),x=nt(b),S=x.ok?x.data:b;if(!u.ok){let E={type:"http_error",message:`HTTP ${u.status}`,status_code:u.status,raw:S};if((u.status===401||u.status===403)&&this.on_unauthorized)try{this.on_unauthorized({error:E,meta:g})}catch(Z){}return o(E,null,g)}if(x.ok&&S&&typeof S=="object"){let E=S.status;if(E===!1||E==="error"){let Z={type:"api_error",message:S.message||"API error",status_code:u.status,raw:S,field:S.field};return o(Z,null,g)}}return o(null,S,g)})).catch(u=>{clearTimeout(d);let b=u&&(u.name==="AbortError"||String(u).includes("AbortError"))?{type:"timeout",message:`Timeout after ${l}ms`}:{type:"network_error",message:(u==null?void 0:u.message)||"Network error",details:u};return o(b,null,{url:i,method:e,status_code:0,headers:null})})}get(e,t,n){return this.request("GET",e,null,t,n)}post(e,t,n,s){return this.request("POST",e,t,n,s)}api_get(e,t,n,s){return this.get(this._wrap_path(e,n),t,s)}api_post(e,t,n,s,r){return this.post(this._wrap_path(e,s),t,n,r)}};function k(a){return Helper.template.sidebarNav([{content:' Devices',route:"/#!/devices",is_active:a=="devices"},{content:' Scanning',route:"/#!/devices/scanning",is_active:a=="scanning"}])}function y(a,e,t){Helper.states.btnLoadingState(e,!0);let n=e.dataset.deviceId,s=e.dataset.deviceName,r=e.dataset.deviceAlias;a.devices.reboot(n,(o,i,c)=>{Helper.states.btnLoadingState(e,!1),console.log("Reboot done",o,i,c),t==null||t.close(),i?(setTimeout(()=>Screens.reinit(),8e3),Toasts.createSuccess("Reboot successful",`Device: ${s}{{value}}",state:d=>`${d}`,connection_state:d=>`${d}`,mac:"{{value}}",alias:' {{value}}',device_id:"{{value}}",last_contact:d=>Helper.unification.timeAgo(d),create_at:d=>Helper.unification.formatDate(d),update_at:d=>Helper.unification.formatDate(d)};return l[i]?typeof l[i]=="function"?l[i](c):l[i].replaceAll("{{value}}",c):c},r="";for(let i in a){let c=(o=n[i])!=null?o:"";r+=`
+ `,console.error("deviceStatusComponent",`DeviceID ${e}`);n.innerHTML=ct(t,r.data.device.alias,r.data.device.device_response)}),n}function pe(a,e){return console.log(a),Modals.create("device-popup",{title:`Device ${a.name}`,body:t=>{var o;let n={area_id:"place-in-area-component-container",ip:"device-ip",status:"status-container",name:"display-name"},s=(i,c)=>{let l={ip:"{{value}}",state:d=>`${d}`,connection_state:d=>`${d}`,mac:"{{value}}",alias:' {{value}}',device_id:"{{value}}",last_contact:d=>Helper.unification.timeAgo(d),create_at:d=>Helper.unification.formatDate(d),update_at:d=>Helper.unification.formatDate(d)};return l[i]?typeof l[i]=="function"?l[i](c):l[i].replaceAll("{{value}}",c):c},r="";for(let i in a){let c=(o=n[i])!=null?o:"";r+=`
${t.ip}`,actions:`
+ `).show()},300)})},()=>{Helper.states.btnLoadingState(r,!1),console.log("CANCELED")}))}),[n,s,r]},onready:t=>{w(e,t.querySelector(".place-in-area-component-container"),"device",a.id,a.area_id),t.querySelector(".status-container").append(B(e,a.id,a.type)),editableString(t.querySelector(".display-name")).editableString.onChange(s=>{console.log("Device Name Changed",s.value),e.devices.update_name({device_id:a.id,name:s.value},(r,o,i)=>{if(console.log("sh_api.devices.update_name",r,o),!o)return console.error("sh_api.devices.update_name",r),Toasts.createError("Error updating","Error of name updating")})})}})}function lt(a){let e=[];for(let t of a.devices)t=Helper.unification.deviceFieldsUnification(t),e.push({deviceName:t.name,alias:t.alias,status:Helper.template.connectionStatusBadge(t.connection_state),ip:`${t.ip}`,actions:`
- `});return e}function dt(a,e,t){a.currentScreen.DOMObject.querySelector(".main-container").innerHTML=Helper.template.table("Devices list",{deviceName:"Device name",alias:"Device alias",status:"Status",ip:"IP",actions:"Actions"},e,``)}function ut(a,e){a.currentScreen.DOMObject.querySelectorAll(".reboot-btn").forEach(t=>{t.addEventListener("click",n=>{n.currentTarget.getAttribute("disabled")||y(e,n.currentTarget)})}),a.currentScreen.DOMObject.querySelectorAll(".details-btn").forEach(t=>{t.addEventListener("click",n=>{let s=v({status:""},JSON.parse(n.currentTarget.dataset.device));pe(s,e).show()})})}function me(a){return{alias:"devices",renderer:()=>Helper.template.mainTemplate(k("devices")),initer:e=>{a.devices.list((t,n,s)=>{if(console.log("sh_api.devices.list",t,n,s),s.status_code!=200)return e.error("Server API ERROR","");dt(e,lt(n.data),n.data.total),ut(e,a),e.ready()})}}}function fe(a,e){return a=Helper.unification.deviceFieldsUnification(a),Modals.create("device-setup",{title:`Setup new device ${a.ip}`,body:t=>{let n="";for(let r in a)r[0]!="_"&&(n+=`
+ `});return e}function dt(a,e,t){a.currentScreen.DOMObject.querySelector(".main-container").innerHTML=Helper.template.table("Devices list",{deviceName:"Device name",alias:"Device alias",status:"Status",ip:"IP",actions:"Actions"},e,``)}function ut(a,e){a.currentScreen.DOMObject.querySelectorAll(".reboot-btn").forEach(t=>{t.addEventListener("click",n=>{n.currentTarget.getAttribute("disabled")||y(e,n.currentTarget)})}),a.currentScreen.DOMObject.querySelectorAll(".details-btn").forEach(t=>{t.addEventListener("click",n=>{let s=b({status:""},JSON.parse(n.currentTarget.dataset.device));pe(s,e).show()})})}function me(a){return{alias:"devices",renderer:()=>Helper.template.mainTemplate(k("devices")),initer:e=>{a.devices.list((t,n,s)=>{if(console.log("sh_api.devices.list",t,n,s),s.status_code!=200)return e.error("Server API ERROR","");dt(e,lt(n.data),n.data.total),ut(e,a),e.ready()})}}}function fe(a,e){return a=Helper.unification.deviceFieldsUnification(a),Modals.create("device-setup",{title:`Setup new device ${a.ip}`,body:t=>{let n="";for(let r in a)r[0]!="_"&&(n+=`
${t.ip}`,mac:`${t.mac}`,wifiSignal:t.wifi_signal,actions:t.status=="setup"?`
+ `},actions:t=>{let n=Helper.template.createElement("button",{class:"btn btn-primary"},"Cancel");n.addEventListener("click",r=>{t.close()});let s=Helper.template.createElement("button",{class:"btn btn-success with-icon"},' Setup');return s.addEventListener("click",r=>{if(r.currentTarget.getAttribute("disabled"))return!1;let o={device_ip:a.ip},i=document.querySelector("#device-setup .setup-form");if(i.querySelectorAll("input[type='text']").forEach(d=>{d.dispatchEvent(new Event("input",{bubbles:!0}))}),i.querySelectorAll(".label.error").length)return!1;let c=i.querySelectorAll("input"),l=i.querySelector("textarea");for(let d of c)o[d.getAttribute("name")]=d.value;o[l.getAttribute("name")]=l.value,Helper.states.btnLoadingState(s,!0),e.devices.setup_new_device(o,(d,u,m)=>{var p,g;if(Helper.states.btnLoadingState(s,!1),(d==null?void 0:d.type)=="api_error"){if(console.error("ERR! sh_api.devices.setup_new_device",d.raw),(p=d.raw)!=null&&p.failed_fields)for(let v of d.raw.failed_fields)t.querySelector(`[name="${v}"]`).parentNode.classList.add("error");if((g=d.raw)!=null&&g.msg){let v=t.querySelector(".setup-form .alert-container");v.innerHTML="",v==null||v.append(Helper.template.createAlert("error",d.raw.msg))}return!1}if(!u)return!1;c.forEach(v=>v.value=""),l.value="",t.close(),Screens.reinit(),setTimeout(()=>{Toasts.createSuccess("Setup successful",`Added new device ID ${a.device_id}`).show()},300)})}),[n,s]},onready:t=>{t.querySelector(".setup-form").querySelectorAll("input").forEach(s=>{s.addEventListener("input",r=>{var o;r.currentTarget.value.length?(r.currentTarget.parentNode.classList.remove("error"),(o=r.currentTarget.parentNode.parentNode.querySelector(".input-info.error"))==null||o.remove()):(r.currentTarget.parentNode.classList.add("error"),r.currentTarget.parentNode.parentNode.querySelector(".input-info.error")||r.currentTarget.parentNode.parentNode.append(Helper.template.createElement("div",{class:"input-info error"},' Field cannot be empty')))})})}})}function pt(a){let e=[];for(let t of a.devices)t=Helper.unification.deviceFieldsUnification(t),e.push({deviceId:t.device_id,deviceName:t.name,deviceType:t.type,status:`${t.status}`,ip:`${t.ip}`,mac:`${t.mac}`,wifiSignal:t.wifi_signal,actions:t.status=="setup"?`
- `:""});return e}function mt(a,e,t){a.currentScreen.DOMObject.querySelector(".main-container").innerHTML=Helper.template.table("Found devices",{deviceId:"Device ID",deviceName:"Device name",deviceType:"Type",status:"Status",ip:"IP",mac:"Mac",wifiSignal:"Signal",actions:"Actions"},e,``)}function ft(a,e){a.currentScreen.DOMObject.querySelectorAll(".setup-btn").forEach(t=>{t.addEventListener("click",n=>{let s=JSON.parse(n.currentTarget.dataset.device);fe(s,e).show()})})}function be(a){return{alias:"devices-scanning",renderer:()=>Helper.template.mainTemplate(k("scanning")),initer:e=>{a.devices.scanning_all((t,n,s)=>{if(console.log("sh_api.devices.scanning_all",t,n),s.status_code!=200)return e.error("Server API ERROR","");mt(e,pt(n.data),n.data.devices.length),ft(e,a),e.ready()})}}}var z={list:me,scanning:be};function L(a){return Helper.template.sidebarNav([{content:' Scopes',route:"/#!/scripts/scopes",is_active:a=="scopes"},{content:' Actions',route:"/#!/scripts/actions",is_active:a=="actions"},{content:' Regular',route:"/#!/scripts/regular",is_active:a=="regular"}])}function J(a,e,t){a.scripts.run({alias:e,params:{}},(n,s,r)=>{s?(t(!0),setTimeout(()=>Screens.reinit(),1e3),setTimeout(()=>Toasts.createSuccess(`${e} running`,`${e} running success`).show(),300)):(t(!1),setTimeout(()=>Toasts.createError(`${e} failed`,`${e} running failed`).show(),300))})}function V(a){return`
+ `:""});return e}function mt(a,e,t){a.currentScreen.DOMObject.querySelector(".main-container").innerHTML=Helper.template.table("Found devices",{deviceId:"Device ID",deviceName:"Device name",deviceType:"Type",status:"Status",ip:"IP",mac:"Mac",wifiSignal:"Signal",actions:"Actions"},e,``)}function ft(a,e){a.currentScreen.DOMObject.querySelectorAll(".setup-btn").forEach(t=>{t.addEventListener("click",n=>{let s=JSON.parse(n.currentTarget.dataset.device);fe(s,e).show()})})}function ve(a){return{alias:"devices-scanning",renderer:()=>Helper.template.mainTemplate(k("scanning")),initer:e=>{a.devices.scanning_all((t,n,s)=>{if(console.log("sh_api.devices.scanning_all",t,n),s.status_code!=200)return e.error("Server API ERROR","");mt(e,pt(n.data),n.data.devices.length),ft(e,a),e.ready()})}}}var z={list:me,scanning:ve};function L(a){return Helper.template.sidebarNav([{content:' Scopes',route:"/#!/scripts/scopes",is_active:a=="scopes"},{content:' Actions',route:"/#!/scripts/actions",is_active:a=="actions"},{content:' Regular',route:"/#!/scripts/regular",is_active:a=="regular"}])}function J(a,e,t){a.scripts.run({alias:e,params:{}},(n,s,r)=>{s?(t(!0),setTimeout(()=>Screens.reinit(),1e3),setTimeout(()=>Toasts.createSuccess(`${e} running`,`${e} running success`).show(),300)):(t(!1),setTimeout(()=>Toasts.createError(`${e} failed`,`${e} running failed`).show(),300))})}function V(a){return`