From d7192c2b7f1d883ad292da84172b201db5274a4d Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 16 Mar 2011 14:45:43 +0100 Subject: [PATCH] adapted Views to new DataController improved debugging and entity defintion --- lib/Definition/EntityDefinition.json | 4 +- lib/Util/Debug.php | 64 ++++++++++------------------ lib/View/JSON.php | 27 +++++++----- lib/View/XML.php | 32 ++++++++++---- 4 files changed, 64 insertions(+), 63 deletions(-) diff --git a/lib/Definition/EntityDefinition.json b/lib/Definition/EntityDefinition.json index 2872296..e49f299 100644 --- a/lib/Definition/EntityDefinition.json +++ b/lib/Definition/EntityDefinition.json @@ -178,9 +178,9 @@ } }, { - "name" : "radition", + "name" : "radiation", "required" : ["title"], - "optional" : ["description", "details:", "owner:", "address:", "tolerance", "public"], + "optional" : ["description", "details:", "owner:", "address:", "tolerance", "resolution", "public"], "icon" : "radioactivity.png", "unit" : "μSv", "interpreter" : "Volkszaehler\\Interpreter\\SensorInterpreter", diff --git a/lib/Util/Debug.php b/lib/Util/Debug.php index be8d5ca..148ef54 100644 --- a/lib/Util/Debug.php +++ b/lib/Util/Debug.php @@ -64,12 +64,6 @@ class Debug { throw new \Exception('Debugging has already been started. please use the static functions!'); } self::$instance = $this; - - // assert options - assert_options(ASSERT_ACTIVE, TRUE); // activate assertions - assert_options(ASSERT_BAIL, FALSE); - assert_options(ASSERT_WARNING, FALSE); - assert_options(ASSERT_CALLBACK, array($this, 'assertHandler')); } /* @@ -82,46 +76,27 @@ class Debug { if (isset(self::$instance)) { $trace = debug_backtrace(FALSE); $info = $trace[0]; + $level = self::$instance->level; - self::$instance->messages[] = array( - 'message' => $message, - 'file' => $info['file'], - 'line' => $info['line'], - 'args' => array_slice($info['args'], 1), - 'trace' => array_slice($trace, 1) - ); + $message = array('message' => $message); + + if ($level > 2) { + $message['file'] = $info['file']; + $message['line'] = $info['line']; + } + + if ($level > 4) { + $message['args'] = array_slice($info['args'], 1); + } + + if ($level > 5) { + $message['trace'] = array_slice($trace, 1); + } + + self::$instance->messages[] = $message; } } - /** - * simple assertion passthrough for future improvements - * - * @param string $code code to be evaluated - */ - public static function assert($code) { - return assert($code); - } - - /** - * handles failed assertions - * - * @param string $file - * @param integer $line - * @param string $code code to be evaluated - */ - public function assertHandler($file, $line, $code) { - $trace = debug_backtrace(); - $info = $trace[2]; - - $this->messages[] = array( - 'message' => 'assertion failed: ' . $code, - 'file' => $info['file'], - 'line' => $info['line'], - 'time' => date('r'), - 'trace' => array_slice($trace, 3) - ); - } - /** * Is debugging enabled? * @return boolean @@ -149,6 +124,11 @@ class Debug { */ public static function getInstance() { return self::$instance; } + /** + * @return integer current debug level + */ + public function getLevel() { return $this->level; } + /** * Tries to determine the current SHA1 hash of your git commit * diff --git a/lib/View/JSON.php b/lib/View/JSON.php index 4fe0fe3..e8b5f6f 100644 --- a/lib/View/JSON.php +++ b/lib/View/JSON.php @@ -160,6 +160,7 @@ class JSON extends View { */ protected function addDebug(Util\Debug $debug) { $jsonDebug['time'] = $debug->getExecutionTime(); + $jsonDebug['level'] = $debug->getLevel(); $jsonDebug['messages'] = $debug->getMessages(); $jsonDebug['queries'] = array_values($debug->getQueries()); @@ -210,25 +211,29 @@ class JSON extends View { } ); - $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); - $this->json['data']['count'] = count($data); - $this->json['data']['rowCount'] = $interpreter->getRowCount(); - $min = $interpreter->getMin(); $max = $interpreter->getMax(); - $average = View::formatNumber($interpreter->getAverage()); + $average = $interpreter->getAverage(); + + $from = $interpreter->getFrom(); + $to = $interpreter->getTo(); + $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); + if (isset($from)) + $this->json['data']['from'] = $from; + if (isset($to)) + $this->json['data']['to'] = $to; if (isset($min)) - $this->json['data']['min'] = $min; + $this->json['data']['min'] = $min; if (isset($max)) - $this->json['data']['max'] = $max; + $this->json['data']['max'] = $max; if (isset($average)) - $this->json['data']['average'] = $average; + $this->json['data']['average'] = View::formatNumber($average); if ($interpreter instanceof Interpreter\MeterInterpreter) $this->json['data']['consumption'] = View::formatNumber($interpreter->getConsumption()); - if (count($data) > 0) { - $this->json['data']['tuples'] = $data; - } + $this->json['data']['count'] = count($data); + if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($data) > 0) + $this->json['data']['tuples'] = $data; } /** diff --git a/lib/View/XML.php b/lib/View/XML.php index 6167b13..504d8a1 100644 --- a/lib/View/XML.php +++ b/lib/View/XML.php @@ -164,6 +164,7 @@ class XML extends View { */ protected function addDebug(Util\Debug $debug) { $xmlDebug = $this->xmlDoc->createElement('debug'); + $xmlDebug->setAttribute('level', $debug->getLevel()); $xmlDebug->appendChild($this->xmlDoc->createElement('time', $debug->getExecutionTime())); $xmlMessages = $this->xmlDoc->createElement('messages'); @@ -232,23 +233,38 @@ class XML extends View { $xmlTuples = $this->xmlDoc->createElement('tuples'); $data = $interpreter->processData( - $this->request->getParameter('tuples'), - $this->request->getParameter('group'), function($tuple) use ($xmlDoc, $xmlTuples) { $xmlTuple = $xmlDoc->createElement('tuple'); - $xmlTuple->setAttribute('timestamp', $tuple[0]); // hardcoded data fields for performance optimization + $xmlTuple->setAttribute('timestamp', $tuple[0]); $xmlTuple->setAttribute('value', View::formatNumber($tuple[1])); $xmlTuple->setAttribute('count', $tuple[2]); $xmlTuples->appendChild($xmlTuple); } ); + $min = $interpreter->getMin(); + $max = $interpreter->getMax(); + $average = $interpreter->getAverage(); + + $from = $interpreter->getFrom(); + $to = $interpreter->getTo(); + $xmlData->appendChild($this->xmlDoc->createElement('uuid', $interpreter->getEntity()->getUuid())); - $xmlData->appendChild($this->xmlDoc->createElement('min', $interpreter->getMin())); - $xmlData->appendChild($this->xmlDoc->createElement('max', $interpreter->getMax())); - $xmlData->appendChild($this->xmlDoc->createElement('average', self::formatNumber($interpreter->getAverage()))); - $xmlData->appendChild($this->xmlDoc->createElement('consumption', self::formatNumber($interpreter->getConsumption()))); - $xmlData->appendChild($xmlTuples); + if (isset($from)) + $xmlData->appendChild($this->xmlDoc->createElement('from', $from)); + if (isset($to)) + $xmlData->appendChild($this->xmlDoc->createElement('to', $to)); + if (isset($min)) + $xmlData->appendChild($this->xmlDoc->createElement('min', $min)); + if (isset($max)) + $xmlData->appendChild($this->xmlDoc->createElement('max', $max)); + if (isset($average)) + $xmlData->appendChild($this->xmlDoc->createElement('average', View::formatNumber($average))); + if ($interpreter instanceof Interpreter\MeterInterpreter) + $xmlData->appendChild($this->xmlDoc->createElement('consumption', View::formatNumber($interpreter->getConsumption()))); + $xmlData->appendChild($this->xmlDoc->createElement('count', count($data))); + if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($data) > 0) + $xmlData->appendChild($xmlTuples); $this->xmlRoot->appendChild($xmlData); }