コード例 #1
0
        private string MakeJsonPayload(SendAlertModel sam)
        {
            string errorMessage = $"The machine {sam.MachineId} has error {sam.AlarmCode}!";
            string jsonPayload  = JsonConvert.SerializeObject(new { data = new { message = errorMessage } });

            return(jsonPayload);
        }
コード例 #2
0
        private void CreateAlarmLog(SendAlertModel sam)
        {
            AlarmSystem.Core.Entity.Dto.Alarm   alarm   = _alarmService.GetAlarmByCode(sam.AlarmCode);
            AlarmSystem.Core.Entity.Dto.Machine machine = _machineService.GetMachineById(sam.MachineId);
            var  date       = DateTime.UtcNow;
            long epochOfNow = new DateTimeOffset(date).ToUnixTimeMilliseconds();

            AlarmSystem.Core.Entity.Dto.AlarmLog al = new AlarmSystem.Core.Entity.Dto.AlarmLog()
            {
                Alarm = alarm, Machine = machine, Date = epochOfNow
            };

            _alarmLogService.CreateAlarmLog(al);
        }
コード例 #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "notify")] HttpRequest req,
            ILogger log)
        {
            string         requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            SendAlertModel sam         = JsonConvert.DeserializeObject <SendAlertModel>(requestBody);

            List <string> watches = new List <string>();

            AlarmSystem.Core.Entity.DB.AlarmLog alarmLog;

            try
            {
                watches  = GetWatchesToNotify(sam);
                alarmLog = CreateAlarmLog(sam);
            }
            catch (InvalidDataException e)
            {
                return(new BadRequestObjectResult(e.Message));
            }
            catch (EntityNotFoundException e)
            {
                return(new NotFoundObjectResult(e.Message));
            }

            string accessSignature = Environment.GetEnvironmentVariable("DefaultFullSharedAccessSignature");
            string hubName         = Environment.GetEnvironmentVariable("NotificationHubName");

            Microsoft.Azure.NotificationHubs.Notification nof = new FcmNotification(MakeJsonPayload(alarmLog));

            if (watches.Count == 0)
            {
                return(new NoContentResult());
            }

            foreach (string watch in watches)
            {
                await _hub.SendDirectNotificationAsync(nof, watch);
            }

            return(new OkResult());
        }
コード例 #4
0
        private List <string> GetWatchesToNotify(SendAlertModel sam)
        {
            List <string>       watches = new List <string>();
            List <MachineWatch> machineSubscriptions = _watchService.GetMachineSubscriptionsByMachine(sam.MachineId);
            List <AlarmWatch>   alarmSubscriptions   = _watchService.GetAlarmSubscriptionsByAlarmCode(sam.AlarmCode);

            foreach (MachineWatch mw in machineSubscriptions)
            {
                watches.Add(mw.WatchId);
            }

            //Since a watch can both be subscribed to a machine and an alarm. We don't want to send the same notification twice to one watch
            //This should probably be optimised so we can combine the two subscriptions list without duplicates
            foreach (AlarmWatch aw in alarmSubscriptions)
            {
                bool alreadyIn = watches.Contains(aw.WatchId);

                if (!alreadyIn)
                {
                    watches.Add(aw.WatchId);
                }
            }
            return(watches);
        }