8383import java .security .cert .Certificate ;
8484import java .security .cert .CertificateException ;
8585import java .util .ArrayList ;
86+ import java .util .Arrays ;
8687import java .util .Base64 ;
8788import java .util .HashMap ;
8889import java .util .Iterator ;
@@ -116,6 +117,7 @@ public class EspApplication extends Application {
116117 public HashMap <String , ChipClient > chipClientMap ;
117118 public HashMap <String , List <DeviceMatterInfo >> matterDeviceInfoMap ;
118119 public ArrayList <String > availableMatterDevices ;
120+ public HashMap <String , HashMap <String , String >> controllerDevices ;
119121 public EspOtaUpdate otaUpdateInfo ;
120122
121123 private SharedPreferences appPreferences ;
@@ -149,6 +151,7 @@ public void onCreate() {
149151 chipClientMap = new HashMap <>();
150152 matterDeviceInfoMap = new HashMap <>();
151153 availableMatterDevices = new ArrayList <>();
154+ controllerDevices = new HashMap <>();
152155
153156 appPreferences = getSharedPreferences (AppConstants .ESP_PREFERENCES , Context .MODE_PRIVATE );
154157 BASE_URL = appPreferences .getString (AppConstants .KEY_BASE_URL , BuildConfig .BASE_URL );
@@ -215,7 +218,11 @@ public void changeAppState(AppState newState, Bundle extras) {
215218 if (!chipClientMap .containsKey (matterNodeId )) {
216219 clientHelper .initChipClientInBackground (matterNodeId );
217220 } 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+ }
219226 }
220227 }
221228 break ;
@@ -529,6 +536,7 @@ public void onSuccess(Bundle data) {
529536 }
530537 }
531538 }
539+ setRemoteDeviceStatus ();
532540 changeAppState (AppState .GET_DATA_SUCCESS , null );
533541 }
534542
@@ -544,6 +552,49 @@ public void onNetworkFailure(Exception exception) {
544552 });
545553 }
546554
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+
547598 private void initChipControllerForHomeGroup () {
548599
549600 Log .d (TAG , "============================= init ChipController for home group" );
@@ -619,12 +670,17 @@ public void fetchDeviceMatterInfo(String matterNodeId, String nodeId) {
619670 }
620671 matterDeviceInfoMap .put (matterNodeId , matterDeviceInfo );
621672 nodeMap .get (nodeId ).setOnline (true );
673+ nodeMap .get (nodeId ).setNodeStatus (AppConstants .NODE_STATUS_MATTER_LOCAL );
622674 availableMatterDevices .add (matterNodeId );
623675 } else {
624676 matterDeviceInfoMap .remove (matterNodeId );
625677 availableMatterDevices .remove (matterNodeId );
626678 chipClientMap .remove (matterNodeId );
627679 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+ }
628684 }
629685 } catch (ExecutionException e ) {
630686 e .printStackTrace ();
@@ -1088,6 +1144,7 @@ public void onSuccess(Bundle data) {
10881144 localNode .setIpAddress (localDevice .getIpAddr ());
10891145 localNode .setPort (localDevice .getPort ());
10901146 localNode .setOnline (true );
1147+ localNode .setNodeStatus (AppConstants .NODE_STATUS_LOCAL );
10911148 localDeviceMap .put (localNode .getNodeId (), localDevice );
10921149 }
10931150
0 commit comments