24
24
import org .slf4j .Logger ;
25
25
import org .slf4j .LoggerFactory ;
26
26
27
+ import com .thefirstlineofcode .basalt .oxm .binary .BinaryUtils ;
27
28
import com .thefirstlineofcode .basalt .xmpp .Constants ;
28
29
import com .thefirstlineofcode .basalt .xmpp .core .ProtocolException ;
29
30
import com .thefirstlineofcode .basalt .xmpp .core .stanza .error .FeatureNotImplemented ;
40
41
import com .thefirstlineofcode .sand .client .ibtr .RegistrationException ;
41
42
import com .thefirstlineofcode .sand .client .thing .AbstractThing ;
42
43
import com .thefirstlineofcode .sand .protocols .actuator .ExecutionException ;
43
- import com .thefirstlineofcode .sand .protocols .thing .ThingIdentity ;
44
+ import com .thefirstlineofcode .sand .protocols .thing .RegisteredThing ;
44
45
45
46
public abstract class AbstractEdgeThing extends AbstractThing implements IEdgeThing , IConnectionListener {
46
47
private static final String ATTRIBUTE_NAME_STREAM_CONFIG = "stream_config" ;
47
- private static final String ATTRIBUTE_NAME_THING_IDENTITY = "thing_identity " ;
48
+ private static final String ATTRIBUTE_NAME_REGISTERED_THING = "registered_thing " ;
48
49
private static final String INTERNET_CONNECTIVITY_TEST_ADDRESS = "http://www.baidu.com" ;
49
50
private static final String SAND_EDGE_CONFIG_DIR = ".com.thefirstlineofcode.sand.client.edge" ;
50
51
51
52
private static final Logger logger = LoggerFactory .getLogger (AbstractEdgeThing .class );
52
53
53
54
protected StandardStreamConfig streamConfig ;
54
- protected ThingIdentity identity ;
55
+ protected RegisteredThing registeredThing ;
55
56
56
57
protected StandardChatClient chatClient ;
57
58
protected Thread autoReconnectThread ;
@@ -81,6 +82,7 @@ public AbstractEdgeThing(String model, StandardStreamConfig streamConfig, boolea
81
82
super (model );
82
83
83
84
this .streamConfig = streamConfig ;
85
+ this .startConsole = startConsole ;
84
86
85
87
powered = true ;
86
88
batteryPower = 100 ;
@@ -91,18 +93,17 @@ public AbstractEdgeThing(String model, StandardStreamConfig streamConfig, boolea
91
93
started = false ;
92
94
stopToReconnect = true ;
93
95
94
- this . startConsole = startConsole ;
96
+ processAttributes ( attributes ) ;
95
97
}
96
98
97
99
private String getStreamConfigString () {
98
100
return String .format ("%s,%s,%s" , streamConfig .getHost (), streamConfig .getPort (), streamConfig .isTlsPreferred () ? "true" : "false" );
99
101
}
100
-
102
+
101
103
protected StandardStreamConfig getStreamConfig (Map <String , String > attributes ) {
102
104
String sStreamConfig = attributes .get (ATTRIBUTE_NAME_STREAM_CONFIG );
103
105
if (sStreamConfig == null ) {
104
- logger .error ("Can't read stream config. Null stream config string." );
105
- throw new IllegalArgumentException ("Can't read stream config. Null stream config string." );
106
+ return null ;
106
107
}
107
108
108
109
StringTokenizer st = new StringTokenizer (sStreamConfig , "," );
@@ -112,7 +113,7 @@ protected StandardStreamConfig getStreamConfig(Map<String, String> attributes) {
112
113
}
113
114
114
115
StandardStreamConfig streamConfig = createStreamConfig (st );
115
- streamConfig .setResource (ThingIdentity .DEFAULT_RESOURCE_NAME );
116
+ streamConfig .setResource (RegisteredThing .DEFAULT_RESOURCE_NAME );
116
117
117
118
return streamConfig ;
118
119
}
@@ -129,12 +130,15 @@ protected StandardStreamConfig createStreamConfig(StringTokenizer st) {
129
130
public StandardStreamConfig getStreamConfig () {
130
131
return streamConfig ;
131
132
}
133
+
134
+ @ Override
135
+ public void setStreamConfig (StandardStreamConfig streamConfig ) {
136
+ this .streamConfig = streamConfig ;
137
+ }
132
138
133
139
@ Override
134
140
public void start () {
135
141
try {
136
- processAttributes (attributes );
137
-
138
142
doStart ();
139
143
} catch (Exception e ) {
140
144
logger .error ("Some thing is wrong. The program can't run correctly." , e );
@@ -143,24 +147,21 @@ public void start() {
143
147
}
144
148
}
145
149
146
- private void processAttributes (Map <String , String > attributes ) {
147
- if (this . streamConfig == null ) {
148
- this . streamConfig = getStreamConfig (attributes );
150
+ protected void processAttributes (Map <String , String > attributes ) {
151
+ if (streamConfig == null ) {
152
+ streamConfig = getStreamConfig (attributes );
149
153
} else {
150
154
attributes .put (ATTRIBUTE_NAME_STREAM_CONFIG , getStreamConfigString ());
151
155
attributesChanged = true ;
152
156
}
153
157
154
- identity = getThingIdentity (attributes );
158
+ registeredThing = getRegisteredThing (attributes );
155
159
156
160
if (doProcessAttributes (attributes ))
157
161
attributesChanged = true ;
158
162
159
163
if (attributesChanged )
160
164
saveAttributes (attributes );
161
-
162
- logger .info ("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}']." ,
163
- thingId , this .streamConfig .getHost (), this .streamConfig .getPort (), this .streamConfig .isTlsPreferred ());
164
165
}
165
166
166
167
protected void doStart () {
@@ -170,6 +171,12 @@ protected void doStart() {
170
171
if (started )
171
172
stop ();
172
173
174
+ if (streamConfig == null )
175
+ throw new IllegalStateException ("Null stream config." );
176
+
177
+ logger .info ("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}']." ,
178
+ thingId , this .streamConfig .getHost (), this .streamConfig .getPort (), this .streamConfig .isTlsPreferred ());
179
+
173
180
if (!isHostLocalLanAddress ()) {
174
181
checkInternetConnectivity (10 );
175
182
}
@@ -307,8 +314,8 @@ public void connect() {
307
314
logger .info ("The thing tries to connect to server." );
308
315
309
316
try {
310
- chatClient .connect (new UsernamePasswordToken (identity .getThingName ().toString (),
311
- identity .getCredentials ()));
317
+ chatClient .connect (new UsernamePasswordToken (registeredThing .getThingName ().toString (),
318
+ registeredThing .getCredentials ()));
312
319
313
320
if (isConnected ()) {
314
321
for (IEdgeThingListener edgeThingListener : edgeThingListeners ) {
@@ -352,7 +359,7 @@ protected StandardChatClient createChatClient() {
352
359
protected StandardStreamConfig createStreamConfigWithResource () {
353
360
StandardStreamConfig cloned = new StandardStreamConfig (streamConfig .getHost (), streamConfig .getPort ());
354
361
cloned .setTlsPreferred (streamConfig .isTlsPreferred ());
355
- cloned .setResource (ThingIdentity .DEFAULT_RESOURCE_NAME );
362
+ cloned .setResource (RegisteredThing .DEFAULT_RESOURCE_NAME );
356
363
357
364
return cloned ;
358
365
}
@@ -368,13 +375,13 @@ protected void startAutoReconnectThread() {
368
375
autoReconnectThread .start ();
369
376
}
370
377
371
- protected void registered (ThingIdentity identity ) {
372
- attributes .put (ATTRIBUTE_NAME_THING_IDENTITY , getThingIdentityString ( identity ));
378
+ protected void registered (RegisteredThing registeredThing ) {
379
+ attributes .put (ATTRIBUTE_NAME_REGISTERED_THING , getRegisteredThingString ( registeredThing ));
373
380
saveAttributes (attributes );
374
381
375
- this .identity = identity ;
382
+ this .registeredThing = registeredThing ;
376
383
377
- logger .info ("The thing has registered. Thing name is '{}'." , identity .getThingName ());
384
+ logger .info ("The thing has registered. Thing name is '{}'." , registeredThing .getThingName ());
378
385
}
379
386
380
387
@ Override
@@ -431,7 +438,7 @@ protected void stopAutoReconnectThread() {
431
438
432
439
@ Override
433
440
public boolean isRegistered () {
434
- return identity != null ;
441
+ return registeredThing != null ;
435
442
}
436
443
437
444
@ Override
@@ -449,13 +456,13 @@ public void register() {
449
456
}
450
457
registration .addConnectionListener (this );
451
458
452
- identity = registration .register (thingId );
453
- if (identity == null )
459
+ registeredThing = registration .register (thingId , loadRegistrationKey () );
460
+ if (registeredThing == null )
454
461
return ;
455
462
456
- registered (identity );
463
+ registered (registeredThing );
457
464
for (IEdgeThingListener edgeThingListener : edgeThingListeners ) {
458
- edgeThingListener .registered (identity );
465
+ edgeThingListener .registered (registeredThing );
459
466
}
460
467
} catch (RegistrationException e ) {
461
468
for (IEdgeThingListener edgeThingListener : edgeThingListeners ) {
@@ -474,8 +481,11 @@ public void register() {
474
481
}
475
482
}
476
483
477
- private String getThingIdentityString (ThingIdentity identity ) {
478
- return String .format ("%s,%s" , identity .getThingName (), identity .getCredentials ());
484
+ protected abstract String loadRegistrationKey ();
485
+
486
+ private String getRegisteredThingString (RegisteredThing registeredThing ) {
487
+ return String .format ("%s,%s,%s" , registeredThing .getThingName (),
488
+ registeredThing .getCredentials (), BinaryUtils .encodeToBase64 (registeredThing .getSecurityKey ()));
479
489
}
480
490
481
491
@ Override
@@ -591,21 +601,21 @@ protected void registrationExceptionOccurred(RegistrationException e) {
591
601
logger .error ("Registration exception occurred." , e );
592
602
}
593
603
594
- protected ThingIdentity getThingIdentity (Map <String , String > attributes ) {
595
- String sThingIdentity = attributes .get (ATTRIBUTE_NAME_THING_IDENTITY );
596
- if (sThingIdentity == null )
604
+ protected RegisteredThing getRegisteredThing (Map <String , String > attributes ) {
605
+ String sRegisteredThing = attributes .get (ATTRIBUTE_NAME_REGISTERED_THING );
606
+ if (sRegisteredThing == null )
597
607
return null ;
598
608
599
- int commaIndex = sThingIdentity .indexOf (',' );
600
- if (commaIndex == -1 ) {
601
- throw new IllegalArgumentException ("Cant read thing identity. Not a valid thing identity string." );
602
- }
609
+ StringTokenizer st = new StringTokenizer (sRegisteredThing , "," );
610
+ if (st .countTokens () != 3 )
611
+ throw new RuntimeException ("Invalid registered thing string!" );
603
612
604
- ThingIdentity identity = new ThingIdentity ();
605
- identity .setThingName (sThingIdentity .substring (0 , commaIndex ).trim ());
606
- identity .setCredentials (sThingIdentity .substring (commaIndex + 1 , sThingIdentity .length ()).trim ());
613
+ RegisteredThing registeredThing = new RegisteredThing ();
614
+ registeredThing .setThingName (st .nextToken ().trim ());
615
+ registeredThing .setCredentials (st .nextToken ().trim ());
616
+ registeredThing .setCredentials (st .nextToken ().trim ());
607
617
608
- return identity ;
618
+ return registeredThing ;
609
619
}
610
620
611
621
@ Override
@@ -765,7 +775,10 @@ protected Path getAttributesFilePath() {
765
775
return attributesFilePath ;
766
776
}
767
777
768
- protected abstract boolean doProcessAttributes (Map <String , String > attributes );
778
+ protected boolean doProcessAttributes (Map <String , String > attributes ) {
779
+ return false ;
780
+ }
781
+
769
782
protected abstract void registerIotPlugins ();
770
783
protected abstract void startIotComponents ();
771
784
protected abstract void stopIotComponents ();
0 commit comments