Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev #30

Merged
merged 5 commits into from
Jan 17, 2024
Merged

dev #30

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ If you use two inverters, then connect to the second inverter and read the commu
### **WORK IN PROGRESS**
* display the data from PV strings (#27)
* optimize the timing of interval loop
* improved handling of read timeouts from more then 2 inverters

### 0.1.2 (2024-01-12)
* fix: no Data if interval less 20 sec (#24)
Expand Down
4 changes: 2 additions & 2 deletions lib/modbus_connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ModbusConnect extends DeviceInterface {
constructor(adapterInstance,ip,port) {
super(ip,port);
this.adapter = adapterInstance;
this.lastErrno = 0;
this._id = 0;
}

Expand Down Expand Up @@ -81,8 +80,9 @@ class ModbusConnect extends DeviceInterface {

async _checkError(err) {
if (err.modbusCode == null) {
this.adapter.log.debug('modbusCode == 0!');
await this.close();
//https://github.com/yaacov/node-modbus-serial/issues/96
//this.adapter.log.debug('Client destroy!');
//await this._destroy();
//this.adapter.log.debug('Client destroy!');
await this._create();
Expand Down
78 changes: 45 additions & 33 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Sun2000 extends utils.Adapter {
});

this.lastTimeUpdated = 0;
this.lastStateUpdated = 0;
this.lastStateUpdatedHigh = 0;
this.lastStateUpdatedLow = 0;
this.isConnected = false;
this.inverters = [];
this.settings = {
Expand Down Expand Up @@ -54,7 +55,7 @@ class Sun2000 extends utils.Adapter {
await this.extendObjectAsync('info', {
type: 'channel',
common: {
name: 'info',
name: 'channel info',
role: 'info'
},
native: {}
Expand All @@ -76,25 +77,22 @@ class Sun2000 extends utils.Adapter {
await this.extendObjectAsync('meter', {
type: 'device',
common: {
name: 'meter',
role: 'info'
name: 'device meter'
},
native: {}
});
await this.extendObjectAsync('collected', {
type: 'channel',
common: {
name: 'collected',
role: 'info'
name: 'channel collected'
},
native: {}
});

await this.extendObjectAsync('inverter', {
type: 'device',
common: {
name: 'meter',
role: 'info'
name: 'device inverter'
},
native: {}
});
Expand All @@ -106,7 +104,7 @@ class Sun2000 extends utils.Adapter {
await this.extendObjectAsync(path, {
type: 'channel',
common: {
name: 'modbus'+i,
name: 'channel modbus'+i,
role: 'indicator'
},
native: {}
Expand All @@ -115,7 +113,15 @@ class Sun2000 extends utils.Adapter {
await this.extendObjectAsync(path+'.grid', {
type: 'channel',
common: {
name: 'grid',
name: 'channel grid'
},
native: {}
});

await this.extendObjectAsync(path+'.info', {
type: 'channel',
common: {
name: 'channel info',
role: 'info'
},
native: {}
Expand All @@ -124,17 +130,23 @@ class Sun2000 extends utils.Adapter {
await this.extendObjectAsync(path+'.battery', {
type: 'channel',
common: {
name: 'battery',
role: 'info'
name: 'channel battery'
},
native: {}
});

await this.extendObjectAsync(path+'.string', {
type: 'channel',
common: {
name: 'channel string'
},
native: {}
});

await this.extendObjectAsync(path+'.derived', {
type: 'channel',
common: {
name: 'derived',
role: 'indicator'
name: 'channel derived'
},
native: {}
});
Expand Down Expand Up @@ -212,18 +224,26 @@ class Sun2000 extends utils.Adapter {
if (!this.lastTimeUpdated) this.lastUpdated = 0;
if (this.lastTimeUpdated > 0) {
const sinceLastUpdate = new Date().getTime() - this.lastTimeUpdated; //ms
this.log.debug('Watchdog: time to last update '+sinceLastUpdate/1000+' sec');
this.log.debug('Watchdog: time of last update '+sinceLastUpdate/1000+' sec');
const lastIsConnected = this.isConnected;
this.isConnected = this.lastStateUpdated > 0 && sinceLastUpdate < this.settings.intervall*2;
this.log.debug('lastIsConncted '+lastIsConnected+' isConnectetd '+this.isConnected+' lastStateupdated '+this.lastStateUpdated);
this.isConnected = this.lastStateUpdatedHigh > 0 && sinceLastUpdate < this.settings.intervall*3;
if (this.lastStateUpdatedLow == 0) {
if (this.lastStateUpdatedHigh == 0) {
this.log.warn('Not data can be read! Please check your settings.');
} else {
this.log.warn('Not all data can be read! Please reduce the intervall value.');
}
}
if (this.isConnected !== lastIsConnected ) this.setState('info.connection', this.isConnected, true);
this.lastStateUpdatedLow = 0;
this.lastStateUpdatedHigh = 0;

if (this.isConnected !== lastIsConnected ) this.setState('info.connection', this.isConnected, true);
if (sinceLastUpdate > this.settings.intervall*10) {
this.log.warn('watchdog: restart Adapter...');
this.restart();
}
}
},30000);
},60000);
}


Expand Down Expand Up @@ -265,45 +285,37 @@ class Sun2000 extends utils.Adapter {

async dataPolling() {

function timeLeft(target) {
const left = target - new Date().getTime();
function timeLeft(target,factor =1) {
const left = Math.round((target - new Date().getTime())*factor);
if (left < 0) return 0;
return left;
}

const start = new Date().getTime();

this.log.debug('### DataPolling START '+ Math.round((start-this.lastTimeUpdated)/1000)+' sec ###');
if (this.lastTimeUpdated > 0 && (start-this.lastTimeUpdated)/1000 > this.settings.intervall/1000 + 1) {
this.log.warn('time intervall '+(start-this.lastTimeUpdated)/1000+' sec');
}
this.lastTimeUpdated = start;
let stateUpdated = 0;

const nextLoop = this.settings.intervall - start % (this.settings.intervall) + start;

//High Loop
for (const item of this.inverters) {
this.modbusClient.setID(item.modbusId);
//this.log.info('### Left Time '+timeLeft/1000);
stateUpdated += await this.state.updateStates(item,this.modbusClient,dataRefreshRate.high,timeLeft(nextLoop));
this.lastStateUpdatedHigh += await this.state.updateStates(item,this.modbusClient,dataRefreshRate.high,timeLeft(nextLoop));
}

if (timeLeft(nextLoop) > 500) {
await this.state.runProcessHooks(dataRefreshRate.high);
//Low Loop
for (const item of this.inverters) {
for (const [i,item] of this.inverters.entries()) {
this.modbusClient.setID(item.modbusId);
//this.log.info('### Left Time '+timeLeft/1000);
stateUpdated += await this.state.updateStates(item,this.modbusClient,dataRefreshRate.low,timeLeft(nextLoop));
//this.log.debug('+++++ Loop: '+i+' Left Time: '+timeLeft(nextLoop,(i+1)/this.inverters.length)+' Faktor '+((i+1)/this.inverters.length));
this.lastStateUpdatedLow += await this.state.updateStates(item,this.modbusClient,dataRefreshRate.low,timeLeft(nextLoop,(i+1)/this.inverters.length));
}
//if (timeLeft(nextLoop) > 1000) {
await this.state.runProcessHooks(dataRefreshRate.low);
//}
}

this.lastStateUpdated = stateUpdated;

if (this.pollingTimer) this.clearTimeout(this.pollingTimer);
this.pollingTimer = this.setTimeout(() => {
this.dataPolling(); //recursiv
Expand Down