static void Main(string[] args) { Console.WriteLine("--=== Press enter to start ===--"); Console.ReadLine(); var start = DateTime.Now; while (true) { using (var db = new DataContext()) { Console.WriteLine("Svirp tryllestav"); long rfid; var readLine = Console.ReadLine(); try { rfid = Convert.ToInt64(readLine); } catch { Console.WriteLine("Kunne ikke læse tag"); continue; } var now = DateTime.Now; Console.Clear(); if (start.AddMinutes(61) < now) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Tiden er udløbet!"); Console.ResetColor(); break; } var scout = db.Scouts .Include(s => s.MagicGamesInterval) .FirstOrDefault(s => s.Rfid == rfid); if (scout?.MagicGamesInterval == null) continue; var lastSwipe = scout.MagicGamesInterval.LastSwipe; if ((lastSwipe.HasValue && (now - lastSwipe.Value).Minutes == 0)) continue; var elapsed = (now - start).Minutes; if (elapsed == 0 || (elapsed % scout.MagicGamesInterval.Amount) != 0) continue; scout.MagicGamesInterval.LastSwipe = now; var point = new MagicGamesTimePoint { House = scout.House, Time = now }; db.MagicGamesTimePoints.Add(point); db.SaveChanges(); } } }
private static async Task Run() { int last; using (var db = new DataContext()) { last = await db.BoxterSwipes .Select(bs => bs.SwipeId) .DefaultIfEmpty(0) .MaxAsync(); } while (true) { try { Thread.Sleep(DELAY); using (var httpClient = new HttpClient()) { var json = await httpClient.GetStringAsync(URL); var dtos = JsonConvert.DeserializeObject<BoxterImportDto[]>(json) .Where(bi => bi.Id > last) .OrderBy(bi => bi.Id) .ToList(); if (!dtos.Any()) continue; bool tournoutPointAdded = false; using (var db = new DataContext()) { foreach (var dto in dtos) { long rfid; try { rfid = Convert.ToInt64(dto.Tag); } catch { if (_isWarnEnabled) _log.Warn($"Rfid not valid: {dto.Tag}"); continue; } var scout = await db.Scouts.FirstOrDefaultAsync(s => s.Rfid == rfid); last = dto.Id; if (scout == null) { if (_isWarnEnabled) _log.Warn($"Rfid not found: {rfid}"); continue; } var swipe = new BoxterSwipe { SwipeId = dto.Id, Scout = scout, BoxId = dto.BoxId, BoxIdFriendly = dto.BoxIdFriendly, AppMode = dto.AppMode, AppResponse = dto.AppResponse, CreateDate = dto.CreateDate }; db.BoxterSwipes.Add(swipe); if (dto.AppMode.Equals("Bogen", StringComparison.InvariantCultureIgnoreCase)) { tournoutPointAdded = true; var point = new TurnoutPoint { Amount = int.Parse(dto.AppResponse), HouseId = scout.HouseId, Time = dto.CreateDate }; db.TurnoutPoints.Add(point); } await db.SaveChangesAsync(); } } if (tournoutPointAdded) await httpClient.PostAsync("http://localhost/Api/Turnout/", null); _log.Info("Got new data from Boxter"); } } catch (Exception ex) { if (_isErrorEnabled) _log.Error(ex); } } }