| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,184 @@ |
| 1 |
+/* This file is part of Jeedom. |
|
| 2 |
+* |
|
| 3 |
+* Jeedom is free software: you can redistribute it and/or modify |
|
| 4 |
+* it under the terms of the GNU General Public License as published by |
|
| 5 |
+* the Free Software Foundation, either version 3 of the License, or |
|
| 6 |
+* (at your option) any later version. |
|
| 7 |
+* |
|
| 8 |
+* Jeedom is distributed in the hope that it will be useful, |
|
| 9 |
+* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 10 |
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 11 |
+* GNU General Public License for more details. |
|
| 12 |
+* |
|
| 13 |
+* You should have received a copy of the GNU General Public License |
|
| 14 |
+* along with Jeedom. If not, see <http://www.gnu.org/licenses/>. |
|
| 15 |
+*/ |
|
| 16 |
+ |
|
| 17 |
+const request = require('request');
|
|
| 18 |
+var express = require('express');
|
|
| 19 |
+ |
|
| 20 |
+var Jeedom = {}
|
|
| 21 |
+Jeedom.log = {}
|
|
| 22 |
+Jeedom.com = {}
|
|
| 23 |
+Jeedom.http = {}
|
|
| 24 |
+ |
|
| 25 |
+/***************************ARGS*******************************/ |
|
| 26 |
+ |
|
| 27 |
+Jeedom.getArgs = function() {
|
|
| 28 |
+ var result = {}
|
|
| 29 |
+ var args = process.argv.slice(2,process.argv.length); |
|
| 30 |
+ for (var i = 0, len = args.length; i < len; i++) {
|
|
| 31 |
+ if (args[i].slice(0,2) === '--') {
|
|
| 32 |
+ result[args[i].slice(2,args[i].length)] = args[i + 1] |
|
| 33 |
+ } |
|
| 34 |
+ } |
|
| 35 |
+ return result |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+/***************************LOGS*******************************/ |
|
| 39 |
+ |
|
| 40 |
+Jeedom.log.setLevel = function(_level){
|
|
| 41 |
+ var convert = {debug : 0,info : 10,notice : 20,warning : 30,error : 40,critical : 50,none : 60}
|
|
| 42 |
+ Jeedom.log.level = convert[_level] |
|
| 43 |
+} |
|
| 44 |
+ |
|
| 45 |
+Jeedom.log.debug = function(_log){
|
|
| 46 |
+ if(Jeedom.log.level > 0){
|
|
| 47 |
+ return; |
|
| 48 |
+ } |
|
| 49 |
+ console.log('['+(new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''))+'][DEBUG] : '+_log)
|
|
| 50 |
+} |
|
| 51 |
+ |
|
| 52 |
+Jeedom.log.info = function(_log){
|
|
| 53 |
+ if(Jeedom.log.level > 10){
|
|
| 54 |
+ return; |
|
| 55 |
+ } |
|
| 56 |
+ console.log('['+(new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''))+'][INFO] : '+_log)
|
|
| 57 |
+} |
|
| 58 |
+ |
|
| 59 |
+Jeedom.log.error = function(_log){
|
|
| 60 |
+ if(Jeedom.log.level > 40){
|
|
| 61 |
+ return; |
|
| 62 |
+ } |
|
| 63 |
+ console.log('['+(new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''))+'][ERROR] : '+_log)
|
|
| 64 |
+} |
|
| 65 |
+ |
|
| 66 |
+/***************************PID*******************************/ |
|
| 67 |
+ |
|
| 68 |
+Jeedom.write_pid = function(_file){
|
|
| 69 |
+ var fs = require('fs');
|
|
| 70 |
+ fs.writeFile(_file, process.pid.toString(), function(err) {
|
|
| 71 |
+ if(err) {
|
|
| 72 |
+ Jeedom.log.error("Can't write pid file : "+err);
|
|
| 73 |
+ process.exit() |
|
| 74 |
+ } |
|
| 75 |
+ }); |
|
| 76 |
+} |
|
| 77 |
+ |
|
| 78 |
+/***************************COM*******************************/ |
|
| 79 |
+ |
|
| 80 |
+Jeedom.isObject = function(item) {
|
|
| 81 |
+ return (item && typeof item === 'object' && !Array.isArray(item)); |
|
| 82 |
+} |
|
| 83 |
+ |
|
| 84 |
+Jeedom.mergeDeep = function(target, ...sources) {
|
|
| 85 |
+ if (!sources.length) return target; |
|
| 86 |
+ const source = sources.shift(); |
|
| 87 |
+ if (Jeedom.isObject(target) && Jeedom.isObject(source)) {
|
|
| 88 |
+ for (const key in source) {
|
|
| 89 |
+ if (Jeedom.isObject(source[key])) {
|
|
| 90 |
+ if (!target[key]) Object.assign(target, { [key]: {} });
|
|
| 91 |
+ Jeedom.mergeDeep(target[key], source[key]); |
|
| 92 |
+ } else {
|
|
| 93 |
+ Object.assign(target, { [key]: source[key] });
|
|
| 94 |
+ } |
|
| 95 |
+ } |
|
| 96 |
+ } |
|
| 97 |
+ return Jeedom.mergeDeep(target, ...sources); |
|
| 98 |
+} |
|
| 99 |
+ |
|
| 100 |
+Jeedom.com.config = function(_apikey,_callback,_cycle){
|
|
| 101 |
+ Jeedom.com.apikey = _apikey; |
|
| 102 |
+ Jeedom.com.callback = _callback; |
|
| 103 |
+ Jeedom.com.cycle = _cycle; |
|
| 104 |
+ Jeedom.com.changes = {};
|
|
| 105 |
+ if(Jeedom.com.cycle > 0){
|
|
| 106 |
+ setInterval(function() {
|
|
| 107 |
+ if(Object.keys(Jeedom.com.changes).length > 0){
|
|
| 108 |
+ Jeedom.com.send_change_immediate(Jeedom.com.changes); |
|
| 109 |
+ Jeedom.com.changes = {};
|
|
| 110 |
+ } |
|
| 111 |
+ }, Jeedom.com.cycle * 1000); |
|
| 112 |
+ } |
|
| 113 |
+} |
|
| 114 |
+ |
|
| 115 |
+Jeedom.com.add_changes = function(_key,_value){
|
|
| 116 |
+ if (_key.indexOf('::') != -1){
|
|
| 117 |
+ tmp_changes = {}
|
|
| 118 |
+ var changes = _value |
|
| 119 |
+ var keys = _key.split('::').reverse();
|
|
| 120 |
+ for (var k in keys){
|
|
| 121 |
+ if (typeof tmp_changes[keys[k]] == 'undefined'){
|
|
| 122 |
+ tmp_changes[keys[k]] = {}
|
|
| 123 |
+ } |
|
| 124 |
+ tmp_changes[keys[k]] = changes |
|
| 125 |
+ changes = tmp_changes |
|
| 126 |
+ tmp_changes = {}
|
|
| 127 |
+ } |
|
| 128 |
+ if (Jeedom.com.cycle <= 0){
|
|
| 129 |
+ Jeedom.com.send_change_immediate(changes) |
|
| 130 |
+ }else{
|
|
| 131 |
+ Jeedom.com.changes = Jeedom.mergeDeep(Jeedom.com.changes,changes) |
|
| 132 |
+ } |
|
| 133 |
+ } else{
|
|
| 134 |
+ if (Jeedom.com.cycle <= 0){
|
|
| 135 |
+ Jeedom.com.send_change_immediate({_key:_value})
|
|
| 136 |
+ }else{
|
|
| 137 |
+ Jeedom.com.changes[_key] = _value |
|
| 138 |
+ } |
|
| 139 |
+ } |
|
| 140 |
+} |
|
| 141 |
+ |
|
| 142 |
+Jeedom.com.send_change_immediate = function(_changes){
|
|
| 143 |
+ Jeedom.log.debug('Send data to jeedom : '+JSON.stringify(_changes));
|
|
| 144 |
+ request.post({url:Jeedom.com.callback+'?apikey='+Jeedom.com.apikey, json: _changes}, function(error, response, body){
|
|
| 145 |
+ if(response.statusCode != 200){
|
|
| 146 |
+ Jeedom.log.error('Error on send to jeedom : '+JSON.stringify(error));
|
|
| 147 |
+ } |
|
| 148 |
+ }) |
|
| 149 |
+} |
|
| 150 |
+ |
|
| 151 |
+Jeedom.com.test = function(_changes){
|
|
| 152 |
+ request.post({url:Jeedom.com.callback+'?apikey='+Jeedom.com.apikey, json: {}}, function(error, response, body){
|
|
| 153 |
+ if(response.statusCode != 200){
|
|
| 154 |
+ Jeedom.log.error('Callback error.Please check your network configuration page : '+JSON.stringify(error));
|
|
| 155 |
+ process.exit(); |
|
| 156 |
+ } |
|
| 157 |
+ }) |
|
| 158 |
+} |
|
| 159 |
+ |
|
| 160 |
+/***************************HTTP SERVER*******************************/ |
|
| 161 |
+ |
|
| 162 |
+Jeedom.http.config = function(_port,_apikey){
|
|
| 163 |
+ Jeedom.http.apikey = _apikey; |
|
| 164 |
+ Jeedom.http.app = express(); |
|
| 165 |
+ Jeedom.http.app.get('/', function(req, res) {
|
|
| 166 |
+ res.setHeader('Content-Type', 'text/plain');
|
|
| 167 |
+ res.status(404).send('Not found');
|
|
| 168 |
+ }); |
|
| 169 |
+ Jeedom.http.app.listen(_port,'127.0.0.1', function() {
|
|
| 170 |
+ Jeedom.log.debug('HTTP listen on 127.0.0.1 port : '+_port+' started');
|
|
| 171 |
+ }); |
|
| 172 |
+} |
|
| 173 |
+ |
|
| 174 |
+Jeedom.http.checkApikey = function(_req){
|
|
| 175 |
+ return (_req.query.apikey == Jeedom.http.apikey) |
|
| 176 |
+} |
|
| 177 |
+ |
|
| 178 |
+/***************************EXPORTS*******************************/ |
|
| 179 |
+ |
|
| 180 |
+exports.getArgs = Jeedom.getArgs; |
|
| 181 |
+exports.log = Jeedom.log; |
|
| 182 |
+exports.write_pid = Jeedom.write_pid; |
|
| 183 |
+exports.com = Jeedom.com; |
|
| 184 |
+exports.http = Jeedom.http; |