3 require
'bayeosSerial.php';
4 require
'BayEOSGatewayClient.php';
9 $config=parse_ini_file(
'/etc/bayeos-serial-router.ini');
14 for($i=0;$i<count(
$config[
'device']);$i++){
22 private $read_error_count;
27 $this->baySerial=
new baySerial();
34 private function openDevice(){
36 if($this->device==
'auto'){
37 $this->device=$this->findDevice();
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());
48 echo date(
'Y-m-d H:i:s').
': '.$this->name.
': '.$this->device.
' opened'.
"\n";
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];
58 $fp=fopen($lockfile,
'r');
62 exec(
'ps '.(
int)$pid, $lines_out);
63 if(count($lines_out) < 2) {
65 fwrite(STDERR,date(
'Y-m-d H:i:s').
': '.$this->name.
': Old lockfile '.$lockfile.
' detected. Deleting '.
"\n");
76 private function closeDevice(){
77 $this->baySerial->close();
78 unlink(str_replace(
'/dev/',
'/var/lock/',$this->device));
83 if(
$data=$this->baySerial->getFrame($this->getOption(
'read_timeout',120))){
84 $this->read_error_count=0;
85 return $data[
'frame'];
87 $this->read_error_count++;
88 if($this->read_error_count>$this->
getOption(
'maxerror_before_reopen',2)){
91 $this->read_error_count=0;
99 $this->writer->saveFrame(
$data);
if(!isset($config['names'])) else $names
getOption($key, $default='')