You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PiFaceSensor.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 11/24/15
  6. * Time: 12:49 AM
  7. */
  8. namespace App\Http\Business\Sensors;
  9. use App\Http\DBO\SensorsValueDbo;
  10. class PiFaceSensor extends AbstractSensor
  11. {
  12. /**
  13. * @return SensorsValueDbo
  14. */
  15. public function getValue()
  16. {
  17. $data = $this->_sensor->getData();
  18. $device = intval(isset($data['Device']) ? $data['Device'] : 0);
  19. $pin = intval(isset($data['Pin']) ? $data['Pin'] : 0);
  20. $trueValue = isset($data['True']) ? $data['True'] : 'Yes';
  21. $falseValue = isset($data['False']) ? $data['False'] : 'No';
  22. $args = [
  23. 'camotion-piface',
  24. escapeshellarg('--device=' . $device),
  25. escapeshellarg('--read=' . $pin)
  26. ];
  27. $cli = implode(' ', $args) . ' | grep Pin | grep -Eo "[01]$"';
  28. $code = 0;
  29. $output = [];
  30. exec($cli, $output, $code);
  31. if ($code != 0) {
  32. abort(500, "PiFace failed with code " . $code);
  33. }
  34. $value = new SensorsValueDbo();
  35. if ($output[0] == "0") {
  36. $value->setValue($falseValue);
  37. }
  38. else {
  39. $value->setValue($trueValue);
  40. }
  41. return $value;
  42. }
  43. }