forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openhabimitator.js
63 lines (61 loc) · 3.07 KB
/
openhabimitator.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
var io = require('socket.io-client');
var mongoose = require('mongoose')
, logger = require('../logger.js');
var Openhab = require('../models/openhab');
var testSockets = {};
mongoose.connect('mongodb://test:test@localhost/openhab', function(error) {
if (error) {
logger.error("mongo connection error: " + error);
} else {
logger.info("openHAB-cloud connected to mongodb");
Openhab.find({}).limit(200).exec(function(error, testOpenhabs) {
if (error) {
logger.error("error getting openhabs: " + error);
} else {
logger.info("connecting test openhab sockets");
for (testOpenhabId in testOpenhabs) {
var testOpenhab = testOpenhabs[testOpenhabId];
console.log(testOpenhab);
var newSocket = io.connect('http://localhost:3000', {query: "uuid=" + testOpenhab.uuid +
"&secret=" + testOpenhab.secret, forceNew: true});
newSocket.on('binaryTest', function(data) {
console.log(data);
});
testSockets[testOpenhab.uuid] = newSocket;
}
logger.info("starting connect/disconnect cycle");
setInterval(function() {
for (testOpenhabId in testOpenhabs) {
var testOpenhab = testOpenhabs[testOpenhabId];
if (testSockets[testOpenhab.uuid] != null) {
testSockets[testOpenhab.uuid].emit('itemupdate', { itemName: 'TestItem1', itemStatus: '100' });
// Randomly disconnect
if (Math.random() <= 0.2) {
testSockets[testOpenhab.uuid].disconnect();
delete testSockets[testOpenhab.uuid];
}
} else {
// Randomly reconnect
if (Math.random() > 0.2) {
var newSocket = io.connect('http://localhost:3000', {
query: "uuid=" + testOpenhab.uuid +
"&secret=" + testOpenhab.secret, forceNew: true
});
newSocket.on('binaryTest', function(data) {
console.log(data);
});
testSockets[testOpenhab.uuid] = newSocket;
}
}
}
Openhab.count({status:"online"}, function(error, onlineCount) {
Openhab.count({status:"offline"}, function(error, offlineCount) {
logger.info("Tick: sockets count = " + Object.keys(testSockets).length +
", online = " + onlineCount + ", offline = " + offlineCount);
});
});
}, 10000);
}
});
}
});