forked from Z-Wave-Me/home-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
128 lines (104 loc) · 3.85 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
/*** Z-Way Home Automation Engine main executable *****************************
Version: 0.1.2
(c) ZWave.Me, 2013
-------------------------------------------------------------------------------
Author: Gregory Sitnin <[email protected]>
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");
//--- Load configuration
var config, files, templates, schemas, modules, namespaces;
try {
config = loadObject("config.json") || {
"controller": {},
"vdevInfo": {},
"locations": []
};
files = loadObject("files.json") || {};
schemas = loadObject("schemas.json") || [];
} catch (ex) {
console.log("Error loading config.json or files.json:", ex.message);
}
if (!config) {
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");
//--- 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);
controller.on('core.init', function () {
console.log('Starting ZWay Automation webserver');
});
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) {
return { status: 500, body: e.toString() };
}
};
ws.allowExternalAccess("JS.Run", controller.auth.ROLE.ADMIN);
}