Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<classpathentry kind="lib" path="lib/jna-4.1.0.jar"/>
<classpathentry kind="lib" path="lib/jna-platform-4.1.0.jar"/>
<classpathentry kind="lib" path="lib/gson-2.2.4.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.iotf.client-0.0.3.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.iotf.client-0.0.3.jar" sourcepath="C:/Users/fstanell/Projects/ibm iot/iot-java"/>
<classpathentry kind="lib" path="lib/graphhopper-0.4-20150128.170053-90.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.paho.client.mqttv3-1.0.2.jar"/>
<classpathentry kind="lib" path="lib/org.eclipse.paho.mqtt.utility-1.0.2.jar"/>
Expand Down
3 changes: 3 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/com/
/maps/
/devices/
4 changes: 2 additions & 2 deletions bin/admin.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group.name = simulator
nr.of.cars = 5
nr.of.ambulances = 3
nr.of.cars = 4
nr.of.ambulances = 4
6 changes: 3 additions & 3 deletions bin/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
api-key = a-0yngjl-qd4wocrack
api-token = eA5HT)4ccFU2dULL7A
orga-id = 0yngjl
api-key = a-5rcpia-hxwn2ipopc
api-token = tSzH3*7wou5pLfZd4e
orga-id = 5rcpia
devices = devices/test-devices.json
Binary file not shown.
Binary file modified bin/com/capgemini/hackathon/device/simulation/bo/Ambulance.class
Binary file not shown.
Binary file modified bin/com/capgemini/hackathon/device/simulation/bo/Car.class
Binary file not shown.
Binary file not shown.
Binary file modified bin/com/capgemini/hackathon/device/simulation/bo/Hospital.class
Binary file not shown.
Binary file modified bin/com/capgemini/hackathon/device/simulation/bo/Simulation.class
Binary file not shown.
Binary file modified bin/com/capgemini/hackathon/device/simulation/bo/Vehicle.class
Binary file not shown.
146 changes: 73 additions & 73 deletions bin/devices/test-devices.json
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
[{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car0",
"orgId": "0yngjl",
"clientId": "d:0yngjl:car-simulator:car0",
"typeId": "car-simulator",
"deviceId": "car0",
"apiToken": "E3BF69B9-C5B6-41AE-A5AB-70A35F583E64"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car1",
"orgId": "0yngjl",
"clientId": "d:0yngjl:car-simulator:car1",
"typeId": "car-simulator",
"deviceId": "car1",
"apiToken": "6CFA16A6-210F-4CDD-95FA-7C3158ED9C91"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car2",
"orgId": "0yngjl",
"clientId": "d:0yngjl:car-simulator:car2",
"typeId": "car-simulator",
"deviceId": "car2",
"apiToken": "F36A8081-11BD-4088-A1A2-0F72069B44F8"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car3",
"orgId": "0yngjl",
"clientId": "d:0yngjl:car-simulator:car3",
"typeId": "car-simulator",
"deviceId": "car3",
"apiToken": "BFFAE321-AE51-4A3C-BDE9-5F992A653C7C"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car4",
"orgId": "0yngjl",
"clientId": "d:0yngjl:car-simulator:car4",
"typeId": "car-simulator",
"deviceId": "car4",
"apiToken": "D2340AED-C527-4DED-97EB-31EEF58CBFAB"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance0",
"orgId": "0yngjl",
"clientId": "d:0yngjl:ambulance-simulator:ambulance0",
"typeId": "ambulance-simulator",
"deviceId": "ambulance0",
"apiToken": "FF7B5C52-98AA-4242-A110-44F306263DC5"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance1",
"orgId": "0yngjl",
"clientId": "d:0yngjl:ambulance-simulator:ambulance1",
"typeId": "ambulance-simulator",
"deviceId": "ambulance1",
"apiToken": "A27E0DA5-B118-4E2E-8200-3F06112ECD64"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance2",
"orgId": "0yngjl",
"clientId": "d:0yngjl:ambulance-simulator:ambulance2",
"typeId": "ambulance-simulator",
"deviceId": "ambulance2",
"apiToken": "41B2ED2B-2E05-44ED-A46A-0DA82CB5592C"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Hospital",
"id": "hospital",
"orgId": "0yngjl",
"clientId": "d:0yngjl:hospital:hospital1",
"typeId": "hospital",
"deviceId": "hospital1",
"apiToken": "FAD566AC-4C27-43E8-8DF8-EEA6576DAFFB"
}]
[{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car0",
"orgId": "5rcpia",
"clientId": "d:5rcpia:car-simulator:car0",
"typeId": "car-simulator",
"deviceId": "car0",
"apiToken": "01562EAC-5459-498E-9B1C-8DA4F37D074C"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car1",
"orgId": "5rcpia",
"clientId": "d:5rcpia:car-simulator:car1",
"typeId": "car-simulator",
"deviceId": "car1",
"apiToken": "1719BB4F-99A0-42D0-8FF2-4A76BC64EAFD"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car2",
"orgId": "5rcpia",
"clientId": "d:5rcpia:car-simulator:car2",
"typeId": "car-simulator",
"deviceId": "car2",
"apiToken": "E3161649-B59E-4EC9-97E6-2A4711F7DB43"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Car",
"id": "car3",
"orgId": "5rcpia",
"clientId": "d:5rcpia:car-simulator:car3",
"typeId": "car-simulator",
"deviceId": "car3",
"apiToken": "451326E3-BC56-4D78-8CD3-70B669F830F8"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance0",
"orgId": "5rcpia",
"clientId": "d:5rcpia:ambulance-simulator:ambulance0",
"typeId": "ambulance-simulator",
"deviceId": "ambulance0",
"apiToken": "9DBB21F6-B888-4B36-8379-3F8C91912246"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance1",
"orgId": "5rcpia",
"clientId": "d:5rcpia:ambulance-simulator:ambulance1",
"typeId": "ambulance-simulator",
"deviceId": "ambulance1",
"apiToken": "BC0CD3FD-F67E-4F23-945F-E805165D5590"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance2",
"orgId": "5rcpia",
"clientId": "d:5rcpia:ambulance-simulator:ambulance2",
"typeId": "ambulance-simulator",
"deviceId": "ambulance2",
"apiToken": "AF289EC5-EE47-4458-AA3E-0A4ACB7EC058"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Ambulance",
"id": "ambulance3",
"orgId": "5rcpia",
"clientId": "d:5rcpia:ambulance-simulator:ambulance3",
"typeId": "ambulance-simulator",
"deviceId": "ambulance3",
"apiToken": "CEC839DD-F3D4-4E97-889E-FBA8D3E0BBC7"
},{
"simulatorClazz": "com.capgemini.hackathon.device.simulation.bo.Hospital",
"id": "hospital",
"orgId": "5rcpia",
"clientId": "d:5rcpia:hospital:hospital1",
"typeId": "hospital",
"deviceId": "hospital1",
"apiToken": "48E34C00-8A32-4C8B-93AC-6C38FE642A46"
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package com.capgemini.hackathon.device.service;

import java.util.Properties;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.charset.Charset;
import java.util.Date;

import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.iotf.client.api.APIClient;
import com.ibm.iotf.client.device.Command;
import com.ibm.iotf.client.device.CommandCallback;
import com.ibm.iotf.client.device.DeviceClient;
import com.ibm.iotf.util.LoggerUtility;

public class DeviceClientCustom extends DeviceClient{

/**
* Overrided version of DeviceClient to change the JSON Messages
* to format Geospatial needs.
*/

private static final String CLASS_NAME = DeviceClientCustom.class.getName();

private static final Pattern COMMAND_PATTERN = Pattern.compile("iot-2/cmd/(.+)/fmt/(.+)");

private CommandCallback commandCallback = null;

private APIClient apiClient;

public DeviceClientCustom(MqttAsyncClient arg0) {
super(arg0);

}

public DeviceClientCustom(MqttClient arg0) {
super(arg0);

}

public DeviceClientCustom(Properties options) throws Exception {
super(options);
LoggerUtility.fine(CLASS_NAME, "DeviceClientCustom", "options = " + options);
this.clientId = "d" + CLIENT_ID_DELIMITER + getOrgId() + CLIENT_ID_DELIMITER + getDeviceType() + CLIENT_ID_DELIMITER + getDeviceId();

if (getAuthMethod() == null) {
this.clientUsername = null;
this.clientPassword = null;
}
else if (!getAuthMethod().equals("token")) {
throw new Exception("Unsupported Authentication Method: " + getAuthMethod());
}
else {
// use-token-auth is the only authentication method currently supported
this.clientUsername = "use-token-auth";
this.clientPassword = getAuthToken();
}
createClient(this.new MqttDeviceCallBack());

options.setProperty("auth-method", "device");

apiClient = new APIClient(options);
}

public String getDeviceType() {
String type;
type = options.getProperty("type");
if(type == null) {
type = options.getProperty("Device-Type");
}
return trimedValue(type);
}

public String getFormat() {
String format = options.getProperty("format");
if(format != null && ! format.equals(""))
return format;
else
return "json";

}


private void subscribeToCommands() {
try {
mqttAsyncClient.subscribe("iot-2/cmd/+/fmt/+", 2);
} catch (MqttException e) {
e.printStackTrace();
}
}

public APIClient api() {
return this.apiClient;
}

public boolean publishEvent(String event, Object data) {
return publishEvent(event, data, 0);
}



@Override
public boolean publishEvent(String event, Object data, int qos) {
if (!isConnected()) {
return false;
}
final String METHOD = "publishEvent(2)";
JsonObject payload = new JsonObject();

String timestamp = ISO8601_DATE_FORMAT.format(new Date());
payload.addProperty("ts", timestamp);

// Handle null object
if(data == null) {
data = new JsonObject();
}

JsonElement dataElement = gson.toJsonTree(data);
payload.add("d", dataElement);

String topic = "iot-2/evt/" + event + "/fmt/json";

JsonObject dataJson = (JsonObject) data;
payload.add("latitude", dataJson.get("latitude"));
payload.add("longitude", dataJson.get("longitude"));
payload.add("vin", dataJson.get("vin"));

MqttMessage msg = new MqttMessage(payload.toString().getBytes(Charset.forName("UTF-8")));
msg.setQos(qos);
msg.setRetained(false);

try {
mqttAsyncClient.publish(topic, msg).waitForCompletion();
} catch (MqttPersistenceException e) {
e.printStackTrace();
return false;
} catch (MqttException e) {
e.printStackTrace();
return false;
}
return true;
}

private class MqttDeviceCallBack implements MqttCallbackExtended {

/**
* If we lose connection trigger the connect logic to attempt to
* reconnect to the IBM Watson IoT Platform.
*
* @param exception
* Throwable which caused the connection to get lost
*/
public void connectionLost(Throwable exception) {
final String METHOD = "connectionLost";
LoggerUtility.log(Level.SEVERE, CLASS_NAME, METHOD, exception.getMessage());
try {
reconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* A completed deliver does not guarantee that the message is received by the service
* because devices send messages with Quality of Service (QoS) 0. <br>
*
* The message count
* represents the number of messages that were sent by the device without an error on
* from the perspective of the device.
* @param token
* MQTT delivery token
*/
public void deliveryComplete(IMqttDeliveryToken token) {
final String METHOD = "deliveryComplete";
LoggerUtility.fine(CLASS_NAME, METHOD, "token " + token.getMessageId());
messageCount++;
}

/**
* The Device client does not currently support subscriptions.
*/
public void messageArrived(String topic, MqttMessage msg) throws Exception {
final String METHOD = "messageArrived";
if (commandCallback != null) {
/* Only check whether the message is a command if a callback
* has been defined, otherwise it is a waste of time
* as without a callback there is nothing to process the generated
* command.
*/
Matcher matcher = COMMAND_PATTERN.matcher(topic);
if (matcher.matches()) {
String command = matcher.group(1);
String format = matcher.group(2);
Command cmd = new Command(command, format, msg);
LoggerUtility.fine(CLASS_NAME, METHOD, "Event received: " + cmd.toString());
commandCallback.processCommand(cmd);
}
}
}

@Override
public void connectComplete(boolean reconnect, String serverURI) {
// TODO Auto-generated method stub

}

}

public void setCommandCallback(CommandCallback callback) {
this.commandCallback = callback;
}
}
Loading