Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it possible to get config by passing environment variables #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"author": "Sushil Singh",
"description": "Api Gateway and Controller for Mitsubishi Airconditioners",
"dependencies": {
"express": "^4.15.4",
"source-map-support": "^0.4.16",
"base-64": "^0.1",
"request": "",
"dotenv": "^10.0.0",
"express": "^4.15.4",
"neodoc": "",
"sjcl": "^1.0"
"request": "",
"sjcl": "^1.0",
"source-map-support": "^0.4.16"
},
"devDependencies": {
"@types/node": "^8.0.19",
Expand Down
76 changes: 44 additions & 32 deletions src/kumoConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env node
require('dotenv').config()
require('source-map-support').install();
const request = require('request');
const readline = require('readline');
const readline = require('readline');
const Writable = require('stream').Writable;
const fs = require('fs');
import { KumoCfg, KumoCfgElement } from './kumoCfgInterface';
const S = 0;
const W = '44c73283b498d432ff25f5c8e06a016aef931e68f0a00ea710e36e6338fb22db';

var mutableStdout = new Writable({
write: function(chunk: any, encoding:any, callback:()=>void) {
write: function (chunk: any, encoding: any, callback: () => void) {
if (!this.muted)
process.stdout.write(chunk, encoding);
callback();
Expand All @@ -23,14 +24,14 @@ var rl = readline.createInterface({
terminal: true
});

function processcfg(acc:any, j:any): KumoCfg {
function processcfg(acc: any, j: any): KumoCfg {
var mycfg: KumoCfg = {}
mycfg[acc] = parsechildren(j);
return mycfg;
}

function parsechildren(j:any) {
let mycl: {[hash:string]:KumoCfgElement} = {}
function parsechildren(j: any) {
let mycl: { [hash: string]: KumoCfgElement } = {}
for (let children in j) {
let child = j[children]
for (let zone in child['zoneTable']) {
Expand All @@ -42,7 +43,7 @@ function parsechildren(j:any) {
return mycl;
}

function parsezone(z:any): KumoCfgElement {
function parsezone(z: any): KumoCfgElement {
let myc: KumoCfgElement = {
serial: z.serial,
label: z.label,
Expand All @@ -58,25 +59,25 @@ function parsezone(z:any): KumoCfgElement {

function post(post_data: string) {
request.post({
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'accept-Language': 'en-US,en',
'Content-Length': post_data.length,
'Content-Type': 'application/json'
},
url: 'https://geo-c.kumocloud.com/login',
body: post_data
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'accept-Language': 'en-US,en',
'Content-Length': post_data.length,
'Content-Type': 'application/json'
},
url: 'https://geo-c.kumocloud.com/login',
body: post_data
},
function (error: any, resp: any, body: any) {
if (error != null) {
console.error("Unable to get config");
console.error(error)
}
try {
console.log(`Downloaded the config file from cloud...\n${body}`);
var dt = JSON.parse(body);
var cfg = processcfg(dt[0]['username'], dt[2]['children'])
console.log("Downloaded the config file from cloud...")
var cfg = processcfg(dt[0]['username'], dt[2]['children']);
var out = fs.createWriteStream('kumo.cfg');
out.write('module.exports = \n')
out.write(JSON.stringify(cfg))
Expand All @@ -91,22 +92,33 @@ function post(post_data: string) {
)
}

rl.write('Please enter Kumo cloud credentials\n')
rl.question("Enter username:", function (user: string) {
console.log("Enter password:");
mutableStdout.muted = true;
rl.question("Enter password:", function (pass: string) {
let msg = {
username: user,
password: pass,
appVersion: "2.2.0"
};
var post_data = JSON.stringify(msg);
post(post_data)
rl.close()
})
})


function submit(user: string, pass: string) {
let msg = {
username: user,
password: pass,
appVersion: "2.2.0"
};
var post_data = JSON.stringify(msg);
post(post_data);
}

let userEnv = process.env.KUMO_USER;
let pwEnv = process.env.KUMO_PASS;

if (userEnv && pwEnv) {
submit(userEnv, pwEnv);
} else {
rl.write('Please enter Kumo cloud credentials\n')
rl.question("Enter username:", function (user: string) {
console.log("Enter password:");
mutableStdout.muted = true;
rl.question("Enter password:", function (pass: string) {
submit(user, pass);
rl.close()
})
})
}