-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhp-ilo.js
275 lines (258 loc) · 7.44 KB
/
hp-ilo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* jshint -W097 */
/* jshint strict:false */
/* global require */
/* global RRule */
/* global __dirname */
/* jslint node: true */
'use strict';
var fs = require('fs');
var utils = require('@iobroker/adapter-core');
var adapter;
var request = require('request');
var pollTimer = null;
var init = 0;
var ErrorCount = 0;
var PushoverNotificationSent = false;
var TelegramNotificationSent = false;
var EmailNotificationSent = false;
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}
function ILO_Request(url, next){
url = "https://"+adapter.config.ip+url;
request.timeout = 60000;
request({
"timeout": request.timeout,
"rejectUnauthorized": false,
"url": url,
"method": "GET",
"headers":
{
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "BASIC " + new Buffer(adapter.config.username+":"+adapter.config.password).toString('base64')
}
}, function(err, response, body)
{
if (err)
{
adapter.log.error('Request error: ' + err + ' , URL: ' + url);
}
else if (response.statusCode != 200)
{
adapter.log.debug('Response: ' + response.statusCode+": "+url);
}
else if (next)
{
next(JSON.parse(body));
}
});
}
function Read_ILO()
{
ILO_Request("/rest/v1/Chassis/1/Thermal", function(body){
if (init++ === 0) {
adapter.setObjectNotExists("info.ILO_Status", {
type: "state",
common: {
name: "ILO Status",
type: "string",
role: "state",
read: true,
write: true
},
native: {}
});
for (var f in body.Fans) {
var fan = body.Fans[f];
adapter.createState('', 'fans', fan.FanName.replace(/ /g, '_'), {
name: fan.FanName.replace(/ /g, '_'),
desc: 'Fan ' + fan.PhysicalContext + ' speed in %',
type: 'number',
def: 0,
read: true,
write: true,
role: 'state'
});
}
for (var t in body.Temperatures)
{
var temp = body.Temperatures[t];
if (temp.CurrentReading > 0)
{
adapter.createState('', 'temperatures', temp.Name.replace(/ /g, '_'), {
name: temp.PhysicalContext,
desc: 'Temperature ' + temp.PhysicalContext + ' in degrees',
type: 'number',
def: 0,
read: true,
write: true,
role: 'state'
});
adapter.setObjectNotExists("temperatures." + temp.Name.replace(/ /g, '_') + "_max", {
type: "state",
common: {
name: temp.PhysicalContext + "_max",
type: "number",
role: "state",
read: true,
write: true
}
});
}
}
}
ErrorCount = 0;
for (var f in body.Fans)
{
var fan = body.Fans[f];
adapter.setState('fans.' + fan.FanName.replace(/ /g, '_'), fan.CurrentReading), true;
}
for (var t in body.Temperatures)
{
var temp = body.Temperatures[t];
if (temp.CurrentReading > 0)
{
adapter.log.debug('Current Reading > 0');
adapter.setState('temperatures.' + temp.Name.replace(/ /g, '_'), temp.CurrentReading, true);
Send_Notification(temp);
}
}
if (ErrorCount > 0)
{
adapter.setState("info.ILO_Status", true, true);
}
else
{
adapter.setState("info.ILO_Status", false, true);
var PushoverNotificationSent = false;
var TelegramNotificationSent = false;
var EmailNotificationSent = false;
}
}, !true);
};
function Send_Notification(temp)
{
var SendToPushover = adapter.config.pushover;
var SendToTelegram = adapter.config.telegram;
var SendToEmail = adapter.config.email;
var PushoverInstance = '';
var TelegramInstance = '';
var EmailInstance = '';
var EmailRecipient ='';
if (SendToPushover)
{
var InstanceID = adapter.config.pushover_instance;
if (InstanceID)
{
PushoverInstance = 'pushover.' + adapter.config.pushover_instance;
adapter.log.debug('Send Notifications to Pushover Instance "' + PushoverInstance + '"');
}
else
{
adapter.log.error('Send Notifications to Pushover Instance in adapter config enabled but no instance defined!');
SendToPushover = false;
}
}
if (SendToTelegram)
{
var InstanceID = adapter.config.telegram_instance;
if (InstanceID)
{
TelegramInstance = 'telegram.' + adapter.config.telegram_instance;
adapter.log.debug('Send Notifications to Telegram Instance "' + TelegramInstance + '"');
}
else
{
adapter.log.error('Send Notifications to Telegram Instance in adapter config enabled but no instance defined!');
SendToTelegram = false;
}
}
if (SendToEmail)
{
var InstanceID = adapter.config.email_instance;
if (InstanceID)
{
EmailInstance = 'email.' + adapter.config.email_instance;
EmailRecipient = adapter.config.email_recipient;
adapter.log.debug('Send Notifications to E-Mail Instance "' + EmailInstance + '" to recipient "' + EmailRecipient + '"');
}
else
{
adapter.log.error('Send Notifications to E-Mail Instance in adapter config enabled but no instance defined!');
SendToEmail = false;
}
}
adapter.getState('temperatures.' + temp.Name.replace(/ /g, '_') + '_max', function (err, state)
{
if (state)
{
var max = state.val;
var aktuell = temp.CurrentReading;
adapter.log.debug('State "temperatures.' + temp.Name.replace(/ /g, '_') + '_max' + '" for notification. Actual Value: ' + aktuell + ', Max Value: ' + max);
if (aktuell > max)
{
adapter.log.debug('Limit reached for State "temperatures.' + temp.Name.replace(/ /g, '_') + '_max' + '" for notification. Actual Value: ' + aktuell + ', Max Value: ' + max);
ErrorCount++;
if (SendToPushover === true & PushoverNotificationSent === false)
{
adapter.log.debug('Starting pushover notification...');
adapter.sendTo(PushoverInstance, {
message: "Aktuelle Temperatur " + temp.Name + " = " + aktuell + " Grad Celsius.",
title: "ILO Temperatur Warnung!",
priority: 0
});
PushoverNotificationSent = true;
}
if (SendToTelegram === true & TelegramNotificationSent === false)
{
adapter.log.debug('Starting telegram notification...');
adapter.sendTo(TelegramInstance, "send", {
text: ("ILO Temperatur Warnung!! Aktuelle Temperatur " + temp.Name + " = " + aktuell + " Grad Celsius.")
});
TelegramNotificationSent = true;
}
if (SendToEmail === true & EmailNotificationSent === false)
{
adapter.log.debug('Starting email notification...');
adapter.sendTo(EmailInstance, {
to: EmailRecipient,
subject: "ILO Temperatur Warnung!!",
text: "Aktuelle Temperatur " + temp.Name + " = " + aktuell + " Grad Celsius."
});
EmailNotificationSent = true;
}
}
}
});
}
function startAdapter(options) {
options = options || {};
Object.assign(options,{
name: "hp-ilo",
stateChange: function (id, state) {
if (id && state && !state.ack)
{
id = id.substring(adapter.namespace.length + 1);
}
},
unload: function (callback) {
callback();
},
ready: function () {
main();
}
});
adapter = new utils.Adapter(options);
return adapter;
}
function main()
{
adapter.log.info('Ready. Configured HP ILO: ' + adapter.config.ip);
Read_ILO();
adapter.subscribeStates('*');
pollTimer = setInterval(Read_ILO, 120000);
}