123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?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();
- }
- public 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);
- }
- public 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;
- }
-
- public 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;
- }
-
- public function setWbroadcast($id, $state = true)
- {
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'setParameterValues';
- $data['parameterValues'] = array();
-
- $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSIDAdvertisementEnabled',$state,'xsd:boolean');
-
- $return = $this->post($url, $data);
-
- return $return;
- }
-
- public function setWstatus($id, $state = true)
- {
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'setParameterValues';
- $data['parameterValues'] = array();
-
- $data['parameterValues'][] = array('InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',$state,'xsd:boolean');
-
- $return = $this->post($url, $data);
-
- return $return;
- }
-
- public function setParameter($id, $parameter, $value, $type)
- {
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'setParameterValues';
- $data['parameterValues'] = array();
-
- $data['parameterValues'][] = array($parameter,$value,"xsd:$type");
-
- $return = $this->post($url, $data);
-
- return $return;
- }
- public function getParameter($id, $parameter)
- {
- $query = json_encode(array('_id' => $id));
- $url = "{$this->url}/devices?query={$query}&projection={$parameter}";
- return $this->get($url);
- }
- public function getTR069Data($data)
- {
- $result = array();
- $result['_id'] = $data['_id'] ?? null;
- $result['_deviceId'] = $data['_deviceId'] ?? null;
- $result['SSID'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['SSID'] ?? null;
- $result['keyPassphrase'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['KeyPassphrase'] ?? null;
- $result['SSIDAdvertisementEnabled'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['SSIDAdvertisementEnabled'] ?? null;
- $result['Status'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['Enable'] ?? null;
-
- //InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.PreSharedKey
- $result['preSharedKey'] = $data['InternetGatewayDevice']['LANDevice']['1']['WLANConfiguration']['1']['PreSharedKey']['1']['PreSharedKey'] ?? null;
-
- $result['CatvPower'] = $data['InternetGatewayDevice']['VS_AppCfg']['VsCatvCfg']['CatvPower'] ?? null;
- $result['ExternalIPAddress'] = $data['InternetGatewayDevice']['WANDevice']['1']['WANConnectionDevice']['1']['WANPPPConnection']['1']['ExternalIPAddress'] ?? null;
- return $result;
- }
- public function refreshObject($id, $object)
- {
- $url = "{$this->url}/devices/{$id}/tasks?connection_request";
-
- $data = array();
- $data['name'] = 'refreshObject';
- $data['objectName'] = $object;
- $return = $this->post($url, $data);
- }
- }
|