83
83
import java .security .cert .Certificate ;
84
84
import java .security .cert .CertificateException ;
85
85
import java .util .ArrayList ;
86
+ import java .util .Arrays ;
86
87
import java .util .Base64 ;
87
88
import java .util .HashMap ;
88
89
import java .util .Iterator ;
@@ -116,6 +117,7 @@ public class EspApplication extends Application {
116
117
public HashMap <String , ChipClient > chipClientMap ;
117
118
public HashMap <String , List <DeviceMatterInfo >> matterDeviceInfoMap ;
118
119
public ArrayList <String > availableMatterDevices ;
120
+ public HashMap <String , HashMap <String , String >> controllerDevices ;
119
121
public EspOtaUpdate otaUpdateInfo ;
120
122
121
123
private SharedPreferences appPreferences ;
@@ -149,6 +151,7 @@ public void onCreate() {
149
151
chipClientMap = new HashMap <>();
150
152
matterDeviceInfoMap = new HashMap <>();
151
153
availableMatterDevices = new ArrayList <>();
154
+ controllerDevices = new HashMap <>();
152
155
153
156
appPreferences = getSharedPreferences (AppConstants .ESP_PREFERENCES , Context .MODE_PRIVATE );
154
157
BASE_URL = appPreferences .getString (AppConstants .KEY_BASE_URL , BuildConfig .BASE_URL );
@@ -215,7 +218,11 @@ public void changeAppState(AppState newState, Bundle extras) {
215
218
if (!chipClientMap .containsKey (matterNodeId )) {
216
219
clientHelper .initChipClientInBackground (matterNodeId );
217
220
} else {
218
- clientHelper .getCurrentValues (nodeId , matterNodeId , nodeMap .get (nodeId ));
221
+ try {
222
+ clientHelper .getCurrentValues (nodeId , matterNodeId , nodeMap .get (nodeId ));
223
+ } catch (ExecutionException e ) {
224
+ e .printStackTrace ();
225
+ }
219
226
}
220
227
}
221
228
break ;
@@ -529,6 +536,7 @@ public void onSuccess(Bundle data) {
529
536
}
530
537
}
531
538
}
539
+ setRemoteDeviceStatus ();
532
540
changeAppState (AppState .GET_DATA_SUCCESS , null );
533
541
}
534
542
@@ -544,6 +552,49 @@ public void onNetworkFailure(Exception exception) {
544
552
});
545
553
}
546
554
555
+ private void setRemoteDeviceStatus () {
556
+
557
+ for (Map .Entry <String , HashMap <String , String >> entry : controllerDevices .entrySet ()) {
558
+
559
+ String controllerNodeId = entry .getKey ();
560
+ HashMap <String , String > matterOnlyDevices = entry .getValue ();
561
+
562
+ for (Map .Entry <String , String > controllerDevice : matterOnlyDevices .entrySet ()) {
563
+ String matterDeviceId = controllerDevice .getKey ();
564
+ String jsonStr = controllerDevice .getValue ();
565
+
566
+ if (jsonStr != null ) {
567
+ try {
568
+ JSONObject deviceJson = new JSONObject (jsonStr );
569
+ boolean enabled = deviceJson .optBoolean (AppConstants .KEY_ENABLED );
570
+ boolean reachable = deviceJson .optBoolean (AppConstants .KEY_REACHABLE );
571
+
572
+ if (enabled && reachable ) {
573
+
574
+ if (matterRmNodeIdMap .containsValue (matterDeviceId )) {
575
+ for (Map .Entry <String , String > matterDevice : matterRmNodeIdMap .entrySet ()) {
576
+ if (matterDeviceId .equals (matterDevice .getValue ())) {
577
+ String rmNodeId = matterDevice .getKey ();
578
+ if (nodeMap .containsKey (rmNodeId )) {
579
+ int nodeStatus = nodeMap .get (rmNodeId ).getNodeStatus ();
580
+ if (nodeStatus != AppConstants .NODE_STATUS_MATTER_LOCAL && nodeStatus != AppConstants .NODE_STATUS_LOCAL ) {
581
+ Log .d (TAG , "Set Node status to remotely controllable for node id : " + rmNodeId );
582
+ nodeMap .get (rmNodeId ).setNodeStatus (AppConstants .NODE_STATUS_REMOTELY_CONTROLLABLE );
583
+ }
584
+ }
585
+ }
586
+ }
587
+ }
588
+ }
589
+ } catch (JSONException e ) {
590
+ e .printStackTrace ();
591
+ }
592
+ }
593
+ }
594
+ EventBus .getDefault ().post (new UpdateEvent (UpdateEventType .EVENT_DEVICE_STATUS_UPDATE ));
595
+ }
596
+ }
597
+
547
598
private void initChipControllerForHomeGroup () {
548
599
549
600
Log .d (TAG , "============================= init ChipController for home group" );
@@ -619,12 +670,17 @@ public void fetchDeviceMatterInfo(String matterNodeId, String nodeId) {
619
670
}
620
671
matterDeviceInfoMap .put (matterNodeId , matterDeviceInfo );
621
672
nodeMap .get (nodeId ).setOnline (true );
673
+ nodeMap .get (nodeId ).setNodeStatus (AppConstants .NODE_STATUS_MATTER_LOCAL );
622
674
availableMatterDevices .add (matterNodeId );
623
675
} else {
624
676
matterDeviceInfoMap .remove (matterNodeId );
625
677
availableMatterDevices .remove (matterNodeId );
626
678
chipClientMap .remove (matterNodeId );
627
679
nodeMap .get (nodeId ).setOnline (false );
680
+ if (!Arrays .asList (AppConstants .NODE_STATUS_REMOTELY_CONTROLLABLE , AppConstants .NODE_STATUS_LOCAL ,
681
+ AppConstants .NODE_STATUS_ONLINE ).contains (nodeMap .get (nodeId ).getNodeStatus ())) {
682
+ nodeMap .get (nodeId ).setNodeStatus (AppConstants .NODE_STATUS_OFFLINE );
683
+ }
628
684
}
629
685
} catch (ExecutionException e ) {
630
686
e .printStackTrace ();
@@ -1088,6 +1144,7 @@ public void onSuccess(Bundle data) {
1088
1144
localNode .setIpAddress (localDevice .getIpAddr ());
1089
1145
localNode .setPort (localDevice .getPort ());
1090
1146
localNode .setOnline (true );
1147
+ localNode .setNodeStatus (AppConstants .NODE_STATUS_LOCAL );
1091
1148
localDeviceMap .put (localNode .getNodeId (), localDevice );
1092
1149
}
1093
1150
0 commit comments