Commit 4e6e37f7 by BANice

线上提交版

parent 741f7b68
...@@ -17,11 +17,13 @@ use Application\Model\StationModel; ...@@ -17,11 +17,13 @@ use Application\Model\StationModel;
use GatewayWorker\Lib\Gateway; use GatewayWorker\Lib\Gateway;
use Lib\cdz_order\Order; use Lib\cdz_order\Order;
use Lib\cdz_order\state\Offline; use Lib\cdz_order\state\Offline;
use Lib\cdz_order\state\Online;
use Lib\Log; use Lib\Log;
use Lib\order_exception\OrderState;
use Lib\port\Port; use Lib\port\Port;
use Lib\rabbitmq\PublishPortShed;
use Lib\redis\RedisKey; use Lib\redis\RedisKey;
use Lib\send\command\StopChargerCommand;
use Lib\send\Send;
use Lib\state\StateCode;
class AccessController extends Controller class AccessController extends Controller
{ {
...@@ -55,8 +57,12 @@ class AccessController extends Controller ...@@ -55,8 +57,12 @@ class AccessController extends Controller
$gateway = $gatewayModel->getColumns(['id', 'station_id'], " mac_address = '$mac' "); $gateway = $gatewayModel->getColumns(['id', 'station_id'], " mac_address = '$mac' ");
Log::debug('gateway_info', $gateway); if ($gateway === false) {
Gateway::closeClient($this->clientId);
return;
}
if (count($gateway) !== 0) { if (count($gateway) !== 0) {
$gatewayId = $gateway['id']; $gatewayId = $gateway['id'];
...@@ -116,7 +122,6 @@ class AccessController extends Controller ...@@ -116,7 +122,6 @@ class AccessController extends Controller
$sessionId .= $hex; $sessionId .= $hex;
} }
$this->response->send('02', $sessionId); $this->response->send('02', $sessionId);
} else { } else {
...@@ -142,6 +147,8 @@ class AccessController extends Controller ...@@ -142,6 +147,8 @@ class AccessController extends Controller
Log::debug('端口同步'); Log::debug('端口同步');
$stopCommandArray = [];
$startLogicNum = $this->get('start_num'); $startLogicNum = $this->get('start_num');
$startLogicNumDec = hexdec($startLogicNum); $startLogicNumDec = hexdec($startLogicNum);
...@@ -220,13 +227,19 @@ class AccessController extends Controller ...@@ -220,13 +227,19 @@ class AccessController extends Controller
$responseOrder = []; $responseOrder = [];
$orderModel = new OrderModel();
foreach ($portIdState as $id => $state) { foreach ($portIdState as $id => $state) {
$port->setId($id); $port->setId($id);
$orderId = $port->getPortUsingOrderId($redis, $id); $orderId = $port->getPortUsingOrderId($redis, $id);
if (isset($orderId) === false || $orderId === false || $orderId == 0) { if (isset($orderId) === false || $orderId === false || $orderId == 0) {
Log::debug('缺少orderId');
continue; continue;
} else { } else {
...@@ -235,13 +248,69 @@ class AccessController extends Controller ...@@ -235,13 +248,69 @@ class AccessController extends Controller
$order->setId($orderId); $order->setId($orderId);
if ($order->getOrderCache($redis) !== false) { if ($order->getOrderCache($redis) !== false) {
//插口脱落重新推送定时任务
Log::debug($offlineReason);
//订单从离线的状态变为正在充电状态 //订单从离线的状态变为正在充电状态
$orderState = new Offline($order, $redis, $offlineReason); $orderState = new Offline($order, $redis, $offlineReason);
if ($state === StateCode::PORT_SHED) {
try {
$time = time();
$publish = new PublishPortShed();
$publish->publishOrder([
'order_id' => $order->getId(),
'time' => date('Y-m-d H:i:s'),
'gateway_mac' => $_SESSION['gateway_mac'],
'shed_time' => $time,
]);
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$orderState->setShedTime($time);
$orderState->getSaveOrder()->saveShedTime();
}
$orderState->toCharger(); $orderState->toCharger();
$orderId = $order->getId();
$orderStateCode = $orderModel->getColumn('state', 'id = ' . $orderId);
if ($orderStateCode == 3) {
$stopCommand = new StopChargerCommand();
$stopCommand->setOrderId($orderId);
$stopCommand->setLogicNum($order->getPortLogicId());
$stopCommand->setGatewayMac($order->getGatewayMac());
$stopCommand->setFinishType(43);
$stopCommand->setClientUid($order->getGatewayMac());
array_push($stopCommandArray, $stopCommand);
unset($stopCommand);
}
$remainChargerTime = $orderState->getRemainChargerTime(); $remainChargerTime = $orderState->getRemainChargerTime();
if ($remainChargerTime > 0) { if ($remainChargerTime > 0) {
...@@ -253,8 +322,33 @@ class AccessController extends Controller ...@@ -253,8 +322,33 @@ class AccessController extends Controller
sprintf('%04x', $remainChargerTime), sprintf('%04x', $remainChargerTime),
]); ]);
if ($orderStateCode != 3) {
$offlineTimeLength = (int)$orderState->getOnlineTime() - (int)$orderState->getOfflineTime();
$orderState->setStartTime((int)strtotime($orderState->getStartTime()) + (int)$offlineTimeLength);
$orderState->getSaveOrder()->saveStartTime();
$userId = $order->getUserId();
if (Gateway::isUidOnline($userId) === 1) {
Gateway::sendToUid($userId, json_encode(['code' => 10000]));
}
}
} else {
Log::debug('剩余充电时间小于0');
} }
} else {
Log::debug('同步订单找不到redis');
} }
...@@ -264,9 +358,6 @@ class AccessController extends Controller ...@@ -264,9 +358,6 @@ class AccessController extends Controller
} }
Log::debug('using order',$responseOrder);
$orderCount = sprintf('%02x', count($responseOrder)); $orderCount = sprintf('%02x', count($responseOrder));
$responseOrderMessage = ''; $responseOrderMessage = '';
...@@ -277,9 +368,18 @@ class AccessController extends Controller ...@@ -277,9 +368,18 @@ class AccessController extends Controller
} }
$this->response->send('03', $startLogicNum . $count . $totalCount . $orderCount . $responseOrderMessage); $this->response->send('03', $startLogicNum . $count . $totalCount . $orderCount . $responseOrderMessage);
foreach ($stopCommandArray as $stopCommand) {
$send = new Send($stopCommand);
$messageData = $send->makeMessage();
Gateway::sendToCurrentClient($messageData);
unset($send);
}
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/24
* Time: 下午2:41
*/
namespace Application\Controller;
use Application\Controller;
class AmmeterController extends Controller
{
protected $parameterMap = [
'06' => [
'capacity' => 6
]
];
public function record()
{
}
}
\ No newline at end of file
...@@ -10,6 +10,7 @@ namespace Application\Controller; ...@@ -10,6 +10,7 @@ namespace Application\Controller;
use Application\Controller; use Application\Controller;
use GatewayWorker\Lib\Gateway;
use Lib\Log; use Lib\Log;
class HeartController extends Controller class HeartController extends Controller
...@@ -27,9 +28,25 @@ class HeartController extends Controller ...@@ -27,9 +28,25 @@ class HeartController extends Controller
$time = sprintf('%08x', time()); $time = sprintf('%08x', time());
if(isset($_SESSION['gateway_mac']) === false)
{
Log::error('心跳包未找到gateway_mac,断开连接');
try{
Gateway::closeCurrentClient();
}catch (\Exception $exception)
{
Log::error($exception->getMessage());
}
}
Log::debug("[heart]", [ Log::debug("[heart]", [
'gateway_id' => $_SESSION['gateway_id'], 'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'data' => $this->message, 'data' => $this->message,
'length' => hexdec(substr($this->message,4*2,4)), 'length' => hexdec(substr($this->message,4*2,4)),
......
...@@ -15,7 +15,6 @@ use Application\Model\OrderExceptionModel; ...@@ -15,7 +15,6 @@ use Application\Model\OrderExceptionModel;
use Application\Model\OrderModel; use Application\Model\OrderModel;
use Application\Model\PortModel; use Application\Model\PortModel;
use Application\Model\StationModel; use Application\Model\StationModel;
use function GuzzleHttp\Psr7\str;
use Lib\api\message\ChargerReloadMessage; use Lib\api\message\ChargerReloadMessage;
use Lib\api\message\ChargerStartMessage; use Lib\api\message\ChargerStartMessage;
use Lib\api\message\PortShedMessage; use Lib\api\message\PortShedMessage;
...@@ -25,7 +24,6 @@ use Lib\cdz_order\state\Charger; ...@@ -25,7 +24,6 @@ use Lib\cdz_order\state\Charger;
use Lib\cdz_order\state\WaitingCharger; use Lib\cdz_order\state\WaitingCharger;
use Lib\Gateway; use Lib\Gateway;
use Lib\Log; use Lib\Log;
use Lib\order_exception\OrderState;
use Lib\port\Port; use Lib\port\Port;
use Lib\rabbitmq\PublishPortShed; use Lib\rabbitmq\PublishPortShed;
use Lib\state\StateCode; use Lib\state\StateCode;
...@@ -147,13 +145,13 @@ class PortController extends Controller ...@@ -147,13 +145,13 @@ class PortController extends Controller
foreach ($offlinePortId as $portId) { foreach ($offlinePortId as $portId) {
$orderId = $port->getPortUsingOrderId(\Events::getRedis(),$portId); $orderId = $port->getPortUsingOrderId(\Events::getRedis(), $portId);
$order = new Order(); $order = new Order();
$order->setId($orderId); $order->setId($orderId);
$charger = new Charger($order,\Events::getRedis()); $charger = new Charger($order, \Events::getRedis());
$charger->toOffline(); $charger->toOffline();
...@@ -179,8 +177,6 @@ class PortController extends Controller ...@@ -179,8 +177,6 @@ class PortController extends Controller
$nowTime = date('Y-m-d H:i:s'); $nowTime = date('Y-m-d H:i:s');
Log::debug('口子状态改变', $this->getRequest()->getParameters());
$portMac = $this->get('mac'); $portMac = $this->get('mac');
$state = $this->get('state'); $state = $this->get('state');
...@@ -197,8 +193,6 @@ class PortController extends Controller ...@@ -197,8 +193,6 @@ class PortController extends Controller
$lastState = $port->getState($portId); $lastState = $port->getState($portId);
Log::debug('口子状态改变-->原状态为->' . $lastState . '更新->' . $state);
if ($state === StateCode::PORT_EXCEPTION) { if ($state === StateCode::PORT_EXCEPTION) {
//口子当前状态为异常,下位机会发送充电结束命令状态为中为异常,所以此处不处理,或者只记录 //口子当前状态为异常,下位机会发送充电结束命令状态为中为异常,所以此处不处理,或者只记录
...@@ -224,7 +218,7 @@ class PortController extends Controller ...@@ -224,7 +218,7 @@ class PortController extends Controller
$order->setId($orderId); $order->setId($orderId);
$waiting = new WaitingCharger($order,\Events::getRedis()); $waiting = new WaitingCharger($order, \Events::getRedis());
$waiting->toCharger(); $waiting->toCharger();
...@@ -240,25 +234,6 @@ class PortController extends Controller ...@@ -240,25 +234,6 @@ class PortController extends Controller
$update = ['shed_time' => $nowTime]; $update = ['shed_time' => $nowTime];
// $orderLastState = OrderState::getState($orderId);
//
// if ($orderLastState == StateCode::PORT_SHED) {
//
// if (isset($lastShedTime)) {
// $gapTime = strtotime($nowTime) - strtotime($lastShedTime);
//
// if (($gapTime / 60) > 12) {
//
// //fixme debug
//
// //口子距离上一次插口脱落时间超过了十分中,下发结束充电命令
// //发送结束充电,结束状态为远程超时结束
//
// Log::debug('todo 插口脱落超时10分钟');
//
// }
// }
// }
$orderModel->update($update, 'id = ' . $orderId); $orderModel->update($update, 'id = ' . $orderId);
...@@ -309,19 +284,6 @@ class PortController extends Controller ...@@ -309,19 +284,6 @@ class PortController extends Controller
} }
if ($state !== StateCode::PORT_CHARGER) {
$orderModel = new OrderModel();
if (isset($orderId) === false) {
$orderId = $port->getPortUsingOrderId(\Events::getRedis(), $portId);
}
$orderModel->update(['is_exception' => 1], 'id = ' . $orderId);
}
} }
$redis = \Events::getRedis(); $redis = \Events::getRedis();
...@@ -365,9 +327,8 @@ class PortController extends Controller ...@@ -365,9 +327,8 @@ class PortController extends Controller
$order->setId($orderId); $order->setId($orderId);
if($order->getOrderCache($redis) !== false) if ($order->getOrderCache($redis) !== false) {
{ $waiting = new WaitingCharger($order, $redis);
$waiting = new WaitingCharger($order,$redis);
$waiting->toCharger(); $waiting->toCharger();
...@@ -378,7 +339,7 @@ class PortController extends Controller ...@@ -378,7 +339,7 @@ class PortController extends Controller
$chargerStartMessage->setAmount(number_format($result['order_amount'] / 100, 2)); $chargerStartMessage->setAmount(number_format($result['order_amount'] / 100, 2));
$chargerStartMessage->setAddress($result['station_name'] . '(' . $result['port_name'] . ')'); $chargerStartMessage->setAddress($result['station_name'] . '-' . $result['port_name']);
$push = new Push(); $push = new Push();
...@@ -437,6 +398,15 @@ class PortController extends Controller ...@@ -437,6 +398,15 @@ class PortController extends Controller
} }
//订单状态修改
$order = new Order();
$order->setId($orderId);
$charger = new Charger($order,$redis);
$charger->toPortShed();
//发送延迟任务 //发送延迟任务
try { try {
...@@ -444,17 +414,18 @@ class PortController extends Controller ...@@ -444,17 +414,18 @@ class PortController extends Controller
$publishPortShedJob = new PublishPortShed(); $publishPortShedJob = new PublishPortShed();
$publishPortShedJob->publishOrder(
[
'order_id' => $orderId,
'time' => date('Y-m-d H:i:s'),
'gateway_mac' => $_SESSION['gateway_mac'],
]
);
} catch (\Exception $exception) { } catch (\Exception $exception) {
Log::error($exception->getMessage()); Log::error($exception->getMessage());
} }
$publishPortShedJob->publishOrder(
[
'order_id' => $orderId,
'time' => date('Y-m-d H:i:s'),
'gateway_mac' => $_SESSION['gateway_mac'],
]
);
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/18
* Time: 上午10:16
*/
namespace Application\Controller;
use Application\Controller;
use Application\Model\OrderModel;
use Lib\cdz_order\Order;
use Lib\cdz_order\state\Charger;
use Lib\cdz_order\state\PortShed;
use Lib\cdz_order\state\State;
use Lib\cdz_order\state\WaitingCharger;
use Lib\Log;
use Lib\port\Port;
use Lib\state\StateCode;
class PortStateController extends Controller
{
protected $parameterMap = [
'0B' => [
'logic' => 1,
'mac' => 8,
'state' => 1,
],
];
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function change()
{
Log::debug('口子状态改变');
$portMac = $this->get('mac');
$state = $this->get('state');
$state = hexdec($state);
$portMacId = $_SESSION['port_mac_id'];
if (!isset($portMacId[$portMac])) {
Log::error('口子状态改变,session中缺少口子的mac地址与id的映射');
return ;
}
$portId = $portMacId[$portMac];
$port = new Port();
$port->setId($portId);
$redis = \Events::getRedis();
$usingOrderId = $port->getPortUsingOrderId($redis, $portId);
$portState = $port->getState($portId);
if(!($portState == -1 && $state == StateCode::PORT_SHED))
{
$port->setPortState([$portId => $state]);
}
if ($usingOrderId === 0) {
$port->setPortState([$portId => $state]);
return;
}
$order = new Order();
$order->setId($usingOrderId);
if ($order->getOrderCache($redis) === false) {
Log::error('using order -> ' . $usingOrderId . '<-redis中缺失订单信息');
return;
}
$orderStateCode = $order->getStateCode();
$orderState = '';
Log::debug('port state ->'.$orderStateCode);
//订单原本的状态对象生成
switch ($orderStateCode) {
case StateCode::PORT_CHARGER:
$orderState = new Charger($order, $redis);
Log::debug('创建充电状态');
break;
case StateCode::PORT_SHED:
$orderState = new PortShed($order, $redis);
Log::debug('订单脱落状态');
break;
case StateCode::WAITING:
$orderState = new WaitingCharger($order, $redis);
break;
default:
break;
}
if (!($orderState instanceof State)) {
Log::error('口子状态改变缺少该state 类' . "($state)");
return;
}
//根据现有的状态修改状态
Log::debug('state->'.$state);
switch ($state) {
case StateCode::PORT_CHARGER:
$orderState->toCharger();
if($orderState instanceof WaitingCharger)
{
$orderModel = new OrderModel();
$date = date('Y-m-d H:i:s');
$orderModel->update(['is_charger' => $state,'start_time' => $date,'updated_at' =>$date ],'id = '.$usingOrderId);
}
break;
case StateCode::PORT_SHED:
$orderState->toPortShed();
break;
default:
break;
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/27
* Time: 下午4:32
*/
namespace Application\Controller;
use Application\Controller;
use Lib\Log;
class PowerOffController extends Controller
{
protected $parameterMap = [
'0F' => []
];
public function powerOff()
{
Log::debug('断电提醒');
}
}
\ No newline at end of file
...@@ -151,20 +151,17 @@ class Events ...@@ -151,20 +151,17 @@ class Events
$redis = Events::getRedis(); $redis = Events::getRedis();
Log::debug('disconnect');
if (isset($_SESSION['gateway_id']) === true) { if (isset($_SESSION['gateway_id']) === true) {
Log::info('gateway_id(' . $_SESSION['gateway_id'] . ')' . 'disconnect');
$redis->zAdd(\Lib\redis\RedisKey::GATEWAY_STATE, 0, $_SESSION['gateway_id']); $redis->zAdd(\Lib\redis\RedisKey::GATEWAY_STATE, 0, $_SESSION['gateway_id']);
} else { } else {
Log::info(isset($_SESSION['gateway_mac']) ? $_SESSION['gateway_mac'] : '');
Log::info('gatway disconnect'); Log::error('网关离线缺少gatewayId');
} }
......
...@@ -11,18 +11,148 @@ namespace Lib\api\message; ...@@ -11,18 +11,148 @@ namespace Lib\api\message;
class ChargerFullMessage extends MessageData class ChargerFullMessage extends MessageData
{ {
protected $templateId ='rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU'; protected $templateId = 'rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU';
private $first = ''; /**
* @var string
*/
private $first = '尊敬的用户,您的爱车已充满电,本次充电已结束
';
/**
* @var string
*/
private $startTime = ''; private $startTime = '';
/**
* @var string
*/
private $remark = '温馨提示:您的爱车已停止充电,如有疑问您可以在公众号后台留言或直接拨打客服热线进行咨询。
感谢您的使用';
private $endTime = '';
private $purchaseTime = '';
public function getPushDataJson(): string public function getPushDataJson(): string
{ {
return ''; return json_encode([
'first' => [
'value' => $this->first,
'color' => '#333',
],
'keyword1' => [
'value' => $this->startTime,
'color' => '#333',
],
'keyword2' => [
'value' => $this->purchaseTime,
'color' => '#333',
],
'keyword3' => [
'value' => $this->endTime,
'color' => '#333',
],
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
]);
}
/**
* @return string
*/
public function getTemplateId(): string
{
return $this->templateId;
}
/**
* @return string
*/
public function getFirst(): string
{
return $this->first;
}
/**
* @param string $first
*/
public function setFirst(string $first): void
{
$this->first = $first;
}
/**
* @return string
*/
public function getStartTime(): string
{
return $this->startTime;
}
/**
* @param string $startTime
*/
public function setStartTime(string $startTime): void
{
$this->startTime = $startTime;
}
/**
* @return string
*/
public function getRemark(): string
{
return $this->remark;
}
/**
* @param string $remark
*/
public function setRemark(string $remark): void
{
$this->remark = $remark;
}
/**
* @return string
*/
public function getEndTime(): string
{
return $this->endTime;
}
/**
* @param string $endTime
*/
public function setEndTime(string $endTime): void
{
$this->endTime = $endTime;
}
/**
* @return string
*/
public function getPurchaseTime(): string
{
return $this->purchaseTime;
}
/**
* @param string $purchaseTime
*/
public function setPurchaseTime(string $purchaseTime): void
{
$this->purchaseTime = $purchaseTime;
} }
......
...@@ -34,7 +34,7 @@ class ChargerRefundMessage extends MessageData ...@@ -34,7 +34,7 @@ class ChargerRefundMessage extends MessageData
private $remark = '温馨提示:超时关闭此次交易,支付金额会在1个工作日内退回您的余额或零钱,请注意查看,若未收到请联系我们的客服为您处理。若为充电口异常,请您及时更换至其它充电口充电 private $remark = '温馨提示:超时关闭此次交易,支付金额会在1个工作日内退回您的余额或零钱,请注意查看,若未收到请联系我们的客服为您处理。
'; ';
/** /**
......
...@@ -13,7 +13,7 @@ class ChargerReloadMessage extends MessageData ...@@ -13,7 +13,7 @@ class ChargerReloadMessage extends MessageData
{ {
protected $templateId = 't_PyOGCKN_fmD0blw0EuIQs-8sS3qOTmpQay12haPZY'; protected $templateId = 't_PyOGCKN_fmD0blw0EuIQs-8sS3qOTmpQay12haPZY';
private $first = '充电继续通知:'; private $first = '尊敬的用户,检测到插头插入,您的爱车已开始继续充电';
private $stationName = ''; private $stationName = '';
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/19
* Time: 下午3:23
*/
namespace Lib\api\message;
class ChargerShedTimeOutMessage extends MessageData
{
protected $templateId = 'rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU';
private $first = '尊敬的用户,您的爱车插头脱落,等待10分钟未检测到插头重新插入,本次充电已结束
';
/**
* @var string
*/
private $startTime = '';
/**
* @var string
*/
private $purchaseTime = '';
/**
* @var string
*/
private $endTime = '';
/**
* @var string
*/
private $remark = '温馨提示:您的爱车已停止充电,如有疑问您可以在公众号后台留言或直接拨打客服热线进行咨询。
感谢您的使用';
public function getPushDataJson(): string
{
return json_encode([
'first' => [
'value' => $this->first,
'color' => '#333',
],
'keyword1' => [
'value' => $this->startTime,
'color' => '#333',
],
'keyword2' => [
'value' => $this->purchaseTime,
'color' => '#333',
],
'keyword3' => [
'value' => $this->endTime,
'color' => '#333',
],
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
]);
}
/**
* @return string
*/
public function getTemplateId(): string
{
return $this->templateId;
}
/**
* @return string
*/
public function getFirst(): string
{
return $this->first;
}
/**
* @param string $first
*/
public function setFirst(string $first): void
{
$this->first = $first;
}
/**
* @return string
*/
public function getStartTime(): string
{
return $this->startTime;
}
/**
* @param string $startTime
*/
public function setStartTime(string $startTime): void
{
$this->startTime = $startTime;
}
/**
* @return string
*/
public function getPurchaseTime(): string
{
return $this->purchaseTime;
}
/**
* @param string $purchaseTime
*/
public function setPurchaseTime(string $purchaseTime): void
{
$this->purchaseTime = $purchaseTime;
}
/**
* @return string
*/
public function getEndTime(): string
{
return $this->endTime;
}
/**
* @param string $endTime
*/
public function setEndTime(string $endTime): void
{
$this->endTime = $endTime;
}
/**
* @return string
*/
public function getRemark(): string
{
return $this->remark;
}
/**
* @param string $remark
*/
public function setRemark(string $remark): void
{
$this->remark = $remark;
}
}
\ No newline at end of file
...@@ -24,6 +24,8 @@ class ChargerStartMessage extends MessageData ...@@ -24,6 +24,8 @@ class ChargerStartMessage extends MessageData
private $amount; private $amount;
private $remark = '感谢你的使用';
public function __construct() public function __construct()
{ {
...@@ -58,7 +60,7 @@ class ChargerStartMessage extends MessageData ...@@ -58,7 +60,7 @@ class ChargerStartMessage extends MessageData
'color' => '#333' 'color' => '#333'
], ],
'remark' => [ 'remark' => [
'value' => '感谢你的使用', 'value' => $this->remark,
'color' => '#333' 'color' => '#333'
], ],
]); ]);
...@@ -135,5 +137,22 @@ class ChargerStartMessage extends MessageData ...@@ -135,5 +137,22 @@ class ChargerStartMessage extends MessageData
$this->amount = $amount; $this->amount = $amount;
} }
/**
* @return string
*/
public function getRemark(): string
{
return $this->remark;
}
/**
* @param string $remark
*/
public function setRemark(string $remark): void
{
$this->remark = $remark;
}
} }
\ No newline at end of file
...@@ -10,7 +10,6 @@ namespace Lib\cdz_order; ...@@ -10,7 +10,6 @@ namespace Lib\cdz_order;
use Lib\cdz_order\state\State; use Lib\cdz_order\state\State;
use Lib\Log;
class Order class Order
{ {
...@@ -93,17 +92,40 @@ class Order ...@@ -93,17 +92,40 @@ class Order
/** /**
* @var int
*/
private $startCapacity;
/**
* @var int
*/
private $endCapacity;
/**
* @var string
*/
private $outTradeNo;
/**
* @var int
*/
private $isRefund = 0;
/**
* @param \Redis $redis * @param \Redis $redis
* @return array|bool * @return array|bool
*/ */
public function getOrderCache(\Redis $redis) public function getOrderCache(\Redis $redis)
{ {
if(isset($this->orderCache) === false){ if (isset($this->orderCache) == false || (is_array($this->orderCache) && count($this->orderCache) === 0)) {
$keyName = State::ORDER_HASH_PREFIX . $this->id; $keyName = State::ORDER_HASH_PREFIX . $this->id;
if ($redis->exists($keyName) === false) { if ($redis->exists($keyName) == false) {
return false; return false;
...@@ -111,6 +133,8 @@ class Order ...@@ -111,6 +133,8 @@ class Order
$this->orderCache = $redis->hGetAll($keyName); $this->orderCache = $redis->hGetAll($keyName);
var_dump($this->orderCache);
foreach ($this->orderCache as $key => $value) { foreach ($this->orderCache as $key => $value) {
$this->$key = $value; $this->$key = $value;
...@@ -124,6 +148,32 @@ class Order ...@@ -124,6 +148,32 @@ class Order
/** /**
* @param \Redis $redis * @param \Redis $redis
* @return array|bool
*/
public function refreshOrderCache(\Redis $redis)
{
$keyName = State::ORDER_HASH_PREFIX . $this->id;
if ($redis->exists($keyName) == false) {
return false;
}
$this->orderCache = $redis->hGetAll($keyName);
foreach ($this->orderCache as $key => $value) {
$this->$key = $value;
}
return $this->orderCache;
}
/**
* @param \Redis $redis
* @param $orderCache * @param $orderCache
*/ */
public function setOrderCache(\Redis $redis, $orderCache): void public function setOrderCache(\Redis $redis, $orderCache): void
...@@ -301,7 +351,7 @@ class Order ...@@ -301,7 +351,7 @@ class Order
*/ */
public function getPortLogicId(): int public function getPortLogicId(): int
{ {
return $this->portLogicId; return isset($this->portLogicId) ? (int)$this->portLogicId : 0;
} }
/** /**
...@@ -344,5 +394,69 @@ class Order ...@@ -344,5 +394,69 @@ class Order
$this->stationName = $stationName; $this->stationName = $stationName;
} }
/**
* @return int
*/
public function getStartCapacity(): int
{
return $this->startCapacity;
}
/**
* @param int $startCapacity
*/
public function setStartCapacity(int $startCapacity): void
{
$this->startCapacity = $startCapacity;
}
/**
* @return int
*/
public function getEndCapacity(): int
{
return $this->endCapacity;
}
/**
* @param int $endCapacity
*/
public function setEndCapacity(int $endCapacity): void
{
$this->endCapacity = $endCapacity;
}
/**
* @return string
*/
public function getOutTradeNo(): string
{
return $this->outTradeNo;
}
/**
* @param string $outTradeNo
*/
public function setOutTradeNo(string $outTradeNo): void
{
$this->outTradeNo = $outTradeNo;
}
/**
* @return int
*/
public function getisRefund(): int
{
return $this->isRefund;
}
/**
* @param int $isRefund
*/
public function setIsRefund(int $isRefund): void
{
$this->isRefund = $isRefund;
}
} }
\ No newline at end of file
...@@ -69,6 +69,18 @@ class SaveOrder ...@@ -69,6 +69,18 @@ class SaveOrder
} }
public function saveEndCapacity()
{
$this->redis->hSet($this->state->getKeyName(), 'endCapacity', $this->state->getOrder()->getEndCapacity());
}
public function saveStartCapacity()
{
$this->redis->hSet($this->state->getKeyName(), 'startCapacity', $this->state->getOrder()->getStartCapacity());
}
public function pipe() public function pipe()
{ {
$this->state->getRedis()->multi(2); $this->state->getRedis()->multi(2);
......
...@@ -9,10 +9,28 @@ ...@@ -9,10 +9,28 @@
namespace Lib\cdz_order\state; namespace Lib\cdz_order\state;
use Application\Model\OrderModel;
use Lib\api\message\ChargerExceedMessage;
use Lib\api\message\ChargerExceptionMessage;
use Lib\api\message\ChargerFinishMessage;
use Lib\api\message\ChargerFullMessage;
use Lib\api\message\PortShedMessage;
use Lib\api\message\Push;
use Lib\cdz_order\Order; use Lib\cdz_order\Order;
use Lib\Log;
use Lib\order_exception\OrderTrace;
use Lib\port\Port;
use Lib\rabbitmq\PublishPortShed;
use Lib\state\StateCode;
class Charger extends State class Charger extends State
{ {
/**
* @var int
*/
protected $selfStateCode = 12;
public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); } public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); }
public function toCharger() public function toCharger()
...@@ -20,34 +38,271 @@ class Charger extends State ...@@ -20,34 +38,271 @@ class Charger extends State
// TODO: Implement toCharger() method. // TODO: Implement toCharger() method.
} }
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$fullMessage = new ChargerFullMessage();
$fullMessage->setEndTime($this->endTime);
$fullMessage->setFirst('尊敬的用户,本次充电已结束
(手动关闭)');
$fullMessage->setPurchaseTime($this->order->getPurchaseTime());
$fullMessage->setStartTime($this->startTime);
$push = new Push();
$push->push($fullMessage, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1241, '正在充电 --> 用户主动结束');
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toPortShed() public function toPortShed()
{ {
// TODO: Implement toPortShed() method. $this->shedTime = time();
$this->order->setStateCode(StateCode::PORT_SHED);
$this->saveOrder->pipe();
$this->saveOrder->saveStateCode();
$this->saveOrder->saveShedTime();
$port = new Port();
$port->setPortState([$this->order->getPortId() => 14]);
$this->saveOrder->exec();
$shedMessage = new PortShedMessage();
$shedMessage->setStationName($this->order->getStationName());
$shedMessage->setPortName($this->order->getPortName());
$shedMessage->setWaitingTime(10);
$shedMessage->setRemark('超时将关闭此次交易哦,请尽快重连插头');
$shedMessage->setTime(date('Y-m-d H:i:s'));
$push = new Push();
try {
$push->push($shedMessage, $this->order->getUserId());
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
try {
$publishPortShedJob = new PublishPortShed();
$publishPortShedJob->publishOrder(
[
'order_id' => $this->order->getId(),
'time' => date('Y-m-d H:i:s'),
'gateway_mac' => $_SESSION['gateway_mac'],
'shed_time' => $this->shedTime,
]
);
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$orderTrace = new OrderTrace();
$this->order->setCurrentState(new PortShed($this->order, $this->redis));
$orderTrace->setOrderId($this->order->getId());
$orderTrace->write($this, $this->order->getCurrentState());
} }
public function toPowerExceed() public function toPowerExceed()
{ {
// TODO: Implement toPowerExceed() method.
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFinishPowerExceed()
{
$exceedMessage = new ChargerExceedMessage();
$exceedMessage->setStartTime($this->startTime);
$exceedMessage->setEndTime($this->endTime);
$exceedMessage->setChargerTime($this->order->getPurchaseTime());
$push = new Push();
$push->push($exceedMessage, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1234, '正在充电 --> 功率超限');
} }
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFull() public function toFull()
{ {
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$fullMessage = new ChargerFullMessage();
$fullMessage->setStartTime($this->startTime);
$fullMessage->setPurchaseTime($this->order->getPurchaseTime());
$fullMessage->setEndTime($this->endTime);
$push = new Push();
$push->push($fullMessage, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1233, '正在充电 --> 充满自停');
// TODO: Implement toFull() method. // TODO: Implement toFull() method.
} }
/** /**
* 充电结束记录充电结束状态和结束充电时间 * 充电结束记录充电结束状态和结束充电时间
* @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public function toFinish() public function toFinish()
{ {
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]); $this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$finishMessage = new ChargerFinishMessage();
$finishMessage->setEndTime($this->endTime);
$finishMessage->setUserId($this->order->getUserId());
$finishMessage->setChargerTime($this->order->getPurchaseTime());
$finishMessage->setChargerPlc($this->order->getStationName() . '-' . $this->order->getPortName() . '号');
$finishMessage->setOrderAmount($this->order->getPayAmount());
$push = new Push();
try {
$push->push($finishMessage, $this->order->getUserId());
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
'is_exception' => 0,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1230, '正在充电 --> 充电时间结束');
} }
public function toOffline() public function toOffline()
{ {
$this->redis->hSet($this->keyName,'offlineTime',time()); $this->redis->hSet($this->keyName, 'offlineTime', time());
// TODO: Implement toOffline() method. // TODO: Implement toOffline() method.
} }
......
...@@ -13,8 +13,23 @@ use Lib\cdz_order\Order; ...@@ -13,8 +13,23 @@ use Lib\cdz_order\Order;
class Created extends State class Created extends State
{ {
/**
* @var int
*/
protected $selfStateCode = 1;
public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); } public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); }
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
public function toCharger() public function toCharger()
{ {
......
...@@ -10,11 +10,19 @@ namespace Lib\cdz_order\state; ...@@ -10,11 +10,19 @@ namespace Lib\cdz_order\state;
use Lib\cdz_order\Order; use Lib\cdz_order\Order;
use Lib\Gateway;
use Lib\Log;
use Lib\order_exception\OrderTrace;
class Offline extends State class Offline extends State
{ {
public function __construct(Order $order, \Redis $redis, int $offlineReason) /**
* @var int
*/
protected $selfStateCode = 10;
public function __construct(Order $order, \Redis $redis, int $offlineReason = 0)
{ {
parent::__construct($order, $redis); parent::__construct($order, $redis);
...@@ -22,6 +30,17 @@ class Offline extends State ...@@ -22,6 +30,17 @@ class Offline extends State
} }
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
/** /**
*记录了离线原因和上线时间 *记录了离线原因和上线时间
* 通过上线时间和离线时间根据离线原因计算剩余充电时间 * 通过上线时间和离线时间根据离线原因计算剩余充电时间
...@@ -30,41 +49,47 @@ class Offline extends State ...@@ -30,41 +49,47 @@ class Offline extends State
{ {
$purchaseTime = $this->order->getPurchaseTime(); $purchaseTime = $this->order->getPurchaseTime();
$startTime = strtotime($this->startTime);
$this->onlineTime = time(); $this->onlineTime = time();
if($this->order->getStateCode() == -1) if ($this->order->getStateCode() == -1) {
{
$this->remainChargerTime = $this->order->getPurchaseTime(); $this->remainChargerTime = $this->order->getPurchaseTime();
}else { } else {
$startTime = $this->getStartTime(); Log::debug('off reason'.$this->offlineReason);
$offlineTime = $this->getOfflineTime();
if ($this->offlineReason == 1) { if ($this->offlineReason == 0) {
$remainTime = $purchaseTime * 60 - ($this->onlineTime - $startTime);
$this->remainChargerTime = (int)($remainTime / 60);
$remainTime = $purchaseTime * 60 - ($offlineTime - $startTime); Log::debug('断网离线');
$this->remainChargerTime = (int)ceil($remainTime / 60);
} else { } else {
$remainTime = $purchaseTime * 60 - ($this->onlineTime - $startTime); $remainTime = $purchaseTime * 60 - ((int)$this->offlineTime - (int)$startTime);
$this->remainChargerTime = (int)($remainTime / 60);
$this->remainChargerTime = (int)ceil($remainTime / 60);
} }
} }
$this->saveOrder->savePortOnlineTime(); $this->saveOrder->savePortOnlineTime();
$this->order->setCurrentState(new Charger($this->order,$this->redis)); $orderTrace = new OrderTrace();
$this->order->setCurrentState(new Charger($this->order, \Events::getRedis()));
$orderTrace->write($this, $this->order->getCurrentState());
} }
......
...@@ -12,6 +12,19 @@ namespace Lib\cdz_order\state; ...@@ -12,6 +12,19 @@ namespace Lib\cdz_order\state;
class Online extends State class Online extends State
{ {
protected $selfStateCode = 11;
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
public function toCharger() public function toCharger()
{ {
// TODO: Implement toCharger() method. // TODO: Implement toCharger() method.
......
...@@ -9,8 +9,257 @@ ...@@ -9,8 +9,257 @@
namespace Lib\cdz_order\state; namespace Lib\cdz_order\state;
class PortShed use Application\Model\OrderModel;
use Lib\api\message\ChargerFinishMessage;
use Lib\api\message\ChargerFullMessage;
use Lib\api\message\ChargerReloadMessage;
use Lib\api\message\ChargerShedTimeOutMessage;
use Lib\api\message\PortShedMessage;
use Lib\api\message\Push;
use Lib\Log;
use Lib\order_exception\OrderTrace;
use Lib\port\Port;
class PortShed extends State
{ {
/**
*
* @var int
*/
protected $selfStateCode = 14;
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFinishByUser()
{
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$fullMessage = new ChargerFullMessage();
$fullMessage->setEndTime($this->endTime);
$fullMessage->setFirst('尊敬的用户,本次充电已结束
(手动关闭)');
$fullMessage->setPurchaseTime($this->order->getPurchaseTime());
$fullMessage->setStartTime($this->startTime);
$push = new Push();
$push->push($fullMessage, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1441, '口子脱落 --> 用户主动结束');
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFinishShed()
{
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$shedTimeOutMessgae = new ChargerShedTimeOutMessage();
$shedTimeOutMessgae->setEndTime($this->endTime);
$shedTimeOutMessgae->setStartTime($this->startTime);
$shedTimeOutMessgae->setPurchaseTime($this->order->getPurchaseTime());
$push = new Push();
$push->push($shedTimeOutMessgae, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1442, '口子脱落 --> 口子脱落超时结束');
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toCharger()
{
$reloadMessage = new ChargerReloadMessage();
$reloadMessage->setPortName($this->order->getPortName());
$reloadMessage->setStationName($this->order->getStationName());
$push = new Push();
try {
$push->push($reloadMessage, $this->order->getUserId());
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$this->order->setStateCode(12);
$this->saveOrder->saveStateCode();
$orderModel = new OrderModel();
$orderModel->update(['is_charger' => 12], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$this->order->setCurrentState(new Charger($this->order, $this->redis));
$orderTrace->setOrderId($this->order->getId());
$orderTrace->write($this, $this->order->getCurrentState());
$port = new Port();
$port->setPortState([$this->order->getPortId() => 12]);
}
public function toPortShed()
{
Log::error('order shed state can not change to shed');
// TODO: Implement toPortShed() method.
}
public function toPowerExceed()
{
Log::error('order shed state can not change to exceed');
// TODO: Implement toPowerExceed() method.
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFull()
{
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$fullMessage = new ChargerFullMessage();
$fullMessage->setStartTime($this->startTime);
$fullMessage->setPurchaseTime($this->order->getPurchaseTime());
$fullMessage->setEndTime($this->endTime);
$push = new Push();
$push->push($fullMessage, $this->order->getUserId());
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->writeRawTrace(1433, '口子脱落 --> 充满自停');
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toFinish()
{
$this->redis->hMSet($this->keyName, ['finishState' => $this->finishState, 'endTime' => $this->endTime]);
$finishMessage = new ChargerFinishMessage();
$finishMessage->setEndTime($this->endTime);
$finishMessage->setUserId($this->order->getUserId());
$finishMessage->setChargerTime($this->order->getPurchaseTime());
$finishMessage->setChargerPlc($this->order->getStationName() . '-' . $this->order->getPortName());
$finishMessage->setOrderAmount($this->order->getPayAmount());
$push = new Push();
try {
$push->push($finishMessage, $this->order->getUserId());
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->finishState,
'updated_at' => $this->endTime,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->endTime,
'state' => 1,
], 'id = ' . $this->order->getId());
// TODO: Implement toFinish() method.
}
public function toOffline()
{
$this->redis->hSet($this->keyName, 'offlineTime', time());
// TODO: Implement toOffline() method.
}
public function toOnline()
{
// TODO: Implement toOnline() method.
}
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/20
* Time: 下午4:19
*/
namespace Lib\cdz_order\state;
class Refund extends State
{
protected $selfStateCode = 3;
public function toCharger()
{
// TODO: Implement toCharger() method.
}
public function toPortShed()
{
// TODO: Implement toPortShed() method.
}
public function toPowerExceed()
{
// TODO: Implement toPowerExceed() method.
}
public function toFull()
{
// TODO: Implement toFull() method.
}
public function toFinish()
{
// TODO: Implement toFinish() method.
}
public function toOffline()
{
// TODO: Implement toOffline() method.
}
public function toOnline()
{
// TODO: Implement toOnline() method.
}
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
}
\ No newline at end of file
...@@ -12,6 +12,8 @@ namespace Lib\cdz_order\state; ...@@ -12,6 +12,8 @@ namespace Lib\cdz_order\state;
use Lib\api\message\Push; use Lib\api\message\Push;
use Lib\cdz_order\Order; use Lib\cdz_order\Order;
use Lib\cdz_order\order\SaveOrder; use Lib\cdz_order\order\SaveOrder;
use Lib\order_exception\Exception;
use Lib\order_exception\OrderTrace;
abstract class State abstract class State
{ {
...@@ -31,6 +33,12 @@ abstract class State ...@@ -31,6 +33,12 @@ abstract class State
'stateCode', 'stateCode',
]; ];
/**
* @var int
*/
protected $selfStateCode;
/** /**
* 订单状态hash前缀 * 订单状态hash前缀
*/ */
...@@ -50,7 +58,7 @@ abstract class State ...@@ -50,7 +58,7 @@ abstract class State
/** /**
* @var string * @var string
*/ */
protected $keyName= ''; protected $keyName = '';
/** /**
...@@ -100,15 +108,17 @@ abstract class State ...@@ -100,15 +108,17 @@ abstract class State
* @var int * @var int
*/ */
protected $finishState; protected $finishState;
/** /**
* State constructor. * State constructor.
* @param Order $order * @param Order $order
*/ */
public function __construct(Order $order,\Redis $redis) { public function __construct(Order $order, \Redis $redis)
{
$this->order = $order; $this->order = $order;
$this->keyName = self::ORDER_HASH_PREFIX.$order->getId(); $this->keyName = self::ORDER_HASH_PREFIX . $order->getId();
$this->redis = $redis; $this->redis = $redis;
...@@ -116,16 +126,18 @@ abstract class State ...@@ -116,16 +126,18 @@ abstract class State
$orderCache = $order->getOrderCache($redis); $orderCache = $order->getOrderCache($redis);
if(is_array($orderCache)) if (is_array($orderCache)) {
{
foreach ($orderCache as $key => $value) {
if (isset($value) === true) {
if (property_exists($this, $key) === true) {
if($key == 'startTime')
{
$value = date('Y-m-d H:i:s',$value);
foreach ($orderCache as $key => $value) }
{ $this->$key = $value;
if(isset($value) === true)
{
if(property_exists($this,$key) === true)
{
$this->$key =$value;
} }
} }
} }
...@@ -148,6 +160,20 @@ abstract class State ...@@ -148,6 +160,20 @@ abstract class State
abstract public function toOnline(); abstract public function toOnline();
abstract public function toFinishByUser();
abstract public function toFinishShed();
/**
*删除订单redis副本
*/
public function destroy()
{
$this->redis->del($this->keyName);
}
/** /**
* @return Order * @return Order
*/ */
...@@ -340,10 +366,70 @@ abstract class State ...@@ -340,10 +366,70 @@ abstract class State
$this->finishState = $finishState; $this->finishState = $finishState;
} }
protected function orderTrace(int $state,string $desc = '')
{
$desc = isset(Exception::STATE_MESSAGE[$state]) ? Exception::STATE_MESSAGE[$state] : $desc;
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
}
/**
* @return int
*/
public function getSelfStateCode(): int
{
return $this->selfStateCode;
}
/**
* @param int $selfStateCode
*/
public function setSelfStateCode(int $selfStateCode): void
{
$this->selfStateCode = $selfStateCode;
}
}
}
\ No newline at end of file
...@@ -9,18 +9,42 @@ ...@@ -9,18 +9,42 @@
namespace Lib\cdz_order\state; namespace Lib\cdz_order\state;
use Application\Model\OrderModel;
use Lib\api\message\ChargerRefundMessage;
use Lib\api\message\ChargerStartMessage;
use Lib\api\message\Push;
use Lib\cdz_order\Order; use Lib\cdz_order\Order;
use Lib\Log;
use Lib\order_exception\OrderTrace;
use Lib\port\Port;
class WaitingCharger extends State class WaitingCharger extends State
{ {
/**
* @var int
*/
protected $selfStateCode = -1;
public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); } public function __construct(Order $order, \Redis $redis) { parent::__construct($order, $redis); }
public function toFinishByUser()
{
// TODO: Implement toFinishByUser() method.
}
public function toFinishShed()
{
// TODO: Implement toFinishShed() method.
}
/** /**
* 订单从等待充电转为开始充电状态 * 订单从等待充电转为开始充电状态
* @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public function toCharger():void public function toCharger(): void
{ {
$this->setStartTime(time()); $this->setStartTime(time());
...@@ -29,13 +53,44 @@ class WaitingCharger extends State ...@@ -29,13 +53,44 @@ class WaitingCharger extends State
$this->saveOrder->pipe(); $this->saveOrder->pipe();
$this->saveOrder->saveStateCode();
$this->saveOrder->saveStartTime(); $this->saveOrder->saveStartTime();
$this->saveOrder->saveStateCode(); $this->saveOrder->saveStateCode();
$this->saveOrder->exec(); $this->saveOrder->exec();
$this->order->setCurrentState(new Charger($this->order,$this->redis)); $orderModel = new OrderModel();
$orderModel->update(['start_time' => date('Y-m-d H:i:s')], 'id = ' . $this->order->getId());
$charger = new Charger($this->order, $this->redis);
$this->order->setCurrentState($charger);
$port = new Port();
$portId = $this->order->getPortId();
$port->setPortState([$portId => 12]);
$orderTrace = new OrderTrace();
$orderTrace->write($this, $charger);
$pushMessage = new ChargerStartMessage();
$pushMessage->setAmount(number_format($this->order->getPayAmount() / 100, 2));
$pushMessage->setAddress($this->order->getStationName() . '-' . $this->order->getPortName() . '号');
$pushMessage->setRemark($pushMessage->getRemark() . '(' . strtoupper(sprintf('%012x', $this->order->getId())) . ')');
$push = new Push();
$push->push($pushMessage, $this->order->getUserId());
} }
...@@ -45,7 +100,6 @@ class WaitingCharger extends State ...@@ -45,7 +100,6 @@ class WaitingCharger extends State
*/ */
public function toPortShed() public function toPortShed()
{ {
throw new \Exception(' waiting state can not change to port shed');
// TODO: Implement toPortShed() method. // TODO: Implement toPortShed() method.
} }
...@@ -71,9 +125,33 @@ class WaitingCharger extends State ...@@ -71,9 +125,33 @@ class WaitingCharger extends State
// TODO: Implement toFull() method. // TODO: Implement toFull() method.
} }
/**
* @throws \GuzzleHttp\Exception\GuzzleException
* 3分钟退款
*/
public function toFinish() public function toFinish()
{ {
$refundMessage = new ChargerRefundMessage();
$refundMessage->setOutTradeNo($this->order->getOutTradeNo());
$refundMessage->setOrderInfo($this->order->getStationName() . '-' . $this->order->getPortName());
$refundMessage->setOrderFee(number_format($this->order->getPayAmount() / 100, 2));
$refundMessage->setRemark($refundMessage->getRemark() . ' (' . $this->order->getOutTradeNo() . ')');
$push = new Push();
$push->push($refundMessage, $this->order->getUserId());
$orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->write($this, new Refund($this->order, $this->redis));
// TODO: Implement toFinish() method. // TODO: Implement toFinish() method.
} }
...@@ -86,7 +164,11 @@ class WaitingCharger extends State ...@@ -86,7 +164,11 @@ class WaitingCharger extends State
$this->saveOrder->savePortOfflineTime(); $this->saveOrder->savePortOfflineTime();
$this->order->setCurrentState(new Offline($this->order,$this->redis)); $orderTrace = new OrderTrace();
$orderTrace->setOrderId($this->order->getId());
$orderTrace->write($this, $this->order->getCurrentState());
} }
......
...@@ -13,7 +13,7 @@ class Exception ...@@ -13,7 +13,7 @@ class Exception
{ {
const STATE_MESSAGE = [ const STATE_MESSAGE = [
'12' => '开始充电', '12' => '开始充电(充电中)',
'10' => '口子离线', '10' => '口子离线',
'11' => '口子上线', '11' => '口子上线',
'13' => '开始充电失败(异常)', '13' => '开始充电失败(异常)',
...@@ -26,6 +26,12 @@ class Exception ...@@ -26,6 +26,12 @@ class Exception
'98' => '更换充电口', '98' => '更换充电口',
'97' => '下发结束充电命令', '97' => '下发结束充电命令',
'96' => '订单更换口子成功', '96' => '订单更换口子成功',
'41' => '用户主动结束',
'42' => '插口脱落超时结束',
'-1' => '等待充电',
'3' => '超时退款',
'1' => '创建订单',
'43' => '脱落超时结束',
]; ];
......
...@@ -10,6 +10,7 @@ namespace Lib\order_exception; ...@@ -10,6 +10,7 @@ namespace Lib\order_exception;
use Application\Model\OrderExceptionModel; use Application\Model\OrderExceptionModel;
use Lib\cdz_order\state\State;
use Lib\Log; use Lib\Log;
class OrderTrace class OrderTrace
...@@ -17,7 +18,7 @@ class OrderTrace ...@@ -17,7 +18,7 @@ class OrderTrace
/** /**
* @var string * @var string
*/ */
private $orderId =''; private $orderId = '';
/** /**
* @var string * @var string
...@@ -34,7 +35,8 @@ class OrderTrace ...@@ -34,7 +35,8 @@ class OrderTrace
* OrderTrace constructor. * OrderTrace constructor.
* @param string $state * @param string $state
*/ */
public function __construct($state = '') { public function __construct($state = '')
{
$this->orderExceptionModel = new OrderExceptionModel(); $this->orderExceptionModel = new OrderExceptionModel();
...@@ -68,22 +70,20 @@ class OrderTrace ...@@ -68,22 +70,20 @@ class OrderTrace
public function writeTrace() public function writeTrace()
{ {
if (!isset(Exception::STATE_MESSAGE[$this->state])) if (!isset(Exception::STATE_MESSAGE[$this->state])) {
{ Log::error('记录订单异常无次state' . "($this->state)");
Log::error('记录订单异常无次state'."($this->state)");
return ; return;
} }
try{ try {
$this->orderExceptionModel->insertRow([ $this->orderExceptionModel->insertRow([
'state' => $this->state, 'state' => $this->state,
'desc' => Exception::STATE_MESSAGE[$this->state], 'desc' => Exception::STATE_MESSAGE[$this->state],
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
'order_id' => $this->orderId, 'order_id' => $this->orderId,
]); ]);
}catch (\Exception $exception) } catch (\Exception $exception) {
{
Log::error($exception->getMessage()); Log::error($exception->getMessage());
...@@ -96,23 +96,47 @@ class OrderTrace ...@@ -96,23 +96,47 @@ class OrderTrace
* @param int $state * @param int $state
* @param string $message * @param string $message
*/ */
public function writeRawTrace(int $state,string $message) public function writeRawTrace(int $state, string $message)
{ {
try{ try {
$this->orderExceptionModel->insertRow([ $this->orderExceptionModel->insertRow([
'state' => $state, 'state' => $state,
'desc' => $message, 'desc' => $message,
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
'order_id' => $this->orderId 'order_id' => $this->orderId,
]); ]);
}catch (\Exception $exception) } catch (\Exception $exception) {
{
Log::error($exception->getMessage()); Log::error($exception->getMessage());
} }
} }
/**
* 根据状态模式写入订单轨迹
* @param int $lastState
* @param int $currentState
*/
public function write(State $lastState, State $currentState)
{
$lastState = $lastState->getSelfStateCode();
$currentState = $currentState->getSelfStateCode();
$this->orderExceptionModel->insertRow([
'state' => (int)($lastState . $currentState),
'desc' => Exception::STATE_MESSAGE[$lastState] . '-->' . Exception::STATE_MESSAGE[$currentState],
'created_at' => date('Y-m-d H:i:s'),
'order_id' => $this->orderId,
]);
}
} }
......
...@@ -345,10 +345,6 @@ class Port ...@@ -345,10 +345,6 @@ class Port
$result = $redis->hGet(RedisKey::PORT_USING_ORDER_ID,$portId); $result = $redis->hGet(RedisKey::PORT_USING_ORDER_ID,$portId);
var_dump('using order');
var_dump($result);
return is_string($result) ? $result : 0; return is_string($result) ? $result : 0;
} }
...@@ -359,8 +355,6 @@ class Port ...@@ -359,8 +355,6 @@ class Port
$result = $redis->hMGet(RedisKey::PORT_USING_ORDER_ID,$portsId); $result = $redis->hMGet(RedisKey::PORT_USING_ORDER_ID,$portsId);
var_dump($result);
return $result; return $result;
} }
......
...@@ -79,8 +79,6 @@ class Send ...@@ -79,8 +79,6 @@ class Send
} }
/** /**
* 发送至下位机 * 发送至下位机
* @return bool * @return bool
...@@ -93,15 +91,12 @@ class Send ...@@ -93,15 +91,12 @@ class Send
$message = $this->makeMessage(); $message = $this->makeMessage();
Log::debug('send messag'.$message); Log::debug('send message' . $message);
if (strlen($message) % 2 === 1) {
Log::error('length wrong- ' . $message);
if(strlen($message)%2 === 1)
{
Log::error('length wrong- '.$message);
} }
//debug
if (Gateway::isUidOnline($clientUid) === 1) { if (Gateway::isUidOnline($clientUid) === 1) {
......
...@@ -32,8 +32,16 @@ class StopChargerCommand extends Command ...@@ -32,8 +32,16 @@ class StopChargerCommand extends Command
private $gatewayMac; private $gatewayMac;
/**
* @var string
*/
protected $headerCtrl = '05'; protected $headerCtrl = '05';
/**
* @var int
*/
protected $finishType = 0;
public function getMessageBody(): string public function getMessageBody(): string
{ {
...@@ -43,13 +51,18 @@ class StopChargerCommand extends Command ...@@ -43,13 +51,18 @@ class StopChargerCommand extends Command
$orderId = sprintf('%012x', $this->orderId); $orderId = sprintf('%012x', $this->orderId);
return $this->messageBody = $logicNum . $orderId; $finishType = sprintf('%02x', $this->finishType);
return $this->messageBody = $logicNum . $orderId . $finishType;
} }
public function getClientId(): string public function getClientId(): string
{ {
return $this->clientId;
// TODO: Implement getClientId() method. // TODO: Implement getClientId() method.
} }
...@@ -123,5 +136,38 @@ class StopChargerCommand extends Command ...@@ -123,5 +136,38 @@ class StopChargerCommand extends Command
$this->headerCtrl = $headerCtrl; $this->headerCtrl = $headerCtrl;
} }
/**
* @return int
*/
public function getFinishType(): int
{
return $this->finishType;
}
/**
* @param int $finishType
*/
public function setFinishType(int $finishType): void
{
$this->finishType = $finishType;
}
/**
* @param string $clientId
*/
public function setClientId(string $clientId): void
{
$this->clientId = $clientId;
}
/**
* @param string $clientUid
*/
public function setClientUid(string $clientUid): void
{
$this->clientUid = $clientUid;
}
} }
\ No newline at end of file
...@@ -13,6 +13,8 @@ class StateCode ...@@ -13,6 +13,8 @@ class StateCode
{ {
const WAITING = -1;
const PORT_OFFLINE = 10; const PORT_OFFLINE = 10;
const PORT_ONLINE = 11; const PORT_ONLINE = 11;
...@@ -43,6 +45,13 @@ class StateCode ...@@ -43,6 +45,13 @@ class StateCode
const FINISH_CHARGER = 36; const FINISH_CHARGER = 36;
const FINISH_USER = 41;
const FINISH_SHED_TIMEOUT = 42;
const FINISH_REFUND = 43;
......
...@@ -23,42 +23,50 @@ class Api ...@@ -23,42 +23,50 @@ class Api
// static::add('00','HeartController@heart',[new AuthMiddleware(),new HasMacMiddleware()]); // static::add('00','HeartController@heart',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('00','HeartController@heart'); static::add('00', 'HeartController@heart');
static::add('01','CertifyController@certify'); static::add('01', 'CertifyController@certify');
static::add('02','AccessController@access'); static::add('02', 'AccessController@access');
static::add('03','AccessController@syncPort',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('03', 'AccessController@syncPort', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('04','PowerController@charger',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('04', 'PowerController@charger', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('05','PowerController@stop',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('06', 'AmmeterController@recordd', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('08','PortController@online',[new AuthMiddleware(),new HasMacMiddleware()]); //debug
// static::add('05','PowerController@stop',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('09','PortController@offline',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('05', 'PowerController@finish', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('07','PowerController@power',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('08', 'PortController@online', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('17','PowerController@onePower',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('09', 'PortController@offline', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('0B','PortController@portState',[new AuthMiddleware(),new HasMacMiddleware()]); static::add('07', 'PowerController@power', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('0A','SettingController@setting'); static::add('17', 'PowerController@onePower', [new AuthMiddleware(), new HasMacMiddleware()]);
//static::add('0B','PortController@portState',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('0B', 'PortStateController@change', [new AuthMiddleware(), new HasMacMiddleware()]);
static::add('0A', 'SettingController@setting');
static::add('0F', 'PowerOffController@powerOff', [new AuthMiddleware(), new HasMacMiddleware()]);
} }
public static function add(string $command, string $controllerAction,array $middleware =[]) public static function add(string $command, string $controllerAction, array $middleware = [])
{ {
$route = new Route(); $route = new Route();
$route->setCommand($command); $route->setCommand($command);
$controllerAction = explode('@',$controllerAction); $controllerAction = explode('@', $controllerAction);
$route->setController($controllerAction[0]); $route->setController($controllerAction[0]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment