BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
bayeos-serial-router.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 require 'bayeosSerial.php';
4 require 'BayEOSGatewayClient.php';
5 
6 /*
7  * Read configuration file and set some defaults
8  */
9 $config=parse_ini_file('/etc/bayeos-serial-router.ini');
10 $config['writer_sleep_time']=0;
11 
12 if(! isset($config['names'])){
13  $names=array();
14  for($i=0;$i<count($config['device']);$i++){
15  $names[$i]='Serial.'.$i; //-> storage path /tmp/Serial.$i ...
16  }
17 } else $names=$config['names'];
18 
19 
21  private $baySerial;
22  private $read_error_count;
23  private $device;
24 
25  //Init Writer
26  protected function initWriter(){
27  $this->baySerial= new baySerial();
28  //
29  $this->openDevice();
30  }
31 
32  //Open Device
33  //Will create a lockfile in /var/lock/
34  private function openDevice(){
35  $this->device=$this->getOption('device');
36  if($this->device=='auto'){
37  $this->device=$this->findDevice();
38  }
39  if(! $this->device)
40  die("No device available");
41  $this->baySerial->confDefaults($this->device);
42  $this->baySerial->confBaudRate($this->getOption('baud',38400));
43  if($this->baySerial->open()===FALSE)
44  die("Could not open baySerial device");
45  $fp=fopen(str_replace('/dev/','/var/lock/',$this->device),'w');
46  fwrite($fp,''.getmypid());
47  fclose($fp);
48  echo date('Y-m-d H:i:s').': '.$this->name.': '.$this->device.' opened'."\n";
49  }
50 
51  //Find Device
52  private function findDevice(){
53  $devices=glob($this->getOption('device_search','/dev/ttyUSB*'));
54  for($i=0;$i<count($devices);$i++){
55  $lockfile=str_replace('/dev/','/var/lock/',$devices[$i]);
56  if(! file_exists($lockfile)) return $devices[$i];
57  //Lockfile exists
58  $fp=fopen($lockfile,'r');
59  $pid=fgets($fp,10);
60  fclose($fp);
61  $lines_out = array();
62  exec('ps '.(int)$pid, $lines_out);
63  if(count($lines_out) < 2) {
64  // Process is not running
65  fwrite(STDERR,date('Y-m-d H:i:s').': '.$this->name.': Old lockfile '.$lockfile.' detected. Deleting '."\n");
66  unlink($lockfile);
67  return $devices[$i];
68  }
69 
70  }
71  return false;
72 
73  }
74 
75  //close device and remove lock file
76  private function closeDevice(){
77  $this->baySerial->close();
78  unlink(str_replace('/dev/','/var/lock/',$this->device));
79  }
80 
81  //Generate Data
82  protected function readData(){
83  if($data=$this->baySerial->getFrame($this->getOption('read_timeout',120))){
84  $this->read_error_count=0;
85  return $data['frame'];
86  }
87  $this->read_error_count++;
88  if($this->read_error_count>$this->getOption('maxerror_before_reopen',2)){
89  $this->closeDevice();
90  $this->openDevice();
91  $this->read_error_count=0;
92  }
93  return FALSE;
94  }
95 
96  //Save Data
97  protected function saveData($data){
98  //echo "saveData: ".array_pop(unpack('H*',$data))."\n";
99  $this->writer->saveFrame($data);
100  }
101 
102 
103 }
104 
105 
106 $my_client = new PHPSerialRouter($names,$config,array('sleep_between_childs'=>1));
107 $my_client->run();
108 
109 ?>
if(!isset($config['names'])) else $names
$fp
Definition: testFifo.php:5
getOption($key, $default='')