-
Notifications
You must be signed in to change notification settings - Fork 39
/
jarvis.min.js
1 lines (1 loc) · 3.99 KB
/
jarvis.min.js
1
"use strict";const utils=require("@iobroker/adapter-core");const JarvisConverter=require("./lib/jarvis.converter.js");const JarvisServer=require("./lib/jarvis.server.js");const{random}=require("./lib/helpers");function startAdapter(e){const s=new utils.Adapter({...e||{},systemConfig:true,name:"jarvis"});let n=null;s.on("ready",function(){if(s.config.init!==true){s.getForeignObject("system.adapter.web.0",(e,n)=>{s.log.debug("Web Configuration: "+(!n?"No Web-Adapter installed!":JSON.stringify(n.native)));s.getForeignObject("system.adapter."+s.namespace,(e,t)=>{if(e||!t||!t.native){s.setState("info.connection",false,true);return s.terminate("Error system.adapter."+s.namespace+" not found!",11)}t.native={scheduledHour:random(0,23),scheduledMinute:random(0,59),authentication:n&&n.native&&n.native.auth===true?"group":"none",authenticationGroup:["system.group.administrator","system.group.user"],authenticationConfiguration:n&&n.native&&n.native.auth===true?"group":"none",authenticationConfigurationGroup:["system.group.administrator"],certPublic:n&&n.native&&n.native.certPublic!==undefined?n.native.certPublic:"",certPrivate:n&&n.native&&n.native.certPrivate!==undefined?n.native.certPrivate:"",certChained:n&&n.native&&n.native.certChained!==undefined?n.native.certChained:"",init:true,socketHost:"",socketPort:8400+s.instance,socketSecure:n&&n.native&&n.native.secure!==undefined?n.native.secure:false,versions:t.native.versions,webProtocol:n&&n.native&&n.native.secure===true?"https":"http"};s.log.debug("Write default config to jarvis: "+JSON.stringify(t.native));s.setForeignObject(t._id,t)})})}else if(s.config.socketSecure&&(!s.config.certPublic||!s.config.certPrivate)){s.log.error("Secure connection is enabled, but no certificates have been selected! Thus, fallback to insecure connection!");s.getForeignObject("system.adapter."+s.namespace,(e,t)=>{if(e||!t||!t.native){s.setState("info.connection",false,true);return s.terminate("Error system.adapter."+s.namespace+" not found!",11)}t.native={...t.native,socketSecure:false};s.log.debug("Reverted configuration due to incorrect secure connection settings: "+JSON.stringify(t.native));s.setForeignObject(t._id,t)})}else{const e=[];const t=new JarvisConverter(s);e.push(t.run());if(s.config.socketSecure&&s.config.certPublic&&s.config.certPrivate){e.push(new Promise(i=>{s.getCertificates(s.config.certPublic,s.config.certPrivate,s.config.certChained,(e,t,n)=>{i(t)})}))}else{e.push(Promise.resolve(null))}e.push(new Promise(n=>{s.getForeignObject("system.config",(e,t)=>n(t&&t.native&&t.native.secret||null))}));Promise.all(e).then(e=>{n=new JarvisServer(s,{...s.config,port:s.config.socketPort,certificates:e[1],encryptionKey:e[2]});n.init().loadBackups().listen().startServer();if(false){n.proxy()}n.on("CLIENT_LIST",e=>{s.setState("clients.connected",JSON.stringify(e,null,3),true)});s.setState("info.connection",true,true)})}});s.on("message",function(a){s.log.debug("Got message: "+JSON.stringify(a));if(n&&a.command==="get-users"){Promise.allSettled([n.getUsers(),n.getGroups()]).then(e=>{const t=e[0].value;const n=e[1].value;s.sendTo(a.from,a.command,{users:t,groups:n},a.callback)})}else if(a.command==="get-hashed-token"&&a.message){}else if(a.command==="get-pro-status"){s.log.debug("Get pro data...");s.getForeignObject("system.meta.uuid",(e,r)=>{s.getState("info.pro",(e,t)=>{const n=t.val&&JSON.parse(t.val);const i={licence:t.val,licenceSignature:n&&n.signature,adapterInstance:s.instance,ioBrokerId:r.native.uuid};s.sendTo(a.from,a.command,i,a.callback)})})}else if(n&&a.command==="get-backups"&&a.message&&a.message.id){const e="jarvis."+s.instance+"."+a.message.id;s.sendTo(a.from,a.command,n.getBackupList(e),a.callback)}else if(n&&a.command==="restore"&&a.message&&a.message.id&&a.message.date){const e="jarvis."+s.instance+"."+a.message.id;n.restore(e,a.message.date)}});s.on("unload",function(t){try{s.log.info("Adapter stopped und unloaded.");s.setState("info.connection",false,true);n.close();t()}catch(e){t()}});return s}if(module&&module.parent){module.exports=startAdapter}else{startAdapter()}