示例#1
0
 private void Initialize(GenericSensor i_Parent)
 {
     this.m_Parent = i_Parent;
     LowAlarm      = double.MinValue;
     HighAlarm     = double.MaxValue;
     Status        = default(eStatus);
     m_DelayTimer  = new Timer(new TimerCallback(notifyStatusChange));
 }
示例#2
0
 private static void Update_push <Action>(GenericSensor <int, decimal> sensor, int map, int value)
 {
     if (value == inputs[map])
     {
         return;
     }
     value = inputs[map];
     sensor.SetValue(value);
 }
示例#3
0
 public void SampleAdded(GenericSensor genericSensor, Samples.Sample sample)
 {
     if (OnSampleAdded != null)
     {
         OnSampleAdded(ParentLogger, new SampleAddedEventArgs {
             Logger = ParentLogger, Sensor = genericSensor, Sample = sample
         });
     }
 }
示例#4
0
        private static void Update_position <Action>(GenericSensor <Position, Position> sensor, int map, Position value)
        {
            try
            {
                value.Longitude = position.Longitude;
            } catch
            {
                value           = new Position();
                value.Longitude = position.Longitude;
            }

            value.Latitude = position.Latitude;

            sensor.SetValue(value);
        }
示例#5
0
 public SensorAlarm(GenericSensor i_Parent)
 {
     Initialize(i_Parent);
 }
示例#6
0
 private static void Update_poll <Action>(GenericSensor <int, decimal> sensor, int map, int value)
 {
     value = inputs[map];
     sensor.SetValue(value);
 }
