private List <PropaneLevel> GetFullPropaneList(PropaneMgr pmgr, PropaneLevel plNow) { var plList = pmgr.GetAll(); if (plNow != null) { var plCnt = plList.Count; if ((plCnt > 0) && (!plList[plCnt - 1].IsSame(plNow))) { plList.Add(plNow); } } return(plList); }
public EquipModel(CStat.Models.CStatContext context, IWebHostEnvironment hostEnv, IConfiguration config, UserManager <CStatUser> userManager) { _context = context; _hostEnv = hostEnv; _ardMgr = new ArdMgr(_hostEnv, _config, userManager); _config = config; Settings = CSSettings.GetCSSettings(_config, userManager); PropaneMgr pmgr = new PropaneMgr(_hostEnv, _config, userManager); _ar = _ardMgr.GetLast(); if (_ar == null) { _ar = new ArdRecord(PropMgr.NotSet, PropMgr.NotSet, PropMgr.NotSet, 0, PropMgr.ESTNow); } _pl = pmgr.GetTUTank(); if (_pl == null) { _pl = new PropaneLevel(0, PropMgr.ESTNow, PropMgr.NotSet); } ActiveEqHistory = new List <List <double> >(); int NumActive = Settings.ActiveEquip.Count; if (NumActive > 0) { List <ArdRecord> ardHist = _ardMgr.GetAll(); if (ardHist == null) { ardHist = new List <ArdRecord>(); } List <PropaneLevel> propaneHist = pmgr.GetAll(30); if (propaneHist == null) { propaneHist = new List <PropaneLevel>(); } // Ensure latest is at end of list int ahCount = ardHist.Count; if (ahCount > 0) { if (ardHist[ahCount - 1].TimeStamp != _ar.TimeStamp) { if (ardHist[ahCount - 2].TimeStamp == _ar.TimeStamp) { _ar = _ardMgr.GetLast(); // get latest } else { ardHist.Add(_ar); // set latest if (ahCount >= ArdMgr.MAX_USE_ARS) { ardHist.RemoveAt(0); } } } } int plCount = propaneHist.Count; if (plCount > 0) { if (propaneHist[plCount - 1].ReadingTime != _pl.ReadingTime) { if (propaneHist[plCount - 2].ReadingTime == _pl.ReadingTime) { _pl = pmgr.GetTUTank(); // get latest } else { propaneHist.Add(_pl); // set latest if (plCount >= PropaneMgr.MAX_USE_PLS) { propaneHist.RemoveAt(0); } } } } foreach (var ar in Settings.ActiveEquip) { List <double> dlist; dlist = ar.PropName switch { "freezerTemp" => ardHist.Select(a => a.FreezerTempF).Reverse().ToList(), "frigTemp" => ardHist.Select(a => a.FridgeTempF).Reverse().ToList(), "kitchTemp" => ardHist.Select(a => a.KitchTempF).Reverse().ToList(), "propaneTank" => propaneHist.Select(p => p.LevelPct).Reverse().ToList(), "waterPres" => ardHist.Select(a => a.WaterPress).Reverse().ToList(), _ => new List <double>() }; ActiveEqHistory.Add(dlist); } } }
private string HandleEquip(List <string> words) { string result = ""; if (_cmdSrc == CmdSource.PROPANE) { // Persist Daily Reading and Notify if needed for Propane PropaneMgr pmgr = new PropaneMgr(_hostEnv, _config, _userManager); var plNow = pmgr.GetTUTank(); // get value, log to file and check if (plNow == null) { return(result); } if (_cmdDateRange != null) { var plList = GetFullPropaneList(pmgr, plNow); var plCnt = plList.Count; int NumOut = 0; for (int i = 0; i < plCnt; ++i) { var pl = plList[i]; if (_cmdDateRange.In(pl.ReadingTime)) { result = AppendPropLine(pmgr, plList[i], result); if (++NumOut == 31) { return(result); } } } } else if ((_cmdInstsList.FindAll(c => (c == CmdInsts.LAST) || (c == CmdInsts.CURRENT))).Any()) { if (_cmdNumber < 1) { _cmdNumber = 1; } if (_cmdNumber == 1) { return(AppendPropLine(pmgr, plNow, result)); } var plList = GetFullPropaneList(pmgr, plNow); var plCnt = plList.Count; var NumOut = _cmdNumber > 31 ? 31 : _cmdNumber; for (int i = plCnt - 1; i >= 0; --i) { result = AppendPropLine(pmgr, plList[i], result); if (--NumOut == 0) { break; } } } else if ((_cmdInstsList.FindAll(c => (c == CmdInsts.PRIOR))).Any()) { if (_cmdNumber < 1) { _cmdNumber = 1; } var plList = pmgr.GetAll(_cmdNumber + 1); var plCnt = plList.Count; var NumOut = _cmdNumber; for (int i = plCnt - 1; i >= 0; --i) { if (plList[i].IsSame(plNow)) { continue; } result = AppendPropLine(pmgr, plList[i], result); if (--NumOut == 0) { break; } } } else if ((_cmdInstsList.FindAll(c => (c == CmdInsts.FIRST))).Any()) { var plList = GetFullPropaneList(pmgr, plNow); var plCnt = plList.Count; var NumOut = (_cmdNumber > 0) ? ((_cmdNumber > 31) ? 31 : _cmdNumber) : ((plCnt > 31) ? 31 : plCnt); if (NumOut > plCnt) { NumOut = plCnt; } for (int i = 0; i < plCnt; ++i) { result = AppendPropLine(pmgr, plList[i], result); if (--NumOut == 0) { break; } } } else { return(AppendPropLine(pmgr, plNow, result)); } } else if (_cmdSrc == CmdSource.ELECTRIC) { var powMgr = new PowerMgr(); double totKWHrs = 0; bool success = false; string powStr = ""; if (_cmdDateRange != null) { success = powMgr.TryGetKWHrs(_cmdDateRange.Start, _cmdDateRange.End, out totKWHrs); if (_cmdDateRange.Start.Date != _cmdDateRange.End.Date) { powStr = "Power used from " + _cmdDateRange.Start.Month + "/" + _cmdDateRange.Start.Day + "/" + _cmdDateRange.Start.Year % 100 + " to " + _cmdDateRange.End.Month + "/" + _cmdDateRange.End.Day + "/" + _cmdDateRange.End.Year % 100 + " = " + totKWHrs + " KWHrs"; } else { powStr = "Power used on " + _cmdDateRange.Start.Month + "/" + _cmdDateRange.Start.Day + "/" + _cmdDateRange.Start.Year % 100 + " = " + totKWHrs + " KWHrs"; } } else { DateTime Date = PropMgr.ESTNow; success = powMgr.TryGetKWHrs(Date, Date, out totKWHrs); powStr = "Power used on " + Date.Month + "/" + Date.Day + "/" + Date.Year % 100 + " = " + totKWHrs + " KWHrs"; } result = success ? powStr : "Sorry. Unable to get Total KwHrs now."; } else { result = "Equipment Report :\n"; ArdMgr am = new ArdMgr(_hostEnv, _config, _userManager); var ar = am.ReportLastestValues(ref result, false); // Ard only : No propane } return((result.Length == 0) ? "Huh?" : result); }
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); } }