Esempio n. 1
0
    public override SensorRead Read()
    {
      var read = new SensorRead();

      try
      {
#if DEBUG      
        var deviceDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
        var w1file = deviceDir.GetFiles("test.txt").FirstOrDefault();
#else
        var deviceDir = new DirectoryInfo("/sys/bus/w1/devices/" + _iddevice);
        if (deviceDir == null) return read;

        var w1file = deviceDir.GetFiles("w1_slave").FirstOrDefault();
        if (w1file == null) return read;
#endif

        string text = string.Empty;

        var process = new Process();
        process.StartInfo = new ProcessStartInfo("cat", w1file.FullName);
        process.StartInfo.RedirectStandardInput = false;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow     = true;
        process.Start();
        text = process.StandardOutput.ReadToEnd();

        process.WaitForExit(2000);

        /*
        // for reading the file
        Log.Debug("read from file to end");
        using (StreamReader sr = new StreamReader(w1file.FullName))
        {
          text = sr.ReadToEnd();
        }
        */

        var temptext = text.Substring(text.IndexOf("t=") + 2);

        float value = float.Parse(temptext) / 1000;

        read = new SensorRead { Time = _scheduler.TimeIndex, Name = Name, Value = value };

        if (OnSensorRead != null)
          OnSensorRead(this, new SensorEventArgs(read));
      }
      catch (Exception ex)
      {
        Log.Error(string.Format("Ds18b20Sensor read eror"), ex);
      }

      return read;
    }
Esempio n. 2
0
        /// <summary>
        /// Sensor read handler
        /// </summary>
        public override SensorRead Read()
        {
            var rnd = new Random((int)_scheduler.TimeIndex);
            var value = ((float)rnd.NextDouble() * (MaxRange - MinRange)) + MinRange;
            var read = new SensorRead { Time = _scheduler.TimeIndex, Name = Name, Value = value };

            Log.Debug(string.Format("sensor read {0}", read));

            if (OnSensorRead != null)
                OnSensorRead(this, new SensorEventArgs(read));

            return read;
        }
Esempio n. 3
0
 public SensorEventArgs(SensorRead read)
 {
     Read = read;
 }
Esempio n. 4
0
 /// <summary>
 /// Sensor read function
 /// </summary>
 /// <returns>Measured value</returns>
 public virtual SensorRead Read()
 {
   var read = new SensorRead();
   if (OnSensorRead != null)
     OnSensorRead(this, new SensorEventArgs(read));
   return read;
 }
Esempio n. 5
0
    /// <summary>
    /// Process HBus ack messages
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="ack"></param>
    /// <param name="port"></param>
    /// <returns></returns>
    private bool OnAckReceived(object sender, Message ack, int port)
    {
      try
      {
        Event evt = null;

        //if (Status.NodeStatus != NodeStatusValues.Active) return false;
        if (ack.MessageType == MessageTypes.NackResponse)
        {
          var err = ack.Data.Length > 1 ? ack.Data[0] << 1 | ack.Data[1] : 0;
          Log.Debug(string.Format("HBus: nack from {0}:{1} with error {2}", ack.Source, port, err));
          return false;
        }

        Log.Debug(string.Format("HBus: ack from {0}:{1}", ack.Source, port));

        switch (ack.Command)
        {
          //case NodeCommands.CMD_GET_PIN_INFO:
          //    var pp = new Pin();
          //    var pi = PinSerializer.DeSerialize(ack.Data, ref pp);
          //    break;
          case NodeCommands.CMD_READ_PIN:
            var pe = new PinEvent(ack.Data);
            evt = new Event
            {
              Name = "pin-read",
              Source = pe.Pin,
              Channel = "hbus",
              Value = pe.Value,
              Status = pe.IsActive ? "active" : "inactive",
              Timestamp = DateTime.Now
            };
            break;
          case NodeCommands.CMD_GET_DEVICE_STATUS:
            var ds = new DeviceStatus(ack.Data);
            evt = new Event
            {
              Name = "device-status",
              Source = ds.Device,
              Channel = "hbus",
              Status = ds.Status,
              Timestamp = DateTime.Now
            };
            break;
          //case NodeCommands.CMD_GET_DEVICE_INFO:
          //    var dd = new Device();
          //    var di = DeviceSerializer.DeSerialize(ack.Data, ref dd);
          //    break;
          //case NodeCommands.CMD_GET_SENSOR_INFO:
          //    break;
          case NodeCommands.CMD_READ_SENSOR:
            var sr = new SensorRead(ack.Data);
            evt = new Event
            {
              Name = "sensor-read",
              Source = sr.Name,
              Channel = "hbus",
              Value = sr.Value,
              Unit = "", //sr.Unit, //TODO: add to HBus protocol
              Timestamp = DateTime.Now
            };
            break;
        }

        if (evt != null)
          //Send event to subscribers
          Send(evt, this);

        return true;
      }
      catch (Exception ex)
      {
        Log.Error(string.Format("HBus: error occurred with ack {0} from {1}", ack.Command, ack.Source), ex);

        //Propagate errors to observers
        Error(ex, this);
      }
      return false;
    }
Esempio n. 6
0
    /// <summary>
    /// Process HBus command messages
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="message"></param>
    /// <param name="port"></param>
    /// <returns></returns>
    private bool OnCommandReceived(object sender, Message message, int port)
    {
      Log.Debug(string.Format("HBus command from {0}:{1}", message.Source, port));

      try
      {
        Event evt = null;

        //Clear bus & node errors
        _bus.ClearErrors();

        switch (message.Command)
        {
          case NodeCommands.CMD_PUSH_PIN_EVENT:
            var pe = new PinEvent(message.Data);
            evt = new Event
            {
              Name = "pin-change",
              Source = pe.Pin,
              //Channel = "hbus",
              Value = pe.Value,
              Status = pe.IsActive ? "active" : "inactive",
              Timestamp = DateTime.Now
            };
            break;
          case NodeCommands.CMD_PUSH_DEVICE_EVENT:
            var de = new DeviceEvent(message.Data);
            evt = new Event
            {
              Name = "device-event",
              Source = de.Device,
              //Channel = "hbus",
              Status = de.Status,
              Data = de.Values,
              Timestamp = DateTime.Now
            };
            break;
          case NodeCommands.CMD_PUSH_SENSOR_READ:
            var sr = new SensorRead(message.Data);
            evt = new Event
            {
              Name = "sensor-read",
              Source = sr.Name,
              //Channel = "hbus",
              Value = sr.Value,
              Unit = "", //sr.Unit, //TODO: add to HBus protocol
              Timestamp = DateTime.Now
            };
            break;
        }

        if (evt != null)
          //Send event to subscribers
          Send(evt, this);

        //HBus command processed
        return true;
      }
      catch (Exception ex)
      {
        Log.Error(string.Format("HBus: error with command {0} from {1}", message.Command, message.Source), ex);

        //Propagate errors to observers
        Error(ex, this);
      }
      return false;
    }