コード例 #1
0
        public async Task DoWork(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                DateTime enow     = PropMgr.ESTNow;
                DateTime expected = new DateTime(enow.Year, enow.Month, enow.Day, 3, 0, 0);
                DateTime lastW    = (_csSettings.LastTaskUpdate != null) ? _csSettings.LastTaskUpdate : new DateTime(2020, 1, 1);
#if DEBUG
                double MinWait = 0;                                                                              // for testing
#else
                double MinWait = 720;                                                                            // 12 hrs
#endif
                if (((enow - lastW).TotalMinutes >= MinWait) || (Math.Abs((enow - expected).TotalMinutes) < 65)) // done not more than twice a day and covers DST change with delay/offset
                {
                    // Check Stock for possibly needed items
                    OrderedEvents         ordEvs         = new OrderedEvents(_context);
                    IList <InventoryItem> InventoryItems = _context.InventoryItem.Include(i => i.Inventory).Include(i => i.Item).ToList();
                    foreach (var invIt in InventoryItems)
                    {
                        if (IndexInvModel.MayNeedItem(_context, invIt, ordEvs))
                        {
                            // Item likely needs to be ordered. Mark as needed.
                            if (invIt.State == IndexInvModel.STOCKED_STATE)
                            {
                                invIt.State = IndexInvModel.NEEDED_STATE;
                                _context.Attach(invIt).State = EntityState.Modified;

                                try
                                {
                                    _context.SaveChanges();
                                    _csSettings.LastStockUpdate = PropMgr.ESTNow;
                                    _csSettings.Save();
                                    await Task.Run(() => IndexInvModel.NotifyNeedAsync(_hostEnv, _configuration, _userManager, "CStat:Stock> Needed : " + invIt.Item.Name, true)); // potentially clean Message log
                                }
                                catch (DbUpdateConcurrencyException)
                                {
                                    continue;
                                }
                            }
                        }
                    }

                    // Check for new Tasks
                    AutoGen ag = new AutoGen(_context);
                    _csSettings.LastTaskUpdate = PropMgr.ESTNow;
                    _csSettings.Save();
                    ag.GenTasks(_hostEnv);

                    // Notify users Tasks Due
                    CTask.NotifyUserTaskDue(_hostEnv, _configuration, _userManager, _context, 24, true); // Potentially clean log

                    // Persist Daily Reading and Notify if needed for Propane
                    PropaneMgr pmgr = new PropaneMgr(_hostEnv, _configuration, _userManager);
                    pmgr.CheckValue(pmgr.GetTUTank(true)); // get value, log to file and check

                    // Check for any unexpected propane usage (not potentially impacted from events) from recent daily usage
                    var plList = pmgr.GetAll(3);
                    var plCnt  = plList.Count;
                    if (plCnt > 1)
                    {
                        for (int i = 0; i < plCnt - 1; ++i)
                        {
                            var plStart   = plList[i];
                            var plEnd     = plList[i + 1];
                            var dateRange = new DateRange(plStart.ReadingTime, plEnd.ReadingTime);

                            var evList = ag.GetImpactingEvents(dateRange);
                            if (evList.Count == 0) // no impacting events
                            {
                                var totalHrs = dateRange.TotalHours();
                                if ((totalHrs > 0) && _csSettings.GetPropaneProperties(out double tankGals, out double pricePerGal))
                                {
                                    var totalGals = ((plStart.LevelPct - plEnd.LevelPct) / 100) * tankGals;
                                    if ((totalGals / totalHrs) > ((double)2 / 24)) // averaging more than 2 gals/day?
                                    {
                                        // Send a one time alert (allowResend = false)
                                        CSSMS sms = new CSSMS(_hostEnv, _configuration, _userManager);
                                        sms.NotifyUsers(CSSMS.NotifyType.EquipNT, "CStat:Equip> " + "Non Event Propane Usage was " + totalGals.ToString("0.##") + " gals. during " + totalHrs.ToString("0.#") + " hours starting " +
                                                        plStart.ReadingTime.Month + "/" + plStart.ReadingTime.Day + "/" + plStart.ReadingTime.Year % 100, false, true); // potentially clean Message Log
                                    }
                                }
                            }
                        }
                    }

                    // Check for new EMails to store
                    CSEMail.SaveEMails(_configuration, _userManager);

                    // Check/Truncate Size of Arduino file
                    ArdMgr amgr = new ArdMgr(_hostEnv, _configuration, _userManager);
                    amgr.GetAll(true);

                    // Clean/{Reset to full view} Camera
                    using (var ptz = new PtzCamera())
                    {
                        ptz.Cleanup(_hostEnv);
                    }

                    _logger.LogInformation($"CStat Daily Updates Completed at {PropMgr.ESTNow}");
                }

                // Run again at 3:00 AM
                DateTime now          = PropMgr.ESTNow;
                DateTime tom          = now.AddDays(1);
                DateTime start        = new DateTime(tom.Year, tom.Month, tom.Day, 3, 0, 0); // Restart at 3:00 AM tomorrow
                int      MSecsToStart = (int)Math.Round((start - now).TotalMilliseconds);
                await System.Threading.Tasks.Task.Delay(MSecsToStart, stoppingToken);
            }
        }
コード例 #2
0
ファイル: Edit.cshtml.cs プロジェクト: ronripp/CStat
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if ((_Event.Description == null) || (_Event.Description.Trim().Length == 0) || (_Event.Type <= 0))
            {
                UpdateViewData("Event Type and/or Event Description must be set.");
                return(Page());
            }

            if (!ModelState.IsValid)
            {
                UpdateViewData("One or more fields are not valid.");
                return(Page());
            }

            if (_Event.ChurchId == -1)
            {
                _Event.ChurchId = null;
            }

            if (_Event.StartTime >= _Event.EndTime)
            {
                UpdateViewData("End Date/Time must be later than Start Date/Time.");
                return(Page());
            }

            // Check if Start or End Dates have changed and delete tasks associated with event and generate new ones.
            int NumTasksRemoved = RemoveChangedEventTasks(_context, _Event);

            UpdateAttendance(_Event.Id, _Event.Staff ?? 0, _Staff);

            _context.Attach(_Event).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EventExists(_Event.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            if (NumTasksRemoved > 0)
            {
                // Regenerate tasks based on event now
                var autogen = new AutoGen(_context);
                autogen.GenTasks(_hostEnv);
            }

            if (string.IsNullOrEmpty(_RedirectURL))
            {
                return(RedirectToPage("./Index"));
            }
            _RedirectURL = "";
            return(NotFound());
        }