1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
-
- namespace App\Http\Business;
-
- use App\Http\DBO\HostsDbo;
- use App\Http\DBO\SensorTypesDbo;
- use Luticate\Auth\Business\LuticatePermissionsBusiness;
- use Luticate\Utils\LuBusiness;
- use App\Http\DataAccess\SensorsDataAccess;
- use App\Http\DBO\SensorsDbo;
- use Luticate\Utils\LuRequest;
-
- class SensorsBusiness extends LuBusiness {
- protected static function getDataAccess()
- {
- return new SensorsDataAccess();
- }
-
- protected static function checkSensor(SensorsDbo $sensor, $sensor_id = null)
- {
- $existingSensor = SensorsDataAccess::getByName($sensor->getName());
- if (!is_null($existingSensor) && $sensor_id != $existingSensor->getId()) {
- self::badInput("Sensor name already exists");
- }
- if (is_null($sensor->getName()) || strlen($sensor->getName()) == 0) {
- self::badInput("Missing sensor name");
- }
- HostsBusiness::getById($sensor->getHostId());
- SensorTypesBusiness::getById($sensor->getSensorTypeId());
- if (is_null($sensor->getDescription())) {
- $sensor->setDescription("");
- }
- if (is_null($sensor->getData()) || strlen($sensor->getData()) == 0) {
- $sensor->setData("{}");
- }
- $json = json_decode($sensor->getData());
- if (is_null($json)) {
- self::badInput("Sensor data could not be converted to json");
- }
- }
-
- public static function getAllLight($_user, $page = 0, $perPage = PHP_INT_MAX, $query = "")
- {
- $values = self::getAll($page, $perPage, $query);
- if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_SENSOR_EDIT")->getValue()) {
- return $values;
- }
- return $values->map(function($sensor)
- {
- $sensor->setData(null);
- return $sensor;
- });
- }
-
- public static function getLight($_user, $sensor_id)
- {
- $sensor = self::getById($sensor_id);
- if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_SENSOR_EDIT")->getValue()) {
- return $sensor;
- }
- $sensor->setData(null);
- return $sensor;
- }
-
- public static function add(SensorsDbo $sensor)
- {
- self::checkSensor($sensor);
- return SensorsDataAccess::addId($sensor);
- }
-
- public static function edit(SensorsDbo $sensor, $sensor_id)
- {
- self::getById($sensor_id);
- self::checkSensor($sensor, $sensor_id);
- $sensor->setId($sensor_id);
- return SensorsDataAccess::editById($sensor_id, $sensor);
- }
-
- public static function getValue($sensor_id)
- {
- /**
- * @var $sensor SensorsDbo
- * @var $host HostsDbo
- * @var $type SensorTypesDbo
- */
- $sensor = self::getById($sensor_id);
- $host = HostsBusiness::getById($sensor->getHostId());
- $type = SensorTypesBusiness::getById($sensor->getSensorTypeId());
- return LuRequest::proxy('POST', $host->getUrl() . "/sensors/value", [],
- ["sensor" => $sensor->__toString(), "type" => $type->__toString()], ["X-Token" => $host->getToken()]);
- }
- }
|