Browse Source

camera Trendnet TV-IP311PI

tags/0.1.0
Robin Thoni 8 years ago
parent
commit
d87ce8320b
4 changed files with 92 additions and 4 deletions
  1. 87
    0
      app/Http/Business/Cameras/Trendnet/TVIP311PI.php
  2. 1
    1
      app/Http/DBO/CamerasDbo.php
  3. 2
    1
      composer.json
  4. 2
    2
      composer.lock

+ 87
- 0
app/Http/Business/Cameras/Trendnet/TVIP311PI.php View File

@@ -0,0 +1,87 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/24/15
6
+ * Time: 3:10 PM
7
+ */
8
+
9
+namespace App\Http\Business\Cameras\Trendnet;
10
+
11
+
12
+use App\Http\Business\Cameras\AbstractCamera;
13
+use App\Http\DBO\CamerasDbo;
14
+use GuzzleHttp\Client;
15
+use GuzzleHttp\Exception\RequestException;
16
+use Luticate\Utils\LuLog;
17
+
18
+class TVIP311PI extends AbstractCamera
19
+{
20
+    const URI = "/Streaming/channels/1/httpPreview";
21
+
22
+    private $_client;
23
+    private $_url;
24
+
25
+    public function __construct(CamerasDbo $camera)
26
+    {
27
+        parent::__construct($camera);
28
+        $this->_client = new Client();
29
+        $data = $camera->getData();
30
+        $this->_url = $data["Host"] . ":" . $data["Port"] . self::URI;
31
+        $this->_options = [];
32
+    }
33
+
34
+    public function prepareImage()
35
+    {
36
+        $response = null;
37
+        $username = $this->_camera->getData()["Username"];
38
+        $passwd = $this->_camera->getData()["Password"];
39
+        $host = $this->_camera->getData()["Host"];
40
+        $port = $this->_camera->getData()["Port"];
41
+        $uri = self::URI;
42
+        $nonce = "";
43
+
44
+        try {
45
+            $this->_client->request('GET', $this->_url, $this->_options);
46
+        } catch (RequestException $e) {
47
+            $response = $e->getResponse();
48
+            if ($response->getStatusCode() == 401) {
49
+                $nonce = array();
50
+                preg_match("/nonce=\"([a-f0-9]+)\"/", $response->getHeader("WWW-Authenticate")[0], $nonce);
51
+                $nonce = $nonce[1];
52
+            }
53
+        } catch (\Exception $e) {
54
+            LuLog::log($e);
55
+        }
56
+
57
+        $cnonce = md5("42_" . rand());
58
+        $realm = "TV-IP311PI";
59
+        $resp = md5(md5("$username:$realm:$passwd").":$nonce:".md5("GET:$uri"));
60
+        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
61
+        if(socket_connect($sock, $host, $port))
62
+        {
63
+            socket_write($sock, "GET $uri HTTP/1.1\r\n".
64
+                "Host: $host\r\n".
65
+                "Authorization: Digest username=\"$username\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", response=\"$resp\", cnonce=\"$cnonce\", nc=\"00000001\"".
66
+                "Connection: close\r\n".
67
+                "Accept: */*\r\n\r\n");
68
+            $data = "";
69
+            while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 35000)
70
+                $data .= $buf;
71
+            socket_close($sock);
72
+            if(substr_count($data, "--boundarySample") >= 2)
73
+            {
74
+                $data = substr($data, strpos($data, "\r\n\r\n") + 4);
75
+                $data = substr($data, strpos($data, "\r\n\r\n") + 4);
76
+                $data = substr($data, 0, strpos($data, "--boundarySample") - 2);
77
+                $this->_image = $this->_imagine->load($data);
78
+            }
79
+            else {
80
+                abort(500, "Failed to found boundary in device response");
81
+            }
82
+        }
83
+        else {
84
+            abort(500, "Failed to connect to device");
85
+        }
86
+    }
87
+}

+ 1
- 1
app/Http/DBO/CamerasDbo.php View File

@@ -42,7 +42,7 @@ class CamerasDbo extends LuDbo {
42 42
             $dbo->setCameraTypeId($json["CameraTypeId"]);
43 43
         }
44 44
         if (isset($json["Data"])) {
45
-            $dbo->setData($json["Data"]);
45
+            $dbo->setData(json_decode($json["Data"], true));
46 46
         }
47 47
         return $dbo;
48 48
     }

+ 2
- 1
composer.json View File

@@ -13,7 +13,8 @@
13 13
         "laravel/lumen-framework": "5.1.*",
14 14
         "vlucas/phpdotenv": "~1.0",
15 15
         "luticate/utils": "0.1.x",
16
-        "imagine/imagine": "^0.6.3"
16
+        "imagine/imagine": "^0.6.3",
17
+        "guzzlehttp/guzzle": "^6.1"
17 18
     },
18 19
     "require-dev": {
19 20
         "phpunit/phpunit": "~4.0",

+ 2
- 2
composer.lock View File

@@ -4,8 +4,8 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "hash": "dea3dc2239d27bc91939d5b7a03005bb",
8
-    "content-hash": "7695630f903cb71a2d515af94ba7ce33",
7
+    "hash": "d48c4f20baf08d1cd1e96d7f89fd9747",
8
+    "content-hash": "a7015d165fe7b4c580ab6e1adf21f9e4",
9 9
     "packages": [
10 10
         {
11 11
             "name": "danielstjules/stringy",

Loading…
Cancel
Save