Next4Me
 All Classes Files Functions Variables
Trigger.php
Go to the documentation of this file.
1 <?php
16 class Application_Model_Trigger extends Zend_Db_Table
17 {
18  //==== Start checkPointInCircle =========================================================================
30  function checkPointInCircle($LatReference, $LatCurrent, $LongReference, $LongCurrent, $Range){
31  $lat1=(float)$LatReference;
32  $lat2=(float)$LatCurrent;
33  $lon1=(float)$LongReference;
34  $lon2=(float)$LongCurrent;
35  $range=(int)$Range;
36 
37  //echo "date primite:".$lat1."-".$lat2."-".$lon1."-".$lon2."-".$range;
38  $R = 6371000; // km (change this constant to get miles)
39  $dLat = ($lat2-$lat1) * M_PI / 180;
40  $dLon = ($lon2-$lon1) * M_PI / 180;
41  $a = sin($dLat/2) * sin($dLat/2) + cos($lat1 * M_PI / 180 ) * cos($lat2 * M_PI / 180 ) * sin($dLon/2) * sin($dLon/2);
42  $c = 2 * atan2(sqrt($a), sqrt(1-$a));
43  $d = $R * $c;
44  $d = round($d,2);
45 
46  if ($d<=$range){
47  $response = true;
48  }else{
49  $response = false;
50  }
51 
52  return $response;
53  }
54  //==== End checkPointInCircle =========================================================================
55 
56  //==== Start timeInterval =========================================================================
69  function timeInInterval($h,$m,$h1,$m1,$h2,$m2){
70  //filter the parameters
71  $h = intval($h);
72  $m = intval($m);
73  $h1 = intval($h1);
74  $m1 = intval($m1);
75  $h2 = intval($h2);
76  $m2 = intval($m2);
77  //cases like 00:00 - 14:00
78  if ($h1 < $h2){
79  if (($h < $h1) || ($h > $h2)){
80  return false;
81  } else if (($h == $h1) || ($h == $h2)) {
82  if ($h == $h1){
83  if ($m < $m1){
84  return false;
85  }
86  } else {
87  if ($m > $m2){
88  return false;
89  }
90  }
91  }
92  }
93  //cases like 12:00 - 12:30 and 12:30-12:00
94  if ($h1 == $h2){
95  if ($m1 > $m2){
96  //split it into 2 intervals
97  //12:30-12:00 => 12:30-23:59 and 00:00-12:00
98  $tmp1 = timeInInterval($h,$m,$h1,$m1,23,59);
99  $tmp2 = timeInInterval($h,$m,0,0,$h2,$m2);
100  if (($tmp1 == 0) && ($tmp2 == 0)){
101  return false;
102  }
103  } else {
104  if ($h != $h2){
105  return false;
106  } else if (($m > $m2) || ($m < $m1)) {
107  return false;
108  }
109  }
110  }
111  //cases like 08:00 - 01:00
112  if ($h1 > $h2){
113  //split it into 2 intervals : 08:00 - 23:59 and 00:00-01:00
114  $tmp1 = timeInInterval($h,$m,$h1,$m1,23,59);
115  $tmp2 = timeInInterval($h,$m,0,0,$h2,$m2);
116  if (($tmp1 == 0) && ($tmp2 == 0)){
117  return false;
118  }
119  }
120  return true;
121  }
122  //==== End timeInterval ====================================================================
123 
124 
125  //==== Start speed =========================================================================
133  function speed($entity){
134  $resultId = 0;
135  $page="Trigger.php";
136  $method="speed";
137  $result=json_encode($entity);
138  $message="";
139  $type="1";
140  $this->ErrorCode = new Application_Model_ErrorCode;
141  $this->Log = new Application_Model_Log;
142  try{
143  $data=array();
144  if(!isset($entity['Speed'])){
145  $resultId = 3205;
146  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
147  $resultCode=sprintf($r['Message'], "Speed");
148  $message=sprintf($r['Message'], "Speed");
149  $type=3;
150  }else{$speed=(int)$entity['Speed'];}
151 
152  if(!isset($entity['Function'])){
153  $resultId = 3205;
154  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
155  $resultCode=sprintf($r['Message'], "Function");
156  $message=sprintf($r['Message'], "Function");
157  $type=3;
158  }else{$function=$entity['Function'];}
159 
160  if(!isset($entity['Value'])){
161  $resultId = 3205;
162  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
163  $resultCode=sprintf($r['Message'], "Value");
164  $message=sprintf($r['Message'], "Value");
165  $type=3;
166  }else{$value=(int)$entity['Value'];}
167 
168  $speedName="";
169  $cfg=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
170  $minStandStill=$cfg['global']['trigger']['speed']['StandStill']['Min'];
171  $maxStandStill=$cfg['global']['trigger']['speed']['StandStill']['Max'];
172  $minLowSpeed=$cfg['global']['trigger']['speed']['LowSpeed']['Min'];
173  $maxLowSpeed=$cfg['global']['trigger']['speed']['LowSpeed']['Max'];
174  $minHighSpeed=$cfg['global']['trigger']['speed']['HighSpeed']['Min'];
175  $maxHighSpeed=$cfg['global']['trigger']['speed']['HighSpeed']['Max'];
176  $response=-3;
177 
178  switch($speed){
179  case ($speed>= $minStandStill && $speed<=$maxStandStill):
180  $speedName="StandStill";
181  break;
182  case ($speed>= $minLowSpeed && $speed<=$maxLowSpeed):
183  $speedName="LowSpeed";
184  break;
185  case ($speed>= $minHighSpeed && $speed<=$maxHighSpeed):
186  $speedName="HighSpeed";
187  break;
188  }
189  switch ($function){
190  case 'Equal':
191  if($speed==$value){
192  $response=0;
193  }else{
194  $response=-3;
195  }
196  break;
197  }
198 
199  }
200  catch(Exception $e){
201  $resultCode=$e->getMessage();
202  $resultId=101;
203  $type=3;
204  }
205  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
206  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
207  return $response;
208  }
209  //==== End speed ==============================================================================
210 
211  //==== Start geofence =========================================================================
219  function geofence($entity){
220  $resultId = 0;
221  $page="Trigger.php";
222  $method="geofence";
223  $result=json_encode($entity);
224  $message="";
225  $type="1";
226  $this->ErrorCode = new Application_Model_ErrorCode;
227  $this->Log = new Application_Model_Log;
228  $this->ControlSessionHistory = new Application_Model_ControlSessionHistory;
229  try{
230  $data=array();
231  if(!isset($entity['Function'])){
232  $resultId = 3207;
233  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
234  $resultCode=sprintf($r['Message'], "Function");
235  $message=sprintf($r['Message'], "Function");
236  $type=3;
237  }else{$function=$entity['Function'];}
238 
239  if(!isset($entity['Value'])){
240  $resultId=3207;
241  $r=$this->ErrorCode->getErrorCode($resultId, Zend_Registry::get('language'));
242  $resultCode=sprintf($r['Message'], "Value");
243  $message=sprintf($r['Message'], "Value");
244  $type=3;
245  }else{$value=$entity['Value'];}
246 
247  $val=explode(",", $value);
248 
249  if(!isset($val[0])){
250  $resultId = 3206;
251  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
252  $resultCode=sprintf($r['Message'], "LatReference");
253  $message=sprintf($r['Message'], "LatReference");
254  $type=3;
255  }else{$latReference=$val[0];}
256 
257  if(!isset($val[1])){
258  $resultId = 3206;
259  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
260  $resultCode=sprintf($r['Message'], "LongReference");
261  $message=sprintf($r['Message'], "LongReference");
262  $type=3;
263  }else{$longReference=$val[1];}
264 
265  if(!isset($val[2])){
266  $resultId = 3206;
267  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
268  $resultCode=sprintf($r['Message'], "Range");
269  $message=sprintf($r['Message'], "Range");
270  $type=3;
271  }else{$range=$val[2];}
272 
273  if(!isset($entity['LatCurrent'])){
274  $resultId = 3206;
275  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
276  $resultCode=sprintf($r['Message'], "LatCurenta");
277  $message=sprintf($r['Message'], "LatCurenta");
278  $type=3;
279  }else{$latCurrent = $entity['LatCurrent'];}
280 
281  if(!isset($entity['LongCurrent'])){
282  $resultId = 3206;
283  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
284  $resultCode=sprintf($r['Message'], "LongCurrent");
285  $message=sprintf($r['Message'], "LongCurrent");
286  $type=3;
287  }else{$longCurrent = $entity['LongCurrent'];}
288 
289  if($resultId==0) {
290  switch($function)
291  {
292  case 'IN':
293  $res = $this->checkPointInCircle($latReference, $latCurrent, $longReference, $longCurrent, $range);
294  if($res == true){ $resultCode=0;}else{$resultCode=-3;}
295  break;
296 
297  case 'OUT':
298  $res = $this->checkPointInCircle($data['Value']['LatReference'], $data['Value']['LatCurrent'], $data['Value']['LongReference'], $data['Value']['LongCurrent'], $data['Value']['Range']);
299  if($res == true){$resultCode=-3;}else{$resultCode=0;}
300  break;
301 
302  case 'CROSS_IN':
303  $resCSH=$this->ControlSessionHistory->getControlSessionHistory($entity);
304  //echo "<pre>history"; print_r($resCSH); echo "</pre>";
305  if(!isset($resCSH['resultCode'][0]['Long'])){
306  $resultId = 3206;
307  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
308  $resultCode=sprintf($r['Message'], "LongLast");
309  $message=sprintf($r['Message'], "LongLast");
310  $type=3;
311  }else{$longLast = $resCSH['resultCode'][0]['Long'];}
312 
313  if(!isset($resCSH['resultCode'][0]['Lat'])){
314  $resultId = 3206;
315  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
316  $resultCode=sprintf($r['Message'], "LatLast");
317  $message=sprintf($r['Message'], "LatLast");
318  $type=3;
319  }else{$latLast = $resCSH['resultCode'][0]['Lat'];}
320 
321  if($resultId==0) {
322  $res1 = $this->checkPointInCircle($latLast, $latCurrent, $longLast, $longCurrent, $range);
323  if($res1 == false )
324  {
325  $res = $this->checkPointInCircle($latReference, $latCurrent, $longReference, $longCurrent, $range);
326  if( $res == true){$resultCode = 0;}else{$resultCode = -3;}
327  }else{
328  $resultCode = -3;
329  }
330  }
331  break;
332 
333  case 'CROSS_OUT':
334  $resCSH=$this->ControlSessionHistory->getControlSessionHistory($entity);
335  //echo "<pre>history22"; print_r($resCSH); echo "</pre>";
336  if(!isset($resCSH['resultCode'][0]['Long'])){
337  $resultId = 3206;
338  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
339  $resultCode=sprintf($r['Message'], "LongLast");
340  $message=sprintf($r['Message'], "LongLast");
341  $type=3;
342  }else{$longLast = $resCSH['resultCode'][0]['Long'];}
343 
344  if(!isset($resCSH['resultCode'][0]['Lat'])){
345  $resultId = 3206;
346  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
347  $resultCode=sprintf($r['Message'], "LatLast");
348  $message=sprintf($r['Message'], "LatLast");
349  $type=3;
350  }else{$latLast = $resCSH['resultCode'][0]['Lat'];}
351 
352 
353  $res1 = $this->checkPointInCircle($latLast, $latCurrent, $longLast, $longCurrent, $range);
354  if( $res1 == true ){
355  $res = $this->checkPointInCircle($latReference, $latCurrent, $longReference, $longCurrent, $range);
356  if( $res == false ){$resultCode = 0;}else{$resultCode = -3;}
357  }else{
358  $resultCode = -3;
359  }
360 
361  break;
362  }
363  }
364  }
365  catch(Exception $e){
366  $resultCode=$e->getMessage();
367  $resultId=101;
368  $type=3;
369  }
370  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
371  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
372  return $response;
373  }
374  //==== End geofence ============================================================================
375 
376  //==== Start timeStamp =========================================================================
384  function timeStamp($entity){
385  $resultId = 0;
386  $page="Trigger.php";
387  $method="timeStamp";
388  $result=json_encode($entity);
389  $message="";
390  $type="1";
391  $this->ErrorCode = new Application_Model_ErrorCode;
392  $this->Log = new Application_Model_Log;
393  try{
394  $cfg=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
395  $startActiuneActivaViitoare=$cfg['global']['trigger']['startActiuneActivaViitoare'];
396  $endActiuneActivaViitoare=$cfg['global']['trigger']['endActiuneActivaViitoare'];
397  $startActiuneActivaApropiata=$cfg['global']['trigger']['startActiuneActivaApropiata'];
398  $endActiuneActivaApropiata=$cfg['global']['trigger']['endActiuneActivaApropiata'];
399 
400  $data=array();
401  if(!isset($entity['Function'])){
402  $resultId = 3207;
403  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
404  $resultCode=sprintf($r['Message'], "Function");
405  $message=sprintf($r['Message'], "Function");
406  $type=3;
407  }else{$data['Function']=$entity['Function'];}
408 
409  if(!isset($entity['Value'])){
410  $resultId=3207;
411  $r=$this->ErrorCode->getErrorCode($resultId, Zend_Registry::get('language'));
412  $resultCode=sprintf($r['Message'], "Value");
413  $message=sprintf($r['Message'], "Value");
414  $type=3;
415  }else{$timeStamp=$entity['Value'];}
416 
417  switch($data['Function'])
418  {
419  case 'Before':
420  $dataNow= Zend_Date::now();
421  $dataValue= new Zend_Date();
422  $dataValue->set($timeStamp, Zend_Date::TIMESTAMP);
423  $difference=$dataNow->sub($dataValue);
424  $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
425  // $measure->convertTo(Zend_Measure_Time::DAY);
426  $rez=$measure->getValue();
427 
428  switch ($rez)
429  {
430  //Actiune Viitoare are mai mult de 30 zile pina devine activa
431  case ($startActiuneActivaViitoare > $rez):
432  $resultCode=-3;
433  break;
434  //Actiune Activa Viitoare are intre 30 si 7 zile pina devine activa Ex: interva [30-7)
435  case ($startActiuneActivaViitoare <= $rez && $endActiuneActivaViitoare > $rez): //
436  $resultCode=-2;
437  break;
438  //Actiune Activa Apropiata are intre 7 si 0 zile pina devine activa Ex: interval [7-0)
439  case ($startActiuneActivaApropiata <= $rez && $endActiuneActivaApropiata > $rez):
440  $resultCode=-1;
441  break;
442  //Actiune Activa acum Ex [-5 min
443  case ($endActiuneActivaApropiata <= $rez && 0 >= $rez):
444  $resultCode=0;
445  break;
446  //Actiune Trecuta
447  case ($rez>=0):
448  $resultCode=1;
449  break;
450  }
451  break;
452 
453  case 'Equal':
454  $dataNow= Zend_Date::now();
455  $dataValue= new Zend_Date();
456  $dataValue->set($timeStamp, Zend_Date::TIMESTAMP);
457  $difference=$dataNow->sub($dataValue);
458  $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
459  $rez=$measure->getValue();
460  //echo "Masure".$measure."->rez:".$rez."<br><br>";
461  //$measure->convertTo(Zend_Measure_Time::DAY);
462  //$rez=$measure->getValue();
463 
464  switch ($rez)
465  {
466  case ($startActiuneActivaViitoare > $rez):
467  $resultCode=-3;
468  break;
469  case ($startActiuneActivaViitoare <= $rez && $endActiuneActivaViitoare > $rez):
470  $resultCode=2;
471  break;
472  case ($startActiuneActivaApropiata <= $rez && $endActiuneActivaApropiata > $rez):
473  $resultCode=-1;
474  break;
475  case ($endActiuneActivaApropiata <= $rez && 0 >= $rez):
476  $resultCode=0;
477  break;
478  case ($rez>=0):
479  $resultCode=1;
480  break;
481  }
482  break;
483 
484  case 'After':
485  $dataNow= Zend_Date::now();
486  $dataValue= new Zend_Date();
487  $dataValue->set($timeStamp, Zend_Date::TIMESTAMP);
488  $difference=$dataNow->sub($dataValue);
489  $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
490  // $measure->convertTo(Zend_Measure_Time::DAY);
491  $rez=$measure->getValue();
492 
493  switch ($rez)
494  {
495  //Actiune Viitoare are mai mult de 30 zile pina devine activa
496  case ($startActiuneActivaViitoare > $rez):
497  $resultCode=-3;
498  break;
499  //Actiune Activa Viitoare are intre 30 si 7 zile pina devine activa Ex: interva [30-7)
500  case ($startActiuneActivaViitoare <= $rez && $endActiuneActivaViitoare > $rez): //
501  $resultCode=-2;
502  break;
503  //Actiune Activa Apropiata are intre 7 si 0 zile pina devine activa Ex: interval [7-0)
504  case ($startActiuneActivaApropiata <= $rez && $endActiuneActivaApropiata > $rez):
505  $resultCode=-1;
506  break;
507  //Actiune Activa acum Ex [-5 min
508  case ($endActiuneActivaApropiata <= $rez && 0 >= $rez):
509  $resultCode=0;
510  break;
511  //Actiune Trecuta
512  case ($rez>=0):
513  $resultCode=1;
514  break;
515  }
516  break;
517 
518  case 'Between':
519  $val=explode(",", $timeStamp);
520 
521  if(!isset($val[0])){
522  $resultId = 3209;
523  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
524  $resultCode=sprintf($r['Message'], "StartTimeStamp");
525  $message=sprintf($r['Message'], "StartTimeStamp");
526  $type=3;
527  }else{$startTimeStamp = $val[0];}
528  if(!isset($val[1])){
529  $resultId = 3209;
530  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
531  $resultCode=sprintf($r['Message'], "EndTimeStamp");
532  $message=sprintf($r['Message'], "EndTimeStamp");
533  $type=3;
534  }else{$endTimeStamp = $val[1];}
535 
536  if( $resultId == 0)
537  {
538  $dataNow= Zend_Date::now();
539  $startDataValue= new Zend_Date();
540  $startDataValue->set($startTimeStamp, Zend_Date::TIMESTAMP);
541  $endDataValue = new Zend_Date();
542  $endDataValue ->set($endTimeStamp, Zend_Date::TIMESTAMP);
543 
544  $diffStart=$dataNow->sub($startDataValue);
545  $measure = new Zend_Measure_Time($diffStart->toValue(), Zend_Measure_Time::SECOND);
546  //$measure->convertTo(Zend_Measure_Time::DAY);
547  $start=$measure->getValue();
548 
549  $diffEnd=$dataNow->sub($endDataValue);
550  $measure = new Zend_Measure_Time($diffEnd->toValue(), Zend_Measure_Time::SECOND);
551  $end=$measure->getValue();
552 
553  //Actiune Viitoare are mai mult de 30 zile pina devine activa
554  if ($startActiuneActivaViitoare > $start){
555  $resultCode=-3;
556  }
557  //Actiune Activa Viitoare are intre 30 si 7 zile pina devine activa Ex: interva [30-7)
558  if ($startActiuneActivaViitoare <= $start && $endActiuneActivaViitoare > $start){
559  $resultCode=-2;
560  }
561  //Actiune Activa Apropiata are intre 7 si 0 zile pina devine activa Ex: interval [7-0)
562  if ($startActiuneActivaApropiata <= $start && $endActiuneActivaApropiata > $start){
563  $resultCode=-1;
564  }
565  //Actiune Activa acum Ex [-5 min
566  if ($endActiuneActivaApropiata <= $start && 0 >= $start && 0 <= $end){
567  $resultCode=0;
568  }
569  //Actiune Trecuta
570  if ($end >= 0){
571  $resultCode=1;
572  }
573 
574  }
575  break;
576 
577  case 'NotBetween':
578  $val=explode(",", $timeStamp);
579 
580  if(!isset($val[0])){
581  $resultId = 3209;
582  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
583  $resultCode=sprintf($r['Message'], "StartTimeStamp");
584  $message=sprintf($r['Message'], "StartTimeStamp");
585  $type=3;
586  }else{$startTimeStamp = $val[0];}
587  if(!isset($val[1])){
588  $resultId = 3209;
589  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
590  $resultCode=sprintf($r['Message'], "EndTimeStamp");
591  $message=sprintf($r['Message'], "EndTimeStamp");
592  $type=3;
593  }else{$endTimeStamp = $val[1];}
594 
595 
596 
597  if( $resultId == 0)
598  {
599  if ( ( intval(time()) > intval($startTimeStamp)) && ( intval(time()) < intval($endTimeStamp)) )
600  {
601  $resultCode = 1;
602  }else{
603  $resultCode = 0;
604  }
605  }
606  break;
607 
608  }
609 
610  }
611  catch(Exception $e){
612 
613  $resultCode=$e->getMessage();
614  $resultId=101;
615  $type=3;
616  }
617  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
618  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
619  return $response;
620  }
621  //==== End timeStamp ===============================================================================
622 
623  //==== Start timeRepeating =========================================================================
631  function timeRepeating($entity){
632  $resultId = 0;
633  $page="Trigger.php";
634  $method="timeRepeating";
635  $result=json_encode($entity);
636  $message="";
637  $type="1";
638  $this->ErrorCode = new Application_Model_ErrorCode;
639  $this->Log = new Application_Model_Log;
640 
641  try{
642  $data=array();
643  if(!isset($entity['Function'])){
644  $resultId = 3207;
645  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
646  $resultCode=sprintf($r['Message'], "Function");
647  $message=sprintf($r['Message'], "Function");
648  $type=3;
649  }else{$data['Function']=$entity['Function'];}
650 
651  if(!isset($entity['Value'])){
652  $resultId=3207;
653  $r=$this->ErrorCode->getErrorCode($resultId, Zend_Registry::get('language'));
654  $resultCode=sprintf($r['Message'], "Value");
655  $message=sprintf($r['Message'], "Value");
656  $type=3;
657  }else{$value=$entity['Value'];}
658 
659  switch($data['Function'])
660  {
661  case 'HourOfDay':
662  $val=explode(",", $value);
663  if(!isset($val[0])){
664  $resultId = 3209;
665  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
666  $resultCode=sprintf($r['Message'], "HH");
667  $message=sprintf($r['Message'], "HH");
668  $type=3;
669  }else{$hh = $val[0];}
670  if(!isset($val[1])){
671  $resultId = 3209;
672  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
673  $resultCode=sprintf($r['Message'], "MM");
674  $message=sprintf($r['Message'], "MM");
675  $type=3;
676  }else{$mm = $val[1];}
677 
678  //echo "HS->".intval(date('H'))." ;HH->".intval($hh)."<->MS".intval(date('i'))." ;MM->".intval($mm);
679 
680  if($resultId==0){
681  if( (intval(date('H')) == intval($hh)) && (intval(date('i')) == intval($mm)) ){
682  $resultCode=0;
683  }else{
684  $resultCode=-3;
685  }
686  }
687  break;
688 
689  case 'DayOfWeek':
690  $val=explode(",", $value);
691  if(in_array(date('w') , $val)){
692  $resultCode = 0;
693  }else{
694  $resultCode= -3;
695  }
696  break;
697 
698  case 'DayOfMonth':
699  $val=explode(",", $value);
700  if( in_array(date('d') , $val)){
701  $resultCode=0;
702  }else{
703  $resultCode=-3;
704  }
705  break;
706 
707  case 'DayOfYear':
708  $val=explode(",", $value);
709  if( in_array(date('z') , $val) ){
710  $resultCode = 0;
711  }else{
712  $resultCode = -3;
713  }
714  break;
715 
716  case 'CalendarDay':
717  if(!isset($data['Value']['DD'])){
718  $resultId = 3209;
719  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
720  $resultCode=sprintf($r['Message'], "DD");
721  $message=sprintf($r['Message'], "DD");
722  $type=3;
723  }
724  if(!isset($data['Value']['MM'])){
725  $resultId = 3209;
726  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
727  $resultCode=sprintf($r['Message'], "MM");
728  $message=sprintf($r['Message'], "MM");
729  $type=3;
730  }
731 
732  if($resultId==0){
733  if( (intval(date('m'))==intval($data['Value']['MM'])) && (intval(date('d'))==intval($data['Value']['DD'])) ){
734  $resultCode=0;
735  }else{
736  $resultCode=-3;
737  }
738  }
739  break;
740 
741  case 'HourlyInterval':
742  if(!isset($data['Value']['HH1'])){
743  $resultId = 3209;
744  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
745  $resultCode=sprintf($r['Message'], "HH1");
746  $message=sprintf($r['Message'], "HH1");
747  $type=3;
748  }
749  if(!isset($data['Value']['MM1'])){
750  $resultId = 3209;
751  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
752  $resultCode=sprintf($r['Message'], "MM1");
753  $message=sprintf($r['Message'], "MM1");
754  $type=3;
755  }
756  if(!isset($data['Value']['HH2'])){
757  $resultId = 3209;
758  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
759  $resultCode=sprintf($r['Message'], "HH2");
760  $message=sprintf($r['Message'], "HH2");
761  $type=3;
762  }
763  if(!isset($data['Value']['MM2'])){
764  $resultId = 3209;
765  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
766  $resultCode=sprintf($r['Message'], "MM2");
767  $message=sprintf($r['Message'], "MM2");
768  $type=3;
769  }
770 
771  if($resultId==0){
772  $response=$this->timeInInterval(date('H'), date('i'), $data['Value']['HH1'], $data['Value']['MM1'], $data['Value']['HH2'], $data['Value']['MM1']);
773  if($response==true){ $resultCode = 0; }else{ $resultCode = -3; }
774  }
775  break;
776  }
777  }
778  catch(Exception $e){
779  $resultCode=$e->getMessage();
780  $resultId=101;
781  $type=3;
782  }
783  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
784  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
785  return $response;
786  }
787  //==== End timeRepeating =============================================================================
788 
789  //==== Start device =========================================================================
797  function device($entity){
798  $resultId = 0;
799  $page="Trigger.php";
800  $method="device";
801  $result=json_encode($entity);
802  $message="";
803  $type="1";
804  $this->ErrorCode = new Application_Model_ErrorCode;
805  $this->Log = new Application_Model_Log;
806  try{
807  $data=array();
808  if(!isset($entity['Function'])){
809  $resultId = 3207;
810  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
811  $resultCode=sprintf($r['Message'], "Function");
812  $message=sprintf($r['Message'], "Function");
813  $type=3;
814  }else{$data['Function']=$entity['Function'];}
815 
816  if(!isset($entity['Value'])){
817  $resultId=3207;
818  $r=$this->ErrorCode->getErrorCode($resultId, Zend_Registry::get('language'));
819  $resultCode=sprintf($r['Message'], "Value");
820  $message=sprintf($r['Message'], "Value");
821  $type=3;
822  }else{$data['Value']=$entity['Value'];}
823 
824  if($data['Function']=="Equals"){
825  if(!isset($data['Value']['CurrentDevice'])){
826  $resultId = 3209;
827  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
828  $resultCode=sprintf($r['Message'], "CurrentDevice");
829  $message=sprintf($r['Message'], "CurrentDevice");
830  $type=3;
831  }
832  if(!isset($data['Value']['ReferenceDevice'])){
833  $resultId = 3209;
834  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
835  $resultCode=sprintf($r['Message'], "ReferenceDevice");
836  $message=sprintf($r['Message'], "ReferenceDevice");
837  $type=3;
838  }
839 
840  if($resultId==0){
841  if($data['Value']['CurrentDevice']==$data['Value']['ReferenceDevice']){
842  $resultCode = 0;
843  }else{
844  $resultCode = -3;
845  }
846  }
847  }
848  }
849  catch(Exception $e){
850 
851  $resultCode=$e->getMessage();
852  $resultId=101;
853  $type=3;
854  }
855  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
856  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
857  return $response;
858  }
859  //==== End device =============================================================================
860 
861 
862  //==== Start activity =========================================================================
870  function activity($entity){
871  $resultId = 0;
872  $page="Trigger.php";
873  $method="activity";
874  $result=json_encode($entity);
875  $message="";
876  $type="1";
877  $this->ErrorCode = new Application_Model_ErrorCode;
878  $this->Log = new Application_Model_Log;
879  try{
880  $data=array();
881  if(!isset($entity['Function'])){
882  $resultId = 3210;
883  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
884  $resultCode=sprintf($r['Message'], "Function");
885  $message=sprintf($r['Message'], "Function");
886  $type=3;
887  }else{$data['Function']=$entity['Function'];}
888 
889  if(!isset($entity['Value'])){
890  $resultId = 3210;
891  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
892  $resultCode=sprintf($r['Message'], "Value");
893  $message=sprintf($r['Message'], "Value");
894  $type=3;
895  }else{$data['Value']=$entity['Value'];}
896 
897  if($resultId==0){
898  switch($data['Function'])
899  {
900  case 'Equals':
901  if(!isset($data['Value']['ReferenceActivity'])){
902  $resultId = 3210;
903  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
904  $resultCode=sprintf($r['Message'], "ReferenceActivity");
905  $message=sprintf($r['Message'], "ReferenceActivity");
906  $type=3;
907  }
908 
909  if(!isset($data['Value']['CurentActivity'])){
910  $resultId = 3210;
911  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
912  $resultCode=sprintf($r['Message'], "CurentActivity");
913  $message=sprintf($r['Message'], "CurentActivity");
914  $type=3;
915  }
916 
917  if($resultId==0){
918  if($data['CurentActivity']== $data['ReferenceActivity']){
919  $resultCode = 0;
920  }else{
921  $resultCode = -3;
922  }
923  }
924  break;
925 
926  case 'NotEquals':
927  if(!isset($data['Value']['ReferenceActivity'])){
928  $resultId = 3210;
929  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
930  $resultCode=sprintf($r['Message'], "ReferenceActivity");
931  $message=sprintf($r['Message'], "ReferenceActivity");
932  $type=3;
933  }
934 
935  if(!isset($data['Value']['CurentActivity'])){
936  $resultId = 3210;
937  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
938  $resultCode=sprintf($r['Message'], "CurentActivity");
939  $message=sprintf($r['Message'], "CurentActivity");
940  $type=3;
941  }
942 
943  if($resultId==0){
944  if($data['CurentActivity']!= $data['ReferenceActivity']){
945  $resultCode = 0;
946  }else{
947  $resultCode = -3;
948  }
949  }
950  break;
951 
952  case 'InList':
953  if(!isset($data['Value']['ReferenceActivity'])){
954  $resultId = 3210;
955  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
956  $resultCode=sprintf($r['Message'], "ReferenceActivity");
957  $message=sprintf($r['Message'], "ReferenceActivity");
958  $type=3;
959  }
960 
961  if(!isset($data['Value']['CurentActivity'])){
962  $resultId = 3210;
963  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
964  $resultCode=sprintf($r['Message'], "CurentActivity");
965  $message=sprintf($r['Message'], "CurentActivity");
966  $type=3;
967  }
968 
969  if($resultId==0){
970  if(in_array($data['CurentActivity'], $data['ReferenceActivity'])){
971  $resultCode = 0;
972  }else{
973  $resultCode = -3;
974  }
975  }
976  break;
977  }
978 
979  }
980  }
981  catch(Exception $e){
982  $resultCode=$e->getMessage();
983  $resultId=101;
984  $type=3;
985  }
986  $this->Log->save($page,$method,json_encode($entity),$resultId.'=>'.$resultCode,$type);
987  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
988  return $response;
989  }
990  //==== End activity =========================================================================
991 
992 }
993 ?>