123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace TR069Bundle\Services;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- class TR069Service
- {
- /**
- * @var ContainerInterface
- */
- protected $serviceContainer;
- protected $user;
- protected $pass;
- protected $host;
- protected $port;
- protected $url;
- /**
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(ContainerInterface $serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- if($this->serviceContainer->getParameter("tr069_service")) {
- $this->user = $this->serviceContainer->getParameter("tr069_user");
- $this->pass = $this->serviceContainer->getParameter("tr069_pass");
- $this->host = $this->serviceContainer->getParameter("tr069_host");
- $this->port = $this->serviceContainer->getParameter("tr069_port");
-
- $this->url = "http://{$this->host}:{$this->port}";
- }
- }
- private function get($url, $headers = array()) {
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers[] = "Accept: application/json";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
-
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- private function post($url, $data = array(), $headers = array()) {
-
- $ch = curl_init();
- $data_string = json_encode($data);
-
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
- $headers[] = "Content-Type: application/json";
- $headers[] = "Content-Length: " . strlen($data_string);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- $result = curl_exec($ch);
- if (curl_errno($ch)) {echo 'Error:' . curl_error($ch);}
- curl_close($ch);
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- public function getDevices($query = null) {
- $url = "{$this->url}/devices";
-
- if($query && is_array($query)) {
- $url .= "?query=".json_encode($query);
- }
- return $this->get($url);
- }
-
- public function getDevice($id = null) {
- if(is_null($id)) return array();
- $url = "{$this->url}/devices/{$id}";
- return $this->get($url);
- }
- function setWlan($id, $ssid, $password = null){
-
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'setParameterValues';
- $data['parameterValues'] = array();
-
- if($password)
- $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey',$password,'xsd:string');
-
-
- if($ssid)
- $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',$ssid,'xsd:string');
-
- $return = $this->post($url, $data);
-
- return $return;
- }
-
- function setCATV($id, $state = true){
-
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'setParameterValues';
- $data['parameterValues'] = array();
-
- $data['parameterValues'][] = array('InternetGatewayDevice.VS_AppCfg.VsCatvCfg.CatvPower',$state,'xsd:boolean');
-
-
- $return = $this->post($url, $data);
-
- return $return;
- }
- }
|