rendered paste body<?php
/**
* LazerBall Impact Counter Parser
*
* @copyright 2006 Johnny Robeson
* @package lbic
* @credit Matias Griese for the parsing routine
*/
/**
* Parse Packets
*
PROTOCOL
Packet structure
(AAAACn)
AAAA = address (FFFF=GLOBAL)
C = command
F=front 1-9 (zones,0=None) > send
B=back 1-9 (zones,0=None) > send
M=Motion 1 (Motion 1=active) > send
E=Energy 0-1 (Battery 1=Good, 0=Low) > send
A=ACK 1 (Acknowledge=1) > send
W=beep 1-9 (seconds) < receive
C=command 0-9 < receive
0=Acknowledge
1=battery check
2=Front zone poll
3=Back zone poll
4=Read Motion
5=Beep
6=Calibration
7=resend last message
You need to send the address of the unit for each command. 0000C2 responds
with the state of the front zone.
0000C0 responds with an acknowledge.
0000C51 beeps the beeper for 1 second.
0000C6 returns the timing value of the last hit and clears the value.
*/
class LBICPacketParser
{
private $enableDebug = false;
private $buffer = '';
/**
* Constructor
*
* @param bool $debug
*/
function __construct($debug = false)
{
$this->enableDebug = $debug;
}
/**
* Parse Data
*
* $data = "Let's Play LazerBall!! 7B06(7B06F7)Err.. This is not right(7B15F2)(7001B2)(7B06F7)(7B15F2)(7001B2)(7";
* @param str $data
* return array
*/
function parse($data = '')
{
$commands = array();
$pattern1 = '/(\([A-Z0-9]{4}[F|B][0-7]\))/';
$pattern2 = '/\(([A-Z0-9]{4})([F|B][0-7])\)|(Let\'s Play Lazerball!!) ([A-Z0-9]{4})/';
$this->debug("DATA IN: '$data'");
$this->debug("BUFFER: '$this->buffer'");
$data = $this->buffer.$data;
$this->debug("DATA APPEND: '$data'");
//$stream = trim($stream);
$test = preg_split($pattern1, $data, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
//$this->debug(var_dump($test));
$saved = '';
while($foo = array_shift($test)) {
$foo = trim($foo, "\r\n");
if (!empty($saved)) {
$this->debug("DISCARD: '$saved'");
$saved = '';
continue;
}
preg_match($pattern2, $foo, $matches);
if (isset($matches[4])) {
list($string, $dummy, $dummy2, $initMessage, $radioID) = $matches;
$this->debug('INIT: '. $radioID);
$commands[] = array('type' => 'init',
'time' => time(),
'radioID' => $radioID);
$this->buffer = '';
} else if (isset($matches[2])) {
list($string, $radioID, $zone) = $matches;
$this->debug('HIT: ' .$radioID . ' IN: ' . $zone);
$commands[] = array('type' => 'hit',
'time' => time(),
'radioID' => $radioID,
'zone' => $zone);
$this->buffer = '';
} else {
$saved = $foo;
}
}
if (!empty($saved)) {
$this->debug("INCOMPLETE: '$saved'");
$this->buffer = $saved;
}
return $commands;
}
function debug($string)
{
if ($this->enableDebug) {
if (empty($string)) return;
$debugString = $string;
echo 'DEBUG: ' .$debugString . "\n";
//if (is_resource($this->debugFile)) {
// fwrite($this->debugFile,$debugString);
//}
}
}
}
?>