UNPKG

anniot-device

Version:
179 lines (165 loc) 6.24 kB
#!/usr/bin/env node 'use strict'; /* All rights reserved to Project Ann®, AnnIoT™ is trademark of Project Ann 2018-2019 */ const program = require('commander'); const prompts = require('prompts'); const updateNotifier = require('update-notifier'); const pkg = require('../package.json'); const boxen = require('boxen'); const chalk = require("chalk"); const figures = require("figures"); const ora = require('ora'); const dataModule = require("./module/data"); var data = new dataModule(); const notifier = updateNotifier({ pkg, updateCheckInterval: 1000 }) if (notifier.update) { console.log(chalk.blue(figures.info) + ` Update available: ` + chalk.red(pkg.version) + " " + chalk.cyan(figures.arrowRight) + " " + chalk.green(notifier.update.latest)); } /* var uid = process.env.SUDO_UID; if (uid == undefined) { console.log(boxen(chalk.red(figures.cross) + " Process not started as sudo try to add $sudo to begining of app", { padding: 1 })); return; } */ program.version(pkg.version) program.command("login") .description("Login to your device") .action(function () { var uid = parseInt(process.env.SUDO_UID); if (uid) process.setuid(uid); var dtCh = ""; try { dtCh = data.check(); } catch (err) { console.log(chalk.red(figures.cross) + chalk.cyan(" Cannot write data file. \n\n Try delete " + process.cwd() + "/iotConfig.data with sudo perm")); console.log(chalk.cyan(figures.pointer + " " + err.message)) process.exit(); } let questions = [{ type: 'text', name: 'mail', message: 'What is your mail adress ?' }, { type: 'password', name: 'id', message: 'Enter your device id ?' } ]; if (dtCh) { //Overwrite old data var edata = { type: 'toggle', name: 'value', message: 'Overwrite old data ?', initial: true, active: 'yes', inactive: 'no' } questions.push(edata); } prompts(questions).then(function (re) { console.clear(); if (re.value == false) { console.clear() console.log(chalk.red(figures.cross) + " Device login canceled") return; } if (re.mail == undefined || re.mail == "") { console.clear() console.log(chalk.red(figures.cross) + " Mail cannot be empty") return; } else if (re.id == undefined || re.id == "") { console.clear() console.log(chalk.red(figures.cross) + " Device id cannot be empty") return; } try { data.write({ mail: re.mail, id: re.id }) } catch(err) { console.log(err); console.log(chalk.red(figures.cross) + chalk.cyan(" Cannot read data file. Are you sudo ? \n\n Try delete " + process.cwd() + "/iotConfig.data")); return; } console.clear() console.log( chalk.red("\n\n---Information---\n\n") + chalk.cyan("Device information saved, you can start service by \n") + chalk.bgRed("$") + chalk.red(" anniot serviceStart") ) process.exit(); }) }); program.command("signOut") .description("Sign out in this device") .action(function () { var uid = parseInt(process.env.SUDO_UID); if (uid) process.setuid(uid); try { if (!data.check()) { console.log(chalk.red(figures.cross) + chalk.cyan(" Signout Failed. Cant find data")) return; } data.delete() console.log(chalk.green(figures.tick) + chalk.cyan(" Successfully signed out")) } catch (err) { console.log(chalk.red(figures.cross) + chalk.cyan(" Signing out failed \n\n") + chalk.cyan(figures.pointer) + " " + err.message) } }); program.command("serviceStart") .description("Start IoT service") .action(function () { process.title = "AnnIoT" var ann = require("anniot-core"); var dat = require("./module/data.js"); var data = new dat(); var figures = require('figures'); var chalk = require('chalk'); try { data.read(); } catch (e) { console.log(chalk.red(figures.cross) + chalk.cyan(" Service start failed: cant find data")) console.log("\n\n" + chalk.bgRed("$") + chalk.red(" anniot login")) return; } var iot = new ann.core({ mail: data.read().mail, id: data.read().id }) .connect(true); console.log("\n" + chalk.green(figures.tick) + " " + chalk.cyan("Service Started\n")) iot.once("connect", function () { console.log(chalk.green(figures.tick) + chalk.cyan(" Device connected")); iot.once("verify", function (data) { console.log(chalk.green(figures.tick) + chalk.cyan(" Connection verified. Device Name:",data.info.deviceName)) }) }) iot.on("error", function (err) { console.log(chalk.red(figures.cross) + chalk.cyan(" Connection Failed\n")) console.log(chalk.cyan(err.message)) }); var uid = parseInt(process.env.SUDO_UID); if (uid) process.setuid(uid); }) program.parse(process.argv); var allOptions = ["--debug"]; var allCommands = ["signOut", "serviceStart", "login"]; if (process.argv[2] == undefined) { program.help(); } else if ((process.argv[2] !== undefined && process.argv[3] == undefined) && !allCommands.includes(process.argv[2])) { program.help(); } else if (process.argv[3] !== undefined && (!allOptions.includes(process.argv[2]) || !allCommands.includes(process.argv[3]))) { program.help(); }