parallelise serial port usage and I/O for writing sounds files
parallelise serial port usage and I/O for writing sounds files

file:a/.gitignore -> file:b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.wav
 *.pyc
 /nbproject/private/
+/output.txt

--- a/.gitmodules
+++ b/.gitmodules
@@ -3,5 +3,5 @@
 	url = git://github.com/uskr/pynma.git
 [submodule "js/flotr2"]
 	path = js/flotr2
-	url = git://github.com/HumbleSoftware/Flotr2.git
+	url = https://github.com/HumbleSoftware/Flotr2.git
 

--- /dev/null
+++ b/.idea/dictionaries/Madoka.xml
@@ -1,1 +1,10 @@
-
+<component name="ProjectDictionaryState">
+  <dictionary name="Madoka">
+    <words>
+      <w>tgid</w>
+      <w>timefrom</w>
+      <w>timeto</w>
+      <w>tzoffset</w>
+    </words>
+  </dictionary>
+</component>

--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,15 +2,7 @@
 <project version="4">
   <component name="ChangeListManager">
     <list default="true" id="f90ee5b5-97e4-47ec-9b14-d4f4e896f100" name="Default" comment="">
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/encodings.xml" />
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/modules.xml" />
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/scannr.iml" />
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/scopes/scope_settings.xml" />
-      <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/.idea/vcs.xml" />
-      <change type="MODIFICATION" beforePath="$PROJECT_DIR$/calls.json.php" afterPath="$PROJECT_DIR$/calls.json.php" />
-      <change type="MODIFICATION" beforePath="$PROJECT_DIR$/common.inc.php" afterPath="$PROJECT_DIR$/common.inc.php" />
-      <change type="MODIFICATION" beforePath="$PROJECT_DIR$/scannr.py" afterPath="$PROJECT_DIR$/scannr.py" />
+      <change type="MODIFICATION" beforePath="$PROJECT_DIR$/output.txt" afterPath="$PROJECT_DIR$/output.txt" />
       <change type="MODIFICATION" beforePath="$PROJECT_DIR$/viewcalls.php" afterPath="$PROJECT_DIR$/viewcalls.php" />
     </list>
     <ignored path="scannr.iws" />
@@ -34,11 +26,9 @@
       <file leaf-file-name="viewcalls.php" pinned="false" current="true" current-in-tab="true">
         <entry file="file://$PROJECT_DIR$/viewcalls.php">
           <provider selected="true" editor-type-id="text-editor">
-            <state line="66" column="38" selection-start="2349" selection-end="2349" vertical-scroll-proportion="0.31213874">
+            <state line="41" column="69" selection-start="1388" selection-end="1388" vertical-scroll-proportion="1.2659575">
               <folding>
-                <element signature="n#style#0;n#span#0;n#td#0;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
-                <element signature="n#style#0;n#div#0;n#td#1;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
-                <element signature="n#style#0;n#span#0;n#td#2;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
+                <element signature="e#255#287#0" expanded="true" />
               </folding>
             </state>
           </provider>
@@ -47,25 +37,7 @@
       <file leaf-file-name="common.inc.php" pinned="false" current="false" current-in-tab="false">
         <entry file="file://$PROJECT_DIR$/common.inc.php">
           <provider selected="true" editor-type-id="text-editor">
-            <state line="3" column="21" selection-start="90" selection-end="90" vertical-scroll-proportion="-1.9615384">
-              <folding />
-            </state>
-          </provider>
-        </entry>
-      </file>
-      <file leaf-file-name="scannr.py" pinned="false" current="false" current-in-tab="false">
-        <entry file="file://$PROJECT_DIR$/scannr.py">
-          <provider selected="true" editor-type-id="text-editor">
-            <state line="66" column="23" selection-start="2084" selection-end="2084" vertical-scroll-proportion="0.0">
-              <folding />
-            </state>
-          </provider>
-        </entry>
-      </file>
-      <file leaf-file-name="trunklog.php" pinned="false" current="false" current-in-tab="false">
-        <entry file="file://$PROJECT_DIR$/trunklog.php">
-          <provider selected="true" editor-type-id="text-editor">
-            <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="-0.0">
+            <state line="60" column="117" selection-start="2262" selection-end="2262" vertical-scroll-proportion="-39.23077">
               <folding />
             </state>
           </provider>
@@ -74,7 +46,7 @@
       <file leaf-file-name="calls.json.php" pinned="false" current="false" current-in-tab="false">
         <entry file="file://$PROJECT_DIR$/calls.json.php">
           <provider selected="true" editor-type-id="text-editor">
