-
Notifications
You must be signed in to change notification settings - Fork 6
/
CollectorService.java
2762 lines (2193 loc) · 79.9 KB
/
CollectorService.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.point85.domain.collector;
import java.io.File;
import java.net.InetAddress;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.point85.domain.DomainUtils;
import org.point85.domain.OeeEquipmentEvent;
import org.point85.domain.cron.CronEventClient;
import org.point85.domain.cron.CronEventListener;
import org.point85.domain.cron.CronEventSource;
import org.point85.domain.db.DatabaseEvent;
import org.point85.domain.db.DatabaseEventClient;
import org.point85.domain.db.DatabaseEventListener;
import org.point85.domain.db.DatabaseEventSource;
import org.point85.domain.db.DatabaseEventStatus;
import org.point85.domain.dto.EquipmentEventRequestDto;
import org.point85.domain.email.EmailClient;
import org.point85.domain.email.EmailMessageListener;
import org.point85.domain.email.EmailSource;
import org.point85.domain.file.FileEventClient;
import org.point85.domain.file.FileEventListener;
import org.point85.domain.file.FileEventSource;
import org.point85.domain.http.HttpEventListener;
import org.point85.domain.http.HttpSource;
import org.point85.domain.http.OeeHttpServer;
import org.point85.domain.i18n.DomainLocalizer;
import org.point85.domain.jms.JmsClient;
import org.point85.domain.jms.JmsMessageListener;
import org.point85.domain.jms.JmsSource;
import org.point85.domain.kafka.KafkaMessageListener;
import org.point85.domain.kafka.KafkaOeeClient;
import org.point85.domain.kafka.KafkaSource;
import org.point85.domain.messaging.ApplicationMessage;
import org.point85.domain.messaging.CollectorCommandMessage;
import org.point85.domain.messaging.CollectorNotificationMessage;
import org.point85.domain.messaging.CollectorResolvedEventMessage;
import org.point85.domain.messaging.CollectorServerStatusMessage;
import org.point85.domain.messaging.EquipmentEventMessage;
import org.point85.domain.messaging.MessageType;
import org.point85.domain.messaging.NotificationSeverity;
import org.point85.domain.modbus.ModbusEvent;
import org.point85.domain.modbus.ModbusEventListener;
import org.point85.domain.modbus.ModbusMaster;
import org.point85.domain.modbus.ModbusSource;
import org.point85.domain.mqtt.MqttMessageListener;
import org.point85.domain.mqtt.MqttOeeClient;
import org.point85.domain.mqtt.MqttSource;
import org.point85.domain.mqtt.QualityOfService;
import org.point85.domain.opc.da.DaOpcClient;
import org.point85.domain.opc.da.OpcDaDataChangeListener;
import org.point85.domain.opc.da.OpcDaMonitoredGroup;
import org.point85.domain.opc.da.OpcDaMonitoredItem;
import org.point85.domain.opc.da.OpcDaSource;
import org.point85.domain.opc.da.OpcDaVariant;
import org.point85.domain.opc.da.TagGroupInfo;
import org.point85.domain.opc.da.TagItemInfo;
import org.point85.domain.opc.ua.OpcUaAsynchListener;
import org.point85.domain.opc.ua.OpcUaSource;
import org.point85.domain.opc.ua.UaOpcClient;
import org.point85.domain.persistence.PersistenceService;
import org.point85.domain.plant.Equipment;
import org.point85.domain.plant.EquipmentEventResolver;
import org.point85.domain.plant.KeyedObject;
import org.point85.domain.plant.Material;
import org.point85.domain.plant.Reason;
import org.point85.domain.proficy.ProficyClient;
import org.point85.domain.proficy.ProficyEventListener;
import org.point85.domain.proficy.ProficySource;
import org.point85.domain.proficy.TagData;
import org.point85.domain.proficy.TagDataType;
import org.point85.domain.proficy.TagQuality;
import org.point85.domain.proficy.TagSample;
import org.point85.domain.rmq.RmqClient;
import org.point85.domain.rmq.RmqMessageListener;
import org.point85.domain.rmq.RmqSource;
import org.point85.domain.rmq.RoutingKey;
import org.point85.domain.schedule.ShiftInstance;
import org.point85.domain.schedule.WorkSchedule;
import org.point85.domain.script.EventResolver;
import org.point85.domain.script.OeeContext;
import org.point85.domain.script.OeeEventType;
import org.point85.domain.uom.UnitOfMeasure;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
/**
* CollectorService listens for events from various sources and resolves them
* into OEE performance, availability and quality records.
*
*/
public class CollectorService implements HttpEventListener, OpcDaDataChangeListener, OpcUaAsynchListener,
RmqMessageListener, JmsMessageListener, DatabaseEventListener, FileEventListener, MqttMessageListener,
ModbusEventListener, CronEventListener, KafkaMessageListener, EmailMessageListener, ProficyEventListener {
// logger
private static final Logger logger = LoggerFactory.getLogger(CollectorService.class);
// sec between status checks
private static final long HEARTBEAT_SEC = 60;
// thread pool service
private final ExecutorService executorService = Executors.newCachedThreadPool();
// timer to broadcast status
private Timer heartbeatTimer;
// serializer
protected Gson gson;
// script execution context
private OeeContext appContext;
// resolver
private EquipmentEventResolver equipmentResolver;
// data collectors
private List<DataCollector> collectors;
// JVM host name
private String hostname;
// JVM host IP address
private String ip;
// flag for manual collector
private boolean isManual = false;
// name of a specific collector on this host
private String collectorName;
// data source information
private final Map<String, OpcDaInfo> opcDaSubscriptionMap = new HashMap<>();
private final Map<String, OpcUaInfo> opcUaSubscriptionMap = new HashMap<>();
private final Map<String, HttpServerSource> httpServerMap = new HashMap<>();
private final Map<String, RmqBrokerSource> rmqBrokerMap = new HashMap<>();
private final Map<String, JmsBrokerSource> jmsBrokerMap = new HashMap<>();
private final Map<String, PolledSource> kafkaBrokerMap = new HashMap<>();
private final Map<String, PolledSource> emailBrokerMap = new HashMap<>();
private final Map<String, PolledSource> databaseServerMap = new HashMap<>();
private final Map<String, PolledSource> fileServerMap = new HashMap<>();
private final Map<String, PolledSource> modbusSlaveMap = new HashMap<>();
private final Map<String, MqttBrokerSource> mqttBrokerMap = new HashMap<>();
private final Map<String, CronSource> cronSchedulerMap = new HashMap<>();
private final Map<String, PolledSource> proficyMap = new HashMap<>();
// equipment by name cache
private final ConcurrentMap<String, Equipment> equipmentCache = new ConcurrentHashMap<>();
public CollectorService() {
initialize();
}
public CollectorService(String collectorName) {
this.collectorName = collectorName;
initialize();
}
public CollectorService(boolean isManual) {
this.isManual = isManual;
initialize();
}
private void initialize() {
opcDaSubscriptionMap.clear();
opcUaSubscriptionMap.clear();
httpServerMap.clear();
rmqBrokerMap.clear();
jmsBrokerMap.clear();
kafkaBrokerMap.clear();
emailBrokerMap.clear();
databaseServerMap.clear();
fileServerMap.clear();
cronSchedulerMap.clear();
mqttBrokerMap.clear();
gson = new Gson();
appContext = new OeeContext();
equipmentResolver = new EquipmentEventResolver();
collectors = new ArrayList<>();
}
public OeeEvent createEvent(String sourceId, OeeEventType type, Equipment equipment, OffsetDateTime startTime,
OffsetDateTime endTime) throws Exception {
OeeEvent event = new OeeEvent(equipment);
event.setEventType(type);
event.setStartTime(startTime);
event.setEndTime(endTime);
event.setSourceId(sourceId);
// get the shift from the work schedule
WorkSchedule schedule = equipment.findWorkSchedule();
if (schedule != null) {
List<ShiftInstance> shifts = schedule.getShiftInstancesForTime(startTime.toLocalDateTime());
if (!shifts.isEmpty()) {
event.setShift(shifts.get(0).getShift());
event.setTeam(shifts.get(0).getTeam());
}
}
// material being produced
if (!type.equals(OeeEventType.MATL_CHANGE)) {
OeeEvent setup = PersistenceService.instance().fetchLastEvent(equipment, OeeEventType.MATL_CHANGE);
if (setup != null) {
event.setMaterial(setup.getMaterial());
}
}
return event;
}
public boolean isManual() {
return this.isManual;
}
public OeeContext getAppContext() {
return appContext;
}
// collect all HTTP server info
private void buildHttpServers(EventResolver resolver) {
HttpSource source = (HttpSource) resolver.getDataSource();
String id = source.getId();
HttpServerSource serverSource = httpServerMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found HTTP server specified for host " + source.getHost() + " on HTTP port "
+ source.getPort() + " and HTTPS port " + source.getHttpsPort());
}
serverSource = new HttpServerSource(source);
httpServerMap.put(id, serverSource);
}
}
// collect all RMQ broker info
private void buildRMQBrokers(EventResolver resolver) {
RmqSource source = (RmqSource) resolver.getDataSource();
String id = source.getId();
RmqBrokerSource brokerSource = rmqBrokerMap.get(id);
if (brokerSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found RMQ broker specified for host " + source.getHost() + " on port " + source.getPort());
}
brokerSource = new RmqBrokerSource(source);
rmqBrokerMap.put(id, brokerSource);
}
}
// collect all JMS broker info
private void buildJMSBrokers(EventResolver resolver) {
JmsSource source = (JmsSource) resolver.getDataSource();
String id = source.getId();
JmsBrokerSource brokerSource = jmsBrokerMap.get(id);
if (brokerSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found JMS broker specified for host " + source.getHost() + " on port " + source.getPort());
}
brokerSource = new JmsBrokerSource(source);
jmsBrokerMap.put(id, brokerSource);
}
}
// collect all MQTT broker info
private void buildMQTTBrokers(EventResolver resolver) {
MqttSource source = (MqttSource) resolver.getDataSource();
String id = source.getId();
MqttBrokerSource brokerSource = mqttBrokerMap.get(id);
if (brokerSource == null) {
if (logger.isInfoEnabled()) {
logger.info(
"Found MQTT broker specified for host " + source.getHost() + " on port " + source.getPort());
}
brokerSource = new MqttBrokerSource(source);
mqttBrokerMap.put(id, brokerSource);
}
}
// collect all database server sources
private void buildDatabaseServers(EventResolver resolver) {
DatabaseEventSource eventSource = (DatabaseEventSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod();
String id = eventSource.getId();
PolledSource dbSource = databaseServerMap.get(id);
if (dbSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found database server specified for host " + eventSource.getHost());
}
dbSource = new PolledSource(eventSource);
databaseServerMap.put(id, dbSource);
}
dbSource.getSourceIds().add(sourceId);
dbSource.getPollingIntervals().add(pollingMillis);
}
// collect all file server info
private void buildFileServers(EventResolver resolver) {
FileEventSource eventSource = (FileEventSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod();
String id = eventSource.getId();
PolledSource serverSource = fileServerMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found file server specified for host " + eventSource.getHost());
}
serverSource = new PolledSource(eventSource);
fileServerMap.put(id, serverSource);
}
serverSource.getSourceIds().add(sourceId);
serverSource.getPollingIntervals().add(pollingMillis);
}
// collect Kafka server info
private void buildKafkaServers(EventResolver resolver) {
KafkaSource eventSource = (KafkaSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod() != null ? resolver.getUpdatePeriod()
: KafkaOeeClient.DEFAULT_POLLING_INTERVAL;
String id = eventSource.getId();
PolledSource serverSource = kafkaBrokerMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found Kafka server specified for host " + eventSource.getHost() + " on port "
+ eventSource.getPort());
}
serverSource = new PolledSource(eventSource);
kafkaBrokerMap.put(id, serverSource);
}
serverSource.getSourceIds().add(sourceId);
serverSource.getPollingIntervals().add(pollingMillis);
}
// collect email events
private void buildEmailHosts(EventResolver resolver) {
EmailSource eventSource = (EmailSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod() != null ? resolver.getUpdatePeriod()
: EmailClient.DEFAULT_POLLING_INTERVAL;
String id = eventSource.getId();
PolledSource serverSource = emailBrokerMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found Email server specified for host " + eventSource.getHost() + " on port "
+ eventSource.getPort());
}
serverSource = new PolledSource(eventSource);
emailBrokerMap.put(id, serverSource);
}
serverSource.getSourceIds().add(sourceId);
serverSource.getPollingIntervals().add(pollingMillis);
}
// collect all cron scheduler info
private void buildCronSchedulers(EventResolver resolver) {
CronEventSource eventSource = (CronEventSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
String cronExpression = eventSource.getCronExpression();
String id = eventSource.getId();
CronSource serverSource = cronSchedulerMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found cron source specified for job " + eventSource.getHost());
}
serverSource = new CronSource(eventSource);
cronSchedulerMap.put(id, serverSource);
}
serverSource.getSourceIds().add(sourceId);
serverSource.getCronExpressions().add(cronExpression);
}
// collect all Modbus slave info
private void buildModbusSlaves(EventResolver resolver) {
ModbusSource eventSource = (ModbusSource) resolver.getDataSource();
String sourceId = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod();
String id = eventSource.getId();
PolledSource serverSource = modbusSlaveMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found Modbus slave specified for host " + eventSource.getHost());
}
serverSource = new PolledSource(eventSource);
modbusSlaveMap.put(id, serverSource);
}
serverSource.getSourceIds().add(sourceId);
serverSource.getPollingIntervals().add(pollingMillis);
}
// collect all Proficy slave info
private void buildProficyHosts(EventResolver resolver) {
ProficySource eventSource = (ProficySource) resolver.getDataSource();
String tagName = resolver.getSourceId();
Integer pollingMillis = resolver.getUpdatePeriod();
String id = eventSource.getId();
PolledSource serverSource = proficyMap.get(id);
if (serverSource == null) {
if (logger.isInfoEnabled()) {
logger.info("Found Proficy historian specified for host " + eventSource.getHost());
}
serverSource = new PolledSource(eventSource);
proficyMap.put(id, serverSource);
}
serverSource.getSourceIds().add(tagName);
serverSource.getPollingIntervals().add(pollingMillis);
}
private void connectToRmqBrokers(Map<String, RmqBrokerSource> brokerSources) throws Exception {
for (Entry<String, RmqBrokerSource> entry : brokerSources.entrySet()) {
RmqSource source = entry.getValue().getSource();
RmqClient rmqClient = new RmqClient();
rmqClient.setShouldNotify(false);
String brokerHostName = source.getHost();
Integer brokerPort = source.getPort();
String brokerUser = source.getUserName();
String brokerPassword = source.getUserPassword();
// queue on each RMQ broker
String queueName = "EVT_" + getClass().getSimpleName() + "_" + System.currentTimeMillis();
List<RoutingKey> routingKeys = new ArrayList<>();
routingKeys.add(RoutingKey.EQUIPMENT_SOURCE_EVENT);
// also can accept commands
routingKeys.add(RoutingKey.COMMAND_MESSAGE);
rmqClient.startUp(brokerHostName, brokerPort, brokerUser, brokerPassword, queueName, routingKeys, this);
// add to context
appContext.getRmqClients().add(rmqClient);
if (logger.isInfoEnabled()) {
logger.info("Started RMQ event pubsub: " + source.getId());
}
}
}
private void connectToJmsBrokers(Map<String, JmsBrokerSource> brokerSources) throws Exception {
for (Entry<String, JmsBrokerSource> entry : brokerSources.entrySet()) {
JmsSource source = entry.getValue().getSource();
JmsClient jmsClient = new JmsClient();
jmsClient.setShouldNotify(false);
String brokerHostName = source.getHost();
Integer brokerPort = source.getPort();
String brokerUser = source.getUserName();
String brokerPassword = source.getUserPassword();
jmsClient.startUp(brokerHostName, brokerPort, brokerUser, brokerPassword, this);
// subscribe to event messages
jmsClient.consumeEvents(true);
// add to context
appContext.getJmsClients().add(jmsClient);
if (logger.isInfoEnabled()) {
logger.info("Started JMS client: " + source.getId());
}
}
}
private void connectToKafkaBrokers(Map<String, PolledSource> brokerSources) throws Exception {
for (Entry<String, PolledSource> entry : brokerSources.entrySet()) {
KafkaSource kafkaSource = (KafkaSource) entry.getValue().getEventSource();
// create the Kafka client
KafkaOeeClient kafkaClient = new KafkaOeeClient();
kafkaClient.setShouldNotify(false);
// connect to server
kafkaClient.createConsumer(kafkaSource, KafkaOeeClient.EVENT_TOPIC);
// add to context
appContext.getKafkaClients().add(kafkaClient);
// subscribe to event messages
kafkaClient.registerListener(this);
// find minimum polling interval
List<Integer> intervals = entry.getValue().getPollingIntervals();
int min = KafkaOeeClient.DEFAULT_POLLING_INTERVAL;
for (Integer interval : intervals) {
if (interval < min) {
min = interval;
}
}
kafkaClient.setPollingInterval(min);
kafkaClient.startPolling();
if (logger.isInfoEnabled()) {
logger.info("Started Kafka client: " + kafkaSource.getId());
}
}
}
private void connectToEmailServers(Map<String, PolledSource> emailServers) {
for (Entry<String, PolledSource> entry : emailServers.entrySet()) {
EmailSource emailSource = (EmailSource) entry.getValue().getEventSource();
// create the Email client
EmailClient emailClient = new EmailClient(emailSource);
emailClient.setShouldNotify(false);
// add to context
appContext.getEmailClients().add(emailClient);
// receive event messages
emailClient.registerListener(this);
// find minimum polling interval
List<Integer> intervals = entry.getValue().getPollingIntervals();
int min = EmailClient.DEFAULT_POLLING_INTERVAL;
for (Integer interval : intervals) {
if (interval < min) {
min = interval;
}
}
emailClient.setPollingInterval(min);
emailClient.startPolling();
if (logger.isInfoEnabled()) {
logger.info("Started Email client: " + emailSource.getId());
}
}
}
private void connectToMqttBrokers(Map<String, MqttBrokerSource> brokerSources) throws Exception {
for (Entry<String, MqttBrokerSource> entry : brokerSources.entrySet()) {
MqttSource source = entry.getValue().getSource();
MqttOeeClient mqttClient = new MqttOeeClient();
mqttClient.setShouldNotify(false);
mqttClient.setAuthenticationConfiguration(source.getUserName(), source.getUserPassword());
mqttClient.setSSLConfiguration(source.getKeystore(), source.getKeystorePassword(), source.getKeyPassword());
mqttClient.startUp(source.getHost(), source.getPort(), this);
mqttClient.subscribeToEvents(QualityOfService.EXACTLY_ONCE);
// add to context
appContext.getMqttClients().add(mqttClient);
if (logger.isInfoEnabled()) {
logger.info("Started MQTT client: " + source.getId());
}
}
}
private void connectToDatabaseServers(Map<String, PolledSource> dbSources) throws Exception {
for (Entry<String, PolledSource> entry : dbSources.entrySet()) {
// source of database interface table events
PolledSource databaseSource = entry.getValue();
DatabaseEventSource databaseEventSource = (DatabaseEventSource) databaseSource.getEventSource();
List<String> sourceIds = databaseSource.getSourceIds();
List<Integer> pollingIntervals = databaseSource.getPollingIntervals();
DatabaseEventClient dbClient = new DatabaseEventClient(this, databaseEventSource, sourceIds,
pollingIntervals);
dbClient.connectToServer(databaseEventSource.getId(), databaseEventSource.getUserName(),
databaseEventSource.getUserPassword());
// add to context
appContext.getDatabaseEventClients().add(dbClient);
dbClient.startPolling();
if (logger.isInfoEnabled()) {
logger.info("Polling interface table for database server: " + databaseEventSource.getId());
}
}
}
private void startModbusPolling(Map<String, PolledSource> slaveSources) throws Exception {
for (Entry<String, PolledSource> entry : slaveSources.entrySet()) {
// source of Modbus slave events
PolledSource slaveSource = entry.getValue();
ModbusSource modbusSource = (ModbusSource) slaveSource.getEventSource();
List<String> sourceIds = slaveSource.getSourceIds();
List<Integer> pollingIntervals = slaveSource.getPollingIntervals();
ModbusMaster modbusMaster = new ModbusMaster(this, modbusSource, sourceIds, pollingIntervals);
modbusMaster.connect();
// add to context
appContext.getModbusMasters().add(modbusMaster);
modbusMaster.startPolling();
if (logger.isInfoEnabled()) {
logger.info("Polling Modbus slave: " + modbusSource.getId());
}
}
}
private void startProficyPolling(Map<String, PolledSource> proficySources) {
for (Entry<String, PolledSource> entry : proficySources.entrySet()) {
// source of Proficy events
PolledSource polledSource = entry.getValue();
ProficySource proficySource = (ProficySource) polledSource.getEventSource();
List<String> sourceIds = polledSource.getSourceIds();
List<Integer> pollingIntervals = polledSource.getPollingIntervals();
ProficyClient proficyClient = new ProficyClient(this, proficySource, sourceIds, pollingIntervals);
// add to context
appContext.getProficyClients().add(proficyClient);
proficyClient.startPolling();
if (logger.isInfoEnabled()) {
logger.info("Polling Proficy historian: " + proficySource.getId());
}
}
}
private void startFilePolling(Map<String, PolledSource> fileSources) {
for (Entry<String, PolledSource> entry : fileSources.entrySet()) {
// source of file events
PolledSource fileSource = entry.getValue();
FileEventSource fileEventSource = (FileEventSource) fileSource.getEventSource();
List<String> sourceIds = fileSource.getSourceIds();
List<Integer> pollingIntervals = fileSource.getPollingIntervals();
FileEventClient fileClient = new FileEventClient(this, fileEventSource, sourceIds, pollingIntervals);
// add to context
appContext.getFileEventClients().add(fileClient);
if (logger.isInfoEnabled()) {
logger.info("Polling files on server: " + fileEventSource.getId());
}
fileClient.startPolling();
}
}
private void startCronSchedulers(Map<String, CronSource> cronSources) throws Exception {
for (Entry<String, CronSource> entry : cronSources.entrySet()) {
// source of file events
CronSource cronSource = entry.getValue();
CronEventSource cronEventSource = cronSource.getEventSource();
List<String> sourceIds = cronSource.getSourceIds();
List<String> expressions = cronSource.getCronExpressions();
CronEventClient cronClient = new CronEventClient(this, cronEventSource, sourceIds, expressions);
// add to context
appContext.getCronEventClients().add(cronClient);
cronClient.scheduleJobs();
if (logger.isInfoEnabled()) {
logger.info("Jobs scheduled on server: " + cronEventSource.getHost() + " for job "
+ cronEventSource.getName());
}
}
}
private void startHttpServers(Map<String, HttpServerSource> httpServerSources) throws Exception {
for (Entry<String, HttpServerSource> entry : httpServerSources.entrySet()) {
HttpSource source = entry.getValue().getSource();
Integer port = source.getPort();
Integer httpsPort = source.getHttpsPort();
if (logger.isInfoEnabled()) {
logger.info("Starting embedded HTTP server on HTTP port " + port + " and HTTPS port " + httpsPort);
}
OeeHttpServer httpServer = new OeeHttpServer(port);
if (httpsPort != null) {
httpServer.setHttpsPort(httpsPort);
}
OeeHttpServer.setDataChangeListener(this);
httpServer.setAcceptingEventRequests(true);
httpServer.startup();
// add to context
appContext.getHttpServers().add(httpServer);
if (logger.isInfoEnabled()) {
logger.info("Started HTTP server on port " + " and HTTPS port " + httpsPort);
}
}
}
private void buildOpcUaSubscriptions(EventResolver resolver) throws Exception {
OpcUaSource source = (OpcUaSource) resolver.getDataSource();
String url = source.getEndpointUrl();
OpcUaInfo uaInfo = opcUaSubscriptionMap.get(url);
if (uaInfo == null) {
uaInfo = new OpcUaInfo(source);
opcUaSubscriptionMap.put(url, uaInfo);
}
String nodeName = resolver.getSourceId();
NodeId monitoredNodeId = NodeId.parse(nodeName);
uaInfo.getMonitoredNodes().add(monitoredNodeId);
double publishingInterval = resolver.getUpdatePeriod().doubleValue();
if (publishingInterval < uaInfo.getPublishingInterval()) {
uaInfo.setPublishingInterval(publishingInterval);
}
}
public void subscribeToOpcUaSources(Map<String, OpcUaInfo> uaSubscriptions) throws Exception {
for (Entry<String, OpcUaInfo> entry : uaSubscriptions.entrySet()) {
OpcUaInfo uaInfo = entry.getValue();
UaOpcClient uaClient = new UaOpcClient();
uaClient.connect(uaInfo.getSource());
// add to context
appContext.getOpcUaClients().add(uaClient);
uaClient.registerAsynchListener(this);
double publishingInterval = uaInfo.getPublishingInterval();
for (NodeId monitoredNodeId : entry.getValue().getMonitoredNodes()) {
uaClient.subscribe(monitoredNodeId, publishingInterval, null);
}
}
}
private void buildOpcDaSubscriptions(EventResolver resolver) {
// equipment (group name)
String equipmentName = resolver.getEquipment().getName();
// OPC DA server
OpcDaSource daSource = (OpcDaSource) resolver.getDataSource();
// access path
String sourceId = resolver.getSourceId();
// get subscription
String mapKey = daSource.getHost() + "." + daSource.getProgId();
OpcDaInfo daSubscription = opcDaSubscriptionMap.get(mapKey);
if (daSubscription == null) {
daSubscription = new OpcDaInfo(daSource);
opcDaSubscriptionMap.put(mapKey, daSubscription);
}
// create the tag info
TagItemInfo tagItem = new TagItemInfo(sourceId);
Integer updatePeriod = resolver.getUpdatePeriod();
if (updatePeriod == null) {
updatePeriod = CollectorDataSource.DEFAULT_UPDATE_PERIOD_MSEC;
}
tagItem.setUpdatePeriod(updatePeriod);
daSubscription.addTagItem(equipmentName, tagItem);
if (logger.isInfoEnabled()) {
logger.info("Found subscription. Source: " + daSource + ", equipment: " + equipmentName + ", tag: "
+ tagItem.getPathName());
}
}
private void monitorOpcDaTags(Map<String, OpcDaInfo> daSubscriptions) throws Exception {
for (Entry<String, OpcDaInfo> entry : daSubscriptions.entrySet()) {
// connect to data source
OpcDaInfo subscribingClient = entry.getValue();
OpcDaSource daSource = subscribingClient.getSource();
DaOpcClient opcDaClient = new DaOpcClient();
opcDaClient.connect(daSource);
// put in context
appContext.getOpcDaClients().add(opcDaClient);
// subscribe to tags, one group per equipment
Map<String, List<TagItemInfo>> subscribedItems = entry.getValue().getSubscribedItems();
for (Entry<String, List<TagItemInfo>> tagsEntry : subscribedItems.entrySet()) {
// create OPC DA group, name = equipment
TagGroupInfo tagGroup = new TagGroupInfo(tagsEntry.getKey());
List<TagItemInfo> tagItems = tagsEntry.getValue();
for (TagItemInfo tagItem : tagItems) {
int period = tagItem.getUpdatePeriod();
if (period < tagGroup.getUpdatePeriod()) {
tagGroup.setUpdatePeriod(period);
if (logger.isInfoEnabled()) {
logger.info("Setting update period to " + tagGroup.getUpdatePeriod());
}
}
tagGroup.addTagItem(tagItem);
}
// register for data change events
OpcDaMonitoredGroup opcDaGroup = opcDaClient.registerTags(tagGroup, this);
// start monitoring
opcDaGroup.startMonitoring();
}
}
}
private void buildDataSources() throws Exception {
// query for configuration by host name or IP address
List<String> hostNames = new ArrayList<>(2);
hostNames.add(hostname);
hostNames.add(ip);
hostNames.add("localhost");
// fetch script resolvers from database
List<CollectorState> states = new ArrayList<>();
states.add(CollectorState.DEV);
states.add(CollectorState.READY);
states.add(CollectorState.RUNNING);
List<EventResolver> resolvers = PersistenceService.instance().fetchEventResolversByHost(hostNames, states);
if (resolvers.isEmpty() && logger.isWarnEnabled()) {
logger.warn("No resolvers found for hosts " + hostNames);
}
for (EventResolver resolver : resolvers) {
// build list of runnable collectors
DataCollector collector = resolver.getCollector();
// check if a specific collector is named
if (collectorName != null && !collector.getName().equals(collectorName)) {
if (logger.isInfoEnabled()) {
logger.info("Collector named " + collectorName + " is specified. Skipping collector "
+ collector.getName());
}
continue;
}
// add to our collectors
if (!collectors.contains(collector)) {
collectors.add(collector);
}
if (logger.isInfoEnabled()) {
logger.info("Found data collector '" + collector.getName() + "', for host " + collector.getHost()
+ " in state " + collector.getCollectorState() + ", resolver: " + resolver);
}
// gather data for each data source
if (!resolver.getCollector().getCollectorState().equals(CollectorState.DEV)) {
switch (resolver.getDataSource().getDataSourceType()) {
case OPC_DA: {
buildOpcDaSubscriptions(resolver);
break;
}
case HTTP: {
buildHttpServers(resolver);
break;
}
case OPC_UA: {
buildOpcUaSubscriptions(resolver);
break;
}
case RMQ: {
buildRMQBrokers(resolver);
break;
}
case JMS: {
buildJMSBrokers(resolver);
break;
}
case MQTT: {
buildMQTTBrokers(resolver);
break;
}
case DATABASE: {
buildDatabaseServers(resolver);
break;
}
case FILE: {
buildFileServers(resolver);
break;
}
case CRON: {
buildCronSchedulers(resolver);
break;
}
case MODBUS: {
buildModbusSlaves(resolver);
break;
}
case KAFKA: {
buildKafkaServers(resolver);
break;
}
case EMAIL: {
buildEmailHosts(resolver);
break;
}
case PROFICY: {
buildProficyHosts(resolver);
break;
}
default:
break;
}
}
}
}
public synchronized void startDataCollection() throws Exception {