Example #1
0
        /// <summary>
        /// Send event to all subscribers
        /// </summary>
        /// <param name="event"></param>
        /// <param name="sender"></param>
        public void Send(Event @event, BaseProcessor sender)
        {
            var i = 0;
            foreach (var s in _subscriptions)
            {
                var ok = false;

                //Filter event
                if (s.Value != null)
                    foreach (var filter in s.Value)
                    {
                        if (filter.Evaluate(@event)) ok = true;
                    }
                else
                {
                    ok = true;
                }

                //Send event to subscriber
                if (ok && s.Key.OnSourceEvent != null)
                    s.Key.OnSourceEvent(@event, sender);
                i++;
            }
            Log.Debug(string.Format("Event {0} sent to {1} subscribers", @event, i));
        }
Example #2
0
        private void WriteError(Exception error, BaseProcessor sender)
        {
            try
            {
                //_conn = new MySqlConnection(_cs);
                //_conn.Open();

                const string stm = @"INSERT INTO hbus_errors (error, source, inner, timestamp)
                                     VALUES(@Error, @Source, @Inner, @Timestamp)";

                var cmd = new MySqlCommand(stm, _conn);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@Error", error.Message);
                cmd.Parameters.AddWithValue("@Source", error.Source);
                cmd.Parameters.AddWithValue("@Inner", error.InnerException != null ? error.InnerException.Message : string.Empty);
                cmd.Parameters.AddWithValue("@Timestamp", DateTime.Now);
                //Altri parametri
                cmd.ExecuteNonQuery();

                Log.Debug("Mysql written error");

            }
            catch (MySqlException ex)
            {
                //TODO LOG
                Log.Error("Mysql write error error", ex);
            }
            //finally
            //{
            //    //_conn?.Close();
            //    if (_conn != null)
            //        _conn.Close();
            //}
        }
Example #3
0
 /// <summary>
 /// Send error to subscribers
 /// </summary>
 /// <param name="exception"></param>
 /// <param name="sender"></param>
 public void Error(Exception exception, BaseProcessor sender)
 {
     foreach (var s in _subscriptions)
     {
         //Send error to subscribers
         if (s.Key.OnSourceError != null)
             s.Key.OnSourceError(exception, sender);
     }
     Log.Error(string.Format("error {0} propagated to subscribers", exception));
 }
 public void WriteEvent(Event @event, BaseProcessor sender)
 {
     if (_fields != null)
     {
         for (int i = 0; i < _fields.Length; i++)
         {
             if (_fields[i] == @event.Source)
             {
                 _values[i] = @event.Value;
                 _total++;
                 break;
             }
         }
         if (_total == (_fields.Length - 1))
         {
             PostToThingSpeak(_values);
             _total = 0;
         }
     }
 }
Example #5
0
        private void WriteEvent(Event @event, BaseProcessor sender)
        {
            try
            {
                //_conn = new MySqlConnection(_cs);
                //_conn.Open();

                const string stm = @"INSERT INTO events (name, source, type, value, status, unit, timestamp)
                                     VALUES(@Name, @Source, @Type, @Value, @Status, @Unit, @Timestamp)";

                var cmd = new MySqlCommand(stm, _conn);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@Name", @event.Name);
                cmd.Parameters.AddWithValue("@Source", @event.Source);
                cmd.Parameters.AddWithValue("@Type", @event.Channel);
                cmd.Parameters.AddWithValue("@Value", @event.Value);
                cmd.Parameters.AddWithValue("@Status", @event.Status);
                cmd.Parameters.AddWithValue("@Unit", @event.Unit);
                cmd.Parameters.AddWithValue("@Timestamp", @event.Timestamp);
                //Altri parametri
                cmd.ExecuteNonQuery();

                Log.Debug("Mysql written event");

            }
            catch (MySqlException ex)
            {
                //TODO LOG
                Log.Error("Mysql write event error", ex);
            }
            //finally
            //{
            //    //_conn?.Close();
            //    if (_conn != null)
            //        _conn.Close();
            //}
        }
Example #6
0
        /// <summary>
        /// Unsubscribe client from current endpoint
        /// </summary>
        /// <param name="subscriber"></param>
        public void DeleteSubscriber(BaseProcessor subscriber)
        {
            if (_subscriptions.ContainsKey(subscriber))
                _subscriptions.Remove(subscriber);

            Log.Debug(string.Format("Subscriber {0} removed", subscriber));
        }
Example #7
0
        /// <summary>
        /// Subscribe client endpoint to specific or all events
        /// </summary>
        /// <param name="subscriber"></param>
        /// <param name="filters"></param>
        public void AddSubscriber(BaseProcessor subscriber, EventFilter[] filters = null)
        {
            if (!_subscriptions.ContainsKey(subscriber))
                _subscriptions.Add(subscriber, filters);
            else
                _subscriptions[subscriber] = filters;

            Log.Debug(string.Format("Subscriber {0} added", subscriber));
        }