This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
halmon/application/classes/controller/task/event.php
2013-10-16 09:55:29 +11:00

114 lines
2.4 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* HomeALarmMONitor Receive an Event
*
* @package HAM
* @subpackage CLI/Event
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_Task_Event extends Controller_Template {
/**
* Read in the events
*/
public function action_index() {
$dh = opendir(Kohana::config('config.event_dir'));
$fp = Kohana::config('config.event_file_prefix');
while ($f = readdir($dh)) {
if (preg_match("/^{$fp}/",$f)) {
$eventfile = sprintf('%s/%s',Kohana::config('config.event_dir'),$f);
if ($events = $this->event_file(file_get_contents($eventfile))) {
// Delete the event file, so that it isnt processed again
if (Kohana::config('config.event_file_keep'))
rename($eventfile,Kohana::config('config.event_dir').'/processed-'.$f);
else
unlink($eventfile);
// Trigger
$eo = new Events($events);
$eo->trigger();
} else
rename($eventfile,Kohana::config('config.event_dir').'/bad-'.$f);
}
}
}
private function event_file($file) {
$meta = $event = FALSE;
$et = $cf = $cn = $dt = NULL;
$good = TRUE;
$events = array();
foreach (explode("\n",$file) as $line) {
// Ignore out blank lines
if (! trim($line))
continue;
if (preg_match('/^\[metadata\]/',$line)) {
$meta = TRUE;
$event = FALSE;
continue;
} elseif (preg_match('/^\[events\]/',$line)) {
$meta = FALSE;
$event = TRUE;
continue;
}
if ($meta) {
list ($k,$v) = explode('=',$line,2);
switch ($k) {
case 'PROTOCOL':
switch ($v) {
case 'ADEMCO_CONTACT_ID':
$et = 'ContactID';
break;
default:
throw new Kohana_Exception('Unknown protocol :protocol',array(':protocol'=>$v));
}
break;
case 'CALLINGFROM':
$cf = $v;
break;
case 'CALLERNAME':
$cn = $v;
break;
case 'TIMESTAMP':
$dt = $v;
break;
default:
printf('Extra data in event file - meta section (%s)',$line);
}
} elseif ($event) {
if (! $et)
throw new Kohana_Exception('Event data without any protocol');
$eo = new $et;
if (! $eo->event($line,$dt,$cf,$cn))
$good = FALSE;
else
array_push($events,$eo);
}
}
return $events;
}
}
?>