-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodefhem-mqtt.js
116 lines (87 loc) · 3.2 KB
/
nodefhem-mqtt.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
var net = require('net');
var http = require("http");
var mqtt = require('mqtt');
// local setting - YOU SHOULD EDIT THIS PART
var fhem_server = "127.0.0.1"
var fhem_user = "myuser"
var fhem_password = "mypassword"
var fhem_telnet_port = "1234"
var fhem_http_port = "4321"
var mqtt_broker = "127.0.0.1"
var mqtt_port = 1833
var mqtt_topic = "fhem"
// local settings end - YOU SHOULD STOP EDITING HERE - except you know what you do ;-)
var mqtt_client = mqtt.connect('mqtt://'+mqtt_broker)
function start_client(){
client = new net.Socket();
connected = true
client.setEncoding('utf8');
// Connect via telnet to FHEM Server and start inform to get notified by a device-changes
client.connect(fhem_telnet_port, fhem_server, function() {
client.write('inform on\n');
});
client.on('data', function(data) {
// Just get the first part (devicename) from the inform
data_split = data.split(" ");
get_fhem_data(data_split[1])
})
client.on('error', function(err) {
console.log("Error: Telnet connection error")
})
client.on('end', function(){
console.log("Error: Telnet connection ended")
})
client.on('close', function(){
console.log("Error: Telnet Connection closed");
client.destroy();
restart_client()
})
}
function restart_client(){
setTimeout(start_client, 5000)
}
function get_fhem_data(devicename){
var str = '';
var fhem_device_json = {
hostname: fhem_server,
port: fhem_http_port,
auth: fhem_user+":"+fhem_password,
path: '/fhem?cmd=jsonlist+'+devicename+'&XHR=1',
method: 'GET'
};
var callback = function(response) {
response.on('data', function (chunk) {
str += chunk
});
response.on('error', function(){
console.log("An error occoured - FHEM Query devices - Client error - "+__filename)
})
response.on('end', function() {
fhem_data = JSON.parse(str)
fhem_state = fhem_data.ResultSet.Results.STATE;
fhem_serialNr = fhem_data.ResultSet.Results.DEF;
fhem_alias = fhem_data.ResultSet.Results.ATTRIBUTES.alias;
fhem_type = fhem_data.ResultSet.Results.TYPE;
fhem_room = fhem_data.ResultSet.Results.ATTRIBUTES.room;
fhem_model = fhem_data.ResultSet.Results.ATTRIBUTES.model;
// Change values to better view them in JSON
if (fhem_data.ResultSet.Results.ATTRIBUTES.webCmd != undefined) {
webcmds_raw = fhem_data.ResultSet.Results.ATTRIBUTES.webCmd;
webcmds = webcmds_raw.split(":")
}
else{
webcmds = "NA"
}
if (fhem_state == "???"){
fhem_state="NO STATUS";
}
if (fhem_alias == undefined){
fhem_alias="No Alias";
}
var data = { device: devicename, state: fhem_state, serial: fhem_serialNr, type: fhem_type, alias: fhem_alias, room: fhem_room, model: fhem_model, commands: webcmds};
mqtt_client.publish(mqtt_topic+"/"+fhem_type, JSON.stringify(data))
});
}
var req = http.request(fhem_device_json, callback).end();
}
start_client()