BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
BayEOSGatewayClient.php
Go to the documentation of this file.
1 <?php
95 class BayEOSType {
96 
97  public static function BYTE($value) {
98  return chr($value & 0xFF);
99  }
100 
101  public static function UINT16($value,$endianness = 0) {
102  return pack(($endianness?"n":"v"),$value);
103  }
104 
105  public static function UINT32($value,$endianness = 0){
106  return pack(($endianness?"N":"V"),$value);
107  }
108 
109  public static function UINT64($value,$endianness = 0){
110  $a = gmp_init("$value");
111  $b=array();
112  for($i=0;$i<4;$i++){
113  $res = gmp_div_qr($a, "0x10000");
114  $a=$res[0];
115  $b[$i]=gmp_intval($res[1]);
116  }
117 
118  if($endianness) return pack("nnnn",$b[3],$b[2],$b[1],$b[0]);
119  else return pack("vvvv",$b[0],$b[1],$b[2],$b[3]);
120  }
121 
122  public static function FLOAT32($value,$endianness = 0){
123  $float = pack("f", $value);
124  // set 32-bit unsigned integer of the float
125  $w = unpack("L", $float);
126  return self::UINT32($w[1],$endianness);
127  }
128 
129  public static function INT32($value,$endianness = 0){
130  $int = pack("l", $value);
131  // set 32-bit unsigned integer of the signed long
132  $w = unpack("L", $int);
133  return self::UINT32($w[1],$endianness);
134  }
135 
136  public static function INT16($value,$endianness = 0){
137  $int = pack("s", $value);
138  // set 16-bit unsigned integer of the signed short
139  $w = unpack("S", $int);
140  return self::UINT16($w[1],$endianness);
141  }
142 
143  public static function unpackUINT8($value) {
144  $res=unpack("C",$value);
145  return $res[1];
146  }
147 
148  public static function unpackINT8($value) {
149  $res=unpack("c",$value);
150  return $res[1];
151  }
152 
153  public static function unpackUINT16($value,$endianness = 0) {
154  $res=unpack(($endianness?"n":"v"),$value);
155  return $res[1];
156 
157  }
158 
159  public static function unpackUINT32($value,$endianness = 0) {
160  $res=unpack(($endianness?"N":"V"),$value);
161  return $res[1];
162  }
163 
164  public static function unpackUINT64($value,$endianness = 0) {
165  $res=unpack(($endianness?"N2":"V2"),$value);
166  if($endianness) return $res[1]<<32+$res[2];
167  else return $res[2]<<32+$res[1];
168  }
169 
170  public static function unpackFLOAT32($value,$endianness = 0) {
171  $res=unpack(($endianness?"N":"V"),$value); //INT32-Value
172  $int=pack("L",$res[1]); //Binary INT in Maschine Byte Order
173  $res=unpack("f",$int); //Float
174  return $res[1];
175  }
176 
177  public static function unpackINT32($value,$endianness = 0) {
178  $res=unpack(($endianness?"N":"V"),$value); //INT32-Value
179  $int=pack("L",$res[1]); //Binary INT in Maschine Byte Order
180  $res=unpack("l",$int); //signed long
181  return $res[1];
182  }
183 
184  public static function unpackINT16($value,$endianness = 0) {
185  $res=unpack(($endianness?"n":"v"),$value); //INT16-Value
186  $int=pack("S",$res[1]); //Binary INT in Maschine Byte Order
187  $res=unpack("s",$int); //signed long
188  return $res[1];
189  }
190 }
191 
192 
193 class BayEOS {
202  public static function createDataFrame($values,$type=0x1,$offset=0){
203  $bayeos_frame=pack("C2",0x1,intval($type,0));
204  //Extract offset and data type
205  $offset_type=(0xf0 & $type);
206  $data_type=(0x0f & $type);
207  if($offset_type==0x0) $bayeos_frame.=pack("C",$offset); //Simple offset Frame
208  while(list($key,$value)=each($values)){
209  if($offset_type==0x40) $bayeos_frame.=pack("C",$key); //Offset-Value-Frame
210  switch ($data_type){
211  case 0x1:
212  $bayeos_frame.=BayEOSType::FLOAT32($value); //float32le
213  break;
214  case 0x2:
215  $bayeos_frame.=BayEOSType::INT32($value); //int32le
216  break;
217  case 0x3:
218  $bayeos_frame.=BayEOSType::INT16($value); //int16le
219  break;
220  case 0x4:
221  $bayeos_frame.=pack("C",$value); //int8
222  break;
223  case 0x5:
224  $bayeos_frame.=pack("d",$value); //double - Note: double may only work on little endian mashines..
225  break;
226  }
227  }
228  //echo "BayEOS-Frame: ".array_pop(unpack('H*',$bayeos_frame))."\n";
229 
230  return $bayeos_frame;
231  }
232 
243  public static function parseFrame($frame,$ts=FALSE,$origin='',$rssi=FALSE){
244  if(! $ts) $ts=microtime(TRUE);
245  $type=array_pop(unpack("C",substr($frame,0,1)));
246  $res=array();
247  switch($type){
248  case 0x1:
249  $res['value']=BayEOS::parseDataFrame($frame);
250  $res['type']="DataFrame";
251  break;
252  case 0x2:
253  $res['value']=substr($frame,2);
254  $res['cmd']=array_pop(unpack('C',substr($frame,1,1)));
255  $res['type']="Command";
256  break;
257  case 0x3:
258  $res['value']=substr($frame,2);
259  $res['cmd']=array_pop(unpack('C',substr($frame,1,1)));
260  $res['type']="CommandResponse";
261  break;
262  case 0x4:
263  $res['value']=substr($frame,1);
264  $res['type']="Message";
265  break;
266  case 0x5:
267  $res['value']=substr($frame,1);
268  $res['type']="ErrorMessage";
269  break;
270  case 0x6:
271  $origin.='/XBee'.BayEOSType::unpackUINT16(substr($frame,3,2)).':'.BayEOSType::unpackUINT16(substr($frame,1,2));
272  return BayEOS::parseFrame(substr($frame,5),$ts,$origin,$rssi);
273  case 0x7:
274  $ts-=BayEOSType::unpackUINT32(substr($frame,1,4))/1000;
275  return BayEOS::parseFrame(substr($frame,5),$ts,$origin,$rssi);
276  break;
277  case 0x8:
278  $origin.='/XBee'.BayEOSType::unpackUINT16(substr($frame,3,2)).':'.BayEOSType::unpackUINT16(substr($frame,1,2));
279  $rssi_neu=BayEOSType::unpackUINT8(substr($frame,5,1));
280  if(! $rssi) $rssi=$rssi_neu;
281  if($rssi_neu>$rssi) $rssi=$rssi_neu;
282  return BayEOS::parseFrame(substr($frame,6),$ts,$origin,$rssi);
283  case 0x9:
284  $ts=DateTime::createFromFormat('Y-m-d H:i:s P','2000-01-01 00:00:00 +00:00')->format('U')+
285  BayEOSType::unpackUINT32(substr($frame,1,4));
286  return BayEOS::parseFrame(substr($frame,5),$ts,$origin,$rssi);
287  break;
288  case 0xa:
289  $res['pos']=BayEOSType::unpackUINT32(substr($frame,1,4));
290  $res['value']=substr($frame,5);
291  $res['type']="Binary";
292  break;
293  case 0xb:
294  $length=BayEOSType::unpackUINT8(substr($frame,1,1));
295  $origin=substr($frame,2,$length);
296  return BayEOS::parseFrame(substr($frame,$length+2),$ts,$origin,$rssi);
297  case 0xc:
298  $ts=array_pop(unpack('d',substr($frame,1,8)));
299  return BayEOS::parseFrame(substr($frame,9),$ts,$origin,$rssi);
300  break;
301  default:
302  error_log('ParseFrame: Unexpected type '.$type);
303  $res['type']='Unknown';
304  $res['value']=$frame;
305  }
306  $res['ts']=$ts;
307  $res['ts_f']=DateTime::createFromFormat('U',round($ts))->format('Y-m-d H:i:s P');
308  $res['origin']=$origin;
309  if($rssi) $res['rssi']=$rssi;
310  return $res;
311  }
312 
313 
320  public static function parseDataFrame($frame){
321  if(substr($frame,0,1)!=pack("C",0x1)) return FALSE;
322  $type=array_pop(unpack("C",substr($frame,1,1)));
323  //Extract offset and data type
324  $offset_type=(0xf0 & $type);
325  $data_type=(0x0f & $type);
326  $pos=2;
327  $key=0;
328  $res=array();
329  if($offset_type==0x0){
330  $key=array_pop(unpack("C",substr($frame,2,1)));
331  $pos++;
332  }
333  while($pos<strlen($frame)){
334  if($offset_type==0x40){
335  $key=array_pop(unpack("C",substr($frame,$pos,1)));
336  $pos++;
337  } else $key++;
338  switch ($data_type){
339  case 0x1:
340  $value=BayEOSType::unpackFLOAT32(substr($frame,$pos,4)); //float32le
341  $pos+=4;
342  break;
343  case 0x2:
344  $value=BayEOSType::unpackINT32(substr($frame,$pos,4));
345  $pos+=4;
346  break;
347  case 0x3:
348  $value=BayEOSType::unpackINT16(substr($frame,$pos,2));
349  $pos+=2;
350  break;
351  case 0x4:
352  $value=BayEOSType::unpackINT8(substr($frame,$pos,1));
353  $pos++;
354  break;
355  case 0x5:
356  $value=array_pop(unpack("d",substr($frame,$pos,8))); //double - Note: double may only work on little endian mashines..
357  break;
358  }
359  $res[$key]=$value;
360  }
361  return $res;
362  }
363 
364 
365 
366 }
367 
379  function __construct($path,$max_chunk=5000,$max_time=60){
380  $this->path=$path;
381  $this->max_chunk=$max_chunk;
382  $this->max_time=$max_time;
383  if(! is_dir($this->path)){
384  if(! mkdir($this->path,0700,TRUE)){
385  die("could not create ".$this->dir);
386  }
387  }
388  chdir($this->path);
389  $files=glob('*');
390  $last=end($files);
391  if(strstr($last.'$','.act$')){
392  //Found active file -- unexpected shutdown...
393  rename($last,str_replace('.act','.rd',$last));
394  }
395  $this->start_new_file();
396  }
397 
398 
399 
400 
415  function save($values,$origin='',$type=0x41,$offset=0,$ts=0){
416  $frame=BayEOS::createDataFrame($values,$type,$offset);
417  if($origin){
418  $origin=substr($origin,0,255);
419  $frame=pack("C",0xb). //Starting Byte
420  pack("C",strlen($origin)). //length of orginin string
421  $origin. //Origin String
422  $frame;
423  }
424  $this->saveFrame($frame,$ts);
425  }
426 
427 
440  function saveDataFrame($values,$type=0x1,$offset=0,$ts=0){
441  $this->saveFrame(BayEOS::createDataFrame($values,$type,$offset),$ts);
442  }
443 
459  function saveOriginDataFrame($origin,$values,$type=0x1,$offset=0,$ts=0){
460  $origin=substr($origin,0,255);
461  $this->saveFrame(
462  pack("C",0xb). //Starting Byte
463  pack("C",strlen($origin)). //length of orginin string
464  $origin. //Origin String
465  BayEOS::createDataFrame($values,$type,$offset),$ts);
466  }
478  function saveOriginFrame($origin,$frame,$ts=0){
479  $origin=substr($origin,0,255);
480  $this->saveFrame(
481  pack("C",0xb). //Starting Byte
482  pack("C",strlen($origin)). //length of orginin string
483  $origin. //Origin String
484  $frame,$ts);
485  }
486 
502  function saveRoutedFrameRSSI($MyId,$PanId,$rssi,$frame,$ts=0){
503  $this->saveFrame(
504  pack("C",0x8). //Starting Byte
505  BayEOSType::INT16($MyId). //MyID
506  BayEOSType::INT16($PanId). //PANID
507  pack("C",$rssi). //RSSI
508  $frame,$ts);
509  }
510 
520  function saveMessage($sting,$ts=0){
521  $this->saveFrame(pack("C",0x4).$sting,$ts);
522  }
523 
533  function saveErrorMessage($sting,$ts=0){
534  $this->saveFrame(pack("C",0x5).$sting,$ts);
535  }
536 
548  public function saveFrame($frame,$ts=0){
549  if(! $ts) $ts=microtime(TRUE);
550  fwrite($this->fp,
551  pack('d',$ts).pack('s',strlen($frame)).$frame);
552 
553  if(ftell($this->fp)>$this->max_chunk || //max size reached or ...
554  (mktime()-$this->current_ts)>$this->max_time){ //max time reached...
555  //Close current file and start new one...
556  fclose($this->fp);
557  rename($this->current_name.'.act',$this->current_name.'.rd');
558  $this->start_new_file();
559  }
560 
561  }
562 
563  private function start_new_file(){
564  $this->current_ts=mktime();
565  $tmp=microtime();
566  list($usec,$sec)=explode(' ',$tmp);
567  $this->current_name=$sec.'-'.$usec;
568  $this->fp=fopen($this->current_name.'.act','w');
569  }
570 
571 
572  private $path;
573  private $max_chunk;
574  private $max_time;
575  private $fp;
576  private $current_name;
577  private $current_ts;
578 
579 }
580 
602  function __construct($path,$name,$url,$pw='import',$user='import',$absolute_time=TRUE,$rm=TRUE,$gateway_version='1.9',$sleep_time=10){
603  if(! filter_var($url, FILTER_VALIDATE_URL))
604  die("URL '$url' not valid\n");
605  if(! $pw)
606  die("No gateway password found\n");
607 
608  $this->path=$path;
609  $this->name=$name;
610  $this->url=$url;
611  $this->pw=$pw;
612  $this->user=$user;
613  $this->absolute_time=$absolute_time;
614  $this->rm=$rm;
615  $this->gateway_version=$gateway_version;
616  $this->sleep_time=$sleep_time;
617  }
618 
625  function send(){
626  $count=0;
627  while($post=$this->sendFile()){
628  $count+=$post;
629  }
630  return $count;
631 
632  }
633 
639  private function sendFile(){
640  chdir($this->path);
641  $files=glob('*.rd');
642  if(count($files)==0) return 0; //nothing to do
643 
644  //open oldest file
645  //echo "opening $files[0]\n";
646  $fp=fopen($files[0],'r');
647  //build up post request
648  $data="sender=".urlencode($this->name)."&password=".urlencode($this->pw);
649  $frames='';
650  //reference time
651  $ref= DateTime::createFromFormat('Y-m-d H:i:s P','2000-01-01 00:00:00 +00:00')->format('U');
652  //size of data-types:
653  $size_of_double=strlen(pack('d',1.0));
654  $size_of_short=strlen(pack('s',1));
655 
656  //echo "$size_of_double - $size_of_short\n";
657  $count=0;
658  while(! feof($fp)){
659  //read timestamp, length and bayeosframe
660  $tmp=fread($fp,$size_of_double);
661  if(strlen($tmp)==0) break;
662  $tmp=unpack('d',$tmp);
663  $ts=$tmp[1];
664  $tmp=unpack('s',fread($fp,$size_of_short));
665  $length=$tmp[1];
666  $bayeos_frame=fread($fp,$length);
667  if($bayeos_frame){
668  $count++;
669  if($this->absolute_time){
670  if($this->gateway_version=='1.8') $bayeos_frame=pack("C",0x9).BayEOSType::UINT32(round($ts-$ref)).$bayeos_frame;
671  else $bayeos_frame=pack("C",0xc).BayEOSType::UINT64(round($ts*1000)).$bayeos_frame;
672  }else
673  $bayeos_frame=pack("C",0x7).BayEOSType::UINT32(round((microtime(TRUE)-$ts)*1000)).$bayeos_frame;
674  $frames.="&bayeosframes[]=".($this->gateway_version=='1.8'?
675  base64_encode($bayeos_frame):urlencode(base64_encode($bayeos_frame)));
676  }
677  }
678  fclose($fp);
679  if($frames){
680  //Frames to post...
681  if($res=$this->post($data.$frames)){
682  //Post successful
683  if($res==1){
684  if($this->rm) unlink($files[0]);
685  else rename($files[0],str_replace('.rd','.bak',$files[0]));
686  } elseif($res==2){
687  fwrite(STDERR, date('Y-m-d H:i:s').' '.$this->name." Will keep failed file as ".
688  str_replace('.rd','.bak',$files[0])."\n");
689  rename($files[0],str_replace('.rd','.bak',$files[0]));
690  }
691  return $count;
692  }
693  } else {
694  //Empty file...
695  if(filesize($files[0])>0)
696  rename($files[0],str_replace('.rd','.bak',$files[0]));
697  else
698  unlink($files[0]);
699  return 0;
700  }
701  return 0;
702 
703  }
710  private function post($data){
711  //echo "POST:".$this->url.'-'.$this->pw.'-'.$this->user."\n$data\n";
712  $ch=curl_init($this->url);
713  curl_setopt($ch,CURLOPT_POST,1);
714  curl_setopt($ch,CURLOPT_TIMEOUT,120);
715  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
716  curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
717  curl_setopt($ch,CURLOPT_HEADER,1);
718  curl_setopt($ch,CURLOPT_USERAGENT,'BayEOS-PHP/1.0.10');
719  curl_setopt($ch, CURLOPT_USERPWD, $this->user . ":" . $this->pw);
720  //curl_setopt($ch,CURLOPT_NOBODY,1);
721  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
722  $res=curl_exec($ch);
723  //echo "CURL-res: $res\n";
724  curl_close($ch);
725  if($res===FALSE){
726  fwrite(STDERR, date('Y-m-d H:i:s').' '.$this->name." curl_exec failed\n");
727  return 0;
728  }
729  $res=explode("\n",$res);
730  for($i=0;$i<count($res);$i++){
731  if(preg_match('|^HTTP/1\\.[0-9] 200 OK|i',$res[$i])) return 1;
732  elseif(preg_match('|^HTTP/1\\.[0-9] 500|i',$res[$i],$matches)){
733  fwrite(STDERR, date('Y-m-d H:i:s').' '.$this->name." Post Error: $res[$i]\n");
734  return 2;
735  }
736  elseif(preg_match('|^HTTP/1\\.[0-9] [45]|i',$res[$i],$matches)){
737  fwrite(STDERR, date('Y-m-d H:i:s').' '.$this->name." Post Error: $res[$i]\n");
738  return 0;
739  }
740  }
741  return 0;
742 
743  }
744 
750  public function run(){
751  while(TRUE){
752  $c = $this->send();
753  if($c){
754  echo date('Y-m-d H:i:s')." ".$this->name.": Successfully sent $c frames\n";
755  }
756  sleep($this->sleep_time);
757  }
758 
759  }
760 
761  private $path;
762  private $url;
763  private $user;
764  private $pw;
765  private $name;
766  private $absolute_time;
767  private $rm;
768  private $gateway_version;
769  private $sleep_time;
770 
771 }
772 
773 
795  function __construct($path,$name,$url,$pw='import',$user='import',$absolute_time=TRUE,$rm=TRUE,$gateway_version='1.9',$sleep_time=10){
796  $this->sender = new BayEOSSender($path,$name,$url,$pw,$user,$absolute_time,$rm,$gateway_version,$sleep_time);
798  $this->startSender();
799 
800  }
801 
802  function startSender(){
803  if($this->sender_pid){
804  fwrite(STDERR,date('Y-m-d H:i:s')." Sender is running with pid ".$this->sender_pid."\n");
805  return;
806  }
807  $pid = pcntl_fork();
808  if ($pid == -1) {
809  die('Could not fork');
810  } else if ($pid) {
811  // we are parent!
812  $this->sender_pid=$pid;
813  echo date('Y-m-d H:i:s')." Sender started with pid ".$this->sender_pid."\n";
814  return;
815  } else {
816  //we are Child and
817  $this->sender->run();
818  exit();
819  }
820  }
821 
822  function stopSender(){
823  if(! $this->sender_pid){
824  fwrite(STDERR,date('Y-m-d H:i:s')." Sender is not running\n");
825  return;
826  }
827  posix_kill($this->sender_pid,SIGTERM);
828  $res=pcntl_waitpid($this->sender_pid,$status);
829  echo date('Y-m-d H:i:s')." Stopping sender with pid ".$this->sender_pid.": ".
830  ($res>0?'ok':'failed')."\n";
831  $this->sender_pid=0;
832  return;
833 
834  }
835 
836  function stop(){
837  $this->stopSender();
838  echo date('Y-m-d H:i:s')." Stopping main process\n";
839  exit;
840  }
841  private $sender;
842  private $sender_pid;
843 }
844 
862 function __construct($names,$options=array(),$defaults=array()){
863  if(! is_array($names) && $names)
864  $names=array($names);
865  if(count(array_unique($names))<count($names))
866  die("Duplicate names detected!");
867  if(count($names)==0)
868  die("No name given");
869 
870  $prefix='';
871  if(! isset($options['sender'])){
872  $prefix=gethostname().'/';
873  }
874  if(isset($options['sender']) && ! is_array($options['sender']) && count($names)>1){
875  $prefix=$options['sender'].'/';
876  unset($options['sender']);
877  }
878  for($i=0;$i<count($names);$i++){
879  $sender_defaults[$i]=$prefix.$names[$i];
880  }
881 
882  $defaults=array_merge(array('writer_sleep_time'=>15,
883  'max_chunk'=>5000,
884  'max_time'=>60,
885  'data_type'=>0x1,
886  'sender_sleep_time'=>5,
887  'sender'=>$sender_defaults,
888  'bayeosgateway_user'=>'import',
889  'bayeosgateway_version'=>'1.9',
890  'absolute_time'=>TRUE,
891  'rm'=>TRUE,
892  'sleep_between_childs'=>0,
893  'tmp_dir'=>sys_get_temp_dir()),$defaults);
894  while(list($key,$value)=each($defaults)){
895  if(! isset($options[$key])){
896  echo "Option '$key' not set using default: ".(is_array($value)?implode(', ',$value):$value)."\n";
897  $options[$key]=$value;
898  }
899  }
900  $this->names=$names;
901  $this->options=$options;
902  }
903 
913  function getOption($key,$default=''){
914  if(isset($this->options[$key])){
915  if(is_array($this->options[$key])){
916  if(isset($this->options[$key][$this->i]))
917  return $this->options[$key][$this->i];
918  if(isset($this->options[$key][$this->name]))
919  return $this->options[$key][$this->name];
920  } else
921  return $this->options[$key];
922  }
923  return $default;
924 
925  }
931  function run(){
932  for($i=0;$i<count($this->names);$i++){
933  $this->i=$i;
934  $this->name=$this->names[$i];
935  $path=$this->getOption('tmp_dir').'/'.str_replace(array('/','\\','"','\''),'_',$this->name);
936  $this->pid_w[$i] = pcntl_fork();
937  if ($this->pid_w[$i] == -1) {
938  die('Could not fork writer process!');
939  } else if ($this->pid_w[$i]) {
940  // We are the parent
941  echo date('Y-m-d H:i:s')." Started writer for ".$this->name." with pid ".$this->pid_w[$i]."\n";
942  } else {
943  // We are child:
944  //Start writer and run it...
945  $this->initWriter();
946  $this->writer = new BayEOSWriter($path,
947  $this->getOption('max_chunk'),
948  $this->getOption('max_time'));
949  while(TRUE){
950  //;
951  $data=$this->readData();
952  if($data!=FALSE) $this->saveData($data);
953  else fwrite(STDERR,date('Y-m-d H:i:s')." ".$this->name.": readData failed\n");
954  sleep($this->getOption('writer_sleep_time'));
955  }
956  exit();
957 
958 
959  }
960  $this->pid_r[$i] = pcntl_fork();
961  if ($this->pid_r[$i] == -1) {
962  die('Could not fork sender process');
963  } else if ($this->pid_r[$i]) {
964  // We are the parent
965  echo date('Y-m-d H:i:s')." Started sender for ".$this->name." with pid ".$this->pid_r[$i]."\n";
966  sleep($this->getOption('sleep_between_childs'));
967  } else {
968  // We are child:
969  //Start sender and run it...
970  $s = new BayEOSSender($path,
971  $this->getOption('sender'),
972  $this->getOption('bayeosgateway_url'),
973  $this->getOption('bayeosgateway_pw'),
974  $this->getOption('bayeosgateway_user'),
975  $this->getOption('absolute_time'),
976  $this->getOption('rm'),
977  $this->getOption('bayeosgateway_version'),
978  $this->getOption('sender_sleep_time'));
979  $s->run();
980  exit();
981 
982  }
983 
984 
985 
986  }
987  //This is only for the parent process...
988  declare(ticks = 1);
989 
990  pcntl_signal(SIGTERM, function($signo) {
991  switch ($signo) {
992  case SIGTERM:
993  // Aufgaben zum Beenden bearbeiten
994  for($i=0;$i<count($this->pid_w);$i++){
995  posix_kill($this->pid_w[$i],SIGTERM);
996  $res=pcntl_waitpid($this->pid_w[$i],$status);
997  echo date('Y-m-d H:i:s')." Stopping writer for ".$this->names[$i]." with pid ".$this->pid_w[$i].': '.
998  ($res>0?'ok':'failed')."\n";
999 
1000  }
1001  for($i=0;$i<count($this->pid_r);$i++){
1002  posix_kill($this->pid_r[$i],SIGTERM);
1003  $res=pcntl_waitpid($this->pid_r[$i],$status);
1004  echo date('Y-m-d H:i:s')." Stopping sender for ".$this->names[$i]." with pid ".$this->pid_r[$i].": ".
1005  ($res>0?'ok':'failed')."\n";
1006  }
1007  echo date('Y-m-d H:i:s')." Stopping main process\n";
1008 
1009  exit;
1010  break;
1011  }
1012 
1013  });
1014 
1015  while(TRUE){
1016  //Sleep until we get SIGTERM
1017  sleep(1);
1018  }
1019  }
1020 
1026  protected function initWriter(){
1027  }
1028 
1034  protected function readData(){
1035  die("no readData() found!\n");
1036  return FALSE;
1037  }
1038 
1044  protected function saveData($data){
1045  $this->writer->saveDataFrame($data,$this->getOption('data_type'));
1046  }
1047 
1048  protected $names;
1049  protected $options;
1050  protected $writer;
1051  protected $i; //The current number
1052 }
1053 
1054 ?>
static unpackUINT16($value, $endianness=0)
static parseDataFrame($frame)
static UINT16($value, $endianness=0)
static BYTE($value)
static createDataFrame($values, $type=0x1, $offset=0)
static unpackINT16($value, $endianness=0)
static FLOAT32($value, $endianness=0)
saveErrorMessage($sting, $ts=0)
static INT32($value, $endianness=0)
$frame
Definition: testPHP.php:14
static unpackINT8($value)
__construct($names, $options=array(), $defaults=array())
saveRoutedFrameRSSI($MyId, $PanId, $rssi, $frame, $ts=0)
static UINT64($value, $endianness=0)
$fp
Definition: testFifo.php:5
saveFrame($frame, $ts=0)
saveOriginDataFrame($origin, $values, $type=0x1, $offset=0, $ts=0)
$count
Definition: BayEOSWriter.php:8
getOption($key, $default='')
saveMessage($sting, $ts=0)
$pos
Definition: dumpCat.php:12
static unpackUINT64($value, $endianness=0)
__construct($path, $name, $url, $pw='import', $user='import', $absolute_time=TRUE, $rm=TRUE, $gateway_version='1.9', $sleep_time=10)
__construct($path, $name, $url, $pw='import', $user='import', $absolute_time=TRUE, $rm=TRUE, $gateway_version='1.9', $sleep_time=10)
save($values, $origin='', $type=0x41, $offset=0, $ts=0)
static parseFrame($frame, $ts=FALSE, $origin='', $rssi=FALSE)
static UINT32($value, $endianness=0)
$w
Definition: BayEOSWriter.php:7
static unpackUINT32($value, $endianness=0)
__construct($path, $max_chunk=5000, $max_time=60)
$s
Definition: BayEOSSender.php:7
saveOriginFrame($origin, $frame, $ts=0)
static INT16($value, $endianness=0)
static unpackFLOAT32($value, $endianness=0)
saveDataFrame($values, $type=0x1, $offset=0, $ts=0)
static unpackINT32($value, $endianness=0)
static unpackUINT8($value)