/// <summary> /// Fetches all of the required camera information from the API. /// </summary> /// <returns>A list of all cameras.</returns> public async Task <IEnumerable <SynologyCamera> > GetCamerasAsync() { _logger.LogInformation("GetCameras: Fetching Cameras"); CookieContainer cookieContainer = new CookieContainer(); using (HttpClient client = GetHttpClient(Config.Url, cookieContainer)) { cookieContainer.Add(client.BaseAddress, new Cookie("id", Cookie.Value)); string cameraInfoUri = string.Format(URI_CAMERA_INFO, _cameraPath, Config.ApiVersionCamera); HttpResponseMessage result = await client.GetAsync(cameraInfoUri); SynologyResponse <SynologyCameras> response = await GetResponse <SynologyCameras>(result); if (response.Success) { _logger.LogInformation($"GetCameras: Successful. Found {response.Data.Cameras.Count()} cameras."); return(response.Data.Cameras); } else { _logger.LogError($"GetCameras: Failed due to error code '{response.Error.Code}'"); } } return(null); }
public void Create() { var v = new SynologyResponse <string>(); Assert.IsNotNull(v); Assert.IsNull(v.Data); Assert.IsNull(v.Error); Assert.IsNull(v.Errors); Assert.IsFalse(v.Success); Assert.AreEqual(default, v.Code);
public void Create() { var obj = new SynologyResponse <string>(); Assert.IsNotNull(obj); Assert.AreEqual(default(int), obj.Code); Assert.IsNull(obj.Data); Assert.IsNull(obj.Error); Assert.IsNull(obj.Errormsg); Assert.IsNull(obj.Errors); Assert.AreEqual(default(int), obj.HttpStatus); Assert.IsFalse(obj.Success); }
/// <summary> /// Takes a snapshot of the specified camera. /// </summary> /// <returns>A string to the file path.</returns> public async Task <byte[]> TakeSnapshotAsync(string cameraName) { CookieContainer cookieContainer = new CookieContainer(); using (HttpClient client = GetHttpClient(Config.Url, cookieContainer)) { cookieContainer.Add(client.BaseAddress, new Cookie("id", Cookie.Value)); if (Cameras.TryGetValue(cameraName, out int id)) { _logger.LogDebug($"{cameraName}: Found with Synology ID '{id}'."); string resource = string.Format(URI_CAMERA_SNAPSHOT + $"&profileType={(int)Config.Quality}", _cameraPath, Config.ApiVersionCamera, id); _logger.LogDebug($"{cameraName}: Taking snapshot from '{resource}'."); _logger.LogInformation($"{cameraName}: Taking snapshot"); HttpResponseMessage response = await client.GetAsync(resource); response.EnsureSuccessStatusCode(); if (response.Content.Headers.ContentType.MediaType == "image/jpeg") { // Only return the bytes when we have a valid image back _logger.LogDebug($"{cameraName}: Reading snapshot"); return(await response.Content.ReadAsByteArrayAsync()); } else { // We didn't get an image type back, so this must have errored SynologyResponse errorResponse = await GetErrorResponse(response); if (errorResponse.Success) { // This should never happen, but let's add logging just in case _logger.LogError($"{cameraName}: Failed to get snapshot, but the API reported success."); } else { _logger.LogError($"{cameraName}: Failed to get snapshot with error code '{errorResponse.Error.Code}'"); } } } else { _logger.LogError($"The camera with the name '{cameraName}' was not found in the Synology camera list."); } return(null); } }
/// <summary> /// Generates a login cookie for the username and password in the config. /// </summary> /// <returns>A cookie, or null on failure.</returns> public async Task <Cookie> LoginAsync() { _logger.LogInformation("Login: Authenticating"); string loginUri = string.Format(URI_LOGIN, _loginPath, Config.ApiVersionAuth, Config.Username, Config.Password); _logger.LogDebug($"Login: Logging in ({loginUri})"); CookieContainer cookieContainer = new CookieContainer(); using (HttpClient httpClient = GetHttpClient(Config.Url, cookieContainer)) { HttpResponseMessage result = await httpClient.GetAsync(loginUri); if (result.IsSuccessStatusCode) { SynologyResponse <SynologyLogin> response = await GetResponse <SynologyLogin>(result); if (response.Success) { _logger.LogInformation("Login: Successful"); IEnumerable <Cookie> cookies = cookieContainer.GetCookies(httpClient.BaseAddress).Cast <Cookie>().ToList(); Cookie cookie = cookies.FirstOrDefault(x => x.Name == "id"); if (cookie == null) { _applicationLifetime.StopApplication(); } return(cookie); } else { _logger.LogError($"Login: Failed due to Synology API error code '{response.Error.Code}'"); } } else { _logger.LogError($"Login: Failed due to HTTP status code '{result.StatusCode}'"); } } return(null); }
/// <summary> /// Fetches all the end points, because they're dynamic between DSM versions. /// </summary> public async Task <bool> GetEndPointsAsync() { _logger.LogInformation("API: Querying end points"); using (HttpClient httpClient = GetHttpClient(Config.Url)) { HttpResponseMessage result = await httpClient.GetAsync(URI_INFO); if (result.IsSuccessStatusCode) { SynologyResponse <SynologyApiInfoResponse> response = await GetResponse <SynologyApiInfoResponse>(result); if (response.Success) { // Find the Authentication entry point if (response.Data.TryGetValue(API_LOGIN, out SynologyApiInfo loginInfo)) { _logger.LogDebug($"API: Found path '{loginInfo.Path}' for {API_LOGIN}"); if (loginInfo.MaxVersion < Config.ApiVersionAuth) { _logger.LogError($"API: {API_CAMERA} only supports a max version of {loginInfo.MaxVersion}, but the system is set to use version {Config.ApiVersionAuth}."); } } else { _logger.LogError($"API: Failed to find {API_LOGIN}."); } // Find the Camera entry point if (response.Data.TryGetValue(API_CAMERA, out SynologyApiInfo cameraInfo)) { _logger.LogDebug($"API: Found path '{cameraInfo.Path}' for {API_CAMERA}"); if (cameraInfo.MaxVersion < Config.ApiVersionCamera) { _logger.LogError($"API: {API_CAMERA} only supports a max version of {cameraInfo.MaxVersion}, but the system is set to use version {Config.ApiVersionCamera}."); } } else { _logger.LogError($"API: Failed to find {API_CAMERA}."); } _loginPath = loginInfo.Path; _cameraPath = cameraInfo.Path; _logger.LogInformation("API: Successfully mapped all end points"); return(true); } else { _logger.LogError($"API: Failed due to error code '{response.Error.Code}'"); } } else { _logger.LogError($"API: Failed due to HTTP status code '{result.StatusCode}'"); } } return(false); }