forked from Z-Wave-Me/home-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (141 loc) · 4.91 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*** Z-Way Home Automation Engine main executable *****************************
Version: 0.1.2
(c) ZWave.Me, 2013
-------------------------------------------------------------------------------
Author: Gregory Sitnin <sitnin@z-wave.me>
Description:
This is a main executable script which is loaded and executed solely
by the z-way-server daemon. The very magic starts here.
******************************************************************************/
//--- Define global variables and helpers
var window = global = this;
// load utilities
executeFile("Utils.js");
// do transition script to adopt old versions to new
executeFile("updateBackendConfig.js");
// overload saveObject to allow backup/restore of all JSON files in storage
__saveObject = saveObject;
__storageContent = loadObject("__storageContent") || [];
saveObject = function(name, object) {
if (__storageContent.indexOf(name) === -1 && !!name) {
__storageContent.push(name);
__saveObject("__storageContent", __storageContent);
}
__saveObject(name, object);
};
//--- Load configuration
var config, files, templates, schemas, modules, namespaces;
try {
config = loadObject("config.json");
files = loadObject("files.json") || {};
schemas = loadObject("schemas.json") || [];
} catch (ex) {
console.log("Error loading config.json or files.json:", ex.message);
}
if (!config && config === null) {
console.log("Can't read config.json or it's broken.");
console.log("ZAutomation engine not started.");
} else {
config.libPath = "lib";
config.classesPath = "classes";
config.resourcesPath = "res";
// add modules_categories
config.modules_categories = fs.loadJSON("modulesCategories.json");
//--- Load 3d-party dependencies
executeFile(config.libPath + "/eventemitter2.js");
executeFile(config.libPath + "/underscore.js");
executeFile(config.libPath + "/papaparse.min.js");
executeFile(config.libPath + "/zlib_and_gzip.min.js"); // TODO Test!
executeFile(config.libPath + "/BAOS_API_2011_01_29_001.js");
executeFile(config.libPath + "/IntelHex2bin.js");
executeFile(config.libPath + "/base64.js");
executeFile(config.libPath + "/qrcode.js");
executeFile(config.libPath + "/LimitedArray.js");
//--- Load Automation subsystem classes
executeFile(config.classesPath + "/VirtualDevice.js");
executeFile(config.classesPath + "/DevicesCollection.js");
executeFile(config.classesPath + "/AutomationController.js");
executeFile(config.classesPath + "/AuthController.js");
executeFile(config.classesPath + "/AutomationModule.js");
executeFile("Webserver.js");
executeFile("WebserverRequestRouter.js");
executeFile("ZAutomationAPIProvider.js");
executeFile("StorageProvider.js");
//--- Instantiate Automation Controller
var api = null,
storage = null,
controller = new AutomationController(config),
now = new Date();
controller.on('core.init', function () {
console.log('Starting ZWay Automation webserver');
// first start up
config.controller.first_start_up = !config.controller.first_start_up? now : config.controller.first_start_up;
// count server restarts
config.controller.count_of_reconnects = config.controller.count_of_reconnects? parseInt(config.controller.count_of_reconnects, 10) + 1 : 1;
});
controller.on('core.start', function () {
console.log('ZWay Automation started');
});
controller.on('core.stop', function () {
console.log('ZWay Automation stopped');
});
controller.on('core.error', function (err) {
console.log("--- ERROR:", err.message);
controller.addNotification("error", err.message, "core", "core controller");
});
//--- main
controller.init();
controller.start();
//--- Init JS handler for Admin
JS = function() {
return { status: 400, body: "Bad JS request" };
};
ws.allowExternalAccess("JS", controller.auth.ROLE.ADMIN);
JS.Run = function(url) {
// skip trailing slash
url = url.substring(1);
try {
var r = eval(url);
if (typeof r === "function") {
// special case for functions, otherwise they show up as JSON 'null'
return {
status: 204,
headers: {
"Content-Type": "application/json",
"Connection": "keep-alive"
}
}
}
return {
status: 200,
headers: {
"Content-Type": "application/json",
"Connection": "keep-alive"
},
body: JSON.stringify(r)
};
} catch (e) {
console.log("Error handling request " + url + ": " + e.toString());
return { status: 500, body: e.toString() };
}
};
ws.allowExternalAccess("JS.Run", controller.auth.ROLE.ADMIN);
// overwrite console.debug function
console.debug = function(){
var arr = [];
// use JS API /JS/Run/controller.debug=true / false to activate debug output
if (controller.debug){
for (var key in arguments) {
var arg = '';
//format objects automatically
if (typeof arguments[key] === 'object' && !!arguments[key]) {
arg = JSON.stringify(arguments[key], null, 4);
} else {
arg = arguments[key];
}
arr.push(arg);
}
debugPrint(arr);
}
};
}