public bool ProcessingEvent(short siteId, string DeviceName, string GroupName, ushort Value, ref EventSummary summary) { if (eventElements.ContainsKey(GroupName) == false) { return(false); } string event_unique_id = $"{siteId}.{DeviceName}.{GroupName}"; byte[] key_ = Encoding.UTF8.GetBytes(event_unique_id); byte[] value_ = BitConverter.GetBytes(Value); ushort OldValue = 0; if (txn.ContainsKey(db, key_) == false) { txn.Put(db, key_, value_); OldValue = 0; txn.Commit(); } else { byte[] db_value_ = null; if (txn.TryGet(db, key_, out db_value_) == false) { return(false); } OldValue = BitConverter.ToUInt16(db_value_); txn.Put(db, key_, value_); txn.Commit(); } if (OldValue == Value) { return(false); } summary = eventElements[GroupName].Validating(OldValue, Value); summary.DeviceName = DeviceName; summary.GroupName = GroupName; summary.SiteId = siteId; summary.SetTimestamp(DateTime.Now); return(summary.HasOccurEvent); }
//private async Task SendQueue(string topic, JObject msg_oridinary, CancellationToken token) //{ // foreach (var mqtt_proxy in mqtt_clients) // { // try // { // var msg = CreateMqttMessage(topic, msg_oridinary.ToString(), mqtt_proxy.Options.QosLevel); // var mqtt_client = mqtt_proxy.MqttClient; // if (mqtt_client.IsConnected == false) // continue; // await mqtt_client.PublishAsync(msg, token); // Thread.Sleep(10); // break; // } // catch (Exception ex) // { // logger.LogError(ex, ex.Message); // } // } //} protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { stoppingToken.ThrowIfCancellationRequested(); try { bool isConnected = modbusFactory.ReconnectWhenDisconnected(stoppingToken, 3); if (isConnected == false) { logger.LogWarning("모드버스 접속에 실패했습니다. 5초후 재개"); await Task.Delay(5000, stoppingToken); continue; } stoppingToken.ThrowIfCancellationRequested(); //var parallelResult = Parallel.ForEach<GroupPoint>(modbus.GroupPoints, (async x => foreach (var x in modbus.GroupDigitalPoints) { if (x.Disable == true) { continue; } List <DiMap> results = await modbusFactory.ReadModbusEvent(x); foreach (DiMap map in results) { if (map.DocumentAddress == 40132) { } if (map.Disable == true) { continue; } //cache.Get() //datarow.Add(map.Name, map.Value); string redis_key = $"{modbus.DeviceName}.{map.DocumentAddress}"; ushort redis_value = 0; if (await redis.KeyExistsAsync(redis_key)) { string redis_str = await redis.StringGetAsync(redis_key); redis_value = ushort.Parse(redis_str); } if (map.Value == redis_value) { continue; } else { await redis.StringSetAsync(redis_key, map.Value.ToString()); } using (var session = dbAccess.SessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { EventSummary summary = new EventSummary(); summary.SiteId = SiteId; summary.DeviceName = modbus.DeviceName; summary.GroupName = map.Name; summary.SetTimestamp(DateTime.Now); foreach (DiFlag evt in map.Flags) { EventField ef = event_map.FirstOrDefault(Tx => Tx.Register == map.DocumentAddress && Tx.BitValue == evt.BitValue); bool IsActive = evt.IsActivate(map.Value); if (evt.EventCode.HasValue) { summary.ActiveEvents.Add(evt.EventCode.Value); } string redis_event_key = redis_key + "." + evt.No; if (await redis.KeyExistsAsync(redis_event_key) == false) { await redis.HashSetAsync(redis_event_key, CreateEventNode(map, evt)); } else { await redis.HashSetAsync(redis_event_key, "Status", IsActive); } if (map.Disable == true || map.Level == 0) { continue; } string mysql_key_str = $"{modbus.DeviceName}{map.DocumentAddress}{evt.No}"; string normalizeDeviceName = map.Source + DeviceIndex; //ActiveEvent existEvent = session.Get<ActiveEvent>(mysql_key_str); ActiveEvent existEvent = await GetLatestActiveEventAsync(session, mysql_key_str, stoppingToken); //if (existEvent == null) // continue; switch (EventStatus(existEvent, IsActive, session)) { case Hubbub.EventStatus.Already: mysql_key_str = GetNextEventId(mysql_key_str, session); existEvent = new ActiveEvent(); existEvent.Description = evt.BitName; existEvent.DeviceName = modbus.DeviceName; existEvent.EventLevel = map.Level; existEvent.EventName = evt.BitName; existEvent.Source = map.Source; existEvent.OccurTimestamp = DateTime.Now; existEvent.EventId = mysql_key_str; await session.SaveAsync(existEvent, stoppingToken); break; case Hubbub.EventStatus.New: existEvent = new ActiveEvent(); existEvent.Description = evt.BitName; existEvent.DeviceName = modbus.DeviceName; existEvent.EventLevel = map.Level; existEvent.EventName = evt.BitName; existEvent.Source = map.Source; existEvent.OccurTimestamp = DateTime.Now; existEvent.EventId = mysql_key_str; await session.SaveAsync(existEvent, stoppingToken); if (evt.EventCode.HasValue) { summary.NewEvents.Add(evt.EventCode.Value); } if (ef != null) { await eventPublisherWorker.PublishEvent(SiteId, normalizeDeviceName, ef.Code, Events.Alarm.EventStatus.New, stoppingToken); } break; case Hubbub.EventStatus.Recover: if (evt.EventCode.HasValue) { summary.RecoverEvents.Add(evt.EventCode.Value); } RecoversEvent(existEvent.EventId, session); if (ef != null) { await eventPublisherWorker.PublishEvent(SiteId, normalizeDeviceName, ef.Code, Events.Alarm.EventStatus.Recovery, stoppingToken); } //existEvent.HasRecovered = true; //existEvent.RecoverTimestamp = DateTime.Now; //await session.SaveOrUpdateAsync(existEvent, stoppingToken); break; } } await transaction.CommitAsync(stoppingToken); //var msg = CreateMqttMessage(peiu_event_topic, summary.ToString(), 2); //await mqtt_clients.PeiuEventBrokerProxy.MqttClient.PublishAsync(msg, stoppingToken); } } } } }catch (Exception ex) { } //await SendQueue(peiu_event_topic, datarow, stoppingToken); await Task.Delay(UpdatePeriod.Milliseconds); //Thread.Sleep(UpdatePeriod); } }