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.

SensorsBusiness.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Business;
  3. use App\Http\DBO\HostsDbo;
  4. use App\Http\DBO\SensorTypesDbo;
  5. use Luticate\Utils\LuBusiness;
  6. use App\Http\DataAccess\SensorsDataAccess;
  7. use App\Http\DBO\SensorsDbo;
  8. use Luticate\Utils\LuRequest;
  9. class SensorsBusiness extends LuBusiness {
  10. protected static function getDataAccess()
  11. {
  12. return new SensorsDataAccess();
  13. }
  14. protected static function checkSensor(SensorsDbo $sensor, $sensor_id = null)
  15. {
  16. $existingSensor = SensorsDataAccess::getByName($sensor->getName());
  17. if (!is_null($existingSensor) && $sensor_id != $existingSensor->getId()) {
  18. self::badInput("Sensor name already exists");
  19. }
  20. if (is_null($sensor->getName()) || strlen($sensor->getName()) == 0) {
  21. self::badInput("Missing sensor name");
  22. }
  23. HostsBusiness::getById($sensor->getHostId());
  24. SensorTypesBusiness::getById($sensor->getSensorTypeId());
  25. if (is_null($sensor->getDescription())) {
  26. $sensor->setDescription("");
  27. }
  28. if (is_null($sensor->getData()) || strlen($sensor->getData()) == 0) {
  29. $sensor->setData("{}");
  30. }
  31. $json = json_decode($sensor->getData());
  32. if (is_null($json)) {
  33. self::badInput("Sensor data could not be converted to json");
  34. }
  35. }
  36. public static function add(SensorsDbo $sensor)
  37. {
  38. self::checkSensor($sensor);
  39. return SensorsDataAccess::addId($sensor);
  40. }
  41. public static function edit(SensorsDbo $sensor, $sensor_id)
  42. {
  43. self::getById($sensor_id);
  44. self::checkSensor($sensor, $sensor_id);
  45. $sensor->setId($sensor_id);
  46. return SensorsDataAccess::editById($sensor_id, $sensor);
  47. }
  48. public static function getValue($sensor_id)
  49. {
  50. /**
  51. * @var $sensor SensorsDbo
  52. * @var $host HostsDbo
  53. * @var $type SensorTypesDbo
  54. */
  55. $sensor = self::getById($sensor_id);
  56. $host = HostsBusiness::getById($sensor->getHostId());
  57. $type = SensorTypesBusiness::getById($sensor->getSensorTypeId());
  58. return LuRequest::proxy('POST', $host->getUrl() . "/sensors/value", [],
  59. ["sensor" => $sensor->__toString(), "type" => $type->__toString()], ["X-Token" => $host->getToken()]);
  60. }
  61. }