BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
bayeos-logger-importer.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 require 'BayEOSGatewayClient.php';
4 
5 /*
6  * Read configuration file and set some defaults
7  */
8 $config=parse_ini_file('/etc/bayeos-logger-importer.ini');
9 $names=$config['sender'];
10 $config['writer_sleep_time']=0;
11 $ref_date= DateTime::createFromFormat('Y-m-d H:i:s P','2000-01-01 00:00:00 +00:00')->format('U');
12 
13 
15  private $queue;
16  private $queue_fp;
17  private $queue_file;
18 
19  //Init Writer
20  protected function initWriter(){
21  $this->queue=$this->getOption('tmp_dir').'/'.str_replace(array('/','\\','"','\''),'_',$this->name).'_queue';
22  if(! is_dir($this->queue)){
23  if(! mkdir($this->queue,0777,TRUE)){
24  die("could not create ".$this->queue);
25  }
26  }
27  exec('chmod 1777 '.$this->queue);
28  }
29 
30 
31  private function openQueueFile(){
32  if($this->queue_fp) return 1;
33  do {
34  sleep(2);
35  $files=glob($this->queue.'/*');
36  } while(count($files)==0);
37  $this->queue_file=$files[0];
38  $this->queue_fp=fopen($files[0],'r');
39  }
40 
41  //Generate Data
42  protected function readData(){
43  if($this->queue_fp && feof($this->queue_fp)){
44  fclose($this->queue_fp);
45  $this->queue_fp=0;
46  unlink($this->queue_file);
47  }
48  $this->openQueueFile();
49 
50  $ts=$this->getOption('timeshift')+$GLOBALS['ref_date']+BayEOSType::unpackUINT32(fread($this->queue_fp,4));
51  $length=BayEOSType::unpackUINT8(fread($this->queue_fp,1));
52  $bayeosframe=fread($this->queue_fp,$length);
53  return array('frame'=>$bayeosframe,'ts'=>$ts);
54  }
55 
56  protected function saveData($data){
57  //save the original frame with the ts
58  $this->writer->saveFrame($data['frame'],$data['ts']);
59  }
60 
61 
62 }
63 
64 
65 $my_client = new PHPLoggerImporter($names,$config,array('timeshift'=>0));
66 $my_client->run();
67 
68 ?>
getOption($key, $default='')
static unpackUINT32($value, $endianness=0)
static unpackUINT8($value)