From 363cacdd23affce30a4fe7c43342c68492963b78 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Fri, 18 Sep 2015 17:22:30 +0200
Subject: [PATCH] use new Kernel for WS getPlot function

---
 php/classes/WSClientInterface.php |   4 ++--
 php/classes/WSClientREST.php      |  43 ++++++++++++++++++++++++++++++++++++-------
 php/classes/WSClientSOAP.php      |  26 ++++++++++++++++++++++++++
 php/classes/WebServer.php         | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------
 php/testWebServer.php             |  27 +++++++++++++++++++++++++++
 5 files changed, 174 insertions(+), 52 deletions(-)

diff --git a/php/classes/WSClientInterface.php b/php/classes/WSClientInterface.php
index c41367b..93a82f5 100644
--- a/php/classes/WSClientInterface.php
+++ b/php/classes/WSClientInterface.php
@@ -121,9 +121,9 @@ Interface WSClientInterface
 	public function getDataset($startTime, $stopTime, $datasetID, $sampling = 0, $userID = "", $password = "",
 			$outputFormat = WSOutputFileFormat::NETCDF, $timeFormat = WSOutputTimeFormat::ISO, $gzip = 0);
 
-	/*public function getPlot($startTime, $stopTime, $missionID, $userID = "", $password = "");
+	public function getPlot($startTime, $stopTime, $missionID, $userID = "", $password = "");
 
-	public function getResultPlot($plotDirectoryURL);*/
+	public function getResultPlot($plotDirectoryURL);
 
 	public function getOrbites($startTime, $stopTime, $spacecraft, $coordinateSystem, $units = WSOrbitUnit::KM,
 			$sampling = 0, $userID = "", $password = "", $outputFormat = WSOutputFileFormat::NETCDF,
diff --git a/php/classes/WSClientREST.php b/php/classes/WSClientREST.php
index 7aac843..057095a 100644
--- a/php/classes/WSClientREST.php
+++ b/php/classes/WSClientREST.php
@@ -133,18 +133,47 @@ class WSClientREST implements WSClientInterface
 		return $this->call(__FUNCTION__, $params);
 	}
 
+	public function getPlot($startTime, $stopTime, $missionID, $userID = "", $password = "")
+	{
+		$params = array('parameters' => array(
+				"startTime" => $startTime,
+				"stopTime"  => $stopTime,
+				"missionID" => $missionID
+		));
+	
+		if ($userID != "" && $password != "")
+		{
+			$params["parameters"]["userID"] = $userID;
+			$params["parameters"]["password"] = $password;
+		}
+	
+		return $this->call(__FUNCTION__, $params);
+	}
+	
+	public function getResultPlot($plotDirectoryURL)
+	{
+		$params = array('parameters' => array(
+				"plotDirectoryURL" => $plotDirectoryURL
+		));
+	
+		return $this->call(__FUNCTION__, $params);
+	}
+	
 	private function call($function, $params)
 	{
 		$functionURL = $this->baseURL."/".$function.".php";
 		
-		$isFirstParam = true;
-		foreach ($params as $paramName => $paramValue)
+		if (isset($params['parameters']))
 		{
-			if ($isFirstParam)
-				$functionURL .= "?";
-			else
-				$functionURL .= "&";
-			$functionURL .= $paramName."=".$paramValue;
+			$isFirstParam = true;
+			foreach ($params['parameters'] as $paramName => $paramValue)
+			{
+				if ($isFirstParam)
+					$functionURL .= "?";
+				else
+					$functionURL .= "&";
+				$functionURL .= $paramName."=".$paramValue;
+			}
 		}
 		
 		return file_get_contents($functionURL);
diff --git a/php/classes/WSClientSOAP.php b/php/classes/WSClientSOAP.php
index 52c1187..f88c6ad 100644
--- a/php/classes/WSClientSOAP.php
+++ b/php/classes/WSClientSOAP.php
@@ -134,6 +134,32 @@ class WSClientSOAP implements WSClientInterface
 
 		return $this->call(__FUNCTION__, $params);
 	}
