TR069Service.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $ch = curl_init();
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  34. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  35. $headers[] = "Accept: application/json";
  36. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  37. $result = curl_exec($ch);
  38. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  39. curl_close($ch);
  40. $data = json_decode($result,true);
  41. if(is_array($data))
  42. return $data;
  43. return array();
  44. }
  45. private function post($url, $data = array(), $headers = array()) {
  46. $ch = curl_init();
  47. $data_string = json_encode($data);
  48. curl_setopt($ch, CURLOPT_POST, true);
  49. curl_setopt($ch, CURLOPT_URL, $url);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  52. curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
  53. $headers[] = "Content-Type: application/json";
  54. $headers[] = "Content-Length: " . strlen($data_string);
  55. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  56. $result = curl_exec($ch);
  57. if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
  58. curl_close($ch);
  59. $data = json_decode($result,true);
  60. if(is_array($data))
  61. return $data;
  62. return array();
  63. }
  64. public function getDevices($query = null) {
  65. $url = "{$this->url}/devices";
  66. if($query && is_array($query)) {
  67. $url .= "?query=".json_encode($query);
  68. }
  69. return $this->get($url);
  70. }
  71. public function getDevice($id = null) {
  72. if(is_null($id)) return array();
  73. $url = "{$this->url}/devices/{$id}";
  74. return $this->get($url);
  75. }
  76. function setWlan($id, $ssid, $password = null){
  77. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  78. $data = array();
  79. $data['name'] = 'setParameterValues';
  80. $data['parameterValues'] = array();
  81. if($password)
  82. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey',$password,'xsd:string');
  83. if($ssid)
  84. $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',$ssid,'xsd:string');
  85. $return = $this->post($url, $data);
  86. return $return;
  87. }
  88. function setCATV($id, $state = true){
  89. $url = "{$this->url}/devices/{$id}/tasks?connection_request";
  90. $data = array();
  91. $data['name'] = 'setParameterValues';
  92. $data['parameterValues'] = array();
  93. $data['parameterValues'][] = array('InternetGatewayDevice.VS_AppCfg.VsCatvCfg.CatvPower',$state,'xsd:boolean');
  94. $return = $this->post($url, $data);
  95. return $return;
  96. }
  97. }