Exemplo n.º 1
0
        public ActionResult SaveMyAlarms(List <String> alarms)
        {
            if (alarms != null)
            {
                db.AlarmSubscriptions.RemoveRange(db.AlarmSubscriptions.Where(p => p.UserEmail == HttpContext.User.Identity.Name));

                foreach (var alarm in alarms)
                {
                    AlarmSubscription sub = new AlarmSubscription();

                    sub.UserEmail           = HttpContext.User.Identity.Name;
                    sub.SubscribedAlarmName = alarm.ToUpper();

                    sub.CreatedBy  = HttpContext.User.Identity.Name;
                    sub.ModifiedBy = HttpContext.User.Identity.Name;
                    sub.ModifiedOn = DateTime.Now;
                    sub.CreatedOn  = DateTime.Now;

                    db.AlarmSubscriptions.Add(sub);
                }

                db.SaveChanges();
            }


            return(Json(true, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            AlarmSubscription alarmSubscription = await db.AlarmSubscriptions.FindAsync(id);

            db.AlarmSubscriptions.Remove(alarmSubscription);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AlarmSubscription alarmSubscription = await db.AlarmSubscriptions.FindAsync(id);

            if (alarmSubscription == null)
            {
                return(HttpNotFound());
            }
            return(View(alarmSubscription));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,UserEmail,SubscribedAlarmName,CreatedOn,CreatedByy")] AlarmSubscription alarmSubscription)
        {
            if (ModelState.IsValid)
            {
                alarmSubscription.ModifiedBy = HttpContext.User.Identity.Name;
                alarmSubscription.ModifiedOn = DateTime.Now;

                db.Entry(alarmSubscription).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(alarmSubscription));
        }
Exemplo n.º 5
0
        internal async Task RemoveAlarmSubscriptionAsync(AlarmSubscription subscription)
        {
            var userId = subscription.CallbackHandler.Id;

            if (_subscriptions.TryRemove(userId, out _))
            {
                if (_alarmIndicationHandler.TryRemove(userId, out _))
                {
                    if (!_alarmIndicationHandler.Any())
                    {
                        await DisableAlarmUpdatesAsync().ConfigureAwait(false);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public AlarmSubscription CreateAlarmSubscription()
        {
            var userId       = GetNextReferenceId();
            var waitHandler  = new CallbackHandler <S7AlarmIndicationDatagram>(userId);
            var subscription = new AlarmSubscription(this, waitHandler);

            if (_alarmIndicationHandler.TryAdd(waitHandler.Id, waitHandler))
            {
                if (_subscriptions.TryAdd(waitHandler.Id, subscription))
                {
                    return(subscription);
                }
            }
            ThrowHelper.ThrowException(new InvalidOperationException());
            return(null);
        }
Exemplo n.º 7
0
        internal async Task <AlarmUpdateResult> ReceiveAlarmUpdatesAsync(AlarmSubscription subscription, CancellationToken ct)
        {
            if (_closeCalled || ConnectionState != ConnectionState.Opened)
            {
                ThrowHelper.ThrowNotConnectedException();
            }

            if (!await EnableAlarmUpdatesAsync().ConfigureAwait(false))
            {
                ThrowHelper.ThrowNotConnectedException();
            }

            if (subscription.CallbackHandler != null)
            {
                if (subscription.CallbackHandler.Event != null)
                {
                    subscription.CallbackHandler.Event.Reset();
                    if (!subscription.TryGetDatagram(out var result))
                    {
                        await subscription.CallbackHandler.Event.WaitAsync(ct).ConfigureAwait(false);

                        if (subscription.TryGetDatagram(out result) && result != null)
                        {
                            return(new AlarmUpdateResult(_alarmUpdateHandler?.Id == 0, result.AlarmMessage.Alarms.ToList(), null));
                        }
                        else
                        {
                            _logger?.LogDebug("AlarmIndication handler received with null result for handler id {0}.", subscription.CallbackHandler.Id);
                        }
                    }
                    else if (result != null)
                    {
                        return(new AlarmUpdateResult(_alarmUpdateHandler?.Id == 0, result.AlarmMessage.Alarms.ToList(), null));
                    }
                }
                else
                {
                    _logger?.LogWarning("Alarm indication handler with id {0} has currently no event handler.", subscription.CallbackHandler.Id);
                }
            }
            else
            {
                _logger?.LogWarning("Could not add alarm indication handler with id {0} to handler list", subscription.CallbackHandler.Id);
            }

            return(new AlarmUpdateResult(_alarmUpdateHandler?.Id == 0, null));
        }