TR069Service.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace TR069Bundle\Services;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. class TR069Service
  5. {
  6. /**
  7. * @var ContainerInterface
  8. */
  9. protected $serviceContainer;
  10. protected $user;
  11. protected $pass;
  12. protected $host;
  13. protected $port;
  14. protected $url;
  15. /**
  16. * @param ContainerInterface $serviceContainer
  17. */
  18. public function __construct(ContainerInterface $serviceContainer)
  19. {
  20. $this->serviceContainer = $serviceContainer;
  21. if($this->serviceContainer->getParameter("tr069_service")) {
  22. $this->user = $this->serviceContainer->getParameter("tr069_user");
  23. $this->pass = $this->serviceContainer->getParameter("tr069_pass");
  24. $this->host = $this->serviceContainer->getParameter("tr069_host");
  25. $this->port = $this->serviceContainer->getParameter("tr069_port");
  26. $this->url = "http://{$this->host}:{$this->port}";
  27. }
  28. }
  29. private function get($url, $headers = array())
  30. {
  31. $ch = curl_init();
  32. curl_setopt($ch, CURLOPT_URL, $url);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  35. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  36. $headers[] = "Accept: application/json";
  37. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  38. $result = curl_exec($ch);
  39. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  40. curl_close($ch);
  41. $data = json_decode($result,true);
  42. if(is_array($data))
  43. return $data;
  44. return array();
  45. }
  46. public function post($url, $data = array(), $headers = array())
  47. {
  48. $ch = curl_init();
  49. $data_string = json_encode($data);
  50. curl_setopt($ch, CURLOPT_POST, true);
  51. curl_setopt($ch, CURLOPT_URL, $url);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  54. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  55. $headers[] = "Content-Type: application/json";
  56. $headers[] = "Content-Length: " . strlen($data_string);
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  58. $result = curl_exec($ch);
  59. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  60. curl_close($ch);
  61. $data = json_decode($result,true);
  62. if(is_array($data))
  63. return $data;
  64. return array();
  65. }
  66. public function getDevices($query = null)
  67. {
  68. $url = "{$this->url}/devices";
  69. if($query && is_array($query)) {
  70. $url .= "?query=".json_encode($query);
  71. }
  72. return $this->get($url);
  73. }
  74. public function getDevice($id = null)
  75. {
  76. if(is_null($id)) return array();
  77. $url = "{$this->url}/devices/{$id}";
  78. return $this->get($url);
  79. }
  80. public function setWlan($id, $ssid, $password = null)
  81. {
  82. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  83. $data = array();
  84. $data['name'] = 'setParameterValues';
  85. $data['parameterValues'] = array();
  86. if($password)
  87. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey',$password,'xsd:string');
  88. if($ssid)
  89. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',$ssid,'xsd:string');
  90. $return = $this->post($url, $data);
  91. return $return;
  92. }
  93. public function setCATV($id, $state = true)
  94. {
  95. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  96. $data = array();
  97. $data['name'] = 'setParameterValues';
  98. $data['parameterValues'] = array();
  99. $data['parameterValues'][] = array('InternetGatewayDevice.VS_AppCfg.VsCatvCfg.CatvPower',$state,'xsd:boolean');
  100. $return = $this->post($url, $data);
  101. return $return;
  102. }
  103. public function setWbroadcast($id, $state = true)
  104. {
  105. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  106. $data = array();
  107. $data['name'] = 'setParameterValues';
  108. $data['parameterValues'] = array();
  109. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSIDAdvertisementEnabled',$state,'xsd:boolean');
  110. $return = $this->post($url, $data);
  111. return $return;
  112. }
  113. public function setWstatus($id, $state = true)
  114. {
  115. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  116. $data = array();
  117. $data['name'] = 'setParameterValues';
  118. $data['parameterValues'] = array();
  119. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',$state,'xsd:boolean');
  120. $return = $this->post($url, $data);
  121. return $return;
  122. }
  123. public function setParameter($id, $parameter, $value, $type)
  124. {
  125. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  126. $data = array();
  127. $data['name'] = 'setParameterValues';
  128. $data['parameterValues'] = array();
  129. $data['parameterValues'][] = array($parameter,$value,"xsd:$type");
  130. $return = $this->post($url, $data);
  131. return $return;
  132. }
  133. public function getParameter($id, $parameter)
  134. {
  135. $query = json_encode(array('_id' => $id));
  136. $url = "{$this->url}/devices?query={$query}&projection={$parameter}";
  137. return $this->get($url);
  138. }
  139. public function getTR069Data($data)
  140. {
  141. $result = array();
  142. $result['_id'] = $data['_id'] ?? null;
  143. $result['_deviceId'] = $data['_deviceId'] ?? null;
  144. $result['SSID'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['SSID'] ?? null;
  145. $result['keyPassphrase'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['KeyPassphrase'] ?? null;
  146. $result['SSIDAdvertisementEnabled'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['SSIDAdvertisementEnabled'] ?? null;
  147. $result['Status'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['Enable'] ?? null;
  148. //InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey
  149. $result['preSharedKey'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['PreSharedKey']['1']['PreSharedKey'] ?? null;
  150. $result['CatvPower'] = $data['InternetGatewayDevice']['VS_AppCfg']['VsCatvCfg']['CatvPower'] ?? null;
  151. $result['ExternalIPAddress'] = $data['InternetGatewayDevice']['WANDevice']['1']['WANConnectionDevice']['1']['WANPPPConnection']['1']['ExternalIPAddress'] ?? null;
  152. return $result;
  153. }
  154. public function refreshObject($id, $object)
  155. {
  156. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  157. $data = array();
  158. $data['name'] = 'refreshObject';
  159. $data['objectName'] = $object;
  160. $return = $this->post($url, $data);
  161. }
  162. }