Example #1
0
        public async Task <IActionResult> Alert([FromRoute] string deviceId)
        {
            if (!await _deviceService.ExistsAsync(deviceId))
            {
                return(BadRequest("Device does not exist"));
            }

            var alert = new Alert();

            alert.DeviceId  = deviceId;
            alert.Cancelled = false;

            var lastHeartbeat = _heartbeatService.GetAll()
                                .Where(h => h.DeviceId == deviceId)
                                .OrderByDescending(h => h.Timestamp)
                                .FirstOrDefault();

            //var lastHeartbeat = (await _deviceService.GetByIdAsync(deviceId))
            //    .Heartbeats
            //    .OrderByDescending(h => h.Timestamp)
            //    .FirstOrDefault();

            if (lastHeartbeat != null)
            {
                alert.LocationLat  = lastHeartbeat.Lattitue;
                alert.LocationLong = lastHeartbeat.Longitude;
            }

            await _alertService.AddAsync(alert);

            // Send alert to all connected users

            return(Ok());
        }