Example #1
0
        public async Task <IHttpActionResult> PostTurnout(TurnoutPointDto dto)
        {
            var entity = new TurnoutPoint()
            {
                HouseId   = dto.HouseId,
                Amount    = dto.Amount,
                Time      = DateTime.Now,
                Discarded = false,
            };

            Db.TurnoutPoints.Add(entity);
            await Db.SaveChangesAsync();

            dto = await Db.TurnoutPoints.Select(ToDto()).SingleAsync(p => p.Id == entity.Id);

            Hub.Clients.All.Add(dto);
            ScoreUpdated();

            return(CreatedAtRoute("DefaultApi", new { id = dto.Id }, dto));
        }
        public async Task <IHttpActionResult> PostTurnoutSwipe(TurnoutSwipeDto dto)
        {
            using (var reader = new StringReader(dto.Data))
            {
                string line;
                while ((line = await reader.ReadLineAsync()) != null)
                {
                    var match = Regex.Match(line, @"^([+-])\x02\w{2}(\w{8})\w{2}\x03\x03$");
                    if (!match.Success)
                    {
                        continue;
                    }

                    var sign    = match.Groups[1].ToString();
                    var hexRfid = match.Groups[2].ToString();
                    var rfid    = Convert.ToInt64(hexRfid, 16);

                    var scout = await Db.Scouts.FirstOrDefaultAsync(s => s.Rfid == rfid);

                    if (scout == null)
                    {
                        continue;
                    }

                    var point = new TurnoutPoint
                    {
                        Amount  = sign == "+" ? 1 : -1,
                        HouseId = scout.HouseId,
                        Time    = DateTime.Now
                    };
                    Db.TurnoutPoints.Add(point);
                    await Db.SaveChangesAsync();

                    ScoreUpdated();
                }
                return(Ok());
            }
        }
Example #3
0
        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);
                    }
                }
            }
        }