Skip to content

Commit

Permalink
### 0.7.0: Maintenance Release
Browse files Browse the repository at this point in the history
**Enhancements**

- Inverter typ singleBackflow and multipleBackflow added.
  • Loading branch information
PLCHome committed Sep 1, 2023
1 parent 3349aa9 commit b8d1332
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 182 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.7.0: Maintenance Release

**Enhancements**

- Inverter typ singleBackflow and multipleBackflow added.

### 0.6.0: Maintenance Release

**Enhancements**
Expand Down
204 changes: 112 additions & 92 deletions lib/growatt.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const indexbC = 'indexbC';
const timeout = 50000;
const headers = {};

// const MULTIPLEBACKFLOW = 'multipleBackflow';
// const SINGLEBACKFLOW = 'singleBackflow';
// const ENV = 'env';
// const CONBOX = 'conBox';
// const BOOST = 'boost';
Expand Down Expand Up @@ -409,10 +407,19 @@ module.exports = class growatt {
});
}

getTotalData(type, plantId, sn) {
getTotalData(type, plantId, sn, invId) {
return new Promise((resolve, reject) => {
debugApi('getTotalData:', 'type', type, 'plantId:', plantId, 'sn:', sn);
const params = new Url.URLSearchParams({ plantId, [GROWATTTYPE[type].snParam]: sn });
const splitID = sn.split('_');
const params =
GROWATTTYPE[type].addrParam && GROWATTTYPE[type].invIdParam
? new Url.URLSearchParams({
plantId,
[GROWATTTYPE[type].snParam]: splitID[0],
[GROWATTTYPE[type].addrParam]: splitID[1],
[GROWATTTYPE[type].invIdParam]: invId,
})
: new Url.URLSearchParams({ plantId, [GROWATTTYPE[type].snParam]: sn });
if (this.lifeSignCallback) this.lifeSignCallback();
this.axios
.post(this.getUrl(GROWATTTYPE[type].getTotalData), params.toString(), { headers: this.makeCallHeader() })
Expand Down Expand Up @@ -444,60 +451,67 @@ module.exports = class growatt {
getHistory(type, sn, startDate, endDate, start, allDatasets) {
return new Promise((resolve, reject) => {
debugApi('getHistory:', 'type', type, 'sn:', sn, 'startDate:', startDate, 'endDate:', endDate, 'start:', start, 'allDatasets:', allDatasets);
const params = new Url.URLSearchParams({
[GROWATTTYPE[type].snParam]: sn,
startDate: startDate.toISOString().substring(0, 10),
endDate: endDate.toISOString().substring(0, 10),
start,
});
if (this.lifeSignCallback) this.lifeSignCallback();
this.axios
.post(this.getUrl(GROWATTTYPE[type].getHistory), params.toString(), {
headers: this.makeCallHeader(),
// headers: { cookie: this.cookie, 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
})
.then(res => {
debugVerbose('getHistory result:', res);
if (res.data && res.data.result && res.data.result === 1) {
debugApi('getHistory resolve:', res.data);
if (res.data.obj && res.data.obj.datas && res.data.obj.datas[0]) {
if (allDatasets) {
res.data.obj.datas.forEach(data => this.correctTime(data));
debugApi('getHistory resolve:', res.data.obj.datas);
resolve(res.data.obj.datas);
if (!GROWATTTYPE[type].getHistory) {
resolve({});
} else {
const params = new Url.URLSearchParams({
[GROWATTTYPE[type].snParam]: sn,
startDate: startDate.toISOString().substring(0, 10),
endDate: endDate.toISOString().substring(0, 10),
start,
});
if (this.lifeSignCallback) this.lifeSignCallback();
this.axios
.post(this.getUrl(GROWATTTYPE[type].getHistory), params.toString(), {
headers: this.makeCallHeader(),
// headers: { cookie: this.cookie, 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
})
.then(res => {
debugVerbose('getHistory result:', res);
if (res.data && res.data.result && res.data.result === 1) {
debugApi('getHistory resolve:', res.data);
if (res.data.obj && res.data.obj.datas && res.data.obj.datas[0]) {
if (allDatasets) {
res.data.obj.datas.forEach(data => this.correctTime(data));
debugApi('getHistory resolve:', res.data.obj.datas);
resolve(res.data.obj.datas);
} else {
this.correctTime(res.data.obj.datas[0]);
debugApi('getHistory resolve:', res.data.obj.datas[0]);
resolve(res.data.obj.datas[0]);
}
} else {
this.correctTime(res.data.obj.datas[0]);
debugApi('getHistory resolve:', res.data.obj.datas[0]);
resolve(res.data.obj.datas[0]);
debugApi('getHistory cant find the data');
resolve({});
}
} else if (res.data && res.data.result) {
debugApi('getHistory reject:', res.data);
reject(new Error(JSON.stringify(res.data, getJSONCircularReplacer())));
} else {
debugApi('getHistory cant find the data');
resolve({});
}
} else if (res.data && res.data.result) {
debugApi('getHistory reject:', res.data);
reject(new Error(JSON.stringify(res.data, getJSONCircularReplacer())));
} else {
debugApi('getHistory reject');
if (res.request.path.match('errorMess')) {
reject(new Error(`The server sent an unexpected response: ${res.request.path}`));
} else {
reject(new Error('The server sent an unexpected response, a fatal error has occurred'));
debugApi('getHistory reject');
if (res.request.path.match('errorMess')) {
reject(new Error(`The server sent an unexpected response: ${res.request.path}`));
} else {
reject(new Error('The server sent an unexpected response, a fatal error has occurred'));
}
}
}
})
.catch(e => {
this.connected = false;
debugApi('getHistory err:', JSON.stringify(e, getJSONCircularReplacer(), ' '));
reject(e);
});
})
.catch(e => {
this.connected = false;
debugApi('getHistory err:', JSON.stringify(e, getJSONCircularReplacer(), ' '));
reject(e);
});
}
});
}

getStatusData(type, plantId, sn) {
return new Promise((resolve, reject) => {
debugApi('getStatusData:', 'type', type, 'plantId:', plantId, 'sn', sn);
const params = new Url.URLSearchParams({ plantId, [GROWATTTYPE[type].snParam]: sn });
const splitSn = sn.split('_');
const params = GROWATTTYPE[type].addrParam
? new Url.URLSearchParams({ plantId, [GROWATTTYPE[type].snParam]: splitSn[0], [GROWATTTYPE[type].addrParam]: splitSn[1] })
: new Url.URLSearchParams({ plantId, [GROWATTTYPE[type].snParam]: sn });
if (this.lifeSignCallback) this.lifeSignCallback();
this.axios
.post(this.getUrl(GROWATTTYPE[type].getStatusData), params.toString(), { headers: this.makeCallHeader() })
Expand Down Expand Up @@ -938,52 +952,58 @@ module.exports = class growatt {
const growattType = objs[o];
if (growattType !== '' && GROWATTTYPE[growattType]) {
for (let a = 0; a < device.obj[growattType].length; a += 1) {
const serialNr = device.obj[growattType][a][0];
result[serialNr] = {};
if (options.deviceType) {
result[serialNr][growattType] = device.obj[growattType][a];
}
if (options.growattType) {
result[serialNr].growattType = growattType;
}
if (options.totalData) {
/* eslint-disable-next-line no-await-in-loop */
const totalData = await this.getTotalData(growattType, plantId, serialNr).catch(e => {
debugApi(`getAllPlantDeviceData getTotalData ${growattType} err:`, e);
reject(e);
});
if (totalData && totalData.obj) {
result[serialNr].totalData = totalData.obj;
if (device.obj[growattType][a].length > 2) {
let serialNr = device.obj[growattType][a][0];
const invId = device.obj[growattType][a][3];
if (GROWATTTYPE[growattType].atIndex) {
serialNr += `@${device.obj[growattType][a][GROWATTTYPE[growattType].atIndex]}`;
}
}
if (options.statusData && GROWATTTYPE[growattType].getStatusData) {
/* eslint-disable-next-line no-await-in-loop */
const statusData = await this.getStatusData(growattType, plantId, serialNr).catch(e => {
debugApi(`getAllPlantDeviceData getStatusData ${growattType} err:`, e);
reject(e);
});
if (statusData && statusData.obj) {
result[serialNr].statusData = statusData.obj;
result[serialNr] = {};
if (options.deviceType) {
result[serialNr][growattType] = device.obj[growattType][a];
}
}
if (options.historyLast || options.historyAll) {
/* eslint-disable-next-line no-await-in-loop */
const historyLast = await this.getHistory(
growattType,
serialNr,
options.historyLastStartDate,
options.historyLastEndDate,
options.historyStart,
options.historyAll
).catch(e => {
debugApi(`getAllPlantDeviceData getHistory ${growattType} err:`, e);
reject(e);
});
if (historyLast) {
if (options.historyAll) {
result[serialNr].historyAll = historyLast;
} else {
result[serialNr].historyLast = historyLast;
if (options.growattType) {
result[serialNr].growattType = growattType;
}
if (options.totalData) {
/* eslint-disable-next-line no-await-in-loop */
const totalData = await this.getTotalData(growattType, plantId, serialNr, invId).catch(e => {
debugApi(`getAllPlantDeviceData getTotalData ${growattType} err:`, e);
reject(e);
});
if (totalData && totalData.obj) {
result[serialNr].totalData = totalData.obj;
}
}
if (options.statusData && GROWATTTYPE[growattType].getStatusData) {
/* eslint-disable-next-line no-await-in-loop */
const statusData = await this.getStatusData(growattType, plantId, serialNr).catch(e => {
debugApi(`getAllPlantDeviceData getStatusData ${growattType} err:`, e);
reject(e);
});
if (statusData && statusData.obj) {
result[serialNr].statusData = statusData.obj;
}
}
if (options.historyLast || options.historyAll) {
/* eslint-disable-next-line no-await-in-loop */
const historyLast = await this.getHistory(
growattType,
serialNr,
options.historyLastStartDate,
options.historyLastEndDate,
options.historyStart,
options.historyAll
).catch(e => {
debugApi(`getAllPlantDeviceData getHistory ${growattType} err:`, e);
reject(e);
});
if (historyLast) {
if (options.historyAll) {
result[serialNr].historyAll = historyLast;
} else {
result[serialNr].historyLast = historyLast;
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions lib/growatttype.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const TLXH = 'tlxh';
const STORAGE = 'storage';
const SPA = 'spa';
const HPS = 'hps';
const SINGLEBACKFLOW = 'singleBackflow';
const MULTIPLEBACKFLOW = 'multipleBackflow';

module.exports = {
[MAX]: {
Expand Down Expand Up @@ -223,4 +225,17 @@ module.exports = {
getHistory: '/device/getHPSHistory',
getStatusData: '/panel/hps/getHPSStatusData',
},
[SINGLEBACKFLOW]: {
snParam: 'datalogSn',
addrParam: 'addr',
invIdParam: 'invId',
getTotalData: '/panel/singleBackflow/getSingleBackflowTotalData',
getStatusData: '/panel/singleBackflow/getSingleBackflowStatusData',
},
[MULTIPLEBACKFLOW]: {
snParam: 'ammerSn',
atIndex: 2,
getTotalData: '/panel/multipleBackflow/getMultipleBackflowStatusData',
getStatusData: '/panel/multipleBackflow/getMultipleBackflowTotalData',
},
};
Loading

0 comments on commit b8d1332

Please sign in to comment.