+	
+	public function getPlot($startTime, $stopTime, $missionID, $userID = "", $password = "")
+	{
+		$params = array('parameters' => array(
+				"startTime" => $startTime,
+				"stopTime"  => $stopTime,
+				"missionID" => $missionID
+		));
+		
+		if ($userID != "" && $password != "")
+		{
+			$params["parameters"]["userID"] = $userID;
+			$params["parameters"]["password"] = $password;
+		}
+		
+		return $this->call(__FUNCTION__, $params);
+	}
+	
+	public function getResultPlot($plotDirectoryURL)
+	{
+		$params = array('parameters' => array(
+				"plotDirectoryURL" => $plotDirectoryURL
+		));
+		
+		return $this->call(__FUNCTION__, $params);
+	}
 
 	private function call($function, $params)
 	{
diff --git a/php/classes/WebServer.php b/php/classes/WebServer.php
index 306eff9..8c5fb2c 100644
--- a/php/classes/WebServer.php
+++ b/php/classes/WebServer.php
@@ -366,6 +366,7 @@ class WebServer
 
     $res = $this->init($data);
     $resMgr = $this->initUserMgr();
+    
     $vars = $res['vars'];
     $mission = $vars["missionID"];
 
@@ -380,51 +381,90 @@ class WebServer
     $dom = new DomDocument("1.0");
     $dom->load(plotsXml);
 
-    chdir($resDirName); // Down to working directory
-    $fileS = fopen("request.list", "w");
-
     $missionTag = $dom->getElementById($mission);
     $params = $missionTag->getElementsByTagName('param');
 
-    $yStart   = 0.1;
-      fwrite($fileS,$params->length.PHP_EOL);
-
-      foreach ($params as $param) {
-         $yStop = $yStart + 0.15;
-         fwrite($fileS,$param->getAttribute('name').' 0 '.$yStart.' 0.95 '.$yStop.' 0 0 0 0'.PHP_EOL);
-         $yStart += 0.15;
-      }
-
-    $startTime = strtotime($vars["startTime"]);
-    $endTime = strtotime($vars["stopTime"]);
-
-    $TIMEINTERVAL = $this->timeInterval2Days($endTime - $startTime);
-    $STARTTIME = $this->startTime2Days($startTime);
-    fwrite($fileS, $STARTTIME.PHP_EOL.$TIMEINTERVAL.PHP_EOL);
-
-    fclose($fileS);
-
-    $myParamBuilder = new ParamBuilder();
-
-//  Process   Local Params without codes if they exist     
-    if (file_exists(Param."LocalParamsList.xml")) {
-	$localParams = new DomDocument('1.0');
-	$localParams->load(Param."LocalParamsList.xml");
-	$xp = new domxpath($localParams);
-	foreach ($params as $param) {
-		$var = $param->getAttribute('name');
-		$paramTag = $xp->query('//PARAM[.="'.$var.'"]');
-		if ($paramTag -> length !== 0) {
-			$myParamBuilder->paramLocalBuild($var);
-		}
-	}
-     }
-
-// Run command
-    $cmd = DDBIN."DD_Plot request.list ".$this->userID." ".$this->IP." ".DDPROJECT." ".DDPROJLIB;
-    $reqMgr = new RequestMgr();
-    $pid =  $reqMgr->background($cmd);
-  
+    $paramsList = array();
+    foreach ($params as $param)
+    	$paramsList[] = $param->getAttribute('name');
+
+    $requestObject = (Object) array(
+    	"nodeType" => "request",
+    	"file-format" => "PNG",
+    	"file-output" => "WS",
+    	"ws-result-file" => $resDirName."/request.list.png",
+    	"last-plotted-tab" => 1,
+    	"timesrc" => "Interval",
+    	"startDate" => $vars["startTime"],
+    	"stopDate" => $vars["stopTime"],
+    	"tabs" => array()
+    );
+    
+    $pageObject = (Object) array(
+    	"id" => 1,
+    	"multi-plot-linked" => true,
+    	"page-margins-activated" => true,
+    	"page-margin-x" => 5,
+    	"page-margin-y" => 5,
+    	"page-orientation" => "portrait",
+    	"page-dimension" => "ISO A4",
+    	"page-layout-type" => "vertical",
+    	"page-layout-object" => (Object) array(
+    		"layout-panel-height" => 0.25,
+    		"layout-panel-spacing" => 0,
+    		"layout-expand" => false
+    	),
+    	"panels" => array()
+    );
+    
+    foreach ($paramsList as $paramToPlot)
+    {
+    	$panelObject = (Object) array(
+    		"panel-plot-type" => "timePlot",
+    		"axes" => array(),
+    		"params" => array()
+    	);
+    	
+    	$timeAxisObject = (Object) array(
+    		"id" => "time",
+    		"axis-type" => "time",
+   			"axis-range-extend" => true
+    	);
+    	$panelObject->{"axes"}[] = $timeAxisObject;
+
+    	$yAxisObject = (Object) array(
+    		"id" => "y-left",
+    		"axis-type" => "y-left",
+   			"axis-range-extend" => true
+    	);
+    	$panelObject->{"axes"}[] = $yAxisObject;
+    	
+    	$paramObject = (Object) array(
+    		"id" => 1,
+    		"param-id" => $paramToPlot,
+    		"param-drawing-type" => "serie",
+    		"param-drawing-object" => (Object) array(
+    			"serie-yaxis" => "y-left",
+    			"serie-lines-activated" => true
+    		)
+    	);
+    	$panelObject->{"params"}[] = $paramObject;
+    	
+    	$pageObject->{"panels"}[] = $panelObject;
+    }
+    
+    $requestObject->{"tabs"}[] = $pageObject;
+    
+    require_once(INTEGRATION_SRC_DIR."RequestManager.php");
+    if (!isset($this->requestManager))
+    	$this->requestManager = new RequestManagerClass();
+     
+    try {
+    	$plotResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PARAMS, $requestObject);
+    } catch (Exception $e) {
+    	if ($this->isSoap)  throw new SoapFault("plot01", 'Exception detected : '.$e->getMessage());
+    	else return array('success' => false, 'message' => 'Exception detected : '.$e->getMessage());
+    }
 
     return array('success' => true, 'plotDirectoryURL' => $ID);
   }
