-
Notifications
You must be signed in to change notification settings - Fork 4
/
beckhoff.js
231 lines (182 loc) · 6.69 KB
/
beckhoff.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
const events = require('events');
const utils = require('@iobroker/adapter-core');
const ads = require('node-ads-api');
const lib = require('./lib');
const emitter = new events.EventEmitter();
let adsClient = null;
let checkPlcStateInterval = null;
let oldConnectionState = false;
let oldPlcState = false;
let reconnectTimeout = null;
let adapter;
function startAdapter(options) {
const optionsSave = options || {};
Object.assign(optionsSave, { name: 'beckhoff' });
adapter = new utils.Adapter(optionsSave);
// When Adapter is ready then connecting to PLC and Subscribe necessary Handles
adapter.on('ready', () => {
adapter.subscribeStates('*');
plcConnection();
emitter.on('updateObjects', () => {
lib.createObjectsAndHandles(adsClient, adapter);
});
emitter.on('newSyncReq', () => {
if (adapter.config.targetAmsPort === '901') {
lib.tpyParser(adapter, emitter);
} else {
lib.plcVarSyncronizing(adsClient, adapter, emitter);
}
});
});
// When Adapter would be stopped some last work we have to do
adapter.on('unload', cb => {
emitter.removeAllListeners();
clearTimeout(reconnectTimeout);
if (adsClient !== null) {
adsClient.end(() => {
adsClient = null;
adapter.log.debug('ADS Connection closed.');
});
}
if (checkPlcStateInterval !== null) {
clearInterval(checkPlcStateInterval);
checkPlcStateInterval = null;
}
try {
adapter.setState('info.connection', false, true);
adapter.setState('info.plcRun', false, true);
adapter.log.info('Stopped and Connection closed');
} finally {
cb();
}
});
// These Function is called when one of the subscribed State fires a 'stateChange' event
adapter.on('stateChange', (id, state) => {
if (!state) {
return;
}
const shortId = id.substring(id.indexOf('.', 9) + 1, id.length);
switch (shortId) {
case 'info.connection':
// Only when Value has changed need to do something
if (oldConnectionState === state.val) {
return;
}
oldConnectionState = state.val;
if (state.val === true) {
// Connect Worker to write changes of Symbol Values in PLC to ioBroker
lib.workerPlcToAdapter(adsClient, adapter, emitter);
if (checkPlcStateInterval === null) {
checkPlcStateInterval = setInterval(() => {
lib.checkPlcState(adsClient, adapter, emitter);
}, adapter.config.reconnectInterval * 1000);
}
} else if (checkPlcStateInterval !== null) {
clearInterval(checkPlcStateInterval);
checkPlcStateInterval = null;
}
break;
case 'info.plcRun':
// Only when Value has changed need to do something
if (oldPlcState === state.val) {
break;
}
oldPlcState = state.val;
if (state.val === true) {
emitter.emit('newSyncReq');
}
break;
default:
if (oldConnectionState) {
lib.workerAdapterToPlc(adsClient, adapter, emitter, state, id);
}
break;
}
});
return adapter;
}
/**
* Establish Connection to PLC and handles some Connectionerror
*
* @returns {void}
*/
function plcConnection() {
// Create connection configuration for PLC
const options = {
host: adapter.config.targetIpAdress,
amsNetIdTarget: adapter.config.targetAmsNetId,
amsPortTarget: adapter.config.targetAmsPort === '901' ? '801' : adapter.config.targetAmsPort,
port: adapter.config.targetTcpPort,
amsNetIdSource: adapter.config.sourceAmsNetId,
amsPortSource: adapter.config.sourceAmsPort,
};
// New Connection needed -> Create Ads Client and read Deviceinformation
emitter.on('reConnect', () => {
adapter.log.debug('Start establish Connection to PLC');
adsClient = ads.connect(options, () => {
// eslint-disable-line no-param-reassign
if (adsClient === null) {
adapter.log.debug('Establish Connection to PLC failed -> Try again');
endConnReconnect();
return;
}
adsClient.readState((err, res) => {
if (err) {
adapter.log.error(`ADS Client: Error: ${err}`);
endConnReconnect();
} else {
adapter.setState('info.connection', true, true);
if (res.adsState === ads.ADSSTATE.RUN) {
adapter.setState('info.plcRun', true, true);
}
adapter.log.info(`Connected to PLC. State of PLC: ${ads.ADSSTATE.fromId(res.adsState)}`);
}
});
});
// When the Connection have some Problem write Error Log, set disconnected Event and close Connection properly.
adsClient.on('error', err => {
adapter.log.error(`ADS Client: ${err}`);
endConnReconnect();
});
adsClient.on('timeout', err => {
adapter.log.error(`ADS Client: ${err}`);
endConnReconnect();
});
});
emitter.on('disconnecting', () => {
endConnReconnect();
});
// On first run we need a initial connection
emitter.emit('reConnect');
}
let timeAlreadyRunning = false;
/**
* Set States, end connection to PLC properly and shedule reConnect
*
* @returns {void}
*/
function endConnReconnect() {
adapter.setState('info.connection', false, true);
adapter.setState('info.plcRun', false, true);
if (timeAlreadyRunning) {
return;
}
timeAlreadyRunning = true;
if (adsClient !== null) {
adsClient.end(() => {
adsClient = null;
});
}
adapter.log.info(`Try to reconnect in ${adapter.config.reconnectInterval} seconds`);
reconnectTimeout = setTimeout(() => {
timeAlreadyRunning = false;
emitter.emit('reConnect');
}, adapter.config.reconnectInterval * 1000);
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}