您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TVIP311PI.php 2.8KB

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