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(UdfSecMsg model) { if (model.SectorId > MaxSecAmount) { return; } SetSectorStatus(model.SectorId, model.SectorStatus); }
public void Send(UdfSecMsg model) { var msg = new byte[12]; var type = new byte[] { Convert.ToByte('S'), Convert.ToByte('E'), Convert.ToByte('C'), 0 }; var tagId = BitConverter.GetBytes(model.SectorId); var secStatus = BitConverter.GetBytes((int)model.SectorStatus); Array.Copy(type, 0, msg, 0, 4); Array.Copy(tagId, 0, msg, 4, 4); Array.Copy(secStatus, 0, msg, 8, 4); Send(msg); }
public void TryParseUdfSecMsgModel() { var model = new UdfSecMsg() { SectorId = 45, SectorStatus = SectorStatus.Ok }; var parsed = UdfSecMsg.TryParse(model.ToBytes(), out var result); Assert.IsTrue(parsed, "UdfSecMsg was not parsed"); Assert.IsNotNull(result, "result is null"); Assert.AreEqual(model.SectorId, result.SectorId, "SectorId is not equal"); Assert.AreEqual(model.SectorStatus, result.SectorStatus, "SectorStatus is not equal"); }