This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AgentDaemon.java
94 lines (84 loc) · 4.48 KB
/
AgentDaemon.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
package no.entra.bacnet.agent;
import no.entra.bacnet.agent.devices.DeviceIdRepository;
import no.entra.bacnet.agent.devices.DeviceIdService;
import no.entra.bacnet.agent.devices.InMemoryDeviceIdRepository;
import no.entra.bacnet.agent.importer.DeviceImporter;
import no.entra.bacnet.agent.mqtt.MqttClient;
import no.entra.bacnet.agent.mqtt.PahoMqttClient;
import no.entra.bacnet.agent.observer.BacnetObserver;
import no.entra.bacnet.agent.observer.BlockingRecordAndForwardObserver;
import no.entra.bacnet.agent.recording.BacnetHexStringRecorder;
import no.entra.bacnet.agent.recording.FileBacnetHexStringRecorder;
import org.slf4j.Logger;
import java.io.File;
import static no.entra.bacnet.agent.mqtt.AzureIoTMqttClient.DEVICE_CONNECTION_STRING;
import static no.entra.bacnet.agent.mqtt.PahoMqttClient.*;
import static no.entra.bacnet.agent.utils.PropertyReader.findProperty;
import static no.entra.bacnet.agent.utils.PropertyReader.isEmpty;
import static org.slf4j.LoggerFactory.getLogger;
public class AgentDaemon {
private static final Logger log = getLogger(AgentDaemon.class);
private static final int WAIT_DEVICES_TO_REPORT_IN_SEC = 10;
public static void main(String[] args) {
try {
String path = "bacnet-hexstring-recording.log";
File recordingFile = new File(path);
BacnetHexStringRecorder hexStringRecorder = new FileBacnetHexStringRecorder(recordingFile);
boolean connectToMqtt = true;
boolean findDeviceInfo = true;
MqttClient mqttClient = null;
if (connectToMqtt) {
// String deviceConnectionString = findConnectionString(args);
// mqttClient = new AzureIoTMqttClient(deviceConnectionString);
String brokerUrl = findProperty(MQTT_BROKER_URL);
String username = findProperty(MQTT_USERNAME);
String password = findProperty(MQTT_PASSWORD);
String topic = findProperty(MQTT_TOPIC);
String clientId = findProperty(MQTT_CLIENT_ID);
mqttClient = new PahoMqttClient(brokerUrl,username,password,topic, clientId);
}
DeviceIdRepository deviceIdRepository = new InMemoryDeviceIdRepository();
DeviceIdService deviceIdService = new DeviceIdService(deviceIdRepository);
BacnetObserver bacnetObserver = new BlockingRecordAndForwardObserver(hexStringRecorder, mqttClient, deviceIdService);
UdpServer udpServer = new UdpServer(bacnetObserver);
udpServer.setListening(true);
udpServer.setRecording(true);
udpServer.start();
if (findDeviceInfo) {
DeviceImporter deviceImporter = new DeviceImporter(deviceIdService);
try {
deviceImporter.findDevices();
log.info("Running...find devices.");
log.trace("Wait {} seconds for devices to report in.", WAIT_DEVICES_TO_REPORT_IN_SEC);
Thread.sleep(WAIT_DEVICES_TO_REPORT_IN_SEC * 1000);
log.info("Find Sensors and Properties each Device Supports. ");
deviceImporter.findSensorsAndPropertiesTheDevicesSupports();
log.info("Find information about each Sensor and Property configuration. ");
deviceImporter.findSensorAndPropertiesConfiguration();
log.info("Find Present Value for each sensor.");
deviceImporter.findPresentValueForSensors();
} catch (Exception e) {
log.info("Exception trying to run scheduled imports of trendIds. Reason: {}", e.getMessage(), e);
}
//TODO re-enable schedler
// ScheduledDeviceImporter scheduledDeviceImporter = new ScheduledDeviceImporter(oneTimeDeviceImporter);
// scheduledDeviceImporter.startScheduledImport();
}
} catch (Exception e) {
log.error("Failed to run udpServer.", e);
}
}
private static String findConnectionString(String[] args) {
String deviceConnectionString = findProperty(DEVICE_CONNECTION_STRING);
if (deviceConnectionString == null) {
if (args.length > 0) {
deviceConnectionString = args[0];
}
}
if (isEmpty(deviceConnectionString)) {
log.error("Missing required property: DEVICE_CONNECTION_STRING, exiting ");
System.exit(1);
}
return deviceConnectionString;
}
}