12 require_once
'BayEOSGatewayClient.php';
17 $config=parse_ini_file(
'/etc/bayeos-fifo.ini');
20 for($i=0;$i<count(
$config[
'script']);$i++){
37 private $datetime_format;
41 private $indexed_frame;
49 while((
$c=fgetc($this->fp_data))!==FALSE){
52 while((
$c=fgetc($this->fp_error))!==FALSE){
56 if(
$pos=strpos($this->error,
"\n")){
57 $line=trim(substr($this->error,0,
$pos));
58 fwrite(STDERR,date(
'Y-m-d H:i:s').
" $name: $line\n");
59 $this->error=substr($this->error,
$pos+1);
63 if(
$pos=strpos($this->data,
"\n"))
68 if($timeout<=0)
return FALSE;
69 $line=trim(substr($this->data,0,
$pos));
70 $this->data=substr($this->data,
$pos+1);
79 $pos=strpos($line,$this->delim);
80 $origin=substr($line,0,
$pos);
81 $line=substr($line,
$pos+1);
83 if($this->datetime_format){
84 $pos=strpos($line,$this->delim);
85 $ts=substr($line,0,
$pos);
86 $line=substr($line,
$pos+1);
87 $ts_obj=DateTime::createFromFormat($this->datetime_format,$ts,
new DateTimeZone($this->tz));
89 fwrite(STDERR, date(
'Y-m-d H:i:s').
" Timestamp parse error: $ts with ".$this->datetime_format.
"\n");
92 $ts=floatval($ts_obj->format(
"U.u"));
95 if($this->dec!=
'.') $line=str_replace($this->dec,
'.',$line);
96 $tmp=explode($this->delim,trim($line));
98 for(
$i=0;
$i<count($tmp);
$i++){
99 if($this->indexed_frame){
100 list($key,$value)=explode(
':',$tmp[
$i]);
102 if(! is_numeric($key))
103 fwrite(STDERR, date(
'Y-m-d H:i:s').
"Non numeric key '$key'\n");
106 } elseif($this->data_type<65) {
107 $data[
$i]=(is_numeric($tmp[
$i])?$tmp[
$i]:NAN);
108 } elseif(is_numeric($tmp[
$i]))
112 return array(
'values'=>$data,
'ts'=>$ts,
'origin'=>$origin);
118 $this->writer->saveOriginFrame($data[
'origin'],
122 $this->writer->saveDataFrame($data[
'values'],$this->data_type,0,$data[
'ts']);
126 private function termChilds($pid){
127 exec(
"pgrep -P $pid",$res);
128 if(isset($res[0])) $this->termChilds($res[0]);
129 posix_kill($pid,SIGTERM);
130 $res2=pcntl_waitpid($pid,$status);
132 echo date(
'Y-m-d H:i:s').
" Terminated ".($pid==$this->pid_script?
"sript":
"child of").
' '.$this->
getOption(
'script').
133 " of ".$this->name.
" with pid ".$pid.
"\n";
139 $fifo=$this->options[
'tmp_dir'].
'/'.str_replace(array(
'/',
'\\',
'"',
'\''),
'_',
$name);
141 $this->data_fifo=$fifo.
'.data.fifo';
142 posix_mkfifo($this->data_fifo,$this->
getOption(
'data_fifo_mode',0600));
143 if(! is_writable($this->data_fifo))
144 die(
"Failed to create $fifo.data_fifo\n");
145 $this->error_fifo=$fifo.
'.error.fifo';
146 posix_mkfifo($this->error_fifo,$this->
getOption(
'error_fifo_mode',0600));
147 if(! is_writable($this->error_fifo))
148 die(
"Failed to create $fifo.error_fifo\n");
151 $tmp=explode(
" ",$script);
152 if(! (is_executable($script) || is_executable($tmp[0])))
153 die(
"$script is not executable\n");
158 die(
'Could not fork reader process!');
162 $this->pid_script=$pid;
164 pcntl_signal(SIGTERM,
function($signo) {
168 $this->termChilds($this->pid_script);
169 echo date(
'Y-m-d H:i:s').
" Script ".$this->
getOption(
'script').
" for ".$this->name.
" with pid ".$this->pid_script.
" terminated.\n";
177 $this->fp_data=fopen($this->data_fifo,
'r');
178 $this->fp_error=fopen($this->error_fifo,
'r');
179 stream_set_blocking($this->fp_data,
false);
180 stream_set_blocking($this->fp_error,
false);
181 echo date(
'Y-m-d H:i:s').
" Started script $script for $name with pid $pid\n";
183 if($this->delim==
'\t') $this->delim=
"\t";
186 $this->datetime_format=$this->
getOption(
'datetime_format');
187 $this->origin=$this->
getOption(
'origin');
188 $this->indexed_frame=$this->
getOption(
'indexed_frame');
189 $this->data_type=intval($this->
getOption(
'data_type'),0);
190 if($this->indexed_frame && $this->data_type<64){
191 echo date(
'Y-m-d H:i:s').
" Notice: data_type ".$this->data_type.
192 " does not support indexed frames. Will use ".(($this->data_type&0xf)|0x40).
" instead\n";
193 $this->data_type=($this->data_type&0xf)|0x40;
198 exec(
"$script >".$this->data_fifo.
" 2>".$this->error_fifo.
" </dev/null");
210 array(
'data_type'=>0x1,
214 'indexed_frame'=>FALSE,
215 'tz'=>date_default_timezone_get()));
static createDataFrame($values, $type=0x1, $offset=0)
if(!isset($config['names'])) else $names
getOption($key, $default='')