コード例 #1
0
ファイル: SMSCommand.php プロジェクト: anton-itscript/WM-Web
 /**
  * Find current sms command and set in here response
  *
  * @param $response string
  * @example $response OK   : '@ B AWS01 DT OK   234567890 BV1 126 7558E9F6 $' (without space)
  * @example $response FAIL : '@ B AWS01 DT FAIL                   E3290CA6 $' (without space)
  *
  * @return null|SMSCommand
  */
 public static function setResponse($response)
 {
     /**
      * 1. Min response length
      * 2. Lead X code
      * 3. Start @
      *    Ended $
      * 4. CRC
      */
     if (strlen($response) >= 18 && (substr($response, 1, 1) === 'B' || substr($response, 1, 1) === 'C') && substr($response, 0, 1) === '@' && substr($response, -1) === '$' && substr($response, -9, 8) === It::prepareCRC(substr($response, 1, -9))) {
         $sms_command_code = substr($response, 7, 2);
         $station_id_code = substr($response, 2, 5);
         $qb = new CDbCriteria();
         $qb->compare('sms_command_status', SMSCommand::STATUS_SENT);
         $qb->compare('sms_command_code', $sms_command_code);
         $qb->compare('station.station_id_code', $station_id_code);
         $qb->with = ['station' => ['select' => false]];
         $qb->order = 't.updated ASC';
         /** @var SMSCommand $sms_command */
         $sms_command = SMSCommand::model()->find($qb);
         if (!isset($sms_command)) {
             $sms_command = new SMSCommand();
             $station = Station::Model()->findByAttributes(array('station_id_code' => $station_id_code));
             if (is_null($station)) {
                 return null;
             }
             $sms_command->station_id = $station->station_id;
             $sms_command->sms_command_code = "-";
             $sms_command->sms_command_message = "-";
         }
         $sms_command->sms_command_response = $response;
         $sms_command->sms_command_status = SMSCommand::STATUS_PROCESSED;
         if (!$sms_command->save()) {
             It::sendLetter(yii::app()->params['developer_email'], 'Error', json_encode($sms_command->getErrors()));
         }
         return $sms_command;
     }
     return null;
 }