-            <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="-0.0">
+            <state line="72" column="41" selection-start="2843" selection-end="2843" vertical-scroll-proportion="-43.153847">
               <folding />
             </state>
           </provider>
@@ -93,18 +65,18 @@
   <component name="IdeDocumentHistory">
     <option name="changedFiles">
       <list>
+        <option value="$PROJECT_DIR$/scannr.py" />
         <option value="$PROJECT_DIR$/common.inc.php" />
-        <option value="$PROJECT_DIR$/scannr.py" />
+        <option value="$PROJECT_DIR$/calls.json.php" />
         <option value="$PROJECT_DIR$/viewcalls.php" />
       </list>
     </option>
   </component>
   <component name="PhpWorkspaceProjectConfiguration" backward_compatibility_performed="true" />
   <component name="ProjectFrameBounds">
-    <option name="x" value="57" />
-    <option name="y" value="-8" />
-    <option name="width" value="1391" />
-    <option name="height" value="916" />
+    <option name="x" value="619" />
+    <option name="width" value="825" />
+    <option name="height" value="600" />
   </component>
   <component name="ProjectLevelVcsManager" settingsEditedManually="true">
     <OptionsSetting value="true" id="Add" />
@@ -132,7 +104,6 @@
       <sortByType />
     </navigator>
     <panes>
-      <pane id="Scope" />
       <pane id="ProjectPane">
         <subPane>
           <PATH>
@@ -141,30 +112,32 @@
               <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
             </PATH_ELEMENT>
           </PATH>
-          <PATH>
-            <PATH_ELEMENT>
-              <option name="myItemId" value="scannr" />
-              <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
-            </PATH_ELEMENT>
-            <PATH_ELEMENT>
-              <option name="myItemId" value="scannr" />
-              <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
-            </PATH_ELEMENT>
-          </PATH>
         </subPane>
       </pane>
+      <pane id="Scope" />
     </panes>
   </component>
   <component name="PropertiesComponent">
     <property name="options.splitter.main.proportions" value="0.3" />
     <property name="WebServerToolWindowFactoryState" value="false" />
     <property name="options.lastSelected" value="project.propVCSSupport.Mappings" />
+    <property name="last_opened_file_path" value="$PROJECT_DIR$/../busui/myway/myway_timeliness.php" />
+    <property name="options.splitter.details.proportions" value="0.2" />
     <property name="options.searchVisible" value="true" />
-    <property name="options.splitter.details.proportions" value="0.2" />
   </component>
   <component name="RunManager">
     <configuration default="true" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
       <TestRunner />
+      <method />
+    </configuration>
+    <configuration default="true" type="PhpLocalRunConfigurationType" factoryName="PHP Console">
+      <method />
+    </configuration>
+    <configuration default="true" type="JavascriptDebugSession" factoryName="Local">
+      <JSDebuggerConfigurationSettings>
+        <option name="engineId" value="embedded" />
+        <option name="fileUrl" />
+      </JSDebuggerConfigurationSettings>
       <method />
     </configuration>
     <list size="0" />
@@ -190,32 +163,33 @@
   </component>
   <component name="TaskManager">
     <task active="true" id="Default" summary="Default task">
+      <changelist id="f90ee5b5-97e4-47ec-9b14-d4f4e896f100" name="Default" comment="" />
       <created>1350026709905</created>
       <updated>1350026709905</updated>
     </task>
     <servers />
   </component>
   <component name="ToolWindowManager">
-    <frame x="57" y="-8" width="1391" height="916" extended-state="6" />
+    <frame x="619" y="0" width="825" height="600" extended-state="0" />
     <editor active="false" />
     <layout>
-      <window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
+      <window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
       <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
-      <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
+      <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
       <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="true" content_ui="tabs" />
-      <window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.24981189" sideWeight="0.6706349" order="0" side_tool="false" content_ui="combo" />
+      <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.44036698" sideWeight="0.6706349" order="0" side_tool="false" content_ui="combo" />
       <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
-      <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
-      <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32936507" sideWeight="0.5" order="-1" side_tool="true" content_ui="tabs" />
+      <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
+      <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32936507" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
       <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
-      <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
+      <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
       <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
       <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
+      <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
       <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
-      <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
       <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
+      <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
       <window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
-      <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
     </layout>
   </component>
   <component name="VcsContentAnnotationSettings">
