Преглед изворни кода

sensors get/add/edit/del

tags/0.1.0
Robin Thoni пре 9 година
родитељ
комит
3390777d98

+ 14
- 0
app/Http/Business/SensorTypesBusiness.php Прегледај датотеку

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\Business;
4
+
5
+use Luticate\Utils\LuBusiness;
6
+use App\Http\DataAccess\SensorTypesDataAccess;
7
+use App\Http\DBO\SensorTypesDbo;
8
+
9
+class SensorTypesBusiness extends LuBusiness {
10
+    protected static function getDataAccess()
11
+    {
12
+        return new SensorTypesDataAccess();
13
+    }
14
+}

+ 37
- 0
app/Http/Business/SensorsBusiness.php Прегледај датотеку

@@ -11,4 +11,41 @@ class SensorsBusiness extends LuBusiness {
11 11
     {
12 12
         return new SensorsDataAccess();
13 13
     }
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
+
38
+    public static function add(SensorsDbo $sensor)
39
+    {
40
+        self::checkSensor($sensor);
41
+        return SensorsDataAccess::addId($sensor);
42
+    }
43
+
44
+    public static function edit(SensorsDbo $sensor, $sensor_id)
45
+    {
46
+        self::getById($sensor_id);
47
+        self::checkSensor($sensor, $sensor_id);
48
+        $sensor->setId($sensor_id);
49
+        return SensorsDataAccess::editById($sensor_id, $sensor);
50
+    }
14 51
 }

+ 14
- 0
app/Http/Controller/SensorTypesController.php Прегледај датотеку

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\Controller;
4
+
5
+use Luticate\Utils\LuController;
6
+use App\Http\Business\SensorTypesBusiness;
7
+use App\Http\DBO\SensorTypesDbo;
8
+
9
+class SensorTypesController extends LuController {
10
+    protected function getBusiness()
11
+    {
12
+        return new SensorTypesBusiness();
13
+    }
14
+}

+ 67
- 0
app/Http/Controller/SensorsController.php Прегледај датотеку

@@ -2,13 +2,80 @@
2 2
 
3 3
 namespace App\Http\Controller;
4 4
 
5
+use App\Http\Business\SensorTypesBusiness;
5 6
 use Luticate\Utils\LuController;
6 7
 use App\Http\Business\SensorsBusiness;
7 8
 use App\Http\DBO\SensorsDbo;
9
+use Luticate\Utils\LuMultipleDbo;
8 10
 
9 11
 class SensorsController extends LuController {
10 12
     protected function getBusiness()
11 13
     {
12 14
         return new SensorsBusiness();
13 15
     }
16
+
17
+    /**
18
+     * Get all sensor types
19
+     * @param int $page The page number, 0 based
20
+     * @param int $perPage The number of items per page
21
+     * @param string $query The filter query
22
+     * @return LuMultipleDbo
23
+     */
24
+    public function getAllTypes($page = 0, $perPage = PHP_INT_MAX, $query = "")
25
+    {
26
+        return SensorTypesBusiness::getAll($page, $perPage, $query);
27
+    }
28
+
29
+    /**
30
+     * Get all sensors, sorted by name
31
+     * @param int $page The page number, 0 based
32
+     * @param int $perPage The number of items per page
33
+     * @param string $query The filter query
34
+     * @return \Luticate\Utils\LuMultipleDbo
35
+     */
36
+    public function getAll($page = 0, $perPage = PHP_INT_MAX, $query = "")
37
+    {
38
+        return SensorsBusiness::getAll($page, $perPage, $query);
39
+    }
40
+
41
+    /**
42
+     * Get a sensor
43
+     * @param $sensor_id int The sensor id
44
+     * @return SensorsDbo
45
+     */
46
+    public function get($sensor_id)
47
+    {
48
+        return SensorsBusiness::getById($sensor_id);
49
+    }
50
+
51
+    /**
52
+     * Add a new sensor
53
+     * @param SensorsDbo $sensor The sensor
54
+     * @return int
55
+     */
56
+    public function add(SensorsDbo $sensor)
57
+    {
58
+        return SensorsBusiness::add($sensor);
59
+    }
60
+
61
+    /**
62
+     * Edit an existing sensor
63
+     * @param SensorsDbo $sensor The sensor
64
+     * @param $sensor_id int The sensor id
65
+     * @return bool
66
+     */
67
+    public function edit(SensorsDbo $sensor, $sensor_id)
68
+    {
69
+        return SensorsBusiness::edit($sensor, $sensor_id);
70
+    }
71
+
72
+    /**
73
+     * Delete an existing sensor
74
+     * @param $sensor_id int The sensor id
75
+     * @return bool
76
+     */
77
+    public function del($sensor_id)
78
+    {
79
+        return SensorsBusiness::deleteById($sensor_id);
80
+    }
14 81
 }

