array(),'orm'=>array()); private $_timestamp = NULL; public function __construct($file) { if (! file_exists($file)) throw new Kohana_Exception('File doesnt exist (:file)',array(':file'=>$file)); $this->process(file_get_contents($file)); } public function __toString() { return (string)sprintf('%s %s',join('|',$this->_events['protocol']),date('d-m-Y H:i:s',$this->_timestamp)); } /** * Check that our Event is valid */ public function check() { return $this->_caller AND $this->_timestamp AND $this->_events['protocol']; } private function process($content) { $meta = $event = FALSE; $et = NULL; $good = TRUE; $events = array(); foreach (explode("\n",$content) 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': $protocol = 'Protocol_ContactID'; break; default: throw new Kohana_Exception('Unknown protocol :protocol',array(':protocol'=>$v)); } break; case 'CALLINGFROM': $this->_caller = $v; break; case 'CALLERNAME': $this->_callername = $v; break; case 'TIMESTAMP': $this->_timestamp = $v; break; default: printf('Extra data in event file - meta section (%s)',$line); } } elseif ($event) { $x = new $protocol($line); if ($x->event()) array_push($this->_events['protocol'],$x); } } } public function save() { $result = FALSE; foreach ($this->_events['protocol'] as $id => $po) { $eo = ORM::factory('Event'); $eo->values(array( 'alarmevent'=>$po, 'datetime'=>$this->_timestamp, 'account_id'=>ORM::factory('Account',array('alarmphone'=>$this->_caller,'siteid'=>$po->accnum())), 'protocol'=>$po->protocol(), )); // @todo With this logic and multiple events, if 1 event doesnt save, we'll loose it. if ($eo->save() AND ! $result) $result = TRUE; $this->_events['orm'][$id] = $eo; } return $result; } public function trigger() { // Work through each of the account triggers foreach ($this->_events['orm'] as $id => $orm) { $contacts = array(); foreach ($orm->account_id->trigger->find_all() as $to) { if (! in_array($to->contact->id,$contacts) AND (is_null($to->alarm) OR $to->alarm == $this->_events['protocol'][$id]->etype())) { // Get details of the event. $po = $orm->account_id->model->plugin($this->_events['protocol'][$id],$orm->account_id); if ($to->email) { $e = Email::connect(); $sm = Swift_Message::newInstance(); $sm->setSubject('Notice from your alarm'); $sm->setBody(sprintf('Event ID: %s, Message: %s',$orm->id,(string)$po)); $sm->setTo($to->contact->email); $sm->setFrom('noreply@leenooks.net'); $e->send($sm); } if ($to->sms) { } // We dont need to process any more triggers to the same account. array_push($contacts,$to->contact->id); } } } } } ?>