@@ -271,41 +245,83 @@
     <breakpoint-manager />
   </component>
   <component name="editorHistoryManager">
+    <entry file="file://$PROJECT_DIR$/viewcalls.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="2" column="13" selection-start="46" selection-end="46" vertical-scroll-proportion="0.0">
+          <folding>
+            <element signature="e#255#287#0" expanded="true" />
+          </folding>
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/common.inc.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="34" column="0" selection-start="1179" selection-end="1179" vertical-scroll-proportion="0.0">
+          <folding />
+        </state>
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/calls.json.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="2" column="9" selection-start="42" selection-end="42" vertical-scroll-proportion="0.0">
+          <folding />
+        </state>
+      </provider>
+    </entry>
     <entry file="file://$PROJECT_DIR$/trunklog.php">
       <provider selected="true" editor-type-id="text-editor">
-        <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0">
+        <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/../disclosr/include/template.inc.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="72" column="93" selection-start="2545" selection-end="2635" vertical-scroll-proportion="0.43404254" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/../disclosr/include/common.inc.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" />
+      </provider>
+    </entry>
+    <entry file="file://C:/tmp/h5bp-html5-boilerplate-2279296/index.html">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="38" column="11" selection-start="1245" selection-end="1982" vertical-scroll-proportion="0.93352604" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/../busui/myway/myway_timeliness.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="27" column="5" selection-start="1003" selection-end="1018" vertical-scroll-proportion="26.346153" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/scannr.py">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="66" column="23" selection-start="2084" selection-end="2084" vertical-scroll-proportion="0.0" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/snd.py">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" />
+      </provider>
+    </entry>
+    <entry file="file://$PROJECT_DIR$/common.inc.php">
+      <provider selected="true" editor-type-id="text-editor">
+        <state line="60" column="117" selection-start="2262" selection-end="2262" vertical-scroll-proportion="-39.23077">
           <folding />
         </state>
       </provider>
     </entry>
     <entry file="file://$PROJECT_DIR$/calls.json.php">
       <provider selected="true" editor-type-id="text-editor">
-        <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0">
+        <state line="72" column="41" selection-start="2843" selection-end="2843" vertical-scroll-proportion="-43.153847">
           <folding />
         </state>
       </provider>
     </entry>
-    <entry file="file://$PROJECT_DIR$/scannr.py">
-      <provider selected="true" editor-type-id="text-editor">
-        <state line="66" column="23" selection-start="2084" selection-end="2084" vertical-scroll-proportion="0.0">
-          <folding />
-        </state>
-      </provider>
-    </entry>
-    <entry file="file://$PROJECT_DIR$/common.inc.php">
-      <provider selected="true" editor-type-id="text-editor">
-        <state line="3" column="21" selection-start="90" selection-end="90" vertical-scroll-proportion="-1.9615384">
-          <folding />
-        </state>
-      </provider>
-    </entry>
     <entry file="file://$PROJECT_DIR$/viewcalls.php">
       <provider selected="true" editor-type-id="text-editor">
-        <state line="66" column="38" selection-start="2349" selection-end="2349" vertical-scroll-proportion="0.31213874">
+        <state line="41" column="69" selection-start="1388" selection-end="1388" vertical-scroll-proportion="1.2659575">
           <folding>
-            <element signature="n#style#0;n#span#0;n#td#0;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
-            <element signature="n#style#0;n#div#0;n#td#1;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
-            <element signature="n#style#0;n#span#0;n#td#2;n#tr#0;n#table#0;n#div#0;n#!!top" expanded="true" />
+            <element signature="e#255#287#0" expanded="true" />
           </folding>
         </state>
       </provider>