示例#7
0
        static void Main(string[] args)
        {
            #region documentation

            /* *******************************************
             * https://www.slideshare.net/MarkLechtermann/mqtt-with-net-core
             * commDriver: ICommDriver
             *                  implementata le diverse modalità richieste per comunicare
             *                  HttpCommDriver: permette di comunicare i dati su piattaforma REST
             *                  MqttCommDriver: permette di comunicare usando protocollo mqtt
             *
             * sender: ICommDriver.send(cosa, dove)
             *
             * cosa: payload, le informazioni da spedire
             *                  singola lettura:
             *                    {type, id, value, time}
             *                  più letture:
             *                    [{ },
             *                     {type, id, value, time}
             *                     ]
             *
             * dove si compone di:
             *      destination > parte dipendente dal punto di origine delle informazioni
             *                    dalla centralina      /cars/targa/sensors/type
             *                    verso la centralina   /cars/targa/cmds/type
             *      +
             *      xxx         > parte dipendente dal driver di connessione
             *      topicPrefix > usiamo mqtt
             *                    IP:port/.../mqtt/v1/
             *      endPoint    > usiamo rest
             *                    http://IP:port/.../v1/
             *                    http://192.168.101.72:8080/dobermetrics/protocol/v1
             *
             *
             * la centralina deve ricevere questi comandi
             *      riavvio centralina
             *      apertura porte
             *      avvio motore
             *      spegnimanto motore
             *
             * Esempi:
             * singola lettura
             *      topicPrefix/cars/AG673WK/sensors/temperature/12
             *                                          {type: temperature, id: 12, value: 21.3, time: 1234523434}
             *
             * letture multiple
             *      topicPrefix//cars/AG673WK/sensors/
             *                                          [{type: temperature, id: 12, value: 21.3, time: 1234523434},
             *                                           {type: temperature, id: 14, value: 21.2, time: 1234523434},
             *                                           {type: speed, id: 35, value: 21.3, time: 1234523436},
             *                                           {type: position, id: 37, value: {45, 45}, time: 1234523436}]
             *
             *      topicPrefix//cars/AG673WK/sensors/temperature/
             *                                          [{type: temperature, id: 12, value: 21.3, time: 1234523434},
             *                                           {type: temperature, id: 14, value: 21.2, time: 1234523434}]
             *
             *      topicPrefix//cars/AG673WK/cmds/engine/
             *                                          [{type: status, id: 58, value: 1}]
             *
             *
             *
             *
             * il protocollo dei comandi deve prevedere che ci sia un feedback
             *
             * */

            #endregion

            ICommDriver CommDriver = new HttpCommDriver();

            #region sensors definition documentation


            /* A sensor is a component used to acquire the device inputs status
             * and notify changes to the consumer.
             *
             * Each sensor is mapped to a physical input, such as a digital IO,
             * a analog IO, a field or a group of fields in a memory area where
             * an extern device can write throgh the bus
             *
             * The consumer must instantiate the sensor providing this informations:
             * - the sensor mapped input
             * - its parametrized scaling function
             * - its updater function
             *
             * When the consumer calls sensor.update():
             *  the sensor invokes the updater, which has the reponsability to check
             *  the input status and, if required call sensor.setValue(newValue)
             *  here the sensor fires the event ValueChanged()
             *
             * The GenericSensor accepts:
             *      Tin         Tout        scaleFunction
             * -----------------------------------------------------
             *      int         decimal     LinearScale
             *      int         decimal     ExponentialScale   :TODO
             *      Position    Position    PositionScale
             */
            #endregion

            #region sensors definition

            var termperature1sensor = new GenericSensor <int, decimal>(
                /* type of sensor   */ "temperature",
                /* numeric id       */ 12,
                /* scaling funcion  */ new LinearScale(0, 1024, -200, 600),
                /* mapped input     */ 35,
                /* delegate updater */ Program.Update_poll <Action>); // polling

            var positionSensor = new GenericSensor <Position, Position>(
                "position",
                44,
                new PositionScale(1),// options: for future use
                58,
                Program.Update_position <Action>);

            var statusSensor = new GenericSensor <int, decimal>(
                "engine",
                58,
                new LinearScale(0, 1, 0, 1),
                0,
                Program.Update_push <Action>);// push notification

            var termperature2sensor = new GenericSensor <int, decimal>(
                "temperature",
                13,
                new LinearScale(0, 1024, -200, 600),
                36,
                Program.Update_poll <Action>);

            var pressureSensor = new GenericSensor <int, decimal>(
                "temperature",
                22,
                new LinearScale(0, 4096, 0, 4),
                9,
                Program.Update_poll <Action>);

            // linking the event processors
            termperature1sensor.ValueChanged += Generic_ValueChanged;
            termperature2sensor.ValueChanged += Generic_ValueChanged;
            pressureSensor.ValueChanged      += Generic_ValueChanged;
            positionSensor.ValueChanged      += Generic_ValueChanged;
            statusSensor.ValueChanged        += Generic_ValueChanged;

            #endregion

            #region initialize and update the input array
            Random random = new Random();

            inputs[35]         = 282 + random.Next(-10, 10);
            inputs[36]         = 282 + random.Next(-10, 10);
            inputs[9]          = random.Next(0, 1024);
            position.Longitude = 45M + (decimal)random.Next(-500, 500) / 100;
            position.Latitude  = -12M + (decimal)random.Next(-500, 500) / 200;

            Actions.CycleTask(500, () =>
            {
                var id = 0;
                if (random.Next(0, 100) > 50)// ok, update only 20% of the viewed statuses
                {
                    inputs[id] += random.Next(-1, 2);
                }
                if (inputs[id] < 0)
                {
                    inputs[id] = 0;
                }
                else if (inputs[id] > 1)
                {
                    inputs[id] = 1;
                }

                id          = 35;
                inputs[id] += random.Next(-2, 2);
                if (inputs[id] < 0)
                {
                    inputs[id] = 0;
                }
                else if (inputs[id] > 1024)
                {
                    inputs[id] = 1024;
                }
                id          = 36;
                inputs[id] += random.Next(-2, 2);
                if (inputs[id] < 0)
                {
                    inputs[id] = 0;
                }
                else if (inputs[id] > 1024)
                {
                    inputs[id] = 1024;
                }
                id          = 9;
                inputs[id] += random.Next(-2, 2);
                if (inputs[id] < 0)
                {
                    inputs[id] = 0;
                }
                else if (inputs[id] > 4096)
                {
                    inputs[id] = 4096;
                }

                var pos = position.Longitude;
                pos    += (decimal)random.Next(-50, 50) / 100;
                if (pos < -180)
                {
                    pos = -180;
                }
                else if (pos > 180)
                {
                    pos = 180;
                }
                position.Longitude = pos;
                pos  = position.Latitude;
                pos += (decimal)random.Next(-50, 50) / 100;
                if (pos < -90)
                {
                    pos = -90;
                }
                else if (pos > 90)
                {
                    pos = 90;
                }
                position.Latitude = pos;
            });
            #endregion

            // update the sensors
            Actions.CycleTask(2000, () =>
            {
                ////Console.WriteLine("\nCycle ");
                //termperature1sensor.Update();
                //termperature2sensor.Update();
                //pressureSensor.Update();
                //positionSensor.Update();
                //statusSensor.Update();
            });

            Console.ReadKey();
        }
示例#8
0
 protected override void AddSampleToSensor(GenericSensor sensor, long value, DateTime dateTime)
 {
     sensor.Samples.AddOffline(value, dateTime);
 }
示例#9
0
 public void Dispose()
 {
     OnSampleAdded = null;
     Items.Clear();
     ParentSensor = null;
 }
示例#10
0
 protected virtual void Initialize(GenericSensor parent)
 {
     this.ParentSensor = parent;
 }
示例#11
0
 public SampleList(GenericSensor parent)
 {
     Initialize(parent);
 }
示例#12
0
 public SensorAlarmWarning(GenericSensor m_Parent)
     : base(m_Parent)
 {
 }
示例#13
0
 protected abstract void AddSampleToSensor(GenericSensor sensor, long value, DateTime dateTime);