protected async Task <T?> CallEventAction <T>(string action, object?param, bool eventListener = false) { if (_device == null) { throw new ArgumentNullException(nameof(_device), "The device can't be null."); } if (string.IsNullOrEmpty(_device.Id)) { throw new ArgumentException("The id of the device is empty. Please call again the GetDevices Method and try again.", nameof(_device)); } var query = $"/t_{WebUtility.UrlEncode(_loginObject.SessionToken)}_{WebUtility.UrlEncode(_device.Id)}{action}"; var callActionObject = new CallActionObject { ApiVer = 1, Params = param, RequestId = Utils.GetUniqueRid(), Url = action }; var url = Utils.ApiUrl + query; var json = JsonConvert.SerializeObject(callActionObject); var encryptedJson = await Utils.Encrypt(json, _loginObject.DeviceEncryptionToken); var encryptedResponse = await Utils.PostMethod(url, encryptedJson, eventListener); if (encryptedResponse == null) { return(default);
public static async Task <T?> CallAction <T>(DeviceObject device, LoginObject loginObject, string action, object?param, bool eventListener = false) { if (device == null) { throw new ArgumentNullException(nameof(device), "The device can't be null."); } if (string.IsNullOrEmpty(device.Id)) { throw new ArgumentException("The id of the device is empty. Please call again the GetDevices Method and try again.", nameof(device)); } var query = $"/t_{HttpUtility.UrlEncode(loginObject.SessionToken)}_{HttpUtility.UrlEncode(device.Id)}{action}"; var callActionObject = new CallActionObject { ApiVer = 1, Params = param, RequestId = GetUniqueRid(), Url = action }; var url = ApiUrl + query; var json = JsonConvert.SerializeObject(callActionObject); var encryptedJson = await Encrypt(json, loginObject.DeviceEncryptionToken); var encryptedResponse = await PostMethod(url, encryptedJson, eventListener); if (encryptedResponse == null) { throw new Exception("Server response is empty"); } var decryptedResponse = await Decrypt(encryptedResponse, loginObject.DeviceEncryptionToken); if (decryptedResponse == null) { throw new Exception("Can't decrypt message"); } //special case as event responses are completly differerent if (decryptedResponse.Contains("subscriptionid")) { var direct = JsonConvert.DeserializeObject <T>(decryptedResponse); if (direct != null) { return(direct); } } var res = JsonConvert.DeserializeObject <ApiObjects.DefaultReturnObject>(decryptedResponse); if (res?.Data == null) { return(default);
public T CallAction <T>(DeviceObject device, string action, object param, LoginObject loginObject, bool decryptResponse = false) { if (device == null) { throw new ArgumentNullException(nameof(device)); } if (string.IsNullOrEmpty(device.Id)) { throw new ArgumentException( "The id of the device is empty. Please call again the GetDevices Method and try again."); } string query = $"/t_{HttpUtility.UrlEncode(loginObject.SessionToken)}_{HttpUtility.UrlEncode(device.Id)}{action}"; CallActionObject callActionObject = new CallActionObject { ApiVer = 1, Params = param, RequestId = GetUniqueRid(), Url = action }; //Regex _regex = new Regex("http\\:\\/\\/(192.168.*)\\:37733"); //var match = _regex.Match(_apiUrl); //if (match.Success) //{ // _apiUrl = _apiUrl.Replace(match.Groups[0].Value, "89.163.144.231"); //} string url = _apiUrl + query; //url = url.Replace("172.23.0.8", "89.163.144.231"); string json = JsonConvert.SerializeObject(callActionObject); json = Encrypt(json, loginObject.DeviceEncryptionToken); string response = PostMethod(url, json, loginObject.DeviceEncryptionToken); if (response != null || !response.Contains(callActionObject.RequestId.ToString())) { if (decryptResponse) { string tmp = Decrypt(response, loginObject.DeviceEncryptionToken); return((T)JsonConvert.DeserializeObject(tmp, typeof(T))); } throw new InvalidRequestIdException("The 'RequestId' differs from the 'Requestid' from the query."); } return((T)JsonConvert.DeserializeObject(response, typeof(T))); }