-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiobroker.js
167 lines (142 loc) · 5.52 KB
/
iobroker.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
var daemon = require("daemonize2").setup({
main: "control.js",
name: "iobroker.ctrl",
pidfile: "iobroker.pid"
});
var ObjectsCouch;
var os;
var firstIp;
switch (process.argv[2]) {
case "start":
daemon.start();
break;
case "stop":
daemon.stop();
break;
case "add":
ObjectsCouch = require('./modules/couch.js');
os = require('os');
var ifaces = os.networkInterfaces();
var ipArr = [];
for (var dev in ifaces) {
ifaces[dev].forEach(function (details) {
if (dev !== 'lo') ipArr.push(details.address);
});
}
firstIp = ipArr[0];
var objects = new ObjectsCouch({
logger: {
debug: function (msg) { },
info: function (msg) { },
warn: function (msg) { console.log(msg) },
error: function (msg) { console.log(msg) }
},
connected: function () {
console.log('couchdb connected');
createInstance(process.argv[3]);
}
});
break;
default:
console.log("Usage: ");
console.log(" node iobroker.js start");
console.log(" node iobroker.js stop");
console.log(" node iobroker.js add <adapter-name>");
}
function installAdapter(adapter, callback) {
var fs = require('fs');
if (!fs.existsSync(__dirname + '/adapter/' + adapter + '/adapter.json')) {
console.log('error: adapter ' + adapter + ' not found');
process.exit(1);
}
try {
adapter = JSON.parse(fs.readFileSync(__dirname + '/adapter/' + adapter + '/adapter.json').toString());
} catch (e) {
console.log('error: reading adapter.json ' + e);
process.exit(1);
}
function install() {
var objs = [];
if (adapter.objects && adapter.objects.length > 0) objs = adapter.objects;
adapter.common.enabled = false;
objs.push({
_id: 'system.adapter.' + adapter.common.name,
type: 'adapter',
common: adapter.common,
native: adapter.native
});
function setObject(callback) {
if (objs.length === 0) {
callback();
} else {
var obj = objs.pop();
objects.setObject(obj._id, obj, function (err, res) {
if (err) {
console.log('error setObject ' + obj._id + ' ' + err);
process.exit(1);
} else {
console.log('object ' + obj._id + ' created');
setObject(callback);
}
});
}
}
setObject(callback);
}
install();
}
function createInstance(adapter, callback) {
objects.getObject('system.adapter.' + adapter, function (err, doc) {
if (err || !doc) {
installAdapter(adapter, function () {
createInstance(adapter, callback);
});
return;
}
objects.getObjectView('system', 'instanceStats', { startkey: 'system.adapter.' + adapter + '.', endkey: 'system.adapter.' + adapter + '.\u9999' }, function (err, res) {
if (err || !res) {
console.log('error: view instanceStats ' + err);
process.exit(1);
return;
}
var instance = (res.rows && res.rows[0] && res.rows[0].value ? res.rows[0].value.max + 1 : 0);
objects.getObject('system.adapter.' + adapter, function (err, res) {
var obj = res;
obj._id = 'system.adapter.' + adapter + '.' + instance;
obj.type = 'instance';
obj.parent = 'system.adapter.' + adapter;
delete obj._rev;
obj.common.enabled = false;
obj.common.host = firstIp;
objects.setObject('system.adapter.' + adapter + '.' + instance, obj, function () {
console.log('object ' + 'system.adapter.' + adapter + '.' + instance + ' created');
objects.setObject('system.adapter.' + adapter + '.' + instance + '.alive', {
type: 'state',
name: adapter + '.' + instance + '.alive',
parent: 'system.adapter.' + adapter + '.' + instance,
common: {
type: 'bool',
role: 'indicator.state'
},
native: {}
}, function () {
console.log('object ' + 'system.adapter.' + adapter + '.' + instance + '.alive created');
objects.setObject('system.adapter.' + adapter + '.' + instance + '.connected', {
type: 'state',
name: adapter + '.' + instance + '.connected',
parent: 'system.adapter.' + adapter + '.' + instance,
common: {
type: 'bool',
role: 'indicator.state'
},
native: {}
}, function () {
console.log('object ' + 'system.adapter.' + adapter + '.' + instance + '.connected created');
process.exit(0);
});
});
});
});
});
});
}