<?php
class WorkpointMessage {

	public $nonce = NULL;
	public $reasonForContact = NULL;
	public $items = array();
	public $action = NULL;
	public $status = NULL;

	public function __construct($raw_data) {

		$xml = new SimpleXMLElement($raw_data);
		$data = array();

                $attributes = $xml->Message->attributes();
                $this->nonce = $attributes["nonce"]."";;
                $this->reasonForContact = $xml->Message->ReasonForContact."";
                $msgAttributes = @$xml->Message->ReasonForContact->attributes();
                $this->action = @$msgAttributes["action"]."";;
                $this->status = @$msgAttributes["status"]."";;

		if ($this->status != "busy") {

			$itemList = $xml->Message->ItemList->Item;
			//if (is_array($itemList)) {
			foreach ($itemList as $item) {

				$attr_name = $item->attributes()->name."";
				$attr_index = $item->attributes()->index."";
				$attr_val  = $item."";
				if ($attr_index != "") {
					$this->items[$attr_name][$attr_index] = $attr_val;
				} else {
					$this->items[$attr_name] = $attr_val;
				}
					
				//Log::d($attr_name." = ".$attr_val, "siemens-dls");
					
			}
			//}

		}

		$saveSessionVars = array();
		$saveSessionVars[] = "mac-addr";
		$saveSessionVars[] = "part-number";
		$saveSessionVars[] = "device-type";
		$saveSessionVars[] = "software-version";



		foreach ($saveSessionVars as $var) {
			if (@Yii::app()->session[$var] == "" && @$this->items[$var] != "") {
				Yii::app()->session[$var] = $this->items[$var];
			}
		}
		if (array_key_exists("file-deployment-name", $this->items) && $this->items["file-deployment-name"] != "") {
			Yii::app()->session["file-deployment-name"] = $this->items["file-deployment-name"];
		}

		if (array_key_exists("file-deployment-type", $this->items) && $this->items["file-deployment-type"] != "") {
			Yii::app()->session["file-deployment-type"] = $this->items["file-deployment-type"];
		}

		if (array_key_exists("file-deployment-action", $this->items) && $this->items["file-deployment-action"] != "") {
			Yii::app()->session["file-deployment-action"] = $this->items["file-deployment-action"];
		}
		



		//Log::d("nonce: ".$this->nonce, "siemens-dls");
		//Log::d("ReasonForContact: ".$this->reasonForContact, "siemens-dls");
		Log::d("Items: ".print_r($this->items, true), "siemens-dls");
		//Log::d("Action: ".$this->action, "siemens-dls");
		//Log::d("Status: ".$this->status, "siemens-dls");


	}





}
