-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
186 lines (169 loc) · 6.75 KB
/
index.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
var util = require('util'),
http = require('http'),
https = require('https'),
_ = require('lodash'),
Promise = require('bluebird'),
lastRequest = Promise.resolve(),
debug = process.env.hasOwnProperty('FRONIUS_DEBUG') ? consoleDebug : function () {},
DEFAULT_DEVICE_ID = 1;
//
// Private Helper Functions
//
function consoleDebug() {
console.log.apply(this, arguments)
}
function settlePromise(aPromise) {
return aPromise.reflect();
}
function getRequest(options, path) {
var id = options.deviceId || DEFAULT_DEVICE_ID;
var urlPath = path;
if (!urlPath) {
if (options.version === 0) {
urlPath = '/solar_api/GetInverterRealtimeData.cgi?Scope=Device&DeviceIndex=';
} else {
urlPath = '/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=';
}
urlPath += id + '&DataCollection=CommonInverterData'
}
var requestOptions = _.assign({
deviceId: DEFAULT_DEVICE_ID,
timeout: 20000,
rejectUnauthorized: false,
protocol: 'http:',
path: urlPath,
headers: {},
method: 'GET'
}, options);
var timeoutOccurred = false;
requestOptions.headers['Host'] = requestOptions.host;
//if (!_.isUndefined(requestOptions.username) && !_.isUndefined(requestOptions.password)) {
// requestOptions.headers['Authorization'] =
// "Basic " + new Buffer(requestOptions.username + ":" + requestOptions.password).toString("base64");
//}
debug('REQUEST OPTIONS: ' + JSON.stringify(requestOptions));
return new Promise(function (resolve, reject) {
var data = "";
var proto = (requestOptions.protocol == 'https:') ? https : http;
if (requestOptions.username != null) {
proto = require('http-digest-client')(
requestOptions.username,
requestOptions.password,
(requestOptions.protocol == 'https:')
);
}
var getReq = proto.request(requestOptions, function (response) {
debug('STATUS: ' + response.statusCode);
debug('HEADERS: ' + JSON.stringify(response.headers));
var error;
if (response.statusCode >= 300) {
if (response.statusCode === 401) {
error = new Error("Unauthorized: check username/password");
} else {
error = new Error("Request failed. HTTP Status Code: " + response.statusCode);
}
debug('ERROR:' + 'Host ' + requestOptions.host + ' ' + error);
return reject(error);
}
response.setEncoding('utf8');
response.on('data', function (result) {
debug("DATA CHUNK", result);
data += result;
});
response.on('end', function () {
debug("END");
try {
var json = JSON.parse(data);
if (_.has(json, 'Head') && _.has(json, 'Body')) {
return resolve(json);
} else {
var error = new Error("Invalid response body format: Head and Body expected");
debug('ERROR:' + 'Host ' + requestOptions.host + ' ' + error);
return reject(error);
}
} catch (e) {
var error = new Error("Invalid response body: " + e.toString());
debug('ERROR:' + 'Host ' + requestOptions.host + ' ' + error);
return reject(error);
}
});
}).on('error', function (error) {
if (timeoutOccurred) {
error = new Error("Request timeout occurred - request aborted");
}
debug('ERROR:' + 'Host ' + requestOptions.host + ' ' + error);
getReq.abort();
return reject(error);
}).on('timeout', function () {
timeoutOccurred = true;
getReq.abort();
});
getReq.setTimeout(requestOptions.timeout);
getReq.end();
});
}
function checkRequiredProperties(options, requiredPropsArray) {
for (var i = 0; i < requiredPropsArray.length; ++i) {
if (_.isUndefined(options[requiredPropsArray[i]])) {
return Promise.reject("Request options lacks required property=" + requiredPropsArray[i]);
}
}
return Promise.resolve()
}
//
// Public Functions
//
module.exports.GetInverterRealtimeData = function (options) {
var opts = _.clone(options);
return checkRequiredProperties(opts, ['host', 'deviceId']).then(function () {
return lastRequest = settlePromise(lastRequest).then(function () {
return getRequest(opts).then(function (json) {
return Promise.resolve(json);
})
})
})
};
// GetComponentsData is provided to use an undocumented API service provided by the data logger
// of the Symo inverters. See https://forum.fhem.de/index.php/topic,24614.msg214011.html#msg214011
module.exports.GetComponentsData = function (options) {
var opts = _.clone(options);
return checkRequiredProperties(opts, ['host']).then(function () {
return lastRequest = settlePromise(lastRequest).then(function () {
return getRequest(opts, '/components/5/0/?print=names').then(function (json) {
return Promise.resolve(json);
})
})
})
};
module.exports.GetPowerFlowRealtimeDataData = function (options) {
var opts = _.clone(options);
return checkRequiredProperties(opts, ['host']).then(function () {
return lastRequest = settlePromise(lastRequest).then(function () {
return getRequest(opts, '/solar_api/v1/GetPowerFlowRealtimeData.fcgi').then(function (json) {
return Promise.resolve(json);
})
})
})
};
module.exports.GetPowerMeterRealtimeData = function (options) {
var opts = _.clone(options);
opts.scope = "Device";
return checkRequiredProperties(opts, ['host', 'deviceId']).then(function () {
return lastRequest = settlePromise(lastRequest).then(function () {
return getRequest(opts, '/solar_api/v1/GetPowerMeterRealtimeData.fcgi').then(function (json) {
return Promise.resolve(json);
})
})
})
};
module.exports.GetStorageRealtimeData = function (options) {
var opts = _.clone(options);
opts.scope = "Device";
return checkRequiredProperties(opts, ['host', 'deviceId']).then(function () {
return lastRequest = settlePromise(lastRequest).then(function () {
return getRequest(opts, '/solar_api/v1/GetStorageRealtimeData.fcgi').then(function (json) {
return Promise.resolve(json);
})
})
})
};