diff --git a/server/SHServ/Controllers/DevicesRESTAPIController.php b/server/SHServ/Controllers/DevicesRESTAPIController.php index 034621f..d79aef1 100644 --- a/server/SHServ/Controllers/DevicesRESTAPIController.php +++ b/server/SHServ/Controllers/DevicesRESTAPIController.php @@ -349,4 +349,22 @@ return $this -> utils() -> response_success(); } + + public function reset_device($device_id) { + $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"); + } + + $device -> device_api() -> reset(); + + 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 3d2e6d5..13313a8 100644 --- a/server/SHServ/Routes/DevicesRESTAPI_v1.php +++ b/server/SHServ/Routes/DevicesRESTAPI_v1.php @@ -58,6 +58,12 @@ "{$this -> cn}\\DevicesRESTAPIController@resetup_device", "/api/v1/devices/resetup" ); + + $this -> router -> post( + [ "device_id" ], + "{$this -> cn}\\DevicesRESTAPIController@reset_device", + "/api/v1/devices/reset" + ); } protected function devices_restapi_get_routes() { diff --git a/webclient/dist/js/main.js b/webclient/dist/js/main.js index ddafeb3..fef4f8b 100644 --- a/webclient/dist/js/main.js +++ b/webclient/dist/js/main.js @@ -1,4 +1,4 @@ -(()=>{var Ce=Object.defineProperty,Oe=Object.defineProperties;var ke=Object.getOwnPropertyDescriptors;var te=Object.getOwnPropertySymbols;var Ie=Object.prototype.hasOwnProperty,Ne=Object.prototype.propertyIsEnumerable;var ae=(a,e,t)=>e in a?Ce(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)&&ae(a,t,e[t]);if(te)for(var t of te(e))Ne.call(e,t)&&ae(a,t,e[t]);return a},ne=(a,e)=>Oe(a,ke(e));var se=(a,e,t)=>new Promise((n,s)=>{var i=o=>{try{r(t.next(o))}catch(l){s(l)}},c=o=>{try{r(t.throw(o))}catch(l){s(l)}},r=o=>o.done?n(o.value):Promise.resolve(o.value).then(i,c);r((t=t.apply(a,e)).next())});var h,T,D;function Re(){h.dataset.navState="displayed",h.classList.remove("state-off"),h.classList.add("state-on"),T.classList.add("a-show")}function F(){h.dataset.navState="hidden",h.classList.remove("state-on"),h.classList.add("state-off"),T.classList.add("a-hide"),T.classList.remove("a-show"),setTimeout(()=>{T.classList.remove("a-hide")},300)}function ie(){console.log("HUD init"),h=document.querySelector(".hud .nav-toggle"),T=document.querySelector(".hud .navigation"),D=document.querySelector(".hud .reload-screen"),h==null||h.addEventListener("click",a=>{a.currentTarget.dataset.navState!="displayed"?Re():F()}),D==null||D.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]=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 xe(a,e,t,n){return` +(()=>{var Ce=Object.defineProperty,Oe=Object.defineProperties;var ke=Object.getOwnPropertyDescriptors;var te=Object.getOwnPropertySymbols;var Ie=Object.prototype.hasOwnProperty,Re=Object.prototype.propertyIsEnumerable;var ae=(a,e,t)=>e in a?Ce(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)&&ae(a,t,e[t]);if(te)for(var t of te(e))Re.call(e,t)&&ae(a,t,e[t]);return a},ne=(a,e)=>Oe(a,ke(e));var se=(a,e,t)=>new Promise((n,s)=>{var i=o=>{try{r(t.next(o))}catch(l){s(l)}},c=o=>{try{r(t.throw(o))}catch(l){s(l)}},r=o=>o.done?n(o.value):Promise.resolve(o.value).then(i,c);r((t=t.apply(a,e)).next())});var h,T,D;function Ne(){h.dataset.navState="displayed",h.classList.remove("state-off"),h.classList.add("state-on"),T.classList.add("a-show")}function F(){h.dataset.navState="hidden",h.classList.remove("state-on"),h.classList.add("state-off"),T.classList.add("a-hide"),T.classList.remove("a-show"),setTimeout(()=>{T.classList.remove("a-hide")},300)}function ie(){console.log("HUD init"),h=document.querySelector(".hud .nav-toggle"),T=document.querySelector(".hud .navigation"),D=document.querySelector(".hud .reload-screen"),h==null||h.addEventListener("click",a=>{a.currentTarget.dataset.navState!="displayed"?Ne():F()}),D==null||D.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]=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 xe(a,e,t,n){return`
It's empty here yet
- `}var le={template:{sidebarNav:Be,table:ze,createElement:ce,createAlert:Ve,mainTemplate:Ge,connectionStatusBadge:Ke,toogleStateBadge:Qe,circleLoaderHTML:et,emptyHereHTML:tt},unification:{deviceFieldsUnification:Je,timeAgo:Xe,formatDate:Ze},states:{btnLoadingState:We,cardScriptActionLoadingState:Ye}};var q=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 C=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)}update_name(e,t){return this.core.api_post("/api/v1/devices/update-name",e,t)}update_description(e,t){return this.core.api_post("/api/v1/devices/update-description",e,t)}update_alias(e,t){return this.core.api_post("/api/v1/devices/update-alias",e,t)}resetup(e,t){return this.core.api_post("/api/v1/devices/resetup",e,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 de(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 k=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 q(this),this.devices=new C(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}${de(t)}`:e;let n=b({path:e},t||{});return`${this.proxy_path}${de(n)}`}request(e,t,n,s,i){let c=typeof s=="function"?s:()=>{},r=at(this.base_url,t),o=new AbortController,l=Number.isFinite(i==null?void 0:i.timeout_ms)?i.timeout_ms:this.timeout_ms,d=setTimeout(()=>o.abort(),l),u=b(b({},this.default_headers),(i==null?void 0:i.headers)||{});this.token&&(u.Authorization=`Bearer ${this.token}`);let m;n!=null&&(u["Content-Type"]="application/json",m=JSON.stringify(n)),fetch(r,{method:e,headers:u,body:m,signal:o.signal}).then(p=>se(this,null,function*(){clearTimeout(d);let g={url:r,method:e,status_code:p.status,headers:p.headers},v=yield p.text(),j=nt(v),S=j.ok?j.data:v;if(!p.ok){let $={type:"http_error",message:`HTTP ${p.status}`,status_code:p.status,raw:S};if((p.status===401||p.status===403)&&this.on_unauthorized)try{this.on_unauthorized({error:$,meta:g})}catch(ee){}return c($,null,g)}if(j.ok&&S&&typeof S=="object"){let $=S.status;if($===!1||$==="error"){let ee={type:"api_error",message:S.message||"API error",status_code:p.status,raw:S,field:S.field};return c(ee,null,g)}}return c(null,S,g)})).catch(p=>{clearTimeout(d);let v=p&&(p.name==="AbortError"||String(p).includes("AbortError"))?{type:"timeout",message:`Timeout after ${l}ms`}:{type:"network_error",message:(p==null?void 0:p.message)||"Network error",details:p};return c(v,null,{url:r,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,i){return this.post(this._wrap_path(e,s),t,n,i)}};function I(a){return Helper.template.sidebarNav([{content:' Devices',route:"/#!/devices",is_active:a=="devices"},{content:' Scanning',route:"/#!/devices/scanning",is_active:a=="scanning"}])}function _(a,e,t){Helper.states.btnLoadingState(e,!0);let n=e.dataset.deviceId,s=e.dataset.deviceName,i=e.dataset.deviceAlias;a.devices.reboot(n,(c,r,o)=>{Helper.states.btnLoadingState(e,!1),console.log("Reboot done",c,r,o),t==null||t.close(),r?(setTimeout(()=>Screens.reinit(),8e3),Toasts.createSuccess("Reboot successful",`Device: ${s}