Ejemplo n.º 1
0
        public IHttpActionResult GetStatus([FromBody] ControllerStatusViewModel model)
        {
            var result = this.service.UpdateDeviceStatus(model);

            if (result == true)
            {
                return(Ok());
            }
            else
            {
                return(InternalServerError());
            }
        }
Ejemplo n.º 2
0
        public bool UpdateDeviceStatus(ControllerStatusViewModel receivedItem)
        {
            try
            {
                using (var context = this.GetDataContext())
                {
                    var log = context.Logs.FirstOrDefault(it => it.DeviceId == receivedItem.DeviceId);
                    if (log == null)
                    {
                        log                = CreateLog(receivedItem.DeviceId, receivedItem.SystemDateTime);
                        log.IsSended       = true;
                        log.LogDateTime    = DateTime.Now;
                        log.SystemDateTime = receivedItem.SystemDateTime;
                        context.Logs.Add(log);
                    }
                    else
                    {
                        log.IsSended       = true;
                        log.LogDateTime    = DateTime.Now;
                        log.SystemDateTime = receivedItem.SystemDateTime;
                    }
                    context.SaveChanges();
                    var status = context.Statuses.FirstOrDefault(it => it.LogId == log.Id && it.TypeId == receivedItem.TypeId);
                    if (status == null)
                    {
                        status = CreateStatus(log.Id, receivedItem.TypeId, receivedItem.Value);
                        //setting IsSended on Log to send email notifications - only if staatus is change from working to not working
                        if (status.Value.Equals(0))
                        {
                            log.IsSended = false;
                        }
                        context.Statuses.Add(status);
                    }
                    else
                    {
                        if (status.Value.Equals(1) && receivedItem.Value.Equals(0))
                        {
                            log.IsSended = false;
                        }
                        status.Value = receivedItem.Value;
                    }
                    context.SaveChanges();

                    var logH = AutoMapper.Mapper.Map <LogHistory>(log);
                    context.LogHistories.Add(logH);
                    context.SaveChanges();
                    logH.LogDateTime    = DateTime.Now;
                    logH.SystemDateTime = receivedItem.SystemDateTime;

                    var historyStatus = AutoMapper.Mapper.Map <StatusHistory>(status);
                    historyStatus.LogHistoryId = logH.Id;
                    context.StatusHistories.Add(historyStatus);
                    status.Value = receivedItem.Value;

                    context.SaveChanges();
                    return(true);
                }
            }

            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(false);
            }
        }