123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
-
- namespace App\Http\Business;
-
- use Luticate\Utils\LuBusiness;
- use App\Http\DataAccess\SensorsDataAccess;
- use App\Http\DBO\SensorsDbo;
-
- 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 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);
- }
- }
|