/** * Return default template command * * @return array */ public function getCommands() { if (!$this->commands) { $this->commands = SMSCommand::getSMSCommandsCode(); } return $this->commands; }
protected function listenDLTorrentTemp($cycle = 1) { $this->_logger->log(__METHOD__, array('cycle' => $cycle)); $this->_connector->setParams(array('timeout' => 60)); ListenerProcess::addComment($this->listener->listener_id, 'comment', 'Opening COM connection'); $messages = null; $process = $this; $logger = $process->_logger; $this->_connector->onReceiveMessage = function ($message) use(&$process, &$logger) { // $logger->log(__METHOD__ . ' New message', array('message' => $message, 'listener_id' => $process->listener->listener_id, 'overwrite' => $process->settings->overwrite_data_on_listening)); // // $messageId = ListenerLogTemp::addNew($message, $process->listener->listener_id, $process->settings->overwrite_data_on_listening, 'datalogger'); // // ListenerProcess::addComment($process->listener->listener_id, 'comment', 'got msg #' . $messageId); $logger->log(__METHOD__ . ' New message', array('message' => $message, 'listener_id' => $process->listener->listener_id, 'overwrite' => $process->settings->overwrite_data_on_listening)); if (is_null(SMSCommand::setResponse($message))) { $this->_logger->log(__METHOD__ . ' Message type: ' . 'default'); $messageId = ListenerLog::addNew($message, $process->listener->listener_id, $process->settings->overwrite_data_on_listening, 'datalogger'); ListenerProcess::addComment($process->listener->listener_id, 'comment', 'got msg #' . $messageId); } else { $this->_logger->log(__METHOD__ . ' Message type: ' . 'sms command'); } }; $result = $this->_connector->readData($messages); $this->_logger->log(__METHOD__ . ' Complete listen datalogger.', array('cycle' => $cycle, 'result' => $result)); }
<?php /** * @var SMSCommandGenerateMessageForm $form */ ?> <h2><?php echo SMSCommand::getSMSCommandsCode()[$form->sms_command_code]; ?> </h2> <?php echo CHtml::beginForm('', 'post', ['id' => 'generate_command']); ?> <?php echo CHtml::activeHiddenField($form, 'station_id'); echo CHtml::activeHiddenField($form, 'sms_command_code'); ?> <?php if ($form->getParamsList()) { ?> <?php foreach ($form->getParamsList() as $id => $label) { ?> <?php echo CHtml::activeHiddenField($form, "sms_command_params[{$id}]"); ?> <?php
/** * 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; }
/** * Send sms commands * * @param $cycle int */ protected function sendSMSByModem($cycle) { $this->_logger->log(__CLASS__ . ' ' . __METHOD__, array('cycle' => $cycle)); // Find unsent sms /** @var array|SMSCommand[] $sms_commands */ $sms_commands = SMSCommand::model()->with('station')->findAllByAttributes(['sms_command_status' => SMSCommand::STATUS_NEW], ['limit' => 5]); // Sent sms if (isset($sms_commands) && ($count = count($sms_commands))) { foreach ($sms_commands as $sms_command) { $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' sms_command_id: ' . $sms_command->sms_command_id); $sms = new SmsMessageSender($this->_logger, $this->listener, new PhpSerial($this->_logger), Yii::app()->params['com_connect_params'], $this->source, $sms_command->station->phone_number, $sms_command->sms_command_message); if ($sms->send()) { $sms_command->sms_command_status = SMSCommand::STATUS_SENT; if (!$sms_command->save()) { $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' sms_command: ' . 'not save! ' . json_encode($sms_command->getErrors())); } } elseif ($sms->hasError()) { foreach ($sms->errors() as $error) { $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' error: ' . $error); } } $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' sms_command_status: ' . $sms_command->sms_command_status); } $sms_commands_sent = array_filter($sms_commands, function ($sms_command) { return $sms_command->sms_command_status == SMSCommand::STATUS_SENT; }); if ($count = count($sms_commands_sent)) { ListenerProcess::addComment($this->listener->listener_id, 'sms_command', 'Sending complete. Submitted ' . $count . ' SMS command. SMSCommand ids:' . implode(', ', array_keys(CHtml::listData($sms_commands_sent, 'sms_command_id', 'sms_command_message')))); } else { ListenerProcess::addComment($this->listener->listener_id, 'sms_command', 'Sending complete. Submitted ' . $count . ' SMS command.'); } } }
/** * Send command on station by SMS */ public function actionSendSMSCommand() { $form = new SMSCommandSendForm(); /** * Update sms status */ if (isset($_GET['view_id']) || isset($_GET['sms_command_id'])) { if (isset($_GET['view_id'])) { $form->setSMS(SMSCommand::model()->findByPk($_GET['view_id'])); } if (isset($_GET['sms_command_id'])) { $form->setSMS(SMSCommand::model()->findByPk($_GET['sms_command_id'])); } if (Yii::app()->request->isAjaxRequest) { return $this->renderPartial('__sms_command_status', ['sms' => $form->getSMS()]); } } /** * Delete sms */ if (isset($_GET['delete_id'])) { SMSCommand::model()->deleteByPk($_GET['delete_id']); } /** * Create sms */ if (isset($_POST['SMSCommandSendForm'])) { if ($_POST['send']) { $form->setScenario($form::SCENARIO_SEND); $form->setAttributes($_POST['SMSCommandSendForm']); if ($form->validate()) { $form->getSMS()->setAttributes($form->getAttributes()); if ($form->getSMS()->save()) { $form = (new SMSCommandSendForm())->setSMS(SMSCommand::model()->findByPk($form->getSMS()->sms_command_id)); Yii::app()->user->setFlash('SendSMSCommandForm_success', "Command send!"); } } } } /** * Grid */ if (isset($_POST['date_range'])) { if (preg_match("/([\\d]{1,2})\\/([\\d]{1,2})\\/([\\d]{2,4})/i", $_POST['SMSCommand']['updated_from'], $matches1)) { $dateFromViewFormat = $matches1[0]; $dateFrom = $matches1[3] . '-' . $matches1[1] . '-' . $matches1[2] . ' 00:00:00'; Yii::app()->request->cookies['dateFrom'] = new CHttpCookie('dateFrom', $dateFromViewFormat); Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', $dateFrom); } else { unset(Yii::app()->request->cookies['dateFrom']); unset(Yii::app()->request->cookies['from_date']); } if (preg_match("/([\\d]{1,2})\\/([\\d]{1,2})\\/([\\d]{2,4})/i", $_POST['SMSCommand']['updated_to'], $matches2)) { $dateToViewFormat = $matches2[0]; $dateTo = $matches2[3] . '-' . $matches2[1] . '-' . $matches2[2] . ' 23:59:59'; Yii::app()->request->cookies['dateTo'] = new CHttpCookie('dateTo', $dateToViewFormat); Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', $dateTo); } else { unset(Yii::app()->request->cookies['dateTo']); unset(Yii::app()->request->cookies['to_date']); } } if (isset($_GET['reset'])) { unset(Yii::app()->request->cookies['dateFrom']); unset(Yii::app()->request->cookies['dateTo']); unset(Yii::app()->request->cookies['from_date']); unset(Yii::app()->request->cookies['to_date']); } $SMSCommand = new SMSCommand(); $SMSCommand->unsetAttributes(); $SMSCommand->from_date = Yii::app()->request->cookies['from_date']; $SMSCommand->to_date = Yii::app()->request->cookies['to_date']; if ($_GET['SMSCommand']) { $SMSCommand->attributes = $_GET['SMSCommand']; } $dataProvider = $SMSCommand->search(); //csv if ($_GET['getcsv']) { $items = $dataProvider->model->findAll($dataProvider->criteria); if (!is_null($items)) { $data = array(); foreach ($items as $item) { $dataItem = $item->getAttributes(); $data[] = $dataItem; } $ECSVExporter = new ECSVExporter($data); It::downloadFile($ECSVExporter->getString(), 'csv.csv', 'text/csv'); exit; } } return $this->render('send_sms_command', ['form' => $form, 'dataProvider' => $dataProvider, 'dateFrom' => Yii::app()->request->cookies['dateFrom'], 'dateTo' => Yii::app()->request->cookies['dateTo']]); }
function actionIndex() { SMSCommand::setResponse('@BAWS01DTOK234567890BV11267558E9F6$'); // SMSCommand::setResponse('@BAWS01DTFAILE3290CA6$'); }