<?php

function PIE_settings()
  {
    return array('flags'              => ISF_LOCAL,
                 'outgaugehost'       => '127.0.0.1',
                 'outgaugeport'       => 49999);
  }
      
function PIE_start()
  {
    $PIE = PIE::connect();
    
    $fp = fopen(PIE_SCRIPTDIR.'speedlog.csv', 'w');
    $PIE->set('filepointer', $fp); 
  }
      
function PIE_OutGauge($data)
  {
    $PIE = PIE::connect();
    
    $lasttime = $PIE->get('lastOutGauge');
    $thistime = $data['Time'];
    
    $PIE->set('lastOutGauge', $thistime);
    if ($lasttime >= $thistime)
      return;
    
    $speed = number_format(($data['Speed']*3.5398), 1);
    PIE_sendpacket(array( 'Type'     => ISP_BTN,
                          'ClickID'  => 7,
                          'BStyle'   => 0,
                          'T'        => 147,
                          'L'        => 90,
                          'W'        => 20,
                          'H'        => 8,
                          'Text'     => $speed));
                          
    $fp = $PIE->get('filepointer');
    if ($fp)
      fwrite($fp, ''.$thistime.','.$speed.PHP_EOL);    
  }
  
function PIE_shutdown()
  {
    $PIE = PIE::connect(); 
    
    $fp = $PIE->get('filepointer');
    fclose($fp);
  }