UNPKG

qwik

Version:

qwik switch package with setLevel module

397 lines (269 loc) 8.33 kB
//use strict var req = require("./qwik2.js"); //global variable to use functions in this class var lev = {}; //method: setLevel //make sure the user enters a number between 0 and 100 exports.setLevel = function(id, level){ const url = "http://localhost:2020/"; var at = id[0]; if(at !== "@"){ at = '@'; var newID = at + id; var id_validate = req.validateInput(newID); if( id_validate !== true){ if(level <= 100 && level >= 0){ //var lev = {}; lev[newID]=level; console.log(lev); var h; h = http_get(url, lev, null); console.log(h); return lev; }//end if level else{ console.log("Error, please enter a number between 0 and 100 including 0 or 100"); }//end else }//end if id_validate }//end if check for @ symbol else{ id_validate = req.validateInput(id); if( id_validate !== true){ //if( level_validate !== true){ if(level <= 100 && level >= 0){ lev = {}; lev[id]=level; console.log(lev); h = http_get(url, lev, null); console.log(h); return lev; }//end if level else{ console.log("Error, please enter a number between 0 and 100 including 0 or 100"); }//end else }//end if id_validate }//end final else }; //================================================================================================================= //validate input function ///^[a-z]{0,10}$/ exports.validateInput =function(validate){ var i = /^[0-9a-fA-F]{07}$/; return i.test(validate); }; /* exports.validateLevel =function(validate){ var i = /^[0-9]{1,3}$/; return i.test(validate); }; */ //================================================================================================================= //get level function exports.get_level = function(id, cb){ var url = "http://localhost:2020/"; var data = {}; data[id]= "?"; // console.log(lev); http_get(url, data, function(d){ if(cb)cb(d); }); //console.log(h); }; //================================================================================================================== //http_get modified function http_get1(host, data, cb) { var p = ""; for(var i in data) { p += i + '=' + data[i]; } var url = host + p ;//+ '&ep=' + new Date().getTime(); if(host.indexOf('https:') !== -1) var http = require('https'); else var http = require('http'); var req = http.get(url, function(res) { res.setEncoding('utf8'); var body = ''; res.on('data', function(d) { body += d; }); res.on('end', function(chunk) { if(cb) cb(body); }); }); req.on('error', function(e) { console.log("http_get error: " + e.message); if(cb) cb(null); }); //30 sec timeout req.setTimeout(30000, function() { console.log("http_get timeout"); req.abort(); //error gets called }); return url; } //================================================================================================================= //http_get default from slade collins function http_get(host, data, cb) { var p = ""; for(var i in data) { p += i + '=' + data[i] + '&'; } var url = host + p + '&ep=' + new Date().getTime(); console.log(url); if(host.indexOf('https:') !== -1) var http = require('https'); else http = require('http'); //else var http = require('http'); var req = http.get(url, function(res) { res.setEncoding('utf8'); var body = ''; res.on('data', function(d) { body += d; }); res.on('end', function(chunk) { if(cb) cb(body); }); }); req.on('error', function(e) { console.log("http_get error: " + e.message); if(cb) cb(null); }); //30 sec timeout req.setTimeout(30000, function() { console.log("http_get timeout"); req.abort(); //error gets called }); process.on('uncaughtException', function (err) { console.log(err); }); } //========================================================================= //off timer exports.offTimer = function(id, level){ const url = "http://localhost:2020/"; var at = id[0]; if(at !== "@"){ at = '@'; var newID = at + id; var id_validate = req.validateInput(newID); if( id_validate !== true){ if(level <= 100 && level >= 0){ //var lev = {}; lev[newID]=level; console.log(lev); var h; h = http_get3(url, lev, null); console.log(h); return lev; }//end if level else{ console.log("Error, please enter a number between 0 and 100 including 0 or 100"); }//end else }//end if id_validate }//end if check for @ symbol else{ id_validate = req.validateInput(id); if( id_validate !== true){ //if( level_validate !== true){ if(level <= 100 && level >= 0){ lev = {}; lev[id]=level; console.log(lev); h = http_get3(url, lev, null); console.log(h); return lev; }//end if level else{ console.log("Error, please enter a number between 0 and 100 including 0 or 100"); }//end else }//end if id_validate }//end final else }; //============================================================================== function http_get3(host, data, cb) { var p = ""; if(typeof data == 'object') { for(var i in data) { p += i + '+' + data[i] + '&'; } var url = host + p + '&ep=' + new Date().getTime(); } else { var url = host + p; } if(host.indexOf('https:') !== -1) var http = require('https'); else var http = require('http'); var req = http.get(url, function(res) { res.setEncoding('utf8'); var body = ''; res.on('data', function(d) { body += d; }); res.on('end', function(chunk) { if(cb) cb(body); }); }); req.on('error', function(e) { console.log("http_get error: " + e.message); if(cb) cb(null); }); //30 sec timeout req.setTimeout(30000, function() { console.log("http_get timeout"); req.abort(); //error gets called }); return url; } //end off timer //============================================================================== // add device exports.addDevice = function(device, id, type ,name){ const url = "http://localhost:2020/"; var at = id[0]; if(at !== "@"){ at = '@'; var newID = device + "/" + at + id + type + "/" + name; var newIDhasAt = device + "/" + id + type + "/" + name; var id_validate = req.validateInput(newID); if( id_validate !== true){ //var lev = {}; lev[newID]; console.log(lev); var h; h = http_get3(url, lev, null); console.log(h); return lev; }//end if id_validate }//end if check for @ symbol else{ id_validate = req.validateInput(id); if( id_validate !== true){ lev = {}; lev[newIDhasAt]; console.log(lev); h = http_get3(url, lev, null); console.log(h); return lev; }//end if id_validate }//end final else };