#!/usr/bin/perl -w
#################################################################
#                                                               #
#   Pipe demonstration program for receiving from a pipe and    #
#                      sending to a device                      #
#                                                               #
#################################################################

# here, instead of a device on serial 1, we are just going to output
# to the screen
sub printit;

$digitonly = 1;

# put the bit of code for opening the serial port here

#open the pipe for input
$ot = time;
$of = "--";
open (PIPE, "+<webctr") or die $!;
  while (<PIPE>) {
    $t = time;
    chomp;
    $a = $_;
    #get the function
    $a =~ m/(..) (.+)/;
    $fn = $1;
    $val = $2;
    if ($fn eq "ut") {
      if ($t - $ot >= 3) {
        if ($digitonly == 0) {
          printit "Up time: $val";
        } else {
          printit "$val";
        }
        $of = $fn;
        $ot = $t;
      } else {
        $of = $fn;
      }
    } elsif ($fn eq "cn") {
      if ($digitonly == 0) {
        1 while ($val =~ s/(\d+)(\d\d\d)/$1,$2/);
        printit "Web Counter: $val";
      } else {
        printit "$val";
      }
      $of = $fn;
      $ot = $t;
    } else {
      printit " $val";
    }
  };
close PIPE;

# put the bit of code for closing the serial port here

exit (0);

sub printit {
  $a = shift;
  # here we put our program for writing to the port
  # note that the port is already open so we don't
  # have to do that


  # until then, we'll write to the screen instead
  print "$a\n"
}
