/// <summary> /// /// </summary> /// <param name="frameworkId"></param> public void SetPacketStatus(string packetId, PacketStatus status, int?frameworkId = null) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.Find <Part>(packetId); if (source == null) { throw new NullReferenceException(); } if (status == PacketStatus.TransferredToDevice && frameworkId == null) { throw new ArgumentException("Framework id must be specified"); } realm.Write(() => { source.Status = (int)status; source.FrameworkId = frameworkId ?? source.FrameworkId; if (status == PacketStatus.Transmitted) { source.TransmittedDate = DateTimeOffset.UtcNow; } else { source.TransmittedDate = DateTimeOffset.MinValue; } }); } } }
/// <summary> /// /// </summary> /// <param name="packetId"></param> /// <param name="status"></param> /// <param name="frameworkId"></param> public void SetPacketStatus(int frameworkId, PacketStatus status) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.All <Part>().SingleOrDefault(x => x.FrameworkId == frameworkId); if (source == null) { throw new NullReferenceException(); } realm.Write(() => { source.Status = (int)status; if (status == PacketStatus.Transmitted) { source.TransmittedDate = DateTimeOffset.UtcNow; } else { source.TransmittedDate = DateTimeOffset.MinValue; } }); } } }
/// <summary> /// /// </summary> public void DeleteAll() { lock (locker) { string path = PacketBufferHelper.GetBufferDbPath(); if (File.Exists(path)) { File.Delete(path); } } }
/// <summary> /// /// </summary> /// <returns></returns> public int GetTransmittingPackets() { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { return(realm .All <Part>() .Where(x => x.Status == (int)PacketStatus.TransferredToDevice) .Count()); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public uint GetPacketCount(uint group, PacketDirection direction) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var count = realm .All <Part>() .Where(x => x.Group == group && x.Direction == (int)direction) .Count(); return((uint)count); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> public void DeletePackets(uint group, PacketDirection direction) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var toRemove = realm.All <Part>().Where(x => x.Group == group && x.Direction == (int)direction); realm.Write(() => { realm.RemoveRange(toRemove); }); } } }
/// <summary> /// /// </summary> public void SetMessageSendAttempt(string id, int attempt) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var message = realm.Find <MessageRealm>(id); realm.Write(() => { message.SendAttempt = attempt; }); } } }
/// <summary> /// /// </summary> /// <param name="frameworkId"></param> /// <returns></returns> public Packet GetPacket(int frameworkId) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.All <Part>().LastOrDefault(x => x.FrameworkId == frameworkId); if (source == null) { return(null); } return(BuildPacket(source, includePayload: true)); } } }
/// <summary> /// /// </summary> /// <param name="packetId"></param> /// <returns></returns> public Packet GetPacket(string packetId) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.Find <Part>(packetId); if (source == null) { return(null); } return(BuildPacket(source, includePayload: true)); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public List <Packet> GetPackets(uint group, PacketDirection direction, bool includePayload = true) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var parts = realm .All <Part>() .Where(x => x.Group == group && x.Direction == (int)direction) .ToList() .Select(x => BuildPacket(x, includePayload)) .ToList(); return(parts); } } }
/// <summary> /// /// </summary> /// <param name="frameworkId"></param> /// <returns></returns> public Message GetMessageById(string messageId) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.Find <MessageRealm>(messageId); if (source == null) { return(null); } return(BuildMessage(source)); } } }
/// <summary> /// /// </summary> /// <param name="group"></param> /// <returns></returns> public Message GetMessageByGroup(uint group, PacketDirection direction) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var source = realm.All <MessageRealm>().SingleOrDefault(x => x.Group == group /*&& x.Direction == direction*/); if (source == null) { return(null); } return(BuildMessage(source)); } } }
/// <summary> /// /// </summary> /// <param name="groupId"></param> public void DeleteMessage(uint groupId) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var toDelete = realm.All <MessageRealm>().Where(x => x.Group == groupId); if (toDelete.Any()) { realm.Write(() => { realm.RemoveRange(toDelete); }); } } } }
/// <summary> /// /// </summary> /// <param name="packet"></param> public void SavePacket(Packet packet) { lock (locker) { if (string.IsNullOrEmpty(packet.Id)) { throw new ArgumentNullException("Packet id is null or empty"); } using (var realm = PacketBufferHelper.GetBufferInstance()) { #if DEBUG var __part = realm.Find <Part>(packet.Id); var __part2 = realm.All <Part>().SingleOrDefault(x => x.Group == packet.Group && x.Index == packet.Index && x.Direction == (int)packet.Direction); if (__part != null || __part2 != null) { Debugger.Break(); //throw new InvalidOperationException($"Packet with group `{packet.Group}` already saved in buffer"); } #endif realm.Write(() => { realm.Add(new Part() { Id = packet.Id, FrameworkId = packet.FrameworkId, Group = (int)packet.Group, Direction = (int)packet.Direction, Index = (int)packet.Index, TotalParts = (int)packet.TotalParts, Payload = packet.Payload, Status = (int)PacketStatus.None, TransmittedDate = DateTimeOffset.MinValue, }, update: true); }); } } }
/// <summary> /// /// </summary> /// <param name="message"></param> public void SaveMessage(Message message) { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { realm.Write(() => { realm.Add(new MessageRealm() { Id = message.Id, Date = DateTime.UtcNow, Group = message.Group, TotalParts = message.TotalParts, Type = (int)message.Type }); }); } } }
/// <summary> /// /// </summary> public void ResetHungPackets() { lock (locker) { using (var realm = PacketBufferHelper.GetBufferInstance()) { var sources = realm .All <Part>() .Where(x => x.Status == (int)PacketStatus.SendingToDevice) .ToList(); realm.Write(() => { foreach (var source in sources) { source.Status = (int)PacketStatus.None; } }); } } }