<?php

	//echo 'Boo!';

		if ($function == "point")
		{						
			//Grab history for this property.
			$id = findassetbyname($assetname);
			if ($id <= 0)
			{		
				//echo "No Asset";
				//echo "] }\r\n";		
				continue;
			}
			
			$ass = new Asset($id);
			$props = $ass->GetProperties();
			$foundprop = 0;
			
			$readlist = array();				
			
			$data = $ass->GetProperties();
			foreach($data as $d)
			{
				if ($propertyname == "all analog")
				{
					switch($d->type)
					{
						case 'MEASUREMENT':						
							$readlist[] = $d;
					}
				}
				else
				{
					switch($d->type)
					{						
						case 'STATUS':
						case 'ENUM':
							$readlist[] = $d;
					}
				}
			}
			
			$ass->GetValues($readlist,TRUE);
			
			foreach($readlist as $rl)
			{
			
				$node = "";
				//print_r($readlist[0]);
				if ((isset($rl->value['_dynamics'])) && ($rl->value['_dynamics'][0] == 'measurement'))
				{
					$node = "measurement";
				}
				if ((isset($rl->value['_dynamics'])) && ($rl->value['_dynamics'][0] == 'state'))
				{
					$node = "state";
				}
				if ((isset($rl->value['_dynamics'])) && ($rl->value['_dynamics'][0] == 'value'))
				{
					$node = "value";
				}
				
				if ($node == "")
				{
					//echo 'Node?';
					//echo "] }\r\n";		
					continue;
				}
				
				$sourceasset = $rl->origin;
				if ($rl->origintype == 'template')
				{
					$sourceasset = $id;
				}
				
				$measurement = $rl->id;
				
				$pointset = array();
				$v = array();			
				$v['node'] = $node;
				$v['property'] = $measurement;
				$v['asset'] = $sourceasset;
				$v['id'] = 1;
				$pointset[] = $v;
				
				$grain = -100;
							
				$start = gmdate('Y-m-d H:i:s', $stime->getTimestamp());
				$end = gmdate('Y-m-d H:i:s', $etime->getTimestamp());
				
				//print_r($pointset);
				//print_r($start);
				
				$res = GetBulkHistory($pointset,$start,$end,$grain,'continue',true,1);		
				if ($res === FALSE)
				{		
					//echo 'No Data!';
					$res = array();
				}
				
				//echo "Results: ";
				//print_r($res);
				
				$points = array();
				foreach($res as $r)
				{
					
					$p = array();
					$p[0] = DateTime::createFromFormat('Y-m-d H:i:s',$r[1])->getTimestamp();
					$p[1] = $r[2];		
					$points[] = $p;
				}
				
				$firstdp = true;
				
				if ($first == true)
					$first = false;
				else
					echo ','; 
				
				$firstdp = true;
			
			?>	{
		"target":"<?php echo $rl->name.' ( '.$targ->asset.' )'; ?>",
		"datapoints":[ <?php
				foreach($points as $px)
				{
					if ($firstdp == true)
					{
						$firstdp = false;
						echo "\r\n";
					}
					else
						echo ",\r\n";
					
					echo '      ['.$px[1].','.($px[0] * 1000).']';
				}
				?>			
		]
	}<?php 
			}			
		}
		
		if ($function == "current point")
		{	
			
			//Grab history for this property.
			$id = findassetbyname($assetname);
			if ($id <= 0)
			{				
				//echo "] }\r\n";		
				continue;
			}
			
			$ass = new Asset($id);
			$props = $ass->GetProperties();
			$foundprop = 0;
			
			$readlist = array();
			
			
			
			
			$data = $ass->GetProperties();
			foreach($data as $d)
			{
				if ($propertyname == "all analog")
				{
					switch($d->type)
					{
						case 'MEASUREMENT':						
							$readlist[] = $d;
					}
				}
				else
				{
					switch($d->type)
					{						
						case 'STATUS':
						case 'ENUM':
							$readlist[] = $d;
					}
				}
				
			}
			
			$ass->GetValues($readlist);
			
			foreach($readlist as $rl)
			{
			
				$points = array();
				$p = array();
				$p[0] = $stime->getTimestamp();
				$p[1] = ExtractValue($rl);		
				$points[] = $p;
				$p = array();
				$p[0] = $etime->getTimestamp();
				$p[1] = $points[0][1];	
				$points[] = $p;		
				
				if ($first == true)
					$first = false;
				else
					echo ','; 
				
				$firstdp = true;
			
			?>	{
		"target":"<?php echo $rl->name.' ( '.$targ->asset.' )'; ?>",
		"datapoints":[ <?php
			foreach($points as $px)
			{
				if ($firstdp == true)
				{
					$firstdp = false;
					echo "\r\n";
				}
				else
					echo ",\r\n";
				
				echo '      ['.$px[1].','.($px[0] * 1000).']';
			}
			?>			
		]
	}<?php 
	
			}
		}	
?>