@@ -918,7 +958,7 @@ $vars["sampling"] = strtr($vars["sampling"], array($matches[1] => ""));
   	);
   	
   	try {
-  		$downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, id);
+  		$downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, $obj);
   	} catch (Exception $e) {
   		//Nothing to do
   	}
diff --git a/php/testWebServer.php b/php/testWebServer.php
index b1ae973..b5bbaa6 100644
--- a/php/testWebServer.php
+++ b/php/testWebServer.php
@@ -121,6 +121,33 @@ function runSuiteTests($client)
 		echo "[ERROR] - Error during call of getOrbites function".PHP_EOL;
 	else
 		echo "[OK] - getOrbites - ".$res->dataFileURLs.PHP_EOL;
+	
+	//Test getPlot function
+	$res = $client->getPlot("2008-01-01T00:00:00", "2009-01-01T00:00:00", "ACE");
+	if (!isset($res) || !$res->success)
+		echo "[ERROR] - Error during call of getPlot function".PHP_EOL;
+	else
+	{
+		$plotDirectoryURL = $res->plotDirectoryURL;
+		echo "[OK] - getPlot".PHP_EOL;
+		while (true)
+		{
+			echo "    -> getResultPlot".PHP_EOL;
+			$res = $client->getResultPlot($plotDirectoryURL);
+			if (!isset($res))
+			{
+				echo "[ERROR] - Error during call of getResultPlot function".PHP_EOL;
+				break;
+			}
+			else if ($res->success)
+			{
+				echo "    -> plotFileURL : ".$res->plotFileURL.PHP_EOL;
+				break;
+			}
+			echo "    -> Result not ready => Wait 5s".PHP_EOL;
+			sleep(5);
+		}
+	}
 }
 
 $clientSOAP = new WSClientSOAP("http://localhost/NEWAMDA-BENJAMIN/public/wsdl/Methods_AMDA.wsdl");
--
libgit2 0.21.2