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