Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SensorsBusiness.php 3.1KB

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