-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKapuaClient.java
111 lines (93 loc) · 3.78 KB
/
KapuaClient.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
/*
* ******************************************************************************
* * Copyright (c) 2017 Arthur Deschamps
* *
* * All rights reserved. This program and the accompanying materials
* * are made available under the terms of the Eclipse Public License v1.0
* * which accompanies this distribution, and is available at
* * http://www.eclipse.org/legal/epl-v10.html
* *
* * Contributors:
* * Arthur Deschamps
* ******************************************************************************
*/
package communications.kapua;
import company.company.Company;
import org.eclipse.kapua.gateway.client.Application;
import org.eclipse.kapua.gateway.client.mqtt.paho.PahoClient;
import org.eclipse.kapua.gateway.client.profile.kura.KuraMqttProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import simulation.main.Parametrizer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static org.eclipse.kapua.gateway.client.Credentials.userAndPassword;
import static org.eclipse.kapua.gateway.client.Transport.waitForConnection;
/**
* Interface and communicate with Kapua
* @author Arthur Deschamps
* @since 1.0
*/
public class KapuaClient {
private Company company;
private Parametrizer parametrizer;
private org.eclipse.kapua.gateway.client.Client client;
private Application application;
private int communicationsDelay;
private final int port = 1883;
private final String host = "localhost";
private final String accountName = "kapua-sys";
private final String applicationId = "kapua-iot-gateway-simulation-scm";
private final String clientId = "supply-chain-control-simulation";
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private static final Logger logger = LoggerFactory.getLogger(KapuaClient.class);
public KapuaClient(Company company, Parametrizer parametrizer) {
this.parametrizer = parametrizer;
this.company = company;
this.communicationsDelay = parametrizer.getDataSendingDelay();
try {
client = KuraMqttProfile.newProfile(PahoClient.Builder::new)
.accountName(accountName)
.clientId(clientId)
.brokerUrl("tcp://"+host+":"+Integer.toString(port))
.credentials(userAndPassword(accountName, "kapua-password"))
.build();
} catch (Exception e) {
logger.error(e.getMessage());
}
application = client.buildApplication(applicationId).build();
}
/**
* Starts sending data to kapua.
**/
public void startCommunications() {
try {
// Wait for connection
waitForConnection(application.transport());
// Start sending data
ScheduledFuture<?> send = executor.scheduleWithFixedDelay(
new DataSenderRunner(company,application),
0,
communicationsDelay,
TimeUnit.SECONDS
);
checkParametrizer(send);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
private void checkParametrizer(ScheduledFuture<?> send) {
executor.schedule(() -> {
if (communicationsDelay != parametrizer.getDataSendingDelay()) {
logger.info("Data sending delay changed");
communicationsDelay = parametrizer.getDataSendingDelay();
send.cancel(false);
startCommunications();
} else {
checkParametrizer(send);
}
}, 5, TimeUnit.SECONDS);
}
}