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/contactid.php
2013-10-16 09:55:29 +11:00

101 lines
2.5 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is handling Ademco Contact ID Events
*
* @package HAM
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class ContactID {
// Raw event ID
private $id = '';
private $acct;
private $sub;
private $qualifier;
private $type;
private $area;
private $alarm;
private $checksum;
private $datetime;
public function event($id,$dt,$cf='',$cn='') {
// @todo This shouldnt stop program execution, rather it should generate an email to an admin
if (! $this->check($id))
throw new Kohana_Exception('ContactID event :event is not valid',array(':event'=>$id));
// Process event trigger
$this->id = $id;
$this->acct = substr($id,0,4);
$this->sub = substr($id,4,2);
$this->qualifier = substr($id,6,1);
$this->type = substr($id,7,3);
$this->area = substr($id,10,2);
$this->info = substr($id,12,3);
$this->checksum = substr($id,15,1);
$this->datetime = $dt;
// Check the event is from a valid account
// @todo This shouldnt stop program execution, rather it should generate an email to an admin
$ao = ORM::factory('account',array('alarmphone'=>$cf,'siteid'=>$this->acct));
if (! $ao->loaded())
throw new Kohana_Exception('Event from unknown account :id is not valid',array(':id'=>$ao->id));
$eo = ORM::factory('event');
$eo->account_id = $ao->id;
$eo->alarmevent = $id;
$eo->datetime = $dt;
$eo->save();
// @todo This shouldnt stop program execution, rather it should generate an email to an admin
if (! $eo->saved())
throw new Kohana_Exception('ContactID event :event is not valid',array(':event'=>$id));
return $this;
}
public function __get($k) {
if (isset($this->{$k}))
return $this->{$k};
else
throw new Kohana_Exception('Unknown property :property',array(':property'=>$k));;
}
// Check that this event is valid
private function check($id) {
$checksum_map = array(
'0'=>10,
'1'=>1,
'2'=>2,
'3'=>3,
'4'=>4,
'5'=>5,
'6'=>6,
'7'=>7,
'8'=>8,
'9'=>9,
'*'=>11,
'#'=>12,
'A'=>13,
'B'=>14,
'C'=>15,
);
if (strlen($id) != 16)
throw new Kohana_Exception('ContactID event :event has an invalid length :length',array(':event'=>$id,':length'=>strlen($id)));
// Our checksum should be mod15
$c = $t = 0;
while ($c<strlen($id))
$t += $checksum_map[substr($id,$c++,1)];
if ($t%15 === 0)
return TRUE;
else
return FALSE;
}
}
?>