3 次代码提交

作者 SHA1 备注 提交日期
  Robin Thoni 5f24ec73c6
increased image size and for null object exception for TVIP311PI 4 年前
  Robin Thoni 301f68cff4
made Options and Method parameters optional for HttpRequest command 4 年前
  Robin Thoni 0eeb7079ad
added HttpRequest sensor 4 年前

+ 6
- 2
app/Http/Business/Cameras/Trendnet/TVIP311PI.php 查看文件

@@ -44,11 +44,15 @@ class TVIP311PI extends AbstractCamera
44 44
             $this->_client->request('GET', $this->_url);
45 45
         } catch (RequestException $e) {
46 46
             $response = $e->getResponse();
47
-            if ($response->getStatusCode() == 401) {
47
+            if ($response != null && $response->getStatusCode() == 401) {
48 48
                 $nonce = array();
49 49
                 preg_match("/nonce=\"([a-f0-9]+)\"/", $response->getHeader("WWW-Authenticate")[0], $nonce);
50 50
                 $nonce = $nonce[1];
51 51
             }
52
+            else {
53
+              LuLog::log($e);
54
+            }
55
+
52 56
         } catch (\Exception $e) {
53 57
             LuLog::log($e);
54 58
         }
@@ -65,7 +69,7 @@ class TVIP311PI extends AbstractCamera
65 69
                 "Connection: close\r\n".
66 70
                 "Accept: */*\r\n\r\n");
67 71
             $data = "";
68
-            while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 35000)
72
+            while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 50000)
69 73
                 $data .= $buf;
70 74
             socket_close($sock);
71 75
             if(substr_count($data, "--boundarySample") >= 2)

+ 2
- 2
app/Http/Business/Commands/HttpRequestCommand.php 查看文件

@@ -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 查看文件

@@ -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
+}

正在加载...
取消
保存