+ 103
- 0
app/Http/DBO/SensorTypesDbo.php Прегледај датотеку

@@ -0,0 +1,103 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ */
7
+
8
+namespace App\Http\DBO;
9
+
10
+use Luticate\Utils\LuDbo;
11
+
12
+class SensorTypesDbo extends LuDbo {
13
+
14
+    public function jsonSerialize()
15
+    {
16
+        return array(
17
+            "Id" => $this->_id,
18
+            "Name" => $this->_name,
19
+            "Class" => $this->_class,
20
+            "DefaultData" => $this->_defaultData
21
+        );
22
+    }
23
+
24
+    public static function jsonDeserialize($json)
25
+    {
26
+        $dbo = new SensorTypesDbo();
27
+        if (isset($json["Id"])) {
28
+            $dbo->setId($json["Id"]);
29
+        }
30
+        if (isset($json["Name"])) {
31
+            $dbo->setName($json["Name"]);
32
+        }
33
+        if (isset($json["Class"])) {
34
+            $dbo->setClass($json["Class"]);
35
+        }
36
+        if (isset($json["DefaultData"])) {
37
+            $dbo->setDefaultData($json["DefaultData"]);
38
+        }
39
+        return $dbo;
40
+    }
41
+
42
+    public static function generateSample()
43
+    {
44
+        $dbo = new SensorTypesDbo();
45
+        $dbo->setId(42);
46
+        $dbo->setName("sample string");
47
+        $dbo->setClass("sample string");
48
+        $dbo->setDefaultData("sample string");
49
+        return $dbo;
50
+    }
51
+
52
+    /**
53
+     * @var integer
54
+     */
55
+    protected $_id;
56
+    public function getId()
57
+    {
58
+        return $this->_id;
59
+    }
60
+    public function setId($value)
61
+    {
62
+        $this->_id = $value;
63
+    }
64
+
65
+    /**
66
+     * @var string
67
+     */
68
+    protected $_name;
69
+    public function getName()
70
+    {
71
+        return $this->_name;
72
+    }
73
+    public function setName($value)
74
+    {
75
+        $this->_name = $value;
76
+    }
77
+
78
+    /**
79
+     * @var string
80
+     */
81
+    protected $_class;
82
+    public function getClass()
83
+    {
84
+        return $this->_class;
85
+    }
86
+    public function setClass($value)
87
+    {
88
+        $this->_class = $value;
89
+    }
90
+
91
+    /**
92
+     * @var text
93
+     */
94
+    protected $_defaultData;
95
+    public function getDefaultData()
96
+    {
97
+        return $this->_defaultData;
98
+    }
99
+    public function setDefaultData($value)
100
+    {
101
+        $this->_defaultData = $value;
102
+    }
103
+}

+ 9
- 9
app/Http/DBO/SensorsDbo.php Прегледај датотеку

@@ -18,7 +18,7 @@ class SensorsDbo extends LuDbo {
18 18
             "Name" => $this->_name,
19 19
             "Description" => $this->_description,
20 20
             "HostId" => $this->_hostId,
21
-            "EntityTypeId" => $this->_entityTypeId,
21
+            "SensorTypeId" => $this->_sensorTypeId,
22 22
             "Data" => $this->_data
23 23
         );
24 24
     }
