private void ProcessMessages(object obj) { while (!Token.IsCancellationRequested) { var nextmsg = _inputCache.Pop(); if (nextmsg.Length == 0) { if (_inputCache.Count == 0) { Thread.Sleep(ProcessMessagesSleepTimeout); } continue; } if (UdfTagMsg.TryParse(nextmsg, out var tagResult)) { _dbCache.Save(tagResult); } else if (UdfPosMsg.TryParse(nextmsg, out var posResult)) { _dbCache.Save(posResult); } else if (UdfSecMsg.TryParse(nextmsg, out var secResult)) { _dbCache.Save(secResult); } } }
public void CanReceiveAllMessageTypes() { var dbcache = new DbCache(); var tagMsg = new UdfTagMsg { TagId = 15, TagStatus = TagStatus.Inactive }; var posMsg = new UdfPosMsg { TagId = 15, SectorId = 44 }; var secMsg = new UdfSecMsg { SectorId = 44, SectorStatus = SectorStatus.Ok }; using (var receiver = new DbReceiver(_settings, dbcache)) using (var sender = new UdfMessageSender(_settings)) { receiver.Start(); sender.Send(tagMsg); sender.Send(posMsg); sender.Send(secMsg); Thread.Sleep(100); Assert.AreEqual(tagMsg.TagStatus, dbcache.GetTagStatus(tagMsg.TagId), "Tag status was not received"); Assert.AreEqual(posMsg.SectorId, dbcache.GetTagSector(posMsg.TagId), "Tag's sector is not correct"); Assert.AreEqual(secMsg.SectorStatus, dbcache.GetSectorStatus(secMsg.SectorId), "Sector status was not received"); } }
public void Save(UdfPosMsg model) { if (model.TagId > MaxTagAmount) { return; } SetTagSector(model.TagId, model.SectorId); }
public void Send(UdfPosMsg model) { var msg = new byte[12]; var type = new byte[] { Convert.ToByte('P'), Convert.ToByte('O'), Convert.ToByte('S'), 0 }; var tagId = BitConverter.GetBytes(model.TagId); var sectorId = BitConverter.GetBytes(model.SectorId); Array.Copy(type, 0, msg, 0, 4); Array.Copy(tagId, 0, msg, 4, 4); Array.Copy(sectorId, 0, msg, 8, 4); Send(msg); }
public void TryParseUdfPosMsgModel() { var model = new UdfPosMsg() { TagId = 21, SectorId = 45 }; var parsed = UdfPosMsg.TryParse(model.ToBytes(), out var result); Assert.IsTrue(parsed, "UdfPosMsg was not parsed"); Assert.IsNotNull(result, "result is null"); Assert.AreEqual(model.TagId, result.TagId, "TagId is not equal"); Assert.AreEqual(model.SectorId, result.SectorId, "SectorId is not equal"); }