public async Task PublishOutput(MbapHeader header) { if (!NativeEnabled && !AppInsightsEnabled) { return; } if (cache.Contains(header.TransactionId.ToString())) { long ticks = cache.Get <long>(header.TransactionId.ToString()); cache.Remove(header.TransactionId.ToString()); TimeSpan ts = TimeSpan.FromTicks(DateTime.Now.Ticks - ticks); DiagnosticsEvent telem = new DiagnosticsEvent(name, header.UnitId, header.TransactionId, Math.Round(ts.TotalMilliseconds), DateTime.UtcNow.ToString("dd-MM-yyyyThh:mm:ss.ffff")); if (NativeEnabled && mqttClient != null && mqttClient.IsConnected) { string jsonString = JsonConvert.SerializeObject(telem); byte[] data = Encoding.UTF8.GetBytes(jsonString); await mqttClient.PublishAsync(QualityOfServiceLevelType.AtMostOnce, outputPiSystem, "application/json", data); } if (AppInsightsEnabled) { tclient?.TrackEvent(name, telem.GetEventProperties(), telem.GetEventMetrics()); } } }
public void EvictItemAfterTtlExpires() { byte[] encryptedKey = { 4, 8, 15, 16, 23, 42 }; LocalCache <byte[], byte[]> cache = new LocalCache <byte[], byte[]>(); cache.TimeToLive = TimeSpan.FromSeconds(1); cache.GetOrCreate(encryptedKey, createItem); Assert.Equal(1, cache.Count); Assert.True(cache.Contains(encryptedKey), "The cache should contain the key."); Thread.Sleep(1100); Assert.False(cache.Contains(encryptedKey), "The cache should have expired the key."); Assert.Equal(0, cache.Count);
public async Task PublishOutput(MbapHeader header, ushort transactionId) { if (!NativeEnabled && !AppInsightsEnabled) { return; } DiagnosticsEvent telem = new DiagnosticsEvent(name, header.UnitId, transactionId, header.TransactionId, DateTime.UtcNow.ToString("dd-MM-yyyyThh:mm:ss.ffff")); if (NativeEnabled && mqttClient.IsConnected) { string jsonString = JsonConvert.SerializeObject(telem); byte[] msg = Encoding.UTF8.GetBytes(jsonString); await mqttClient.PublishAsync(SkunkLab.Protocols.Mqtt.QualityOfServiceLevelType.AtMostOnce, outputPiSystem, "application/json", msg); } if (AppInsightsEnabled) { tclient?.TrackEvent(name, telem.GetEventProperties(), telem.GetEventMetrics()); } if (cache.Contains(transactionId.ToString())) { Tuple <byte, long> tuple = cache.Get <Tuple <byte, long> >(transactionId.ToString()); cache.Remove(transactionId.ToString()); TimeSpan ts = TimeSpan.FromTicks(DateTime.Now.Ticks - tuple.Item2); DiagnosticsEvent vdts = new DiagnosticsEvent(name, header.UnitId, transactionId, ts.TotalMilliseconds, DateTime.UtcNow.ToString("dd-MM-yyyyThh:mm:ss.ffff")); if (NativeEnabled && mqttClient != null && mqttClient.IsConnected) { string jsonString = JsonConvert.SerializeObject(vdts); byte[] data = Encoding.UTF8.GetBytes(jsonString); await mqttClient.PublishAsync(SkunkLab.Protocols.Mqtt.QualityOfServiceLevelType.AtMostOnce, outputPiSystem, "application/json", data); } if (AppInsightsEnabled) { tclient?.TrackEvent(name, vdts.GetEventProperties(), vdts.GetEventMetrics()); } } }
public byte[] MapOut(byte[] message) { MbapHeader header = MbapHeader.Decode(message); string key = GetProxyMap(header.UnitId, header.TransactionId); if (cache.Contains(key)) { Tuple <ushort, byte> tuple = (Tuple <ushort, byte>)cache[key]; header.TransactionId = tuple.Item1; header.UnitId = tuple.Item2; cache.Remove(key); byte[] buffer = new byte[message.Length]; byte[] src = header.Encode(); Buffer.BlockCopy(src, 0, buffer, 0, src.Length); Buffer.BlockCopy(message, src.Length, buffer, src.Length, message.Length - src.Length); return(buffer); } return(null); }
private void ModbusMessageReceived(string resource, string contentType, byte[] message) { try { MbapHeader header = MbapHeader.Decode(message); string key = GetCacheKey(header); if (cache.Contains(key)) { cache.Remove(key); OnReceive?.Invoke(this, new ChannelReceivedEventArgs(channel.Id, message)); logger?.LogDebug("Output channel received message."); diag?.PublishOutput(header).GetAwaiter(); } else { logger?.LogWarning("Vrtu channel cannot match received message."); } } catch (Exception ex) { logger?.LogError(ex, "Fault receiving message on vrtu channel."); } }
/// <summary> /// Determines whether the <see cref="IDictionary"/> object contains /// an element with the specified key. /// </summary> /// <returns> /// <b>true</b> if the <b>IDictionary</b> contains an element with /// the key; otherwise, <b>false</b>. /// </returns> public virtual bool Contains(object key) { return(LocalCache.Contains(key)); }