Ejemplo n.º 1
0
        public void Execute(SCPTuple tuple)
        {
            if (tuple.IsTick())
            {
                Context.Logger.Warn("ON TICK, Ticks Number {0}", _aggregateValues.Keys.Count);

                foreach (var item in _aggregateValues.Keys)
                {
                    List <double> values;
                    if (_aggregateValues.TryGetValue(item, out values))
                    {
                        var average = values.Average();

                        var aggregate = new SensorAggregate()
                        {
                            Name    = item,
                            Average = average
                        };

                        Context.Logger.Info("Emiting aggreagate");

                        var serialized = JsonConvert.SerializeObject(aggregate);

                        ctx.Emit(Constants.DEFAULT_STREAM_ID, new List <object>()
                        {
                            serialized
                        });
                    }
                }
            }
            else
            {
                var sensor = tuple.GetSensor();

                if (sensor != null)
                {
                    _aggregateValues.AddOrUpdate(sensor.Name, new List <double>()
                    {
                        sensor.Value
                    }, (key, currentValues) =>
                    {
                        currentValues.Add(sensor.Value);

                        return(currentValues);
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public static SensorAggregate GetSensorAggregate(this SCPTuple tuple)
        {
            try
            {
                SensorAggregate sensor = null;

                if (tuple.Size() == 1)
                {
                    var content = tuple.GetString(0)
                                  .Replace("\"", "'")
                                  .Replace("\\", "");

                    sensor = JsonConvert.DeserializeObject <SensorAggregate>(@content);
                }

                return(sensor);
            }
            catch (Exception ex)
            {
                Context.Logger.Error("JSON Serialization error:{0}", ex.ToString());

                return(null);
            }
        }