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