mirror of
				https://github.com/house-of-abbey/GarminHomeAssistant.git
				synced 2025-10-30 23:38:15 +00:00 
			
		
		
		
	separate syncdelegate class file, check for startsync2
This commit is contained in:
		
							
								
								
									
										112
									
								
								source/HomeAssistantSyncDelegate.mc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								source/HomeAssistantSyncDelegate.mc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,112 @@ | |||||||
|  | using Toybox.Communications; | ||||||
|  | using Toybox.Lang; | ||||||
|  |  | ||||||
|  | // SyncDelegate to execute single command via POST request to Home Assistant | ||||||
|  | // | ||||||
|  | class HomeAssistantSyncDelegate extends Communications.SyncDelegate { | ||||||
|  |     private static var syncError as Lang.String or Null; | ||||||
|  |  | ||||||
|  |     // Initialize an instance of this delegate | ||||||
|  |     public function initialize() { | ||||||
|  |         SyncDelegate.initialize(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     //! Called by the system to determine if a sync is needed | ||||||
|  |     public function isSyncNeeded() as Lang.Boolean { | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     //! Called by the system when starting a bulk sync. | ||||||
|  |     public function onStartSync() as Void { | ||||||
|  |         syncError = null; | ||||||
|  |          | ||||||
|  |         if (WifiLteExecutionConfirmDelegate.mCommandData == null) { | ||||||
|  |             syncError = WatchUi.loadResource($.Rez.Strings.WifiLteExecutionDataError) as Lang.String; | ||||||
|  |             onStopSync(); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         var type = WifiLteExecutionConfirmDelegate.mCommandData[:type]; | ||||||
|  |         var data = WifiLteExecutionConfirmDelegate.mCommandData[:data]; | ||||||
|  |         var url; | ||||||
|  |  | ||||||
|  |         switch (type) { | ||||||
|  |             case "service": | ||||||
|  |                 var service = WifiLteExecutionConfirmDelegate.mCommandData[:service]; | ||||||
|  |                 url = Settings.getApiUrl() + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, service.length()); | ||||||
|  |                 var entity_id = ""; | ||||||
|  |                 if (data != null) { | ||||||
|  |                     entity_id = data.get("entity_id"); | ||||||
|  |                     if (entity_id == null) { | ||||||
|  |                         entity_id = ""; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 performRequest(url, data); | ||||||
|  |                 break; | ||||||
|  |             case "entity": | ||||||
|  |                 url = WifiLteExecutionConfirmDelegate.mCommandData[:url]; | ||||||
|  |                 performRequest(url, data); | ||||||
|  |                 break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Performs a POST request to Hass with a given payload and URL, and calls haCallback | ||||||
|  |     private function performRequest(url as Lang.String, data as Lang.Dictionary or Null) { | ||||||
|  |         Communications.makeWebRequest( | ||||||
|  |             url, | ||||||
|  |             data, // May include {"entity_id": xxxx} for service calls | ||||||
|  |             { | ||||||
|  |                 :method  => Communications.HTTP_REQUEST_METHOD_POST, | ||||||
|  |                 :headers => { | ||||||
|  |                     "Content-Type"  => Communications.REQUEST_CONTENT_TYPE_JSON, | ||||||
|  |                     "Authorization" => "Bearer " + Settings.getApiKey() | ||||||
|  |                 }, | ||||||
|  |                 :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON | ||||||
|  |             }, | ||||||
|  |             method(:haCallback) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     //! Handle callback from request | ||||||
|  |     public function haCallback(code as Lang.Number, data as Null or Lang.Dictionary) as Void { | ||||||
|  |         Communications.notifySyncProgress(100); | ||||||
|  |         if (code == 200) { | ||||||
|  |             syncError = null; | ||||||
|  |             if (WifiLteExecutionConfirmDelegate.mCommandData[:type].equals("entity")) { | ||||||
|  |                 var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback]; | ||||||
|  |                 if (callbackMethod != null) { | ||||||
|  |                     var d = data as Lang.Array; | ||||||
|  |                     callbackMethod.invoke(d); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             onStopSync(); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         switch(code) { | ||||||
|  |             case Communications.NETWORK_REQUEST_TIMED_OUT: | ||||||
|  |                 syncError = WatchUi.loadResource($.Rez.Strings.TimedOut) as Lang.String; | ||||||
|  |                 break; | ||||||
|  |             case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE: | ||||||
|  |                 syncError = WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String; | ||||||
|  |                 syncError = ""; | ||||||
|  |             default: | ||||||
|  |                 var codeMsg = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String; | ||||||
|  |                 syncError = codeMsg + code; | ||||||
|  |                 break; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         onStopSync(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     //! Clean up | ||||||
|  |     public function onStopSync() as Void { | ||||||
|  |         if (WifiLteExecutionConfirmDelegate.mCommandData[:exit]) { | ||||||
|  |             System.exit(); | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         Communications.cancelAllRequests(); | ||||||
|  |         Communications.notifySyncComplete(syncError); | ||||||
|  |          | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -46,6 +46,8 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { | |||||||
|         :confirmMethod as Lang.Method, |         :confirmMethod as Lang.Method, | ||||||
|         :state as Lang.Boolean |         :state as Lang.Boolean | ||||||
|     } or Null) { |     } or Null) { | ||||||
|  |         ConfirmationDelegate.initialize(); | ||||||
|  |          | ||||||
|         if (WatchUi has :showToast) { |         if (WatchUi has :showToast) { | ||||||
|             mHasToast = true; |             mHasToast = true; | ||||||
|         } |         } | ||||||
| @@ -68,8 +70,6 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { | |||||||
|             mTimer = new Timer.Timer(); |             mTimer = new Timer.Timer(); | ||||||
|             mTimer.start(method(:onTimeout), timeout, true); |             mTimer.start(method(:onTimeout), timeout, true); | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|         ConfirmationDelegate.initialize(); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     //! Handles the user's response to the confirmation dialog. |     //! Handles the user's response to the confirmation dialog. | ||||||
| @@ -77,6 +77,10 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { | |||||||
|     //! @param response The user's confirmation response as `WatchUi.Confirm` |     //! @param response The user's confirmation response as `WatchUi.Confirm` | ||||||
|     //! @return Always returns `true` to indicate the response was handled. |     //! @return Always returns `true` to indicate the response was handled. | ||||||
|     function onResponse(response) as Lang.Boolean { |     function onResponse(response) as Lang.Boolean { | ||||||
|  |         if (mTimer != null) { | ||||||
|  |             mTimer.stop(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         if (response == WatchUi.CONFIRM_YES) { |         if (response == WatchUi.CONFIRM_YES) { | ||||||
|             if (mToggleMethod != null) { |             if (mToggleMethod != null) { | ||||||
|                 mToggleMethod.invoke(mToggleState); |                 mToggleMethod.invoke(mToggleState); | ||||||
| @@ -104,8 +108,12 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (possibleConnection) { |         if (possibleConnection) { | ||||||
|  |             if (Communications has :startSync2) { | ||||||
|                 var syncString = WatchUi.loadResource($.Rez.Strings.WifiLteExecutionTitle) as Lang.String; |                 var syncString = WatchUi.loadResource($.Rez.Strings.WifiLteExecutionTitle) as Lang.String; | ||||||
|                 Communications.startSync2({:message => syncString}); |                 Communications.startSync2({:message => syncString}); | ||||||
|  |             } else { | ||||||
|  |                 Communications.startSync(); | ||||||
|  |             } | ||||||
|         } else { |         } else { | ||||||
|             var toast = WatchUi.loadResource($.Rez.Strings.WifiLteNotAvailable) as Lang.String; |             var toast = WatchUi.loadResource($.Rez.Strings.WifiLteNotAvailable) as Lang.String; | ||||||
|             if (mHasToast) { |             if (mHasToast) { | ||||||
| @@ -128,109 +136,3 @@ class WifiLteExecutionConfirmDelegate extends WatchUi.ConfirmationDelegate { | |||||||
|         WatchUi.popView(WatchUi.SLIDE_RIGHT); |         WatchUi.popView(WatchUi.SLIDE_RIGHT); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| class HomeAssistantSyncDelegate extends Communications.SyncDelegate { |  | ||||||
|     private static var syncError as Lang.String or Null; |  | ||||||
| 
 |  | ||||||
|     // Initialize an instance of this delegate |  | ||||||
|     public function initialize() { |  | ||||||
|         SyncDelegate.initialize(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     //! Called by the system to determine if a sync is needed |  | ||||||
|     public function isSyncNeeded() as Lang.Boolean { |  | ||||||
|         return true; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     //! Called by the system when starting a bulk sync. |  | ||||||
|     public function onStartSync() as Void { |  | ||||||
|         syncError = null; |  | ||||||
|          |  | ||||||
|         if (WifiLteExecutionConfirmDelegate.mCommandData == null) { |  | ||||||
|             syncError = WatchUi.loadResource($.Rez.Strings.WifiLteExecutionDataError) as Lang.String; |  | ||||||
|             onStopSync(); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|          |  | ||||||
|         var type = WifiLteExecutionConfirmDelegate.mCommandData[:type]; |  | ||||||
|         var data = WifiLteExecutionConfirmDelegate.mCommandData[:data]; |  | ||||||
|         var url; |  | ||||||
| 
 |  | ||||||
|         switch (type) { |  | ||||||
|             case "service": |  | ||||||
|                 var service = WifiLteExecutionConfirmDelegate.mCommandData[:service]; |  | ||||||
|                 url = Settings.getApiUrl() + "/services/" + service.substring(0, service.find(".")) + "/" + service.substring(service.find(".")+1, service.length()); |  | ||||||
|                 var entity_id = ""; |  | ||||||
|                 if (data != null) { |  | ||||||
|                     entity_id = data.get("entity_id"); |  | ||||||
|                     if (entity_id == null) { |  | ||||||
|                         entity_id = ""; |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|                 performRequest(url, data); |  | ||||||
|                 break; |  | ||||||
|             case "entity": |  | ||||||
|                 url = WifiLteExecutionConfirmDelegate.mCommandData[:url]; |  | ||||||
|                 performRequest(url, data); |  | ||||||
|                 break; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     // Performs a POST request to Hass with a given payload and URL, and calls haCallback |  | ||||||
|     private function performRequest(url as Lang.String, data as Lang.Dictionary or Null) { |  | ||||||
|         Communications.makeWebRequest( |  | ||||||
|             url, |  | ||||||
|             data, // May include {"entity_id": xxxx} for service calls |  | ||||||
|             { |  | ||||||
|                 :method  => Communications.HTTP_REQUEST_METHOD_POST, |  | ||||||
|                 :headers => { |  | ||||||
|                     "Content-Type"  => Communications.REQUEST_CONTENT_TYPE_JSON, |  | ||||||
|                     "Authorization" => "Bearer " + Settings.getApiKey() |  | ||||||
|                 }, |  | ||||||
|                 :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON |  | ||||||
|             }, |  | ||||||
|             method(:haCallback) |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     //! Handle callback from request |  | ||||||
|     public function haCallback(code as Lang.Number, data as Null or Lang.Dictionary) as Void { |  | ||||||
|         if (code == 200) { |  | ||||||
|             syncError = null; |  | ||||||
|             if (WifiLteExecutionConfirmDelegate.mCommandData[:type].equals("entity")) { |  | ||||||
|                 var callbackMethod = WifiLteExecutionConfirmDelegate.mCommandData[:callback]; |  | ||||||
|                 if (callbackMethod != null) { |  | ||||||
|                     var d = data as Lang.Array; |  | ||||||
|                     callbackMethod.invoke(d); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|             onStopSync(); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         switch(code) { |  | ||||||
|             case Communications.NETWORK_REQUEST_TIMED_OUT: |  | ||||||
|                 syncError = WatchUi.loadResource($.Rez.Strings.TimedOut) as Lang.String; |  | ||||||
|                 break; |  | ||||||
|             case Communications.INVALID_HTTP_BODY_IN_NETWORK_RESPONSE: |  | ||||||
|                 syncError = WatchUi.loadResource($.Rez.Strings.NoJson) as Lang.String; |  | ||||||
|                 syncError = ""; |  | ||||||
|             default: |  | ||||||
|                 var codeMsg = WatchUi.loadResource($.Rez.Strings.UnhandledHttpErr) as Lang.String; |  | ||||||
|                 syncError = codeMsg + code; |  | ||||||
|                 break; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         onStopSync(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     //! Clean up |  | ||||||
|     public function onStopSync() as Void { |  | ||||||
|         if (WifiLteExecutionConfirmDelegate.mCommandData[:exit]) { |  | ||||||
|             System.exit(); |  | ||||||
|         } |  | ||||||
|          |  | ||||||
|         Communications.cancelAllRequests(); |  | ||||||
|         Communications.notifySyncComplete(syncError); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user