-
Notifications
You must be signed in to change notification settings - Fork 1
/
haas-mtconnect-adapter.js
310 lines (239 loc) · 6.1 KB
/
haas-mtconnect-adapter.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const Adapter = require('./mtconnect-adapter')
const Serial = require('./haas-serial')
const Simulator = require('./data_simulator')
const config = require('./config.json')
//init
const simulation = config.publisher.simulation
const adapter = new Adapter(config.adapter)
const simulator = simulation ? new Simulator() : null
const serialcomm = simulation ? null : new Serial(config.serial)
var first = true
// loop
setInterval(() => {
start()
}, 500)
/**
* Start machine monitoring
*/
function start() {
cmdQ100()
.then(() => {
return cmdQ104()
})
.then(() => {
return cmdQ500()
})
.then(() => {
return cmdQ600()
})
}
/**
* Clean and splits the string with the machine status
*/
function cleanStatus(status) {
const indexStr = status.indexOf('\x02')
const indexEnd = status.indexOf('\x17')
//console.log(status)
status = status.substr(indexStr + 1, (indexEnd - indexStr) - 1).split(',')
/*for( item in status) {
console.log(status[item])
console.log(' ')
}*/
//console.log('\n')
return status
}
/**
* Variable Q100: get machine availability
*/
async function cmdQ100() {
if (first) {
let status
if (simulation) {
status = await simulator.Q100()
} else {
status = await serialcomm.sendCommand('Q100')
}
if(status != null){
adapter.addDataItem('avail', 'AVAILABLE')
}else{
adapter.addDataItem('avail', 'UNAVAILABLE')
}
first = false
}
}
/**
* Variable Q104: get operation mode
*/
async function cmdQ104() {
let status
if (simulation) {
status = await simulator.Q104()
} else {
status = await serialcomm.sendCommand('Q104')
}
if (status != null) {
status = cleanStatus(status)
if(status[1] != null){
switch (status[1].trim()) {
case 'MDI':
adapter.addDataItem('mode', 'MANUAL_DATA_INPUT')
break;
case 'JOG':
adapter.addDataItem('mode', 'MANUAL')
break;
case 'ZERORET':
adapter.addDataItem('mode', 'MANUAL')
break;
default:
adapter.addDataItem('mode', 'AUTOMATIC')
break;
}
}
}
}
/**
* Variable Q500: get machine status
*/
async function cmdQ500() {
let status
if (simulation) {
status = await simulator.Q500()
} else {
status = await serialcomm.sendCommand('Q500')
}
if (status != null) {
status = cleanStatus(status)
if (status[0].trim() === 'PROGRAM') {
if(status[1] != null){
adapter.addDataItem('program', status[1].trim())
}
if(status[2] != null){
adapter.addDataItem('execution', status[2].trim())
}
if(status[3] != null && status[4] != null){
if(status[3] === 'PARTS') adapter.addDataItem('part_count', status[4].trim())
}
}else if(status[0].trim() === 'STATUS'){
if(status[1] != null){
adapter.addDataItem('execution', status[1].trim())
}
}
}
}
/**
* MACRO Q600: fetch machine condition
*/
async function cmdQ600() {
await cmd5021()
await cmd5022()
await cmd5023()
await cmd3027()
await cmd1094()
await sleep(500)
await cmd1098()
//await cmd3026()
}
/**
* Variable 5021: get current machine x position
*/
async function cmd5021() {
let status
if (simulation) {
status = await simulator.Q600_5021()
} else {
status = await serialcomm.sendCommand('Q600 5021')
}
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('x_act', status[2].trim())
}
}
/**
* Variable 5022: get current machine y position
*/
async function cmd5022() {
let status
if (simulation) {
status = await simulator.Q600_5022()
} else {
status = await serialcomm.sendCommand('Q600 5022')
}
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('y_act', status[2].trim())
}
}
/**
* Variable 5023: get current machine z position
*/
async function cmd5023() {
let status
if (simulation) {
status = await simulator.Q600_5023()
} else {
status = await serialcomm.sendCommand('Q600 5023')
}
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('z_act', status[2].trim())
}
}
/**
* Variable 3027: get spindle speed
*/
async function cmd3027() {
let status
if (simulation) {
status = await simulator.Q600_3027()
} else {
status = await serialcomm.sendCommand('Q600 3027')
}
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('speed', status[2].trim())
}
}
/**
* Variable 1094: get coolant level
*/
async function cmd1094() {
let status
if (simulation) {
status = await simulator.Q600_1094()
} else {
status = await serialcomm.sendCommand('Q600 1094')
}
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('coolant_level', status[2].trim())
}
}
/**
* Variable 1098: get spindle load
*/
async function cmd1098() {
let status
if (simulation) {
status = await simulator.Q600_1098()
} else {
status = await serialcomm.sendCommand('Q600 1098')
}
status = cleanStatus(status)
if(status[2] != null){
status[2] = String((Number(status[2])/81.9200).toFixed(3))
adapter.addDataItem('load', status[2].trim())
}
}
/**
* Variable 3026: get tool in spindle
*/
async function cmd3026() {
let status = await serialcomm.sendCommand('Q600 3026')
status = cleanStatus(status)
if(status[2] != null){
adapter.addDataItem('load', status[2].trim())
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}