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

TVIP311PI.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = $data["Host"] . ":" . $data["Port"] . self::URI;
  25. $this->_options = [];
  26. }
  27. public function prepareImage()
  28. {
  29. $response = null;
  30. $username = $this->_camera->getData()["Username"];
  31. $passwd = $this->_camera->getData()["Password"];
  32. $host = $this->_camera->getData()["Host"];
  33. $port = $this->_camera->getData()["Port"];
  34. $uri = self::URI;
  35. $nonce = "";
  36. try {
  37. $this->_client->request('GET', $this->_url, $this->_options);
  38. } catch (RequestException $e) {
  39. $response = $e->getResponse();
  40. if ($response->getStatusCode() == 401) {
  41. $nonce = array();
  42. preg_match("/nonce=\"([a-f0-9]+)\"/", $response->getHeader("WWW-Authenticate")[0], $nonce);
  43. $nonce = $nonce[1];
  44. }
  45. } catch (\Exception $e) {
  46. LuLog::log($e);
  47. }
  48. $cnonce = md5("42_" . rand());
  49. $realm = "TV-IP311PI";
  50. $resp = md5(md5("$username:$realm:$passwd").":$nonce:".md5("GET:$uri"));
  51. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  52. if(socket_connect($sock, $host, $port))
  53. {
  54. socket_write($sock, "GET $uri HTTP/1.1\r\n".
  55. "Host: $host\r\n".
  56. "Authorization: Digest username=\"$username\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", response=\"$resp\", cnonce=\"$cnonce\", nc=\"00000001\"".
  57. "Connection: close\r\n".
  58. "Accept: */*\r\n\r\n");
  59. $data = "";
  60. while(substr_count($data, "--boundarySample") < 2 && socket_recv($sock, $buf, 1024, 0) && strlen($data) < 35000)
  61. $data .= $buf;
  62. socket_close($sock);
  63. if(substr_count($data, "--boundarySample") >= 2)
  64. {
  65. $data = substr($data, strpos($data, "\r\n\r\n") + 4);
  66. $data = substr($data, strpos($data, "\r\n\r\n") + 4);
  67. $data = substr($data, 0, strpos($data, "--boundarySample") - 2);
  68. $this->_image = $this->_imagine->load($data);
  69. }
  70. else {
  71. abort(500, "Failed to found boundary in device response");
  72. }
  73. }
  74. else {
  75. abort(500, "Failed to connect to device");
  76. }
  77. }
  78. }