Esempio n. 1
0
 public function checkTruck()
 {
     $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
     $channel = $connection->channel();
     $channel->queue_declare('check_static_truck', false, true, false, false);
     $callback = function ($msg) {
         global $logger;
         global $db;
         $path = json_decode($msg->body, true);
         $limit = new \DateTime();
         $limit->sub(new \DateInterval('PT5M'));
         $positions = $db->getFromTableWhere('position', ['>at' => $limit->format('Y-m-d H:i:s'), 'path_id' => $path['path_id']]);
         if (count($positions) < 2) {
             $logger->log('warning', 'no position since 5 minutes for path with id : ' . $path['path_id']);
         } else {
             $static = true;
             $previousLongitude = $positions[0]['longitude'];
             $previousLatitude = $positions[0]['latitude'];
             foreach ($positions as $position) {
                 if ($position['longitude'] != $previousLongitude || $position['latitude'] != $previousLatitude) {
                     if ($path['sms_static']) {
                         $db->updateTableWhere('path', ['sms_static' => false], ['id' => $path['path_id']]);
                     }
                     $logger->log('info', 'Worker - Truck for the path with id : ' . $path['path_id'] . 'isn\'t static');
                     $static = false;
                     break;
                 }
             }
             if ($static == true && !$path['sms_static']) {
                 $db->updateTableWhere('path', ['sms_static' => true], ['id' => $path['path_id']]);
                 $logger->log('info', 'Worker - Truck for the path with id : ' . $path['path_id'] . 'is static');
                 $driver = $db->getFromTableWhere('driver', ['id' => $path['driver']]);
                 $message = 'Nous avons détecté une immobilité de votre véhicule depuis plus de 5 minutes, Merci d\'informer le status de votre trajet via l\'application';
                 $logger->log('info', 'Worker - Send SMS to driver with id ' . $driver[0]['id'] . ' (' . $driver[0]['phone'] . ') with message : ' . $message);
                 $sms = new internalSms();
                 $sms->sendSmsToNumber($message, $driver[0]['phone']);
             }
         }
         $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
     };
     $channel->basic_qos(null, 1, null);
     $channel->basic_consume('check_static_truck', '', false, false, false, false, $callback);
     while (count($channel->callbacks)) {
         $channel->wait();
     }
     $channel->close();
     $connection->close();
 }
Esempio n. 2
0
 /**
  * Cette fonction permet de valider une intervention par SMS pour un repairer
  * @param string $key : La clef du webhook
  */
 public function webhookValidateIntervention($key)
 {
     global $logger;
     global $db;
     if ($key != '7Jl2ESGU5wQZeVzD9ZkuQA26VBYorI9J') {
         $logger->log('warning', 'Webhook validate intervention invalid key : ' . $key);
         return false;
     }
     if (!isset($_POST['content'], $_POST['send_by'])) {
         $logger->log('warning', 'Webhook validate intervention no content message or no sender');
         return false;
     }
     $smsText = explode(':', str_replace('\\n', '', trim($_POST['content'])));
     if (count($smsText) < 3) {
         $logger->log('warning', 'Webhook validate intervention less than 3 block in SMS');
         return false;
     }
     if ($smsText[0] != 'intervention') {
         $logger->log('warning', 'Webhook validate intervention block 0 different than intervention');
         return false;
     }
     if (!($interventions = $db->getFromTableWhere('intervention', ['id' => $smsText[1]]))) {
         $logger->log('warning', 'Webhook validate intervention no intervention find in database with id : ' . $smsText[1]);
         return false;
     }
     $intervention = $interventions[0];
     if ($intervention['status'] != internalConstants::$interventionStatus['WAIT'] && $intervention['status'] != internalConstants::$interventionStatus['RUN']) {
         $logger->log('warning', 'Webhook validate intervention si WAIT or RUN');
         return false;
     }
     if ($smsText[2] == 'ok') {
         $logger->log('info', 'Webhook validate intervention - validation for intervention with id : ' . $intervention['id']);
         $intervention['status'] = internalConstants::$interventionStatus['RUN'];
         $db->updateTableWhere('path', ['status' => internalConstants::$pathStatus['FIX']], ['id' => $intervention['path_id']]);
         $text = 'L\'intervention N°' . $intervention['id'] . ' a bien été validée. Quand vous aurez réparé la panne, envoyez le message suivant à ce numéro : "intervention:' . $intervention['id'] . ':finish"';
         $internalSms = new internalSms();
         $internalSms->sendSmsToNumber($text, $_POST['send_by']);
         if ($paths = $db->getFromTableWhere('path', ['id' => $intervention['path_id']])) {
             $path = $paths[0];
             $driver = $db->getFromTableWhere('driver', ['id' => $path['driver_id']])[0];
             $textDriver = 'Un reparateur viens de vous prendre en charge, il arrivera sous peu.';
             $internalSms->sendSmsToNumber($textDriver, $driver['phone']);
         }
     } else {
         if ($smsText[2] == 'finish') {
             $logger->log('info', 'Webhook validate intervention - intervention with id : ' . $intervention['id'] . ' is finished');
             $date = new DateTime();
             $date = $date->format('Y-m-d H:i:s');
             $intervention['end_date'] = $date;
             $intervention['status'] = internalConstants::$interventionStatus['END'];
             $text = 'L\'intervention N°' . $intervention['id'] . ' a bien été terminée. Merci.';
             $internalSms = new internalSms();
             $internalSms->sendSmsToNumber($text, $_POST['send_by']);
         } else {
             $logger->log('error', 'Webhook validate intervention');
             return false;
         }
     }
     $db->updateTableWhere('intervention', $intervention, ['id' => $intervention['id']]);
     return true;
 }