@@ -38,8 +38,8 @@ class SensorsDbo extends LuDbo {
38 38
         if (isset($json["HostId"])) {
39 39
             $dbo->setHostId($json["HostId"]);
40 40
         }
41
-        if (isset($json["EntityTypeId"])) {
42
-            $dbo->setEntityTypeId($json["EntityTypeId"]);
41
+        if (isset($json["SensorTypeId"])) {
42
+            $dbo->setSensorTypeId($json["SensorTypeId"]);
43 43
         }
44 44
         if (isset($json["Data"])) {
45 45
             $dbo->setData($json["Data"]);
@@ -54,7 +54,7 @@ class SensorsDbo extends LuDbo {
54 54
         $dbo->setName("sample string");
55 55
         $dbo->setDescription("sample string");
56 56
         $dbo->setHostId(42);
57
-        $dbo->setEntityTypeId(42);
57
+        $dbo->setSensorTypeId(42);
58 58
         $dbo->setData("sample string");
59 59
         return $dbo;
60 60
     }
@@ -114,14 +114,14 @@ class SensorsDbo extends LuDbo {
114 114
     /**
115 115
      * @var integer
116 116
      */
117
-    protected $_entityTypeId;
118
-    public function getEntityTypeId()
117
+    protected $_sensorTypeId;
118
+    public function getSensorTypeId()
119 119
     {
120
-        return $this->_entityTypeId;
120
+        return $this->_sensorTypeId;
121 121
     }
122
-    public function setEntityTypeId($value)
122
+    public function setSensorTypeId($value)
123 123
     {
124
-        $this->_entityTypeId = $value;
124
+        $this->_sensorTypeId = $value;
125 125
     }
126 126
 
127 127
     /**

+ 3
- 3
app/Http/DataAccess/CamerasDataAccess.php Прегледај датотеку

@@ -31,10 +31,10 @@ class CamerasDataAccess extends LuDataAccess {
31 31
      */
32 32
     public static function getByName($name)
33 33
     {
34
-        $host = Cameras::where("name", "=", $name)->first();
35
-        if (is_null($host)) {
34
+        $camera = Cameras::where("name", "=", $name)->first();
35
+        if (is_null($camera)) {
36 36
             return null;
37 37
         }
38
-        return $host->toDbo();
38
+        return $camera->toDbo();
39 39
     }
40 40
 }

+ 9
- 0
app/Http/DataAccess/Models/SensorTypes.php Прегледај датотеку

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess\Models;
4
+
5
+use App\Http\DBO\SensorTypesDbo;
6
+
7
+class SensorTypes extends SensorTypesModel
8
+{
9
+}

+ 53
- 0
app/Http/DataAccess/Models/SensorTypesModel.php Прегледај датотеку

@@ -0,0 +1,53 @@
1
+<?php
2
+
3
+/**
4
+ * AUTO GENERATED BY LUTICATE GENERATOR
5
+ * ANY CHANGES WILL BE OVERWRITTEN
6
+ * DO NOT DIRECTLY USE THIS FILE
7
+ * USE SensorTypes.php
8
+ * TO MAKE YOUR CHANGES AND DATABASE ACCESS
9
+*/
10
+
11
+namespace App\Http\DataAccess\Models;
12
+
13
+use Luticate\Utils\LuModel;
14
+use App\Http\DBO\SensorTypesDbo;
15
+
16
+class SensorTypesModel extends LuModel
17
+{
18
+    function __construct()
19
+    {
20
+        parent::__construct();
21
+        $this->timestamps = false;
22
+    }
23
+
24
+    public function toDbo()
25
+    {
26
+        $dbo = new SensorTypesDbo();
27
+
28
+        $dbo->setId($this->id);
29
+        $dbo->setName($this->name);
30
+        $dbo->setClass($this->class);
31
+        $dbo->setDefaultData($this->default_data);
32
+
33
+        return $dbo;
34
+    }
35
+
36
+    /**
37
+     * @param $dbo SensorTypesDbo
38
+     * @param $model LuModel|null
39
+     * @return SensorTypes
40
+     */
41
+    public function fromDbo($dbo, $model = null)
42
+    {
43
+        if (is_null($model))
44
+            $model = new SensorTypes();
45
+
46
+        $model->id = $dbo->getId();
47
+        $model->name = $dbo->getName();
48
+        $model->class = $dbo->getClass();
49
+        $model->default_data = $dbo->getDefaultData();
50
+
51
+        return $model;
52
+    }
53
+}

+ 2
- 2
app/Http/DataAccess/Models/SensorsModel.php Прегледај датотеку

@@ -29,7 +29,7 @@ class SensorsModel extends LuModel
29 29
         $dbo->setName($this->name);
30 30
         $dbo->setDescription($this->description);
31 31
         $dbo->setHostId($this->host_id);
32
-        $dbo->setEntityTypeId($this->entity_type_id);
32
+        $dbo->setSensorTypeId($this->sensor_type_id);
33 33
         $dbo->setData($this->data);
34 34
 
35 35
         return $dbo;
@@ -49,7 +49,7 @@ class SensorsModel extends LuModel
49 49
         $model->name = $dbo->getName();
50 50
         $model->description = $dbo->getDescription();
51 51
         $model->host_id = $dbo->getHostId();
52
-        $model->entity_type_id = $dbo->getEntityTypeId();
52
+        $model->sensor_type_id = $dbo->getSensorTypeId();
53 53
         $model->data = $dbo->getData();
54 54
 
55 55
         return $model;

+ 14
- 0
app/Http/DataAccess/SensorTypesDataAccess.php Прегледај датотеку

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Http\DataAccess;
4
+
5
+use Luticate\Utils\LuDataAccess;
6
+use App\Http\DataAccess\Models\SensorTypes;
7
+use App\Http\DBO\SensorTypesDbo;
8
+
9
+class SensorTypesDataAccess extends LuDataAccess {
10
+    protected static function getModel()
11
+    {
12
+        return new SensorTypes();
13
+    }
14
+}

+ 26
- 0
app/Http/DataAccess/SensorsDataAccess.php Прегледај датотеку

@@ -11,4 +11,30 @@ class SensorsDataAccess extends LuDataAccess {
11 11
     {
12 12
         return new Sensors();
13 13
     }
14
+
15
+    protected static function getOrderBy()
16
+    {
17
+        return array(array("name", "ASC"));
18
+    }
19
+
20
+    protected static function getQueryPredicate($query)
21
+    {
22
+        return array(
23
+            array("name", "ilike", "%" . $query . "%", "or"),
24
+            array("description", "ilike", "%" . $query . "%", "or")
25
+        );
26
+    }
27
+
28
+    /**
29
+     * @param $name
30
+     * @return SensorsDbo|null
31
+     */
32
+    public static function getByName($name)
33
+    {
34
+        $sensor = Sensors::where("name", "=", $name)->first();
35
+        if (is_null($sensor)) {
36
+            return null;
37
+        }
38
+        return $sensor->toDbo();
39
+    }
14 40
 }

+ 9
- 0
app/Http/routes.php Прегледај датотеку

@@ -10,6 +10,7 @@ $route = LuRoute::getInstance();
10 10
 $int = LuRoute::REG_INT;
11 11
 $host_id = "{host_id:$int}";
12 12
 $camera_id = "{camera_id:$int}";
13
+$sensor_id = "{sensor_id:$int}";
13 14
 
14 15
 LuticateBusiness::setupAuth();
15 16
 LuticateBusiness::setupRoutes();
@@ -29,4 +30,12 @@ $route->post("/cameras/add", "Cameras", "add", array(CamotionPermissions::CAMERA
29 30
 $route->post("/cameras/$camera_id/edit", "Cameras", "edit", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_EDIT));
30 31
 $route->post("/cameras/$camera_id/del", "Cameras", "del", array(CamotionPermissions::CAMERA_GET, CamotionPermissions::CAMERA_DEL));
31 32
 
33
+
34
+$route->get("/sensors/types", "Sensors", "getAllTypes", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_ADD));
35
+$route->get("/sensors", "Sensors", "getAll", CamotionPermissions::SENSOR_GET);
36
+$route->get("/sensors/$sensor_id", "Sensors", "get", CamotionPermissions::SENSOR_GET);
37
+$route->post("/sensors/add", "Sensors", "add", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_ADD));
38
+$route->post("/sensors/$sensor_id/edit", "Sensors", "edit", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_EDIT));
39
+$route->post("/sensors/$sensor_id/del", "Sensors", "del", array(CamotionPermissions::SENSOR_GET, CamotionPermissions::SENSOR_DEL));
40
+
32 41
 //sleep(1);

Loading…
Откажи
Сачувај