示例#1
0
        public static EventField Parse(XmlNode xml)
        {
            if (xml?.Attributes == null)
            {
                return(null);
            }

            try
            {
                var eventField = new EventField
                {
                    InteractionId   = xml.SelectAttributeValue("interaction-id"),
                    InteractionType = xml.SelectAttributeValue("interaction-type"),
                    DisplayOrder    = xml.ParseAttributeInt("display-seq"),
                    IsRequired      = xml.ParseAttributeBool("is-required"),
                    InputDataType   = xml.SelectAttributeValue("input-data-type"),
                    Description     = xml.SelectSingleNodeValue("description/text()")
                };

                return(eventField);
            }
            catch (Exception ex)
            {
                TraceTool.TraceException(ex);
            }

            return(null);
        }
示例#2
0
        private void AttachEventField(string eventId, EventField eventField)
        {
            Event findedEvent = FindEvent(eventId);

            eventField.Event = findedEvent;
            SaveChanges();
        }
示例#3
0
        public void UpdateFromModifierChanges()
        {
            StatIdentifier genericStat = "generic_stat";

            var template = new StatTemplate()
            {
                Name     = "Generic Stat",
                MaxValue = 1000
            };
            var instance = template.CreateInstance(genericStat);

            var static5         = new EventField <float>(5);
            var changeFrom5to10 = new EventField <float>(5);

            Assert.AreEqual(0.0f, instance.Value);

            var modifier = new StatModifier(StatModificationPhase.Typical, StatModificationType.Additive, static5);

            instance.AddModifier(modifier);

            Assert.AreEqual(5.0f, instance.Value);

            var anotherModifier = new StatModifier(StatModificationPhase.Typical, StatModificationType.Additive, changeFrom5to10);

            instance.AddModifier(anotherModifier);

            Assert.AreEqual(10.0f, instance.Value);

            changeFrom5to10.Value = 10.0f;

            Assert.AreEqual(15.0f, instance.Value);
        }
示例#4
0
        public void UnsubscribeModifierWhenRemoved()
        {
            StatIdentifier genericStat = "generic_stat";

            var template = new StatTemplate()
            {
                Name     = "Generic Stat",
                MaxValue = 1000
            };
            var instance = template.CreateInstance(genericStat);

            var changingModifierValue = new EventField <float>(5);

            Assert.AreEqual(0.0f, instance.Value);

            var modifier = new StatModifier(StatModificationPhase.Typical, StatModificationType.Additive, changingModifierValue);

            instance.AddModifier(modifier);

            Assert.AreEqual(5.0f, instance.Value);

            changingModifierValue.Value = 10.0f;

            Assert.AreEqual(10.0f, instance.Value);

            instance.RemoveModifier(modifier);

            Assert.AreEqual(0.0f, instance.Value);

            changingModifierValue.Value = 15.0f;

            Assert.AreEqual(0.0f, instance.Value);
        }
示例#5
0
 public DemoPlayer()
 {
     behaviours = new List <INodeInstance>();
     Health     = new EventField <int>
     {
         Value = 100
     };
 }
示例#6
0
        public void AddEventField(string eventId, string fieldName, string fieldText)
        {
            EventField eventField = new EventField()
            {
                FieldId   = GenerateId(),
                FieldName = fieldName,
                FieldText = fieldText
            };

            eventContext.EventFields.Add(eventField);
            SaveChanges();
            AttachEventField(eventId, FindEventField(eventField.FieldId));
        }
示例#7
0
        private void AddField(TFieldType fieldId, EventField field)
        {
            EventField multiEventField;

            if (!(field is MultiEventField && _fields.TryGetValue(fieldId, out multiEventField)))
            {
                _fields.Add(fieldId, field);
                return;
            }
            // TODO: typed exception
            if (field.GetType() != multiEventField.GetType())
            {
                throw new Exception("Multi field event type mismatch");
            }
            ((MultiEventField)multiEventField).AddField((MultiEventField)field);
        }
示例#8
0
        public override void ReadFromStream(StringReader reader)
        {
            string word;

            while ((word = reader.ReadWord()) != null)
            {
                if (word.Equals(String.Empty))
                {
                    continue;
                }
                TFieldType fieldId = EnumDescriptionToValueMapper <TFieldType> .GetValueByDescription(word);

                EventField field = CreateField(fieldId);
                field.ReadFromStream(reader);
                AddField(fieldId, field);
            }
        }
 public virtual void Visit(EventField e)
 {
 }
示例#10
0
 public EventFieldMirrorHandler(EventField <T> source, EventField <T> target)
 {
     SourceField = source;
     Target      = target;
 }
示例#11
0
        public void FireEvents()
        {
            var target = new EventField <TestPlayer> ();

            var mainhand       = target.Watch(e => e?.Mainhand);
            var mainhandDamage = mainhand.Watch(e => e?.Damage);

            var damages = new List <int> ();

            mainhandDamage.Handlers[this].Add(new AddToListEventHandler <int> (mainhandDamage, damages));

            target.Value = new TestPlayer()
            {
                Mainhand = new EventField <TestWeapon> ()
                {
                    Value = new TestWeapon()
                    {
                        Damage = new EventField <int> ()
                        {
                            Value = 10
                        }
                    }
                }
            };

            target.Value = new TestPlayer()
            {
                Mainhand = new EventField <TestWeapon> ()
                {
                    Value = new TestWeapon()
                    {
                        Damage = new EventField <int> ()
                        {
                            Value = 20
                        }
                    }
                }
            };

            target.Value.Mainhand.Value = null;

            target.Value.Mainhand.Value = new TestWeapon()
            {
                Damage = new EventField <int> ()
                {
                    Value = 25
                }
            };

            target.Value.Mainhand.Value.Damage.Value = 15;

            mainhand.Dispose();
            mainhandDamage.Dispose();

            target.Value.Mainhand.Value.Damage.Value = 5;

            int[] expectedValues = { 10, 20, 0, 25, 15 };
            Assert.AreEqual(expectedValues.Length, damages.Count);
            for (int i = 0; i < damages.Count; i++)
            {
                int damage = damages[i];
                Assert.AreEqual(expectedValues[i], damage);
            }
        }
        //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);
            }
        }
示例#13
0
 public AddToListEventHandler(EventField <T> source, List <T> target)
 {
     this.source = source;
     this.target = target;
 }