2 Commits

Author SHA1 Message Date
  Robin Thoni 301f68cff4
made Options and Method parameters optional for HttpRequest command 3 months ago
  Robin Thoni 0eeb7079ad
added HttpRequest sensor 3 months ago

+ 2
- 2
app/Http/Business/Commands/HttpRequestCommand.php View File

@@ -22,8 +22,8 @@ class HttpRequestCommand extends AbstractCommand
22 22
     {
23 23
         $data = $this->_command->getData();
24 24
         $url = $data["Url"];
25
-        $method = $data["Method"];
26
-        $options = $data["Options"];
25
+        $method = isset($data["Method"]) ? $data["Method"] : "POST";
26
+        $options = isset($data["Options"]) ? $data["Options"] : array();
27 27
 
28 28
         $client = new Client();
29 29
 

+ 95
- 0
app/Http/Business/Sensors/HttpRequestSensor.php View File

@@ -0,0 +1,95 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/24/15
6
+ * Time: 12:49 AM
7
+ */
8
+
9
+namespace App\Http\Business\Sensors;
10
+
11
+use App\Http\DBO\SensorsValueDbo;
12
+use GuzzleHttp\Client;
13
+use GuzzleHttp\Exception\ClientException;
14
+use GuzzleHttp\Exception\RequestException;
15
+use Luticate\Utils\LuLog;
16
+
17
+class HttpRequestSensor extends AbstractSensor
18
+{
19
+    /**
20
+     * {
21
+     *    "Url": "https://example.com/sensor"
22
+     *    "Method": "GET|POST|PUT|...",
23
+     *    "Options": {
24
+     *    },
25
+     *    "Mapping": {
26
+     *       "Values": {
27
+     *          "ON": 1,
28
+     *          "OFF": 0
29
+     *       },
30
+     *       "ValuesDefault": 0
31
+     *    }
32
+     * }
33
+     *
34
+     */
35
+    protected function map($value, $mapping)
36
+    {
37
+        $default = isset($mapping["ValuesDefault"]) ? $mapping["ValuesDefault"] : $value;
38
+
39
+        $values = isset($mapping["Values"]) ? $mapping["Values"] : array();
40
+        if (isset($values[$value]))
41
+        {
42
+            return $values[$value];
43
+        }
44
+        else
45
+        {
46
+            return $default;
47
+        }
48
+    }
49
+
50
+    /**
51
+     * @return SensorsValueDbo
52
+     */
53
+    public function getValue()
54
+    {
55
+        $data = $this->_sensor->getData();
56
+        $url = $data["Url"];
57
+        $method = isset($data["Method"]) ? $data["Method"] : "GET";
58
+        $options = isset($data["Options"]) ? $data["Options"] : array();
59
+
60
+        $client = new Client();
61
+
62
+        try {
63
+            $response = $client->request($method, $url, $options);
64
+        } catch (ClientException $e) {
65
+            $response = $e->getResponse();
66
+            if (!is_null($response)) {
67
+                LuLog::log($response->getBody());
68
+            }
69
+            LuLog::log($e);
70
+            abort(500, "Failed to execute request (client)");
71
+        } catch (RequestException $e) {
72
+            $response = $e->getResponse();
73
+            if (!is_null($response)) {
74
+                LuLog::log($response->getBody());
75
+            }
76
+            LuLog::log($e);
77
+            abort(500, "Failed to execute request (request)");
78
+        } catch (\Exception $e) {
79
+            LuLog::log($e);
80
+            abort(500, "Failed to execute request (unknown)");
81
+        }
82
+
83
+        if ($response->getStatusCode() != 200)
84
+        {
85
+            abort(500, "Failed to execute request (" . $response->getStatusCode() . ")");
86
+        }
87
+
88
+        $mapping = isset($data["Mapping"]) ? $data["Mapping"] : array();
89
+
90
+        $value = new SensorsValueDbo();
91
+        $value->setValue($this->map((string) $response->getBody(), $mapping));
92
+
93
+        return $value;
94
+    }
95
+}

Loading…
Cancel
Save