Esempio n. 1
0
        public async Task <bool> Login(LoginDetails loginDetails)
        {
            try
            {
                // create a data object to be posted as JSON to the API
                LogonRequest loginData = new LogonRequest()
                {
                    Username = loginDetails.Username,
                    Password = loginDetails.Password,
                    License  = loginDetails.LicenseKey
                };
                string json = JsonConvert.SerializeObject(loginData);

                Uri uri = new Uri(loginDetails.PublicApiUrl + @"/api/v1/logon");

                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                if (_httpClient == null)
                {
                    _httpClient = new HttpClient();
                }
                HttpResponseMessage response = _httpClient.PostAsync(uri, content).Result;

                if (!response.IsSuccessStatusCode)
                {
                    string errorMessage = await response.Content.ReadAsStringAsync();

                    return(false);
                }

                string jsonResponse = await response.Content.ReadAsStringAsync();

                LogonResponse logon = JsonConvert.DeserializeObject <LogonResponse>(jsonResponse);
                if (logon.Success)
                {
                    GetClient(logon.Authorization.AccessToken);
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                string error = "Exception {ex.GetType().Name}: {ex.Message}";
                if (ex.InnerException != null)
                {
                    error += " " + ex.InnerException.Message;
                }

                return(false);
            }
        }
Esempio n. 2
0
        public string GetImage(string _caemraGuid, string strDateTime)
        {
            string strImg = "";

            try
            {
                //Login
                LoginDetails loginDetails = new LoginDetails()
                {
                    PublicApiUrl = PublicApiUrl,
                    Username     = MirasysHostUserName,
                    Password     = MirasysHostPassword,
                    LicenseKey   = LicenseKey
                };

                var Result = Login(loginDetails).GetAwaiter().GetResult();

                if (Result == true)
                {
                    Uri uri = new Uri(loginDetails.PublicApiUrl + @"/api/v1/cameras/playbackimage/" + _caemraGuid + "?utcTime=" + strDateTime + "&direction=backward&requestedsize=medium&includemetadata=true");

                    HttpResponseMessage response = _httpClient.GetAsync(uri).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        string jsonResponse = response.Content.ReadAsStringAsync().Result;

                        SnapshotImage snapshot = JsonConvert.DeserializeObject <SnapshotImage>(jsonResponse);

                        return(snapshot.LatestCameraImageJpegBase64);
                    }
                    else
                    {
                        //Not able to take Image
                    }

                    Uri                 urilogout      = new Uri(loginDetails.PublicApiUrl + @"/api/v1/logon/logout");
                    StringContent       content        = new StringContent(string.Empty, Encoding.UTF8, "application/json");
                    HttpResponseMessage responselogout = _httpClient.PostAsync(urilogout, content).Result;
                }
                else
                {
                    //Unable to login
                }
            }
            catch (Exception ex)
            {
            }

            return(strImg);
        }