iobroker.sun2000 0.1.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+
2
+
3
+ const ModbusRTU = require('modbus-serial');
4
+
5
+ class DeviceInterface {
6
+ constructor (ip, port) {
7
+ this._ip = ip;
8
+ this._port = port;
9
+ }
10
+
11
+ get ipAddress() {
12
+ return this._ip;
13
+ }
14
+
15
+ get port() {
16
+ return this._port;
17
+ }
18
+
19
+ }
20
+
21
+ class ModbusConnect extends DeviceInterface {
22
+ constructor(adapterInstance,ip,port) {
23
+ super(ip,port);
24
+ this.adapter = adapterInstance;
25
+ this.lastErrno = 0;
26
+ this._id = 0;
27
+ }
28
+
29
+ isOpen() {
30
+ if ( this.client) {
31
+ return (this.client.isOpen );
32
+ } else {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ close() {
38
+ return new Promise((resolve,reject) => {
39
+ if (this.isOpen()) {
40
+ this.client.close((err,data) => {
41
+ if (err) reject(err);
42
+ resolve(data);
43
+ } );
44
+ } else {
45
+ resolve({});
46
+ }
47
+ });
48
+ }
49
+
50
+ setID(modbusID) {
51
+ this._id = modbusID;
52
+ }
53
+
54
+ get id() {
55
+ return this._id;
56
+ }
57
+
58
+ async _create() {
59
+ this.client = new ModbusRTU();
60
+ await this.delay(500);
61
+ }
62
+
63
+ async open() {
64
+ if (!this.client) {
65
+ await this._create();
66
+ }
67
+ if (!this.isOpen()) {
68
+ await this.connect();
69
+ }
70
+ }
71
+
72
+
73
+ async _destroy() {
74
+ return new Promise((resolve,reject) => {
75
+ this.client.destroy((err,data) => {
76
+ if (err) reject(err);
77
+ resolve(data);
78
+ });
79
+ });
80
+ }
81
+
82
+ async _checkError(err) {
83
+ if (err.modbusCode == null) {
84
+ //https://github.com/yaacov/node-modbus-serial/issues/96
85
+ //this.adapter.log.debug('Client destroy!');
86
+ //await this._destroy();
87
+ //this.adapter.log.debug('Client destroy!');
88
+ await this._create();
89
+ }
90
+ }
91
+
92
+
93
+ async connect(repeatCounter = 0) {
94
+ try {
95
+ this.adapter.log.info('Open Connection...');
96
+ await this.close();
97
+ await this.client.setTimeout(5000);
98
+ await this.client.connectTcpRTUBuffered(this.ipAddress, { port: this.port} ); //hang
99
+ await this.delay(1000);
100
+ this.adapter.log.info(`Connected Modbus IP to: ${this.ipAddress} /PORT ${this.port}`);
101
+ } catch(err) {
102
+ this.adapter.log.warn('Couldnt connect Modbus TCP to ' + this.ipAddress + ':' + this.port+' '+err.message);
103
+ await this._checkError(err);
104
+ if (repeatCounter > 0) throw err;
105
+ let delay = 5000;
106
+ if (err.code == 'EHOSTUNREACH') delay *= 4;
107
+ await this.delay(delay);
108
+ await this.connect(repeatCounter+1);
109
+ }
110
+ }
111
+
112
+
113
+ async readHoldingRegisters(address, length) {
114
+ try {
115
+ await this.open();
116
+ //this.adapter.log.debug('client.setID: '+this._id);
117
+ await this.client.setID(this._id);
118
+ const data = await this.client.readHoldingRegisters(address, length);
119
+ return data.data;
120
+ } catch (err) {
121
+ //this.adapter.log.warn('Error while readHoldingregister '+err.message);
122
+ await this._checkError(err);
123
+ throw err;
124
+ }
125
+ }
126
+
127
+ async writeRegisters(address,buffer) {
128
+ try {
129
+ await this.open();
130
+ await this.client.setID(this._id);
131
+ await this.client.writeRegisters(address,buffer);
132
+ } catch (err) {
133
+ await this._checkError(err);
134
+ throw err;
135
+ }
136
+ }
137
+
138
+
139
+ delay(ms){
140
+ return new Promise(resolve => setTimeout(resolve, ms));
141
+ }
142
+
143
+ }
144
+
145
+ module.exports = ModbusConnect;