public IHttpActionResult GetUserCameras([FromBody] JObject parameters) { List <JProperty> properties = null; int userId; try { //Getting parameters values properties = parameters.Children().Cast <JProperty>().ToList(); userId = (properties.Where(p => p.Name == "userId").FirstOrDefault()?.Value as JValue)?.Value <int>() ?? 0; } catch { //returning error response return(Content(HttpStatusCode.BadRequest, "Invalid input parameters")); } try { GetUserCamerasResult o = new GetUserCamerasResult(); User tmpUser; //Getting the user object if ((tmpUser = users?.Where(u => u.Id == userId).FirstOrDefault()) != null) { //Filling the response with user's data o.NumberOfCameras = tmpUser.NumberOfCameras; o.Cameras = tmpUser.Cameras; ConfigurationItem[] recordingServerHardware = configApiClient.GetChildItems(configApiClient.GetChildItems(configApiClient.GetItem("/" + ItemTypes.RecordingServerFolder).Path)[0].Path + "/" + ItemTypes.HardwareFolder); //Getting each camera's credentials from the VMS foreach (Camera camera in o.Cameras.Where(c => !string.IsNullOrWhiteSpace(c.Address))) { ConfigurationItem tmpConfigurationItem = recordingServerHardware.Where(c => c.Properties.Where(p => p.Key == "Address" && p.Value.Contains(camera.Address)).FirstOrDefault() != null).FirstOrDefault(); if (tmpConfigurationItem != null) { //Filling getted from the VMS camera's credentials to the response camera.Username = tmpConfigurationItem.Properties.Where(p => p.Key == "UserName").FirstOrDefault()?.Value ?? string.Empty; camera.Password = configApiClient.InvokeMethod(configApiClient.InvokeMethod(tmpConfigurationItem, "ReadPasswordHardware"), "ReadPasswordHardware").Properties.Where(p => p.Key == "Password").FirstOrDefault()?.Value ?? string.Empty; } } //returning success response return(Ok(o)); } else { //returning error response return(Content(HttpStatusCode.BadRequest, "Invalid input parameters")); } } catch (Exception) { //returning error response return(Content(HttpStatusCode.InternalServerError, "Unknown error")); } }
public ActionResult Index() { //Calling the SurveillanceCloudSampleService's GetUserCameras method with the parameters from the session HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://*****:*****@"application/json"; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); //Deserializing the response userCameras = JsonConvert.DeserializeObject <GetUserCamerasResult>(reader.ReadToEnd()); } } catch (Exception) { } //Creating the Model and returning the View DashboardModel dashboardModel = new DashboardModel(userCameras.NumberOfCameras, userCameras.Cameras); return(View("DashboardView", dashboardModel)); }