BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
bayeos-socket.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-socket.ini');
9 $config['writer_sleep_time']=0;
10 if(! isset($config['names'])){
11  $names=array();
12  for($i=0;$i<count($config['socket']);$i++){
13  $names[$i]='Socket.'.$i; //-> storage path /tmp/Serial.$i ...
14  }
15 } else $names=$config['names'];
16 
17 
18 
20  private $socket;
21  private $delim;
22  private $dec;
23  private $datetime_format;
24  private $tz;
25  private $fp;
26 
27  //Init Writer
28  protected function initWriter(){
29  $this->socket=$this->getOption('socket');
30  $this->delim=$this->getOption('delim');
31  $this->dec=$this->getOption('dec');
32  $this->tz=$this->getOption('tz');
33  $this->datetime_format=$this->getOption('datetime_format');
34  }
35 
36  private function connect(){
37  if($this->fp) return;
38  while(! $this->fp = stream_socket_client($this->socket,$error,$errstr,30)){
39  sleep(10);
40  }
41  stream_set_blocking($this->fp, 1);
42 
43  }
44 
45  //Generate Data
46  protected function readData(){
47  $this->connect();
48  $line=stream_get_line($this->fp,1024,"\n");
49  $line=trim($line);
50  if(! $line) {
51  fclose($this->fp);
52  $this->fp=0;
53  sleep(1);
54  return FALSE;
55  }
56  //echo "readData: $line\n";
57  return $this->parseData($line);
58  }
59 
60  private function parseData($line){
61  $ts='';
62  if($this->datetime_format){
63  $pos=strpos($line,$this->delim);
64  $ts=substr($line,0,$pos);
65  // echo $this->delim"\n";
66  $line=substr($line,$pos+1);
67  $ts=floatval(DateTime::createFromFormat($this->datetime_format,$ts,new DateTimeZone($this->tz))->format("U.u"));
68  }
69 
70  if($this->dec!='.') $line=str_replace($this->dec,'.',$line);
71  $data=explode($this->delim,trim($line));
72  for($i=0;$i<count($data);$i++){
73  if(! is_numeric($data[$i])) unset($data[$i]);
74  }
75  return array('values'=>$data,'ts'=>$ts);
76  }
77 
78  protected function saveData($data){
79  //save data with timestamp
80  $this->writer->saveDataFrame($data['values'],$this->getOption('data_type'),0,$data['ts']);
81  }
82 
83 
84 }
85 
86 
88  array('data_type'=>0x41,
89  'delim'=>' ',
90  'dec'=>'.',
91  'tz'=>date_default_timezone_get());
92 $my_client->run();
93 
94 ?>
$config
getOption($key, $default='')
$pos
Definition: dumpCat.php:12
if(!isset($config['names'])) else $names
$my_client