#!/bin/perl

$tlast = 0;
$prevkeyid = "";

open(NAMES, "keyid.list");
while(<NAMES>)
{
	($id, $name) = split(/[ \n\t]/);
	%idlist->{$id} = $name;
	%namelist->{$name} = $id;
}
close(NAMES);


open(LINE,"+</dev/ttyS0");

system("stty 9600 -echo -parenb -parodd cs8 -cstopb -crtscts -ixon -ixoff </dev/ttyS0");

print LINE "r";

LOOP: while (<LINE>)
{
	($lockid, $keyid) = split(/[ \n\t]/);

	if (/Delft/)  # copyright string printed when lock is reset
	{
		$datestr = localtime;
		print $datestr, " ", $_, "\n";
#		print LINE "h";
	}

	next LOOP if ($keyid !~ />/);
	next LOOP if (/-/);

	$tnow = time();

	next LOOP if ( $keyid eq $prevkeyid && $tnow - $tlast <= 2);

	$prevkeyid = $keyid;
	$tlast = $tnow;

	$id = substr($keyid,10,6);

	$datestr = localtime;
	print $datestr, " ", $keyid, " ", %idlist->{$id}, "\n";
#	if (%idlist->{$id}) { print LINE "o"; }
}

