public IList<EdgeDevice> GetEdgeDevices() { using (var db = new VideoAnalyticContext()) { return db.EdgeDevices.ToList(); } }
public IHttpActionResult PutCamera(int id, Camera camera) { using (var db = new VideoAnalyticContext()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != camera.Id) { return(BadRequest()); } db.Entry(camera).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CameraExists(db, id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); } }
public IHttpActionResult PostEvent(Event @event) { using (VideoAnalyticContext db = new VideoAnalyticContext()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Events.Add(@event); try { db.SaveChanges(); } catch (DbUpdateException) { if (EventExists(db, @event.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = @event.Id }, @event)); } }
public IHttpActionResult PutEvent(long id, Event @event) { using (VideoAnalyticContext db = new VideoAnalyticContext()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != @event.Id) { return(BadRequest()); } db.Entry(@event).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EventExists(db, id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); } }
// POST: api/Cameras/Deactivate/1 public async Task <IHttpActionResult> Deactivate(int id) { using (var db = new VideoAnalyticContext()) { Camera camera = db.Cameras.Find(id); if (camera == null) { return(NotFound()); } var deviceId = camera.HostingDevice; EdgeDevice device = db.EdgeDevices.Find(deviceId); if (device == null) { return(NotFound()); } var configStr = device.Configurations; if (string.IsNullOrEmpty(configStr)) { return(InternalServerError()); } var moduleName = camera.Name; var resultConfig = await IoTEdgeManager.DeleteModuleOnDeviceAsync(moduleName, device.Name, camera.Pipeline, JObject.Parse(configStr)); device.Configurations = resultConfig; camera.Status = "NotActive"; db.SaveChanges(); return(Ok()); } }
// GET: api/Cameras public IList <Camera> GetCameras() { using (var db = new VideoAnalyticContext()) { return(db.Cameras.ToList()); } }
public IList <MonitorDataModel> GetEventsByCamera(string Id) { using (VideoAnalyticContext db = new VideoAnalyticContext()) { var events = db.Events.Where(i => i.Source == Id).ToList(); var models = events.Select(ConvertToMonitorData).OrderByDescending(i => i.Time).Take(40).ToList(); return(models); } }
public async Task<HttpResponseMessage> AddEdgeDevice(JObject body) { try { var deviceName = body?.GetValue("deviceName")?.Value<string>(); var deviceDescription = body?.GetValue("deviceDescription")?.Value<string>() ?? ""; var osType = body?.GetValue("osType")?.Value<string>(); var capacity = body?.GetValue("capacity")?.Value<int>() ?? 0; if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(osType)) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid body. deviceName,osType is required"); } using (var db = new VideoAnalyticContext()) { var device = await db.EdgeDevices.SingleOrDefaultAsync(d => d.Name.Equals(deviceName)); if (device != null) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, $"Device with name: {deviceName} already exits."); } var connectString = await IoTEdgeManager.AddDeviceAsync(deviceName); if (string.IsNullOrEmpty(connectString)) { return Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, $"Create Device with name: {deviceName} failed!."); } EdgeDevice edgeDevice = new EdgeDevice() { Name = deviceName, Description = deviceDescription, OSType = osType, Capacity = capacity, ConnectString = connectString, Status = EdgeDeviceStatus.Create.ToString(), CreatedOn = DateTime.UtcNow }; db.EdgeDevices.Add(edgeDevice); db.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, edgeDevice); } } catch (Exception e) { LogUtil.LogException(e); return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message); } }
public IHttpActionResult GetCamera(int id) { using (var db = new VideoAnalyticContext()) { Camera camera = db.Cameras.Find(id); if (camera == null) { return(NotFound()); } return(Ok(camera)); } }
public IHttpActionResult PostCamera(Camera camera) { using (var db = new VideoAnalyticContext()) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Cameras.Add(camera); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = camera.Id }, camera)); } }
public IHttpActionResult DeleteEvent(long id) { using (VideoAnalyticContext db = new VideoAnalyticContext()) { Event @event = db.Events.Find(id); if (@event == null) { return(NotFound()); } db.Events.Remove(@event); db.SaveChanges(); return(Ok(@event)); } }
public IHttpActionResult DeleteCamera(int id) { using (var db = new VideoAnalyticContext()) { Camera camera = db.Cameras.Find(id); if (camera == null) { return(NotFound()); } if (camera.Status == "NotActive") { db.Cameras.Remove(camera); db.SaveChanges(); } return(Ok(camera)); } }
// GET: api/EdgeDevices/CheckEdgeDeviceStatus public async Task<IHttpActionResult> CheckEdgeDeviceStatus() { using (var db = new VideoAnalyticContext()) { var edgeDevices = db.EdgeDevices.ToList(); foreach (var device in edgeDevices) { var isConnected = await IoTEdgeManager.CheckDeviceStatesAsync(device.Name); if (isConnected) { device.Status = EdgeDeviceStatus.Connected.ToString(); } else { device.Status = EdgeDeviceStatus.Disconnected.ToString(); } db.SaveChanges(); } return Ok(); } }
// POST: api/Cameras/Activate/1 public async Task <IHttpActionResult> Activate(int id) { using (var db = new VideoAnalyticContext()) { Camera camera = db.Cameras.Find(id); if (camera == null) { return(NotFound()); } var deviceId = camera.HostingDevice; EdgeDevice device = db.EdgeDevices.Find(deviceId); if (device == null) { return(NotFound()); } var configStr = device.Configurations; if (string.IsNullOrEmpty(configStr)) { configStr = File.ReadAllText($"{WebApiConfig.DataPath}/moduleTemplate/default.json"); } var properties = new JObject { { "RTSP", camera.Stream } }; var moduleName = camera.Name; var resultConfig = await IoTEdgeManager.AddModuleOnDeviceAsync(moduleName, device.Name, camera.Pipeline, properties, JObject.Parse(configStr)); device.Configurations = resultConfig; camera.Status = "Active"; db.SaveChanges(); return(Ok()); } }
public async Task<IHttpActionResult> DeleteEdgeDevice(int id) { using (var db = new VideoAnalyticContext()) { EdgeDevice edgeDevice = await db.EdgeDevices.SingleOrDefaultAsync(d => d.Id == id); if (edgeDevice == null) { return NotFound(); } var cameras = await db.Cameras.Where(d => d.HostingDevice == edgeDevice.Id).ToListAsync(); if (cameras.Count > 0) { return Content(HttpStatusCode.Forbidden, $"Cannot delete Edge Device {id}, since there are {cameras.Count} cameras connected. Delete all cameras connected to this device first. "); } await IoTEdgeManager.DeleteDeviceAsync(edgeDevice.Name); db.EdgeDevices.Remove(edgeDevice); db.SaveChanges(); return Ok(edgeDevice); } }
public void StartMessageProcess() { string eventHubName = ConfigurationManager.AppSettings["EventHubName"]; string eventHubConnectionString = ConfigurationManager.AppSettings["EventHubConnection"]; string consumerGroupSetting = ConfigurationManager.AppSettings["EventHubConsumerGroup"]; int eventHubPartitions = Int32.Parse(ConfigurationManager.AppSettings["EventHubPartitions"]); Console.WriteLine("Message Processing Started"); try { var eventhubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString, eventHubName); var ehConsumerGroup = eventhubClient.GetConsumerGroup(consumerGroupSetting); var cts = new CancellationTokenSource(); // start listening all event hub partitions for (int i = 0; i < eventHubPartitions; i++) { Task.Factory.StartNew((state) => { Debug.WriteLine("Starting worker to process partition: {0}", state); var receiver = ehConsumerGroup.CreateReceiver(state.ToString(), DateTime.UtcNow.AddDays(-10)); while (true) { try { var eventData = receiver.Receive(); if (eventData == null) { System.Threading.Thread.Sleep(300); continue; } string data = Encoding.UTF8.GetString(eventData.GetBytes()); JObject obj = JObject.Parse(data); var deviceId = (string)obj["moduleId"]; if (!string.IsNullOrWhiteSpace(deviceId)) { var type = "Alert"; var name = "UnSafe Action Detected"; using (VideoAnalyticContext db = new VideoAnalyticContext()) { Event e = new Event { Body = data, Source = deviceId, Type = type, Name = name, Time = DateTime.UtcNow }; db.Events.Add(e); db.SaveChanges(); } } } catch (Exception ex) { Debug.WriteLine("Exception in EventHubReader! Message = " + ex.Message); } if (cts.IsCancellationRequested) { Debug.WriteLine("Stopping: {0}", state); receiver.Close(); } } }, i); } } catch (Exception e) { Console.WriteLine(e); } }
private bool CameraExists(VideoAnalyticContext db, int id) { return(db.Cameras.Count(e => e.Id == id) > 0); }
private bool EventExists(VideoAnalyticContext db, long id) { return(db.Events.Count(e => e.Id == id) > 0); }