Browse Source

Se agrega servicio TR069(tr069.api).

Maxi Schvindt 7 năm trước cách đây
mục cha
commit
09cac7c760
2 tập tin đã thay đổi với 147 bổ sung0 xóa
  1. 4 0
      Resources/config/services.yml
  2. 143 0
      Services/TR069Service.php

+ 4 - 0
Resources/config/services.yml

@@ -0,0 +1,4 @@
+services:
+    tr069.api:
+       class: TR069Bundle\Services\TR069Service
+       arguments: ["@service_container"]

+ 143 - 0
Services/TR069Service.php

@@ -0,0 +1,143 @@
+<?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;
+	}
+
+}