-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputHandler.java
195 lines (162 loc) · 5.97 KB
/
OutputHandler.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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQSslConnectionFactory;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OutputHandler extends Thread {
private MqttClient sampleClient = null;
private ArrayList<String> buffer = new ArrayList<String>();
private boolean sizeExceded = false;
private String bufferFileName = "dataFiles/output";
private long limit = 100000;
private int oldestFileIndex = 0;
private int currentFileIndex = 0;
private int noOfFiles;
private final Logger logger = LoggerFactory.getLogger(OutputHandler.class);
private String op = "";
private long fileOpCOunter = 0;
private Date ty = null;
private String topic;
public OutputHandler() {
super();
start();
this.topic = PropertyFetcher.getValue("topic");
this.noOfFiles = Integer.parseInt(PropertyFetcher.getValue("sizeInMB"))/9;
}
private void writeToFile() {
System.out.println("Writting to file");
if (ty!=null)
System.out.println(new Date().getTime() - ty.getTime());
ty = new Date();
this.fileOpCOunter+=1;
FileWriter fw;
try {
fw = new FileWriter(this.bufferFileName+this.currentFileIndex+".txt",false);
this.buffer.forEach((item)->{
try {
fw.write(item);
fw.write("\n");
}catch(IOException ioe){
logger.error("Failed to write into buffer file " + ioe.getMessage());
}});
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
this.currentFileIndex= (this.currentFileIndex+1)%this.noOfFiles;
}
private ArrayList<String> getFromFile() {
if (this.fileOpCOunter == 0) {
this.sizeExceded = false;
}
this.fileOpCOunter-=1;
File bufferFile= new File(this.bufferFileName+this.oldestFileIndex+".txt");
BufferedReader reader;
ArrayList<String> executionBuffer = new ArrayList<String>();
String line;
try {
reader = new BufferedReader(new FileReader(bufferFile));
while ((line = reader.readLine())!= null) {
executionBuffer.add(line);
}
reader.close();
} catch (Exception e) {
logger.error("Failed to get from buffer file " + e.getMessage());
}
this.oldestFileIndex = (this.oldestFileIndex+1)%this.noOfFiles;
return executionBuffer;
}
public void publishMessage(UaMonitoredItem item, DataValue value) {
String content = new JsonResponse("" + item.getReadValueId().getNodeId().getIdentifier(),
"" + value.getValue().getValue(), item.getStatusCode().isGood(), value.getServerTime().getJavaTime())
.toJson();
if (this.buffer.size() >= this.limit) {
this.writeToFile();
this.sizeExceded = true;
this.buffer = new ArrayList<String>();
}
this.buffer.add(content);
}
public void run() {
ArrayList<String> executionBuffer = new ArrayList<String>();
while(true) {
int qos = 2;
try {
if (this.sampleClient == null || !this.sampleClient.isConnected()) {
String broker = PropertyFetcher.getValue("sslUrl");
String clientId = "/vroc/";
this.sampleClient = new MqttClient(broker, clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setSocketFactory(SecureSocketUtil.getSocketFactory());
sampleClient.connect(connOpts);
logger.info("Connected to broker: " + broker);
}
if (this.sizeExceded)
executionBuffer = this.getFromFile();
else {
executionBuffer = new ArrayList<String>(this.buffer);
this.buffer = new ArrayList<String>();
}
if(executionBuffer.size()<=0)
continue;
op = String.join(", ", executionBuffer);
MqttMessage message = new MqttMessage(op.getBytes());
message.setQos(qos);
message.setRetained(true);
try {
sampleClient.publish(this.topic, message);
} catch (MqttPersistenceException e) {
logger.warn("Failed to send message " + e.getMessage());
e.printStackTrace();
} catch (MqttException e) {
logger.warn("Failed to send message " + e.getMessage());
e.printStackTrace();
}
logger.info("Message block with size {} published",executionBuffer.size());
} catch (MqttException me) {
logger.warn("Failed to send message " + me.getMessage());
}
}}
private static void connectAndPublishInSSL() {
ArrayList<String> executionBuffer = new ArrayList<String>();
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory("ssl://localhost:8883");
try {
javax.jms.Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.start();
Destination destination = session.createTopic(".vroc.q");
//session.createQueue("/vroc/q"); - Incase we need queue
MessageProducer producer = session.createProducer(destination);
if (OutputHandler.sizeExceded) {
executionBuffer = OutputHandler.getFromFile();
} else
executionBuffer = new ArrayList<String>(OutputHandler.buffer);
executionBuffer.forEach((c) -> {
try {
producer.send(session.createTextMessage(c));
} catch (JMSException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}