file:b/calllog.php (new)
--- /dev/null
+++ b/calllog.php
@@ -1,1 +1,22 @@
+<?php
+include ('common.inc.php');
+    $sth = $conn->prepare( 'select * from recordings
+            order by call_timestamp desc limit 10');
 
+    $sth->execute(Array());
+
+$row = 0;
+echo "<table>";
+foreach ($sth->fetchAll() as $data) {
+        
+            
+        echo "<tr>";
+        for ($c=0; $c < count($data); $c++) {
+            echo '<td>'.$data[$c] . "</td>\n";
+        }
+        echo "</tr>";
+        }
+        $row++;
+echo "</table>";
+?>
+

--- a/calls.json.php
+++ b/calls.json.php
@@ -1,39 +1,92 @@
 <?php
+include('common.inc.php');
+function getTGIDValuesByHour($TGID, $timeFrom, $timeTo) {
+    global $conn;
+    $sth = $conn->prepare( 'select tgid, min(call_timestamp) as time, count(*), min(length), max(length), avg(length), stddev(length) from recordings
+            group by tgid, date_trunc(\'hour\', call_timestamp) order by time');
+
+    $sth->execute( );
+    //Array($TGID, $timeFrom, $timeTo)
+        return  $sth->fetchAll();
 
 
-function json_graph($sensorID, $timefrom, $timeto) {
-    $values = getSensorValuesByHour($sensorID, $timefrom, $timeto);
-    $label = $sensorID;
-    $data = Array();
-    $tzoffset = $this->get_timezone_offset("UTC");
-    foreach ($values as $value) {
-        $data[] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg']));
-    }
-    $this->output
-            ->set_content_type('application/json')
-            ->set_output(json_encode(Array("label" => $label, "data" => $data,
-                        "previous" => Array(
-                            "from" => $timefrom - (24 * 60 * 60),
-                            "to" => $timefrom)
-                        ,
-                        "next" => Array(
-                            "to" => $timeto + (24 * 60 * 60),
-                            "from" => $timeto)
-                            )
+}
+
+function getTGIDValuesByDay($TGID, $dayFrom, $dayTo) {
+    global $conn;
+    $sth = $conn->prepare('select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ?
+            group by sensor_id, date_trunc(\'day\', time) order by time');
+
+    $sth->execute( Array($TGID));
+    return  $sth->fetchAll();
+}
+function getTGIDDataYears($TGID, $timeFrom, $timeTo) {
+    global $conn;
+    $sth = $conn->prepare("select distinct extract('year' from call_timestamp) as year from recordings where tgid = ? order by year");
+
+    $sth->execute(Array($TGID));
+    return  $sth->fetchAll();
+}
+
+function getTGIDDataMonths($TGID, $timeFrom, $timeTo) {
+    global $conn;
+    $sth = $conn->prepare("select distinct extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ?  order by year, month");
+
+    $sth->execute(Array($TGID));
+    return  $sth->fetchAll();
+}
+
+function getTGIDDataDays($TGID, $timeFrom, $timeTo) {
+    global $conn;
+    $sth = $conn->prepare("select distinct extract('day' from call_timestamp) as day, extract('month' from call_timestamp) as month, extract('year' from call_timestamp) as year from recordings where tgid = ?  order by year,month,day");
+
+
+    $sth->execute(Array($TGID));
+    return  $sth->fetchAll();
+}
+$action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
+$TGID = (isset($_REQUEST['tgid']) ? $_REQUEST['tgid'] : '');
+$timefrom = (isset($_REQUEST['from']) ? $_REQUEST['from'] : '');
+$timeto = (isset($_REQUEST['to']) ? $_REQUEST['to'] : '');
+
+if ($action == "data_description") {
+    $timefrom = strtotime($timefrom);
+    $timeto = strtotime($timeto);
+    $years = getTGIDDataYears($TGID, $timefrom, $timeto);
+
+    $months = getTGIDDataMonths($TGID, $timefrom, $timeto);
+    $days = getTGIDDataDays($TGID, $timefrom, $timeto);
+
+    echo json_encode(Array("years" => $years, "months" => $months, "days" => $days
                     ));
 }
 
-function json_data_description($sensorID, $timefrom, $timeto) {
-    $timefrom = strtotime($timefrom);
-    $timeto = strtotime($timeto);
-    $years = getSensorDataYears($sensorID, $timefrom, $timeto);
 
-    $months = getSensorDataMonths($sensorID, $timefrom, $timeto);
-    $days = getSensorDataDays($sensorID, $timefrom, $timeto);
+if (strpos($action,"graph") !== false) {
+    $values = getTGIDValuesByHour($TGID, $timefrom, $timeto);
+    $label = $TGID;
+    $data = Array();
+    $tzoffset = get_timezone_offset("UTC");
+    foreach ($values as $value) {
+        if ($action == "graphlength") {
+        $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['avg']));
+        } else if ($action == "graphcount") {
+            $data[$value['tgid']][] = Array((strtotime($value['time']) + $tzoffset) * 1000, intval($value['count']));
+        }
+    }
+   echo json_encode(Array("label" => $label, "data" => $data,
+            "previous" => Array(
+                "from" => $timefrom - (24 * 60 * 60),
+                "to" => $timefrom)
+        ,
+            "next" => Array(
+                "to" => $timeto + (24 * 60 * 60),
+                "from" => $timeto)
+        )
+    );
+}
 
-    return json_encode(Array("years" => $years, "months" => $months, "days" => $days
-                    ));
-}
+
 
 ?>
 

--- a/common.inc.php
+++ b/common.inc.php
@@ -1,5 +1,16 @@
 <?php
 date_default_timezone_set("Australia/Sydney");
+try {
+    $conn = new PDO("pgsql:dbname=scannr;user=postgres;password=snmc;host=localhost");
+    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+}
+catch(PDOException $e) {
+    die('Unable to connect to database server.');
+}
+catch(Exception $e) {
+    die('Unknown error in ' . __FILE__ . '.');
+}
+$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 $basePath = "";
 $DATA_DIR = "./data";
 
@@ -22,96 +33,42 @@
     return $offset;
 }
 
-function getSensorValuesByHour($sensorID, $timeFrom, $timeTo) {
-    $sql = 'select min(call_timestamp) as time, count(*), min(length), max(length), avg(length), stddev(length) from recordings
-            group by tgid, date_trunc(\'hour\', call_timestamp) order by time';
 
-    $query = $this->db->query($sql, Array($sensorID, $timeFrom, $timeTo));
-    return $query->result_array();
-}
 
-function getSensorValuesByDay($sensorID, $dayFrom, $dayTo) {
-    $sql = 'select min(time) as time, min(value), max(value), avg(value), stddev(value) from sensor_values where sensor_id = ? 
-            group by sensor_id, date_trunc(\'day\', time) order by time';
 
-    $query = $this->db->query($sql, Array($sensorID));
-    return $query->result_array();
-}
-
-function getSensorDataYears($sensorID, $timeFrom, $timeTo) {
-    $sql = "select distinct extract('year' from call_timestamp) as year from recordings where tgid = ? order by year";
-
-    $query = $this->db->query($sql, Array($sensorID));
-    return $query->result_array();
-}
-
-function getSensorDataMonths($sensorID, $timeFrom, $timeTo) {
-    $sql = "select distinct extract('month' from time) as month, extract('year' from time) as year from sensor_values where sensor_id = ? order by year, month";
-
-    $query = $this->db->query($sql, Array($sensorID));
-    return $query->result_array();
-}
-
-function getSensorDataDays($sensorID, $timeFrom, $timeTo) {
-    $sql = "select distinct extract('day' from time) as day, extract('month' from time) as month, extract('year' from time) as year from sensor_values where sensor_id = ? order by year,month,day";
-
-    $query = $this->db->query($sql, Array($sensorID));
-    return $query->result_array();
-}
 
 function include_header($title) {
     global $basePath;
     ?>
-    <!DOCTYPE html>
+<!DOCTYPE html>
+<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
+<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
+<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
+<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
+<head>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+    <title></title>
+    <meta name="description" content="">
+    <meta name="viewport" content="width=device-width">
 
-    <!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
-    <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
-    <!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
-    <!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
-    <!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
-        <head>
-            <meta charset="utf-8" />
+    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
 
-            <!-- Set the viewport width to device width for mobile -->
-            <meta name="viewport" content="width=device-width" />
+    <link rel="stylesheet" href="css/normalize.css">
+    <link rel="stylesheet" href="css/main.css">
+    <script src="js/vendor/modernizr-2.6.1.min.js"></script>
+    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
+    <!--<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.0.min.js"><\/script>')</script>-->
+    <script type="text/javascript" src="<?php echo $basePath ?>js/flotr2/flotr2.js"></script>
+    <script src="js/plugins.js"></script>
+    <script src="js/main.js"></script>
+</head>
+    <body>
+        <!--[if lt IE 7]>
+<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
+<![endif]-->
 
-            <title><?php echo $title; ?> - Scannr</title>
-
-            <!-- Included CSS Files -->
-            <link rel="stylesheet" href="<?php echo $basePath ?>stylesheets/foundation.css">
-            <link rel="stylesheet" href="<?php echo $basePath ?>stylesheets/app.css">
-
-            <!--[if lt IE 9]>
-                    <link rel="stylesheet" href="<?php echo $basePath ?>stylesheets/ie.css">
-            <![endif]-->
-
-
-            <!-- IE Fix for HTML5 Tags -->
-            <!--[if lt IE 9]>
-                    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
-            <![endif]-->
-
-