IHMInputOutputParamInfoClass.php 12.8 KB
<?php

/**
 * @class IHMInputOutputParamInfoClass
 * @brief Class that's implement an InputOutputInterface used to treat a get param info request
 * @details
 */
class IHMInputOutputParamInfoClass implements InputOutputInterface
{
	private $paramInfoData        = null;
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}

	/*
	 * @brief translate input data from IHM client to AMDA_Integration module for a get param info request
	*/
	public function getInputData($input,$function,$requestId="")
	{
		$this->paramInfoData = new ParamInfoRequestDataClass();
		
		switch ($input->type) {
			case 'plot_init' :
				$this->paramInfoData->setType(ParamInfoTypeEnumClass::PLOTINIT);
				
				if (!isset($input->paramId) || empty($input->paramId))
					throw new Exception("Param info request need a param id as argument");
				$paramFilePath = IHMConfigClass::getLocalParamDBPath().$input->paramId.".xml";
				
				if (!file_exists($paramFilePath))
					throw new Exception("Cannot find parameter definition file");
				
				$this->paramInfoData->setFilePath($paramFilePath);
				break;
			case 'param_info' :
				$this->paramInfoData->setType(ParamInfoTypeEnumClass::PARAMINFO);
				
				if (!isset($input->paramId) || empty($input->paramId))
					throw new Exception("Param info request need a param id as argument");
				
				
				$paramInfoFilePath = IHMConfigClass::getLocalParamInfoPath().'info_'.$input->paramId.".xml";
				
				if (!file_exists($paramInfoFilePath))
					throw new Exception("Cannot find parameter info file " . $paramInfoFilePath);
				
				$this->paramInfoData->setFilePath($paramInfoFilePath);
				break;
			default :
				throw new Exception("Para info request type '".$input->type."' not yet implemented");
		}
		
		$this->paramInfoData->setParamId($input->paramId);
		
		return $this->paramInfoData;
	}

	/*
	 * @brief translate output data from AMDA_Integration module to IHM client for a get param info request
	*/
	public function getOutput($data)
	{
		switch ($data->getType()) {
			case ParamInfoTypeEnumClass::PLOTINIT :
				$result = array("success" => false, "message" => "Cannot get plot init");
				$this->getOutputPlotInit($data, $result);
				break;
			case ParamInfoTypeEnumClass::PARAMINFO :
				$result = array("success" => false, "message" => "Cannot get param info");
				$this->getOutputParamInfo($data, $result);
				break;
			default:
				//Nothing ToDo
		}
		
		return $result;
	}
	
	private function getOutputParamInfo($data, &$result) {
		if (($data->getResult() != NULL)) {
			$result = array(
					"success" => true,
					"data" => $data->getResult()
			);
		}
	}
	
	private function getOutputPlotInit($data, &$result) {
		if (($data->getResult() != NULL) && ($data->getResult()->getName() == "plot") && (count($data->getResult()->getChildren()) >= 1)) {
			$result_data = array();
				
			$panelNode = $data->getResult()->getChildren()[0];
				
			$plotNode = NULL;
			if (count($panelNode->getChildren()) >= 1) {
				foreach ($panelNode->getChildren() as $crtNode) {
					if (!in_array($crtNode->getName(), array('timePlot', 'epochPlot', 'xyPlot', 'instantPlot', 'statusPlot', 'tickPlot')))
						continue;
					$plotNode = $crtNode;
					break;
				}
		
				if (isset($plotNode)) {
					$result_data['panel'] = array();
					switch ($plotNode->getName()) {
						case 'timePlot' :
							$result_data['panel']['plotType'] = 'timePlot';
		
							break;
						case 'epochPlot' :
							$result_data['panel']['plotType'] = 'epochPlot';
		
							break;
						case 'xyPlot' :
							$result_data['panel']['plotType'] = 'xyPlot';
							$result_data['panel']['isotropic'] = ($plotNode->getIsIsotropic() == "true");
							$result_data['additionalObjects'] = $this->unmarshallAdditionalObjectsData($plotNode);
							break;
						case 'instantPlot' :
							$result_data['panel']['plotType'] = 'instantPlot';
		
							break;
						case 'statusPlot' :
							$result_data['panel']['plotType'] = 'statusPlot';
		
							break;
						case 'tickPlot' :
							$result_data['panel']['plotType'] = 'tickPlot';
		
							break;
					}
						
					$result_data['draw'] = $this->unmarshallDrawData($plotNode, $data->getParamId());
				}
		
				$result = array(
						"success" => true,
						"data" => $result_data
				);
				//var_dump($result);
			}
				
			/*if (count($paramsNode->getChildren()) >= 1) {
			 $paramNode = $paramsNode->getChildren()[0];
			if (count($paramNode->getChildren()) >= 1) {
			$drawNode = $paramNode->getChildren()[0];
				
				
			switch ($drawNode->getName()) {
			case 'serie' :
			$data['drawType'] = 'serie';
			break;
			case 'orbitserie' :
			$data['drawType'] = 'orbit-serie';
			break;
			}
				
			$result = array(
					"success" => true,
					"data" => $data
			);
			}
			}*/
		}
	}
	
	private function unmarshallDrawData($plotNode, $paramId) {
		$result_data = array();
		
		if (!isset($plotNode))
			return $result_data;
		
		$paramsNode = $plotNode->getParams();
		
		if (!isset($paramsNode))
			return $result_data;
		
		$paramNode = $paramsNode->getParamById($paramId);
		
		if (!isset($paramNode) || (count($paramNode->getChildren()) < 1))
			return $result_data;
		
		$mainDrawNode = NULL;
		$otherDrawNodes = array();
		
		foreach ($paramNode->getChildren() as $crtDrawNode) {
			if (in_array($crtDrawNode->getName(), array('serie', 'spectro', 'yserie', 'orbitserie')))
				$mainDrawNode = $crtDrawNode;
			else
				$otherDrawNodes[$mainDrawNode->getName()] = $crtDrawNode;
		}
		
		if (!isset($mainDrawNode))
			return $result_data;
		
		switch ($mainDrawNode->getName()) {
			case 'serie' :
				$result_data['type'] = 'serie';
				$this->unmarshallBaseSerieData($mainDrawNode, $result_data);
				//if (array_key_exists('colorserie', $otherDrawNodes))
				//	$this->unmarshallColorSerieData($mainDrawNode, $result_data);
				break;
			case 'spectro' :
				$result_data['type'] = 'spectro';
				$this->unmarshallSpectroData($mainDrawNode, $result_data);
				break;
			case 'yserie' :
				if (!array_key_exists('xserie', $otherDrawNodes))
					return $result_data;
				$this->unmarshallBaseSerieData($mainDrawNode, $result_data);
				//$this->unmarshallXSerieData($mainDrawNode, $result_data);
				//if (array_key_exists('colorserie', $otherDrawNodes))
				//	$this->unmarshallColorSerieData($mainDrawNode, $result_data);
				break;
			case 'orbitserie' :
				$result_data['type'] = 'orbit-serie';
				$result_data['serie-projection'] = $mainDrawNode->getProjection();
				$this->unmarshallBaseSerieData($mainDrawNode, $result_data);
				//if (array_key_exists('colorserie', $otherDrawNodes))
				//	$this->unmarshallColorSerieData($mainDrawNode, $result_data);
				break;
			default :
				return $result_data;
		}
		
		return $result_data;
	}
	
	private function unmarshallSpectroData($drawNode, &$result_array) {
		if (!isset($drawNode))
			return;
		
		if (!empty($drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_YAXIS)))
			$result_array['spectro-yaxis'] = $drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_YAXIS);
		
		if (!empty($drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_MIN)))
			$result_array['spectro-value-min'] = $drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_MIN);
		
		if (!empty($drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_MAX)))
			$result_array['spectro-value-max'] = $drawNode->getAttribute(REQUESTOUTPUTPLOTSPECTRO_MAX);
	}
	
	private function unmarshallAdditionalObjectsData($plotNode) {
		$result_data = array();
		
		if (!isset($plotNode))
			return $result_data;
		
		$additionalObjectsNode = $plotNode->getAdditionalObjects();
		
		//curvePlot
		$result_data['curves'] = array();
		$curveObjectsNode = $additionalObjectsNode->getCurveObjects();
		foreach ($curveObjectsNode as $curveNode) {
			$crt_curve_data = array();
			$crt_curve_data['curve-serie-id'] = $curveNode->getSerieId();
			$this->unmarshallLineData($curveNode, "curve-line-", $crt_curve_data);
			
			if ($curveNode->isFunctionDefined()) {
				$crt_curve_data['curve-name'] = $curveNode->getFunction()->getFunctionName();
				$crt_curve_data['curve-attributes'] = array();
				
				foreach ($curveNode->getFunction()->getAttributes()->getChildren() as $attributeNode) {
					$crt_attribute_data = array();
					
					$crt_attribute_data['curve-param-name'] = $attributeNode->getAttribute(REQUESTOUTPUTPLOTCURVEOBJECTFUNCTION_ATTRIBUTENAME);
					$crt_attribute_data['curve-param-value'] = $attributeNode->getAttribute(REQUESTOUTPUTPLOTCURVEOBJECTFUNCTION_ATTRIBUTEVALUE);
					
					$crt_curve_data['curve-attributes'][] = $crt_attribute_data;
				}
				
			}
			
			$result_data['curves'][] = $crt_curve_data;
		}
		
		return $result_data;
	}
	
	private function unmarshallBaseSerieData($drawNode, &$result_array) {
		if (!isset($drawNode))
			return;
		
		$result_array['serie-id'] = $drawNode->getId();
		
		$result_array['serie-yaxis'] = $drawNode->getYAxisId();
		
		if ($drawNode->isLineDefined()) {
			$result_array['serie-lines-activated'] = true;
			$this->unmarshallLineData($drawNode->getLine(), "serie-lines-", $result_array);
		}
		
		if ($drawNode->isSymbolDefined()) {
			$result_array['serie-symbols-activated'] = true;
			$this->unmarshallSymbolData($drawNode->getSymbol(), "serie-symbols-", $result_array);
		}
		
		if (!empty($drawNode->getColorSerieId())) {
			//ToDo
		}
		
		if ($drawNode->isTimeTicksDefined()) {
			$result_array['serie-timetick-activated'] = true;
			
			$result_array['serie-timetick-step'] = $drawNode->getTimeTicks()->getStep();
			$result_array['serie-timetick-nbmajor'] = $drawNode->getTimeTicks()->getNumber();
			$result_array['serie-timetick-nbminor'] = $drawNode->getTimeTicks()->getMinor();
			$result_array['serie-timetick-color'] = $this->toHexColor($drawNode->getTimeTicks()->getColor());
			
			if (!empty($result_array['serie-timetick-nbmajor']) && intval($result_array['serie-timetick-nbmajor']) > 0)
				$result_array['serie-timetick-type'] = 'nb-major';
			else if (!empty($result_array['serie-timetick-step']) && intval($result_array['serie-timetick-step']) > 0)
				$result_array['serie-timetick-type'] = 'time-step';
			else
				$result_array['serie-timetick-type'] = 'auto';
			
			if ($drawNode->getTimeTicks()->isSymbolDefined()) {
				$this->unmarshallSymbolData($drawNode->getTimeTicks()->getSymbol(), "serie-timetick-symbols-", $result_array);
			}
			
			if ($drawNode->getTimeTicks()->isFirstSymbolDefined()) {
				$result_array['serie-timetick-firstsymbols-activated'] = true;
				$this->unmarshallSymbolData($drawNode->getTimeTicks()->getFirstSymbol(), "serie-timetick-firstsymbols-", $result_array);
			}
			
			if ($drawNode->getTimeTicks()->isFontDefined()) {
				$result_array['serie-timetick-font-activated'] = true;
				$this->unmarshallFontData($drawNode->getTimeTicks()->getFont(), "serie-timetick-font-", $result_array);
			}
		}
		
		if ($drawNode->isIntervalTicksDefined()) {
			$result_array['serie-intervaltick-activated'] = true;
			
			$result_array['serie-intervaltick-mode'] =  $drawNode->getIntervalTicks()->getMode();
			$result_array['serie-intervaltick-color'] =  $this->toHexColor($drawNode->getIntervalTicks()->getColor());
			
			if ($drawNode->getIntervalTicks()->isSymbolDefined()) {
				$this->unmarshallSymbolData($drawNode->getIntervalTicks()->getSymbol(), "serie-intervaltick-", $result_array);
			}
			
			if ($drawNode->getIntervalTicks()->isFontDefined()) {
				$result_array['serie-intervaltick-font-activated'] = true;
				$this->unmarshallFontData($drawNode->getIntervalTicks()->getFont(), "serie-intervaltick-font-", $result_array);
			}
		}
	}
	
	private function unmarshallLineData($lineNode, $prefix, &$result_array) {
		$result_array[$prefix.'style'] = $lineNode->getStyle();
		$result_array[$prefix.'width'] = $lineNode->getWidth();
		$result_array[$prefix.'color'] = $this->toHexColor($lineNode->getColor());
	}
	
	private function unmarshallSymbolData($symbolNode, $prefix, &$result_array) {
		$result_array[$prefix.'type'] = $symbolNode->getType();
		$result_array[$prefix.'size'] = $symbolNode->getSize();
		$result_array[$prefix.'color'] = $this->toHexColor($symbolNode->getColor());
	}
	
	private function  unmarshallFontData($fontNode, $prefix, &$result_array) {
		$result_array[$prefix.'name'] = $fontNode->getFontName();
		$result_array[$prefix.'size'] = $fontNode->getSize();
		$result_array[$prefix.'bold'] = ($fontNode->getWeight() == RequestOutputPlotFontWeight::BOLD);
		$result_array[$prefix.'italic'] = ($fontNode->getStyle() == RequestOutputPlotFontStyle::ITALIC);
	}
	
	private function toHexColor($color) {
		$temp = str_replace(array('[', ']', ' '), '', $color);
		$arr = explode(',', $temp);
		if (count ($arr) != 3)
			return '#000000';
		return '#'.sprintf('%02x', $arr[0]).sprintf('%02x', $arr[1]).sprintf('%02x', $arr[2]);
	}
}

?>