9
9
10
10
import com .orange .lo .sample .lo2iothub .azure .IotHubAdapter ;
11
11
import com .orange .lo .sample .lo2iothub .exceptions .DeviceSynchronizationException ;
12
+ import com .orange .lo .sample .lo2iothub .exceptions .IotDeviceProviderException ;
12
13
import com .orange .lo .sample .lo2iothub .lo .LoAdapter ;
13
14
import com .orange .lo .sample .lo2iothub .utils .Counters ;
14
15
import com .orange .lo .sdk .fifomqtt .DataManagementFifoCallback ;
@@ -32,14 +33,17 @@ public class MessageHandler implements DataManagementFifoCallback {
32
33
private static final Logger LOG = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
33
34
34
35
private final IotHubAdapter iotHubAdapter ;
35
- private final LoAdapter loAdapter ;
36
+ private LoAdapter loAdapter ;
36
37
private final Counters counterProvider ;
37
38
38
- public MessageHandler (IotHubAdapter iotHubAdapter , LoAdapter loAdapter , Counters counterProvider ) {
39
+ public MessageHandler (IotHubAdapter iotHubAdapter , Counters counterProvider ) {
39
40
this .iotHubAdapter = iotHubAdapter ;
40
- this .loAdapter = loAdapter ;
41
41
this .counterProvider = counterProvider ;
42
42
}
43
+ public void setLoAdapter (LoAdapter loAdapter ) {
44
+ this .loAdapter = loAdapter ;
45
+ }
46
+
43
47
44
48
@ Override
45
49
public void onMessage (int loMessageId , String message ) {
@@ -81,29 +85,37 @@ private void handleDataMessage(int loMessageId, String message) {
81
85
}
82
86
83
87
private void handleDeviceCreationEvent (int loMessageId , String message ) {
84
- Optional <String > deviceId = getDeviceId (message );
85
- deviceId .ifPresent (iotHubAdapter ::createOrGetDeviceClientManager );
86
- loAdapter .sendMessageAck (loMessageId );
88
+ try {
89
+ String deviceId = getDeviceId (message );
90
+ iotHubAdapter .createOrGetDeviceClientManager (deviceId );
91
+ } catch (JSONException e ) {
92
+ LOG .error ("Cannot create device because of no device id in payload" );
93
+ } catch (Exception e ) {
94
+ LOG .error ("Cannot create device" , e );
95
+ } finally {
96
+ loAdapter .sendMessageAck (loMessageId );
97
+ }
87
98
}
88
99
89
100
private void handleDeviceRemovalEvent (int loMessageId , String message ) {
90
- Optional <String > deviceId = getDeviceId (message );
91
- deviceId .ifPresent (iotHubAdapter ::deleteDevice );
92
- loAdapter .sendMessageAck (loMessageId );
101
+ try {
102
+ String deviceId = getDeviceId (message );
103
+ iotHubAdapter .deleteDevice (deviceId );
104
+ } catch (JSONException e ) {
105
+ LOG .error ("Cannot delete device because of no device id in payload" );
106
+ } catch (Exception e ) {
107
+ LOG .error ("Cannot delete device" , e );
108
+ } finally {
109
+ loAdapter .sendMessageAck (loMessageId );
110
+ }
93
111
}
94
112
95
113
private static String getSourceDeviceId (String msg ) throws JSONException {
96
114
return new JSONObject (msg ).getJSONObject ("metadata" ).getString ("source" );
97
115
}
98
116
99
- private static Optional <String > getDeviceId (String msg ) {
100
- String id = null ;
101
- try {
102
- id = new JSONObject (msg ).getString (DEVICE_ID_FIELD );
103
- } catch (JSONException e ) {
104
- LOG .error ("No device id in payload" );
105
- }
106
- return Optional .ofNullable (id );
117
+ private static String getDeviceId (String msg ) throws JSONException {
118
+ return new JSONObject (msg ).getString (DEVICE_ID_FIELD );
107
119
}
108
120
109
121
private static String getMessageType (String msg ) {
0 commit comments