Commit 0bcb2a88 by banice

修复了断电续充和更换口子

parent df104c15
......@@ -117,27 +117,28 @@ class Events
*/
public static function onClose($clientId)
{
Log::info('client close', [
'client_id' => $clientId,
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : ''
]);
$gatewayId = isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '';
$stationId = isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '';
if ($gatewayId === '' || $stationId === '') {
$stationName = isset($_SESSION['station_name']) ? $_SESSION['station_name'] : '';
$gatewayName = isset($_SESSION['gateway_name']) ? $_SESSION['gateway_name'] : '';
if ($gatewayId === '' || $stationId === '' || $gatewayName === '' || $stationName === '') {
Log::error('client close can not find station_id or gateway_id');
return;
}
Log::info('gateway offline', [
'gateway_id' => $gatewayId,
'station_id' => $stationId
]);
$exceptionModel = new \Application\model\ExceptionModel();
if (isset($_SESSION['station_id']) && isset($_SESSION['gateway_id'])) {
$exceptionModel->insertRow([
'message' => '网关离线',
'gateway_id' => $_SESSION['gateway_id'],
'station_id' => $_SESSION['station_id'],
'gateway_id' => $gatewayId,
'station_id' => $stationId,
'type' => 1,
]);
}
......@@ -161,6 +162,8 @@ class Events
$exceptionModel = new \Application\model\ExceptionModel();
$orderExceptionModel = new \Application\model\OrderExceptionModel();
$date = date('Y-m-d H:i:s');
......@@ -175,15 +178,17 @@ class Events
$exceptionModel->insertRow([
'gateway_id' => $gatewayId,
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'station_id' => $stationId,
'port_id' => $portId,
'message' => '口子离线',
'created_at' => $date,
'type' => 1,
]);
$portUsingOrderId = $port->getPortUsingOrderId();
//端口当前订单记录离线时间
if ($portUsingOrderId != false && $portUsingOrderId != '') {
$order = new \Lib\cdz\order\Order($portUsingOrderId);
if ($order->getOrderCache(static::$redis) != false) {
......@@ -192,11 +197,30 @@ class Events
$saveOrder->save(['offlineTime']);
unset($saveOrder);
$orderExceptionModel->insertRow([
'order_id' => $order->getId(),
'state' => \Lib\StateCode::PORT_OFFLINE,
'created_at' => date('Y-m-d H:i:s'),
'desc' => '网关离线,订单离线',
]);
}
}
}
$broadcastToAgent = new \Src\BroadcastToAgent();
$broadcastToAgent->broadcast([
'notice_exception_code' => 15,
'gateway_id' => $gatewayId,
'station_id' => $stationId,
'station_name' => $stationName,
'gateway_mac' => isset($_SESSION['gateway_mac']) ? $_SESSION['gateway_mac'] : '',
]);
}
......
......@@ -35,12 +35,13 @@ class HeartController extends Controller
$time = time();
$redis = \Events::getRedis();
if (isset($_SESSION['gateway_id'])) {
$redis->hSet(self::GATEWAY_LAST_HEART_TIME, $_SESSION['gateway_id'], $time);
$redis->hSet(self::GATEWAY_LAST_HEART_TIME,$_SESSION['gateway_id'],$time);
$redis->zIncrBy(self::GATEWAY_HEART_TIMES, 1, $_SESSION['gateway_id']);
}
$redis->zIncrBy(self::GATEWAY_HEART_TIMES,1,$_SESSION['gateway_id']);
$this->response->send('00',sprintf('%08x',time()));
$this->response->send('00', sprintf('%08x', time()));
}
}
\ No newline at end of file
......@@ -68,6 +68,7 @@ class AccessController extends Controller
$gatewayModelResult = $gatewayModel->getColumns([
'station_id',
'id',
'name'
], 'mac_address = ' . "'$mac'");
if ($gatewayModelResult == false) {
......@@ -78,20 +79,30 @@ class AccessController extends Controller
$gatewayId = $gatewayModelResult['id'];
$_SESSION['gateway_name'] = $gatewayModelResult['name'];
//日志记录
$this->logRecord->setGatewayId($gatewayId);
$this->logRecord->setStationId($stationId);
$stationModel = new StationModel();
$stationData = $stationModel->getColumns([
'park_id',
'name',
], 'id = ' . $stationId);
if (isset($stationData['park_id']) && isset($stationData['name'])) {
$parkId = $stationData['park_id'];
$_SESSION['station_name'] = $stationData['name'];
if (isset($parkId)) {
$parkModel = new ParkModel();
$_SESSION['park_id'] = $parkId;
$agentId = $parkModel->getColumn('agent_id', 'id = ' . $parkId);
if (isset($agentId)) {
$_SESSION['agent_id'] = $agentId;
}
$parkId = $stationModel->getColumn('park_id', 'id = ' . $stationId);
if (isset($parkId)) {
$parkModel = new ParkModel();
$_SESSION['park_id'] = $parkId;
$agentId = $parkModel->getColumn('agent_id', 'id = ' . $parkId);
if (isset($agentId)) {
$_SESSION['agent_id'] = $agentId;
}
}
......@@ -103,16 +114,19 @@ class AccessController extends Controller
'id',
'device_mac',
'name',
], 'gateway_id = ' . $gatewayId);
$tmp = [];
$portsIdName = [];
foreach ($deviceModelResult as $item) {
$tmp[$item['device_mac']] = $item['id'];
$portsIdName[$item['id']] = $item['name'];
}
$_SESSION['port_id_name'] = $portsIdName;
$_SESSION['port_mac_id'] = $tmp;
$_SESSION['port_id_mac'] = array_flip($tmp);
......@@ -159,12 +173,14 @@ class AccessController extends Controller
for ($i = 0; $i < $count; $i++) {
$mac = substr($this->message, $this->plc, 8 * 2);
$state = substr($this->message, $this->plc + ($count * 16 - 16 * $i + $i * 2), 2);
$this->plc += 16;
$state = substr($this->message, $this->plc, 2);
$portMacState[$mac] = hexdec($state);
// $portMacState[$mac] = StateCode::PORT_OFFLINE;
}
if (isset($_SESSION['port_mac_id']) == false) {
......@@ -178,8 +194,6 @@ class AccessController extends Controller
$port = new Port();
$portsLogicId = [];
// $portsId = array_values($portsMacId);
$portsId = [];
foreach ($portMacState as $mac => $state) {
......@@ -188,12 +202,15 @@ class AccessController extends Controller
}
}
$logicStartTmp = $logicStart;
foreach ($portMacState as $mac => $state) {
$portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false;
if ($portId === false) {
continue;
}
$port->setPortId($portId);
//设置端口状态
......@@ -202,7 +219,7 @@ class AccessController extends Controller
$port->setStationId($_SESSION['station_id']);
$port->updatePortState();
$portsLogicId[$logicStart++] = $portId;
$portsLogicId[$logicStartTmp] = $portId;
}
\Events::exec();
......@@ -215,14 +232,14 @@ class AccessController extends Controller
}
//更新逻辑地址
foreach ($portsLogicId as $portId => $logicId) {
foreach ($portsLogicId as $logicId => $portId) {
$deviceModel->update(['logic_num' => $logicId], 'id = ' . $portId);
}
$order = new Order();
$responseOrder = [];
$offlineReason = hexdec(substr($this->message, $this->plc, 2));
$offlineReason = hexdec(substr($this->message, -4, 2));
$orderCount = '00';
......@@ -240,27 +257,23 @@ class AccessController extends Controller
if ($order->getOrderCache(\Events::getRedis()) == false) {
continue;
}
$order->setOnlineTime(time());
if ($offlineReason === 1) {
if ($offlineReason == 1) {
$orderState = new OfflinePowerState($order);
} else {
$orderState = new OfflineNetState($order);
}
$portState = $portMacState[$_SESSION['port_id_mac'][$portId]];
$remainTime = $orderState->getRemainChargerTime();
if ($remainTime > 0) {
$logicNum = sprintf('%02x', $order->getPortLogicId());
$orderId = sprintf('%012x', $order->getId());
$time = sprintf('%04x', $remainTime);
array_push($responseOrder, $logicNum . $orderId . $time);
}
......
......@@ -63,6 +63,11 @@ class OverController extends Controller
$orderStateCode = $order->getStateCode();
$order->setEndCapacity($capacity);
$order->setEndCode($state);
$portId = isset($_SESSION['port_logic_id'][$logicId]) ? $_SESSION['port_logic_id'][$logicId] : 0;
if ($portId !== 0) {
$this->logRecord->setPortId($portId);
}
if (Gateway::isUidOnline($order->getUserId()) === 1) {
Gateway::sendToUid($order->getUserId(), json_encode(['code' => 10000]));
......@@ -86,6 +91,22 @@ class OverController extends Controller
if (isset($orderState)) {
switch ($state) {
case StateCode::PORT_EXCEPTION://口子打开失败,适配zegbee设备
$this->logRecord->setDesc('口子打开失败结束');
$port = new Port($order->getPortId());
$port->setStationId($order->getStationId());
$port->setGatewayId($order->getGatewayId());
$port->freePort($orderId);
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s'),
], 'id = ' . $order->getId());
break;
case StateCode::FINISH_USER: //用户主动结束
$this->logRecord->setDesc('用户主动结束充电');
$orderState->toUserFinish();
......@@ -132,7 +153,7 @@ class OverController extends Controller
$this->logRecord->setDesc('更换口子,' . $portId . '关闭成功');
}
$this->response->send('05', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
$this->response->send('15', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
return;
......@@ -157,7 +178,7 @@ class OverController extends Controller
}
$this->response->send('05', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
$this->response->send('15', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
}
......
......@@ -104,7 +104,7 @@ class StartController extends Controller
}
}
$this->response->send('04', sprintf('%012x', $orderId));
$this->response->send('14', sprintf('%012x', $orderId));
}
......
......@@ -11,8 +11,10 @@ namespace Application\controller\business\port;
use Application\controller\Controller;
use Application\model\ExceptionModel;
use Lib\log\Log;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\BroadcastToAgent;
class OfflineController extends Controller
{
......@@ -37,20 +39,28 @@ class OfflineController extends Controller
public function offline()
{
$count = $this->request->get('count');
$count = hexdec($this->request->get('count'));
$portsLogicMac = [];
$str = substr($this->message, $this->plc);
$portsLogic = substr($this->message, $this->plc, $count * 2);
$tmp = str_split($str, 18);
foreach ($tmp as $item) {
$portsLogic = str_split($portsLogic, 2);
$this->plc += ($count * 2);
$str = substr($this->message, $this->plc, -2);
$tmp = str_split($str, 16);
for ($i = 0; $i < $count; $i++) {
$portsLogicMac[hexdec($portsLogic[$i])] = $tmp[$i];
$portsLogicMac[hexdec(substr($item, 0, 2))] = substr($item, 2);
}
if (isset($_SESSION['port_mac_id'])) {
if (!isset($_SESSION['port_mac_id'])) {
return;
}
......@@ -60,13 +70,20 @@ class OfflineController extends Controller
$exceptionModel = new ExceptionModel();
if (!(isset($_SESSION['station_id']) && isset($_SESSION['gateway_id']))) {
if (!(isset($_SESSION['station_id']) || !isset($_SESSION['gateway_id']))) {
return;
}
$broadcastToAgent = new BroadcastToAgent();
$offlinePorts = [];
foreach ($portsLogicMac as $portLogic => $portMac) {
if (isset($portsMacId[$portMac])) {
$portId = $portsMacId[$portMac];
if (isset($_SESSION['port_id_name'][$portId])) {
array_push($offlinePorts, ['name' => $_SESSION['port_id_name'][$portId], 'id' => $portId]);
}
Log::debug('offline port_id' . $portId);
$port->setPortId($portId);
$port->setStationId($_SESSION['station_id']);
$port->setGatewayId($_SESSION['gateway_id']);
......@@ -76,13 +93,24 @@ class OfflineController extends Controller
$exceptionModel->insertRow([
'message' => '口子离线',
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['stationg_id']) ? $_SESSION['station_id'] : '',
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'port_id' => $portId,
'created_at' => date('Y-m-d H:i:s'),
'type' => 1,
]);
}
}
$broadcastToAgent->broadcast([
'notice_exception_code' => StateCode::PORT_OFFLINE,
'ports' => $offlinePorts,
'station_name' => $_SESSION['station_name'],
'gateway_name' => $_SESSION['gateway_name'],
'station_id' => $_SESSION['station_id'],
'gateway_id' => $_SESSION['gateway_id'],
]);
}
}
\ No newline at end of file
......@@ -33,23 +33,30 @@ class OnlineController extends Controller
*/
public function online()
{
$count = $this->request->get('count');
$count = hexdec($this->request->get('count'));
$portsLogicMac = [];
$str = substr($this->message, $this->plc);
$portsLogic = substr($this->message, $this->plc, $count * 2);
$tmp = str_split($str, 18);
foreach ($tmp as $item) {
$portsLogic = str_split($portsLogic, 2);
$this->plc += ($count * 2);
$str = substr($this->message, $this->plc, -2);
$tmp = str_split($str, 16);
for ($i = 0; $i < $count; $i++) {
$portsLogicMac[hexdec($portsLogic[$i])] = $tmp[$i];
$portsLogicMac[hexdec(substr($item, 0, 2))] = substr($item, 2);
}
if (isset($_SESSION['port_mac_id']) === false) {
if (!isset($_SESSION['port_mac_id'])) {
return;
return;
}
$portsMacId = $_SESSION['port_mac_id'];
......
......@@ -105,7 +105,7 @@ class StateController extends Controller
break;
default:
break;
return;
}
Log::debug('now state' . $state);
......
......@@ -109,14 +109,15 @@ class PowerController extends Controller
$logicId = hexdec($this->request->get('logic_id'));
$startTime = hexdec($this->request->get('start_time'));
$intervalTime = hexdec($this->request->get('interval_time')) * 60;
$count = $this->request->get('count');
$count = hexdec($this->request->get('count'));
$powers = substr($this->message, $this->plc, $count * 4);
$this->plc += ($count * 4);
$capacitys = substr($this->message, $this->plc, $count * 8);
$powers = explode($powers, 4);
$capacitys = explode($capacitys, 8);
$powers = str_split($powers, 4);
$capacitys = str_split($capacitys, 8);
if (count($powers) != $count || count($capacitys) != $count) {
Log::error('plural miss power or capacity,less than count ');
......@@ -124,11 +125,6 @@ class PowerController extends Controller
return;
}
if (isset($_SESSION['port_id_logic']) == false) {
return;
}
$portsLogicId = array_flip($_SESSION['port_logic_id']);
if (isset($portsLogicId[$logicId])) {
......
......@@ -18,7 +18,7 @@ class BalanceController extends Controller
'32' => [
'card_id' => 6
'card_id' => 3
]
];
......@@ -36,7 +36,7 @@ class BalanceController extends Controller
$balance = isset($balance) ? $balance : 0;
$balance = sprintf('%04x', $balance);
$balance = sprintf('%08x', $balance);
var_dump($balance);
......
......@@ -13,6 +13,7 @@ use Application\controller\Controller;
use Application\model\BillTmpModel;
use GatewayWorker\Lib\Gateway;
use Lib\log\Log;
class PayRuleController extends Controller
{
......@@ -36,8 +37,12 @@ class PayRuleController extends Controller
Gateway::closeClient($this->clientId);
}
Log::debug($_SESSION['station_id']);
$rule = $billingTmp->getColumn('context', 'id = ' . $_SESSION['station_id']);
Log::debug($rule);
if (isset($rule)) {
$rule = json_decode($rule, true);
......@@ -46,11 +51,10 @@ class PayRuleController extends Controller
$count = count($rule);
foreach ($rule as $item) {
$time = sprintf('%02x', $item['time']);
$price = sprintf('%02x', $item['price']);
$time = sprintf('%04x', $item['time']);
$price = sprintf('%04x', $item['price']);
array_push($back, $time . $price);
}
$this->response->send('31', sprintf('%02x', $count) . implode($back));
}
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 下午4:30
*/
namespace Application\controller\card\charger;
use Application\controller\Controller;
use Lib\cdz\order\Order;
use Lib\cdz_order\state\ChargerState;
use Lib\cdz_order\state\ShedState;
use Lib\cdz_order\state\WaitingState;
use Lib\StateCode;
use Lib\Tool;
class OverController extends Controller
{
protected $parameterMap = [
'35' => [
'type' => 1,
'logic_id' => 1,
'state' => 1,
'order_id' => 6,
'capacity' => 4,
]
];
/**
* 结束充电
*/
public function over()
{
$orderId = hexdec($this->request->get('order_id'));
$state = hexdec($this->request->get('state'));
$capacity = hexdec($this->request->get('capacity'));
$order = new Order($orderId);
$order->setEndCapacity($capacity);
$order->setEndCode($state);
$this->logRecord->setOrderId($orderId);
if ($order->getOrderCache(\Events::getRedis()) !== false) {
$orderStateCode = $order->getStateCode();
$this->logRecord->setPortId($order->getPortId());
switch ($orderStateCode) {
case StateCode::PORT_CHARGER:
$orderState = new ChargerState($order);
break;
case StateCode::PORT_SHED:
$orderState = new ShedState($order);
break;
case StateCode::WAITING:
$orderState = new WaitingState($order);
break;
default:
break;
}
if (isset($orderState)) {
switch ($state) {
case StateCode::FINISH_TIME_END:
$orderState->toTimeoutState();
break;
case StateCode::FINISH_SHED_TIMEOUT:
$orderState->toShedTimeoutState();
break;
case StateCode::FINISH_EXCEED:
$orderState->toPowerExceedState();
break;
default:
break;
}
}
}
$this->response->send('45', Tool::padding($orderId, 6));
}
}
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 下午4:29
*/
namespace Application\controller\card\charger;
use Application\controller\Controller;
use Lib\log\Log;
use Lib\station\gateway\port\Port;
use Application\model\OrderPowerModel;
class PowerController extends Controller
{
protected $parameterMap = [
'17' => [
'logic_id' => 1,
'power' => 2,
'capacity' => 4,
],
];
public function power()
{
$logicId = hexdec($this->request->get('logic_id'));
$startTime = hexdec($this->request->get('start_time'));
$intervalTime = hexdec($this->request->get('interval_time')) * 60;
$count = $this->request->get('count');
$powers = substr($this->message, $this->plc, $count * 4);
$this->plc += ($count * 4);
$capacitys = substr($this->message, $this->plc, $count * 8);
$powers = explode($powers, 4);
$capacitys = explode($capacitys, 8);
if (count($powers) != $count || count($capacitys) != $count) {
Log::error('plural miss power or capacity,less than count ');
return;
}
if (isset($_SESSION['port_id_logic']) == false) {
return;
}
$portsLogicId = array_flip($_SESSION['port_logic_id']);
if (isset($portsLogicId[$logicId])) {
$portId = $portsLogicId[$logicId];
$port = new Port($portId);
$orderId = $port->getPortUsingOrderId();
if ($orderId == false || $orderId == '') {
Log::error('power data can not find order_id', [
'logic_id' => $logicId,
'message' => $this->message,
]);
return;
}
$this->logRecord->setPortId($portId);
$this->logRecord->setOrderId($orderId);
$orderPowerModel = new OrderPowerModel();
for ($i = 0; $i < $count; $i++) {
$orderPowerModel->insertRow([
'created_at' => date('Y-m-d H:i:s', $startTime),
'order_id' => $orderId,
'port_id' => $portId,
'power' => $powers[$i],
'capacity' => $capacitys[$i],
]);
$startTime += $intervalTime;
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 上午9:48
*/
namespace Application\controller\card\charger;
use Application\controller\Controller;
use Application\model\CardModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
use Lib\Tool;
class RequestController extends Controller
{
protected $parameterMap = [
'33' => [
'type' => 1,
'logic_id' => 1,
'card_id' => 3,
'time' => 2,
'price' => 2,
],
];
/**
* 请求充电
*/
public function request()
{
$type = $this->request->get('type');
$logicId = hexdec($this->request->get('logic_id'));
$cardId = $this->request->get('card_id');
$time = hexdec($this->request->get('time'));
$price = hexdec($this->request->get('price'));
if ((isset($_SESSION['gateway_id']) && isset($_SESSION['station_id']) && isset($_SESSION['station_name'])) === false) {
Log::debug('miss gateway)id');
return;
}
if (isset($_SESSION['port_logic_id'])) {
Log::debug('port_logic_id', $_SESSION['port_logic_id']);
if (isset($_SESSION['port_logic_id'][$logicId])) {
$portId = $_SESSION['port_logic_id'][$logicId];
}
}
if (isset($portId) === false) {
return;
}
$cardModel = new CardModel();
$balance = $cardModel->getColumn('balance', 'card_number = ' . $cardId);
Log::debug($balance);
if (isset($balance) === false || $balance == '') {
return;
}
if ((int)$balance < (int)$price) {
$orderId = 0;
} else {
//添加订单记录.
$stationId = $_SESSION['station_id'];
$gatewayId = $_SESSION['gateway_id'];
$stationName = $_SESSION['station_name'];
$date = date('Y-m-d H:i:s');
$orderModel = new OrderModel();
$outTradeNo = '3' . time() . $stationId . $portId . $gatewayId;
$orderModel->insertRow([
'gateway_id' => $gatewayId,
'station_id' => $stationId,
'port_id' => $portId,
'out_trade_no' => $outTradeNo,
'pay_amount' => $price,
'charger_time' => $time,
'created_at' => $date,
'logic_num' => $logicId,
'pay_type' => 3,
'station_name' => $stationName,
]);
$orderId = $orderModel->getColumn('id', 'out_trade_no = ' . $outTradeNo);
}
$this->response->send('33', $type . Tool::padding($logicId, 1) . $cardId . Tool::padding($time, 2) . Tool::padding($price, 2) . Tool::padding($orderId, 6) . Tool::padding($balance, 4));
if ($orderId != 0) {
//todo 订单缓存存储
$order = new Order($orderId);
$order->setStationId($stationId);
$order->setGatewayId($gatewayId);
$order->setCardId($cardId);
$order->setStationId($stationId);
$order->setStationName($stationName);
$order->setCreatedTime(time());
$order->setPayAmount($price);
$order->setPurchaseTime($time);
$order->setPortLogicId($logicId);
$order->setStateCode(-1);
$saveOrder = new OrderSaveRedis($order);
$saveOrder->saveAll();
}
}
}
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 上午10:35
*/
namespace Application\controller\card\charger;
use Application\controller\Controller;
use Lib\cdz\order\Order;
use Lib\cdz_order\state\WaitingState;
use Lib\StateCode;
use Lib\Tool;
class StartController extends Controller
{
protected $parameterMap = [
'34' => [
'type' => 1,
'logic_id' => 1,
'state' => 1,
'order_id' => 6,
'capacity' => 4,
]
];
/**
* 开始充电
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function start()
{
$logicId = $this->request->get('logic_id');
$state = hexdec($this->request->get('state'));
$orderId = hexdec($this->request->get('order_id'));
$capacity = hexdec($this->request->get('capacity'));
$order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) {
$orderStateCode = $order->getStateCode();
$order->setStartCapacity($capacity);
$order->setStartTime(time());
$this->logRecord->setOrderId($orderId);
$this->logRecord->setPortId($order->getPortId());
switch ($orderStateCode) {
case StateCode::WAITING:
$orderState = new WaitingState($order);
break;
default:
return;
}
switch ($state) {
case StateCode::PORT_CHARGER:
$orderState->toChargerState();
break;
case StateCode::PORT_SHED:
$orderState->toShedState();
break;
case StateCode::PORT_EXCEPTION:
$orderState->toExceptionState();
break;
default:
break;
}
}
$this->response->send('34', Tool::padding($orderId, 6));
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 下午4:29
*/
namespace Application\controller\card\port;
class StateController
{
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ namespace Application\controller\setting;
use Application\controller\Controller;
use Application\model\ConfigModel;
use Application\model\StationModel;
use Lib\Gateway;
use Lib\log\Log;
class SettingController extends Controller
{
......@@ -41,6 +43,12 @@ class SettingController extends Controller
}
$invertalTime = $idConfigsValue['5']; //设备上报数据间隔单位 秒
if (isset($_SESSION['station_id']) === false) {
Gateway::closeClient($this->clientId);
Log::error('setting miss station_id');
return;
}
$stationId = $_SESSION['station_id'];
$stationModel = new StationModel();
......
......@@ -12,6 +12,6 @@ namespace Application\model;
class BillTmpModel extends Model
{
protected $table = 'billing_tmps';
protected $table = 'billing_tpls';
}
\ No newline at end of file
......@@ -11,31 +11,36 @@ class Api
public $route = [];
public function boot()
public function boot()
{
$this->add('01', 'accept\CertifyController@certify');
$this->add('00','HeartController@break');
$this->add('02','accept\AccessController@request');
$this->add('03','accept\AccessController@syncPort');
$this->add('00', 'HeartController@break');
$this->add('02', 'accept\AccessController@request');
$this->add('03', 'accept\AccessController@syncPort');
$this->add('04','business\charger\StartController@start');
$this->add('05','business\charger\OverController@over');
$this->add('04', 'business\charger\StartController@start');
$this->add('05', 'business\charger\OverController@over');
$this->add('0B','business\port\StateController@change');
$this->add('0B', 'business\port\StateController@change');
$this->add('07','business\power\PowerController@plural');
$this->add('17','business\power\PowerController@one');
$this->add('07', 'business\power\PowerController@plural');
$this->add('17', 'business\power\PowerController@one');
$this->add('06','business\power\CapacityController@capacity');
$this->add('06', 'business\power\CapacityController@capacity');
$this->add('08','business\port\OnlineController@online');
$this->add('09','business\port\OfflineController@offline');
$this->add('08', 'business\port\OnlineController@online');
$this->add('09', 'business\port\OfflineController@offline');
$this->add('0F','business\power\ShutdownController@shutdown');
$this->add('0A','setting\SettingController@get');
// $this->add('0F', 'business\power\ShutdownController@shutdown');
$this->add('0A', 'setting\SettingController@get');
$this->add('31', 'card\PayRuleController@get');
$this->add('32', 'card\BalanceController@get');
$this->add('33', 'card\charger\RequestController@request');
$this->add('34', 'card\charger\StartController@start');
$this->add('35', 'card\charger\OverController@over');
}
......@@ -48,7 +53,7 @@ class Api
* @param array $middlewar
* @return void
*/
public function add(string $command, string $controllerAction, array $middleware = [])
public function add(string $command, string $controllerAction, array $middleware = [])
{
$route = new Route();
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/29
* Time: 上午10:25
*/
namespace Lib;
class Tool
{
public static function padding($data, $length)
{
$length *= 2;
return sprintf('%0' . $length . 'x', $data);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:22
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class ExceptionState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
// TODO: Implement toChargerState() method.
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/20
* Time: 下午6:45
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\NoticePortShed;
use Lib\cdz\order\Order;
use Lib\log\Log;
class OfflineNetState extends State
{
use NoticePortShed;
/**
* @var int
*/
private $remainChargerTime = 0;
public function __construct(Order $order)
{
parent::__construct($order);
$startTime = $this->order->getStartTime();
if ($startTime === '' || $this->order->getOfflineTime() == '0') {
$this->remainChargerTime = (int)($this->order->getPurchaseTime());
} else {
$purchaseTime = $this->order->getPurchaseTime();
$this->remainChargerTime = $purchaseTime * 60 - ((int)$this->order->getOnlineTime() - $startTime);
Log::debug('remain charger time' . $this->remainChargerTime);
$this->remainChargerTime = (int)($this->remainChargerTime / 60);
}
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
$this->order->stop($this->order, $_SESSION['client_id'], 43);
return;
}
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
/**
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
*/
public function toShedState()
{
if ($this->remainChargerTime > 0) {
$this->publishPortShed($this->order);
}
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
/**
* @return int
*/
public function getRemainChargerTime(): int
{
return $this->remainChargerTime;
}
/**
* @param int $remainChargerTime
*/
public function setRemainChargerTime(int $remainChargerTime): void
{
$this->remainChargerTime = $remainChargerTime;
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/20
* Time: 下午6:45
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class OfflinePowerState extends State
{
/**
* @var int
*/
private $remainChargerTime = 0;
public function __construct(Order $order)
{
parent::__construct($order);
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0;
return;
}
$offlineTime = $this->order->getOfflineTime();
$startTime = $this->order->getStartTime();
if ($startTime === '' || $this->order->getOfflineTime() == '0') {
$this->remainChargerTime = (int)($this->order->getPurchaseTime());
} else {
$purchaseTime = $this->order->getPurchaseTime();
$this->remainChargerTime = $purchaseTime * 60 - ($offlineTime - $startTime);
$this->remainChargerTime = (int)($this->remainChargerTime / 60);
}
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0;
return;
}
$offlineTime = $this->order->getOfflineTime();
$startTime = $this->order->getStartTime();
$purchaseTime = $this->order->getPurchaseTime();
$this->remainChargerTime = $purchaseTime * 60 - ($offlineTime - $startTime);
$this->remainChargerTime = (int)($this->remainChargerTime / 60);
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
/**
* @return int
*/
public function getRemainChargerTime(): int
{
return $this->remainChargerTime;
}
/**
* @param int $remainChargerTime
*/
public function setRemainChargerTime(int $remainChargerTime): void
{
$this->remainChargerTime = $remainChargerTime;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:22
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class PowerExceedState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
// TODO: Implement toChargerState() method.
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:22
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class PowerFullState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
// TODO: Implement toChargerState() method.
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:21
*/
namespace Lib\cdz_order\state;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
use Lib\remote\api\message\ChargerReloadMessage;
use Lib\remote\api\message\ChargerShedTimeOutMessage;
use Lib\remote\api\message\ChargerTimeoutMessage;
use Lib\remote\api\message\FinishByUserMessage;
use Lib\remote\api\message\SendMessage;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
class ShedState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toUserFinish()
{
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => $this->order->getEndCode(),
'end_capacity' => $this->order->getEndCapacity(),
'updated_at' => $this->nowDate,
'end_time' => $this->nowDate,
'state' => 1,
], 'id = ' . $this->order->getId());
$port = new Port();
$port->setState(StateCode::PORT_ONLINE);
$port->setPortId($this->order->getPortId());
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
$port->freePort($this->order->getId());
//用户主动结束充电推送
$finishByUserMessage = new FinishByUserMessage();
$finishByUserMessage->setOrderId($this->order->getId());
$finishByUserMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName());
$finishByUserMessage->setUserId($this->order->getUserId());
$chargerTime = (int)time() - (int)$this->order->getStartTime();
$chargerTime /= 60;
$chargerTime = (int)$chargerTime;
$finishByUserMessage->setChargerTime($chargerTime);
$finishByUserMessage->setEndTime($this->nowDate);
$finishByUserMessage->setOrderAmount($this->order->getPayAmount());
$sendMessage = new SendMessage();
if ($sendMessage->request($finishByUserMessage) == false) {
Log::error('send finish by user message fail');
}
$orderException = new OrderExceptionModel();
$orderException->insertRow([
'order_id' => $this->order->getId(),
'state' => StateCode::PORT_SHED . StateCode::FINISH_USER,
'desc' => '用户主动结束充电',
'created_at' => $this->nowDate,
]);
}
public function toWaitingState()
{
Log::error('shed state can not change to waiting state');
}
/**
* 更新订单状态
* 更新口子状态
* 推送微信消息
* 记录订单异常
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toChargerState()
{
$this->order->setStateCode(StateCode::PORT_CHARGER);
$orderSaveRedis = new OrderSaveRedis($this->order);
$orderSaveRedis->save([
'stateCode',
]);
Log::debug('shed to charger');
$port = new Port($this->order->getPortId());
$port->setState(StateCode::PORT_CHARGER);
$port->updatePortState($this->order->getId());
$chargerReloadMessage = new ChargerReloadMessage();
$chargerReloadMessage->setUserId($this->order->getUserId());
$chargerReloadMessage->setOrderId($this->order->getId());
$chargerReloadMessage->setPortName($this->order->getPortName());
$chargerReloadMessage->setStationName($this->order->getStationName());
$sendMessage = new SendMessage();
if ($sendMessage->request($chargerReloadMessage) === false) {
Log::error('send order reload message fail');
}
$orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow(
[
'order_id' => $this->order->getId(),
'state' => StateCode::PORT_SHED . StateCode::PORT_CHARGER,
'created_at' => $this->nowDate,
'desc' => '订单口子脱落重新插上',
]
);
}
public function toExceptionState()
{
//todo
}
public function toPowerExceedState()
{
Log::error('shed state can not change to power exceed state');
}
public function toPowerFullState()
{
Log::error('shed state can not change to power full state');
}
public function toShedState()
{
Log::error('shed state can not change to shed');
}
/**
* 记录订单异常
* 释放口子
* 推送用户消息
* 更新订单记录,记录结束用电量
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toShedTimeoutState()
{
$orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([
'order_id' => $this->order->getId(),
'state' => StateCode::PORT_SHED . StateCode::FINISH_SHED_TIMEOUT,
'created_at' => $this->nowDate,
'desc' => '订单口子脱落超时结束',
]);
$orderModel = new OrderModel();
$orderModel->update([
'state' => 1,
'updated_at' => $this->nowDate,
'end_time' => $this->nowDate,
'is_stop' => StateCode::FINISH_SHED_TIMEOUT,
'end_capacity' => $this->order->getEndCapacity(),
], 'id = ' . $this->order->getId());
$port = new Port($this->order->getPortId());
$port->setState(StateCode::PORT_ONLINE);
$port->setStationId($this->order->getStationId());
$port->setGatewayId($this->order->getGatewayId());
$port->freePort($this->order->getId());
$shedTimeoutMessage = new ChargerShedTimeOutMessage();
$shedTimeoutMessage->setUserId($this->order->getUserId());
$shedTimeoutMessage->setPurchaseTime($this->order->getPurchaseTime());
$shedTimeoutMessage->setStartTime(date('Y-m-d H:i:s', $this->order->getStartTime()));
$shedTimeoutMessage->setEndTime($this->nowDate);
$sendMessage = new SendMessage();
if ($sendMessage->request($shedTimeoutMessage) === false) {
Log::error('send shed timeout message fail');
}
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => StateCode::FINISH_SHED_TIMEOUT,
'end_capacity' => $this->order->getEndCapacity(),
'state' => 1,
], 'id = ' . $this->order->getId());
}
/**
* 记录订单异常
* 释放口子
* 推送结束消息
* 更新订单记录
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toTimeoutState()
{
$orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([
'order_id' => $this->order->getId(),
'state' => StateCode::PORT_SHED . StateCode::FINISH_TIME_END,
'created_at' => $this->nowDate,
'desc' => '订单充电时间结束',
]);
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => StateCode::FINISH_TIME_END,
'end_capacity' => $this->order->getEndCapacity(),
'state' => 1,
'updated_at' => $this->nowDate,
'end_time' => $this->nowDate,
], 'id = ' . $this->order->getId());
$port = new Port($this->order->getPortId());
$port->setState(StateCode::PORT_ONLINE);
$port->setStationId($this->order->getStationId());
$port->setGatewayId($this->order->getGatewayId());
$port->freePort($this->order->getId());
$chargerTimeoutMessage = new ChargerTimeoutMessage();
$chargerTimeoutMessage->setOrderAmount($this->order->getPayAmount());
$chargerTime = $this->nowTimeStamp - (int)$this->order->getStartTime();
$chargerTime /= 60;
$chargerTimeoutMessage->setChargerTime((int)$chargerTime);
$chargerTimeoutMessage->setEndTime($this->nowDate);
$chargerTimeoutMessage->setUserId($this->order->getUserId());
$chargerTimeoutMessage->setOrderId($this->order->getOutTradeNo());
$chargerTimeoutMessage->setPortName($this->order->getPortName());
$chargerTimeoutMessage->setStationName($this->order->getStationName());
$sendMessage = new SendMessage();
if ($sendMessage->request($chargerTimeoutMessage) === false) {
Log::error('shed state order send charger timeout message fail');
}
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:23
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class ShedTimeoutState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
// TODO: Implement toChargerState() method.
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:21
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
abstract class State
{
/**
* @var State
*/
protected $currentState;
/**
* @var Order
*/
protected $order;
/**
* @var int
*/
protected $nowTimeStamp;
/**
* @var string
*/
protected $nowDate;
public function __construct(Order $order)
{
$this->nowTimeStamp = time();
$this->nowDate = date('Y-m-d H:i:s', $this->nowTimeStamp);
$this->order = $order;
}
public abstract function toWaitingState();
public abstract function toChargerState();
public abstract function toExceptionState();//充电异常
public abstract function toPowerExceedState(); //功率超限
public abstract function toPowerFullState(); //充满自停
public abstract function toShedState(); //口子脱落
public abstract function toShedTimeoutState(); //口子脱落超时
public abstract function toTimeoutState(); //时间耗尽
public abstract function toUserFinish();
public abstract function stopFail();
/**
* @return Order
*/
public function getOrder(): Order
{
return $this->order;
}
/**
* @param Order $order
*/
public function setOrder(Order $order): void
{
$this->order = $order;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:23
*/
namespace Lib\cdz_order\state;
use Lib\cdz\order\Order;
class TimeoutState extends State
{
public function __construct(Order $order)
{
parent::__construct($order);
}
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
}
public function toChargerState()
{
// TODO: Implement toChargerState() method.
}
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
}
public function toShedTimeoutState()
{
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
// TODO: Implement toTimeoutState() method.
}
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/13
* Time: 上午11:21
*/
namespace Lib\cdz_order\state;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
use Lib\remote\api\message\FailMessage;
use Lib\remote\api\message\SendMessage;
use Lib\remote\api\message\StartMessage;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\command\SendCommand;
use Src\command\StopChargerCommand;
class WaitingState extends State
{
/**
* WaitingState constructor.
* @param Order $order
*/
public function __construct(Order $order)
{
parent::__construct($order);
}
/**
*
*/
public function toUserFinish()
{
// TODO: Implement toUserFinish() method.
}
public function toWaitingState()
{
}
public function toRefundFinish()
{
//更新订单状态
$orderModel = new OrderModel();
$orderModel->update([
'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s'),
], 'id = ' . $this->order->getId());
$port = new Port();
$port->setState(StateCode::PORT_ONLINE);
$port->setPortId($this->order->getPortId());
$port->setStationId($this->order->getStationId());
$port->setGatewayId($this->order->getGatewayId());
$port->freePort($this->order->getId());
//释放端口
}
/**
*
* 开始充电
* 更新订单的cache
* 更新数据库中订单
* 推送开始充电消息
* 订单轨迹记录
* 更新端口状态
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toChargerState(): void
{
//订单已经退款则直接结束
//更新订单cache
$this->order->setStartTime($this->nowTimeStamp);
$this->order->setStateCode(12);
\Events::pipe();
$orderSaveRedis = new OrderSaveRedis($this->order);
$orderSaveRedis->save(['stateCode', 'startTime', 'startCapacity']);
//更新口子状态
$port = new Port();
$port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_CHARGER);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($_SESSION['station_id']);
$port->updatePortState($this->order->getId());
\Events::exec();
//更新订单状态
$update = [
'is_charger' => StateCode::PORT_CHARGER,
'start_time' => date('Y-m-d H:i:s', $this->order->getStartTime()),
'start_capacity' => $this->order->getStartCapacity(),
];
$orderModel = new OrderModel();
$orderModel->update($update, 'id = ' . $this->order->getId());
// $orderExceptionModel = new OrderExceptionModel();
//
// $orderExceptionModel->insertRow([
//
// 'order_id' => $this->order->getId(),
// 'state' => '-1' . StateCode::PORT_CHARGER,
// 'desc' => '开始充电',
// 'created_at' => $this->nowDate,
// ]);
$port->addUsingOrder($this->order->getPortId(), $this->order->getId());
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toExceptionState(): void
{
//更新订单状态
$orderModel = new OrderModel();
$orderModel->update([
'is_charger' => StateCode::PORT_EXCEPTION,
], 'id = ' . $this->order->getId());
//添加订单异常
$orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([
'order_id' => $this->order->getId(),
'state' => '-1' . StateCode::PORT_EXCEPTION,
'desc' => '打开口子失败',
'created_at' => date('Y-m-d H:i:s'),
]);
//推送消息
//更新口子状态
$port = new Port();
$port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_EXCEPTION);
$port->updatePortState($this->order->getId());
// TODO: Implement toExceptionState() method.
}
/**
* 功率超限
*/
public function toPowerExceedState()
{
Log::error('waiting state can not change to power exceed state');
}
public function toPowerFullState()
{
Log::error('waiting state can not change to power full state');
}
public function toShedState()
{
Log::error('waiting state can not change to shed state');
}
public function toShedTimeoutState()
{
Log::error('waiting state can not change to shed timeout');
}
public function toTimeoutState()
{
Log::error('waiting state can not change to timeout state');
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
......@@ -153,6 +153,11 @@ class Order
/**
* @var string
*/
private $cardId = '';
/**
* @param $name
* @return string
*/
......@@ -628,5 +633,21 @@ class Order
$this->stationId = $stationId;
}
/**
* @return string
*/
public function getCardId(): string
{
return $this->cardId;
}
/**
* @param string $cardId
*/
public function setCardId(string $cardId): void
{
$this->cardId = $cardId;
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace Lib\cdz\order\save;
use Lib\cdz\order\Order;
use Lib\log\Log;
class OrderSaveRedis
{
......@@ -44,6 +45,8 @@ class OrderSaveRedis
}
}
Log::debug('update', $update);
$this->redis->hMset($this->order->getCacheKeyName(), $update);
$this->redis->expire($this->order->getCacheKeyName(), 60 * 60 * 24);
......@@ -51,5 +54,33 @@ class OrderSaveRedis
}
public function saveAll()
{
$update = [];
try {
$reflection = new \ReflectionClass($this->order);
$properties = $reflection->getDefaultProperties();
foreach ($properties as $property) {
if (isset($this->order->$property) && $this->order->$property != '') {
$update[$property] = $this->order->$property;
}
}
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
$this->redis->hMset($this->order->getCacheKeyName(), $update);
$this->redis->expire($this->order->getCacheKeyName(), 60 * 60 * 24);
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
namespace Lib\cdz\order\state;
use Application\model\ExceptionModel;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order;
......@@ -138,12 +139,24 @@ class ChargerState extends State
'created_at' => date('Y-m-d H:i:s', $this->nowTimeStamp),
]);
$exceptionModel = new ExceptionModel();
$exceptionModel->insertRow([
'message' => '功率超限',
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'port_id' => $this->order->getPortId(),
'created_at' => date('Y-m-d H:i:s'),
'type' => 2,
]);
$powerExceedMessage = new PowerExceedMessage();
$powerExceedMessage->setUserId($this->order->getUserId());
$powerExceedMessage->setStartTime($this->order->getStartTime());
$powerExceedMessage->setStartTime(date('Y-m-d H:i:s', $this->order->getStartTime()));
$powerExceedMessage->setEndTime(date('Y-m-d H:i:s', $this->nowTimeStamp));
$powerExceedMessage->setChargerTime($this->order->getPurchaseTime());
$powerExceedMessage->setRemark($powerExceedMessage->getRemark() . '\n' . sprintf('%012x', $this->order->getId()));
$powerExceedMessage->setRemark($powerExceedMessage->getRemark() . '(' . $this->order->getOutTradeNo() . ')');
$sendMessage = new SendMessage();
......@@ -256,6 +269,16 @@ class ChargerState extends State
'created_at' => $this->nowDate,
]);
$exceptionModel = new ExceptionModel();
$exceptionModel->insertRow([
'message' => '口子脱落',
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'port_id' => $this->order->getPortId(),
'created_at' => date('Y-m-d H:i:s'),
'type' => 2,
]);
//更新端口状态和订单状态
$port = new Port($this->order->getPortId());
$port->setState(StateCode::PORT_SHED);
......@@ -337,12 +360,13 @@ class ChargerState extends State
$chargerTimeoutMessage->setStationName($this->order->getStationName());
$chargerTimeoutMessage->setPortName($this->order->getPortName());
$chargerTimeoutMessage->setOrderId($this->order->getId());
$chargerTimeoutMessage->setOrderAmount($this->order->getPayAmount());
$chargerTimeoutMessage->setEndTime($this->nowDate);
$chargerTimeoutMessage->setUserId($this->order->getUserId());
$chargerTime = $this->nowTimeStamp - $this->order->getStartTime();
$chargerTime /= 60;
$chargerTimeoutMessage->setChargerTime((int)$chargerTime);
$chargerTimeoutMessage->setChargerTime(ceil($chargerTime));
$sendMessage = new SendMessage();
if ($sendMessage->request($chargerTimeoutMessage) == false) {
......
......@@ -26,6 +26,14 @@ class OfflineNetState extends State
{
parent::__construct($order);
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0;
return;
}
$startTime = $this->order->getStartTime();
if ($startTime === '' || $this->order->getOfflineTime() == '0') {
......@@ -33,20 +41,11 @@ class OfflineNetState extends State
} else {
$purchaseTime = $this->order->getPurchaseTime();
Log::debug($purchaseTime * 60);
Log::debug($this->order->getOfflineTime());
Log::debug($this->order->getStartTime());
$this->remainChargerTime = $purchaseTime * 60 - ((int)$this->order->getOnlineTime() - $startTime);
Log::debug('remain charger time' . $this->remainChargerTime);
Log::debug('---');
Log::debug($this->remainChargerTime);
$this->remainChargerTime = (int)($this->remainChargerTime / 60);
$this->remainChargerTime = ceil($this->remainChargerTime / 60);
Log::debug($this->remainChargerTime);
}
......
......@@ -10,12 +10,44 @@ namespace Lib\cdz\order\state;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
class OfflinePowerState extends State
{
/**
* @var int
*/
private $remainChargerTime = 0;
public function __construct(Order $order)
{
parent::__construct($order);
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0;
return;
}
$offlineTime = $this->order->getOfflineTime();
$startTime = $this->order->getStartTime();
if ($startTime === '' || $this->order->getOfflineTime() == '0') {
$this->remainChargerTime = (int)($this->order->getPurchaseTime());
} else {
$purchaseTime = $this->order->getPurchaseTime();
$this->remainChargerTime = $purchaseTime * 60 - ($offlineTime - $startTime);
$this->order->setStartTime(strval((int)$startTime + ((int)time() - (int)$offlineTime)));
$this->remainChargerTime = ceil($this->remainChargerTime / 60);
$saveOrder = new OrderSaveRedis($this->order);
$saveOrder->save(['startTime']);
}
}
public function toUserFinish()
......@@ -24,11 +56,6 @@ class OfflinePowerState extends State
}
/**
* @var int
*/
private $remainChargerTime = 0;
public function toWaitingState()
{
// TODO: Implement toWaitingState() method.
......@@ -89,5 +116,21 @@ class OfflinePowerState extends State
// TODO: Implement stopFail() method.
}
/**
* @return int
*/
public function getRemainChargerTime(): int
{
return $this->remainChargerTime;
}
/**
* @param int $remainChargerTime
*/
public function setRemainChargerTime(int $remainChargerTime): void
{
$this->remainChargerTime = $remainChargerTime;
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ use Lib\remote\api\message\SendMessage;
use Lib\remote\api\message\StartMessage;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\BroadcastToAgent;
use Src\command\SendCommand;
use Src\command\StopChargerCommand;
......@@ -179,7 +180,8 @@ class WaitingState extends State
$failMessage = new FailMessage();
$failMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName());
$failMessage->setAddress($this->order->getPayAmount());
$failMessage->setAmount($this->order->getPayAmount());
$failMessage->setOrderId($this->order->getOutTradeNo());
$failMessage->setUserId($this->order->getUserId());
$sendMessage = new SendMessage();
if ($sendMessage->request($failMessage) === false) {
......@@ -190,9 +192,17 @@ class WaitingState extends State
$port = new Port();
$port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_EXCEPTION);
$port->setStationId($this->order->getStationId());
$port->setGatewayId($this->order->getGatewayId());
$port->updatePortState($this->order->getId());
$broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast([
'data' => $this->order->getOrderCache(\Events::getRedis()),
'notice_exception_code' => StateCode::PORT_EXCEPTION,
]);
// TODO: Implement toExceptionState() method.
}
......
......@@ -48,7 +48,7 @@ class ChargerFullMessage extends Message
'value' => $this->endTime,
'color' => '#333',
],
'remark' => [
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
......
......@@ -17,7 +17,7 @@ class ChargerShedMessage extends Message
{
protected $templateId = 'C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8';
private $first = '尊敬的 户,充电端未检测到插头,请您在10分钟内接入。';
private $first = '尊敬的用户,充电端未检测到插头,请您在10分钟内接入';
private $stationName = '';
......@@ -25,7 +25,7 @@ class ChargerShedMessage extends Message
private $date = '';
private $remark = '感谢您的使用。';
private $remark = '感谢您的使用';
public function __construct()
{
......@@ -52,7 +52,7 @@ class ChargerShedMessage extends Message
'color' => '#333',
],
'remark' => [
'value' => '感谢你的使用',
'value' => '感谢您的使用',
'color' => '#333',
],
......
......@@ -54,7 +54,7 @@ class FailMessage extends Message
],
'keyword1' => [
'value' => sprintf('%012x', $this->orderId),
'value' => $this->orderId,
'color' => '#333',
],
......
......@@ -23,13 +23,13 @@ class FinishByUserMessage extends Message
private $endTime = '';
private $address= '';
private $address = '';
private $chargerTime = 0;
private $orderAmount =0;
private $orderAmount = 0;
private $remark = '感谢你的使用';
private $remark = '感谢您的使用';
/**
* FinishByUserMessage constructor.
......@@ -59,14 +59,14 @@ class FinishByUserMessage extends Message
'color' => '#333',
],
'keyword3' => [
'value' => $this->chargerTime.'分钟',
'value' => $this->chargerTime . '分钟',
'color' => '#333',
],
'keyword4' => [
'value' => '¥'.number_format($this->orderAmount/100,2),
'value' => '¥' . number_format($this->orderAmount / 100, 2),
'color' => '#333',
],
'remark' => [
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
......@@ -171,6 +171,4 @@ class FinishByUserMessage extends Message
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class PowerExceedMessage extends Message
/**
* @var string
*/
private $first = '尊敬的用户,你的爱车功率过大,本次充电已结束';
private $first = '尊敬的用户,您的爱车功率过大,本次充电已结束';
/**
* @var string
......@@ -45,7 +45,8 @@ class PowerExceedMessage extends Message
/**
* PowerExceedMessage constructor.
*/
public function __construct() {
public function __construct()
{
$this->templateId = getenv('MESSAGE_POWER_EXCEED');
......@@ -67,14 +68,14 @@ class PowerExceedMessage extends Message
],
'keyword2' => [
'value' => $this->chargerTime.'分钟',
'value' => $this->chargerTime . '分钟',
'color' => '#333',
],
'keyword3' => [
'value' => $this->endTime,
'color' => '#333',
],
'remark' => [
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
......@@ -179,8 +180,4 @@ class PowerExceedMessage extends Message
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ class ShedMessage extends Message
/**
* @var string
*/
private $first = '尊敬的用户,充电端未检测到插头,请您在%s分钟内接入。';
private $first = '尊敬的用户,充电端未检测到插头,请您在%s分钟内接入';
/**
* @var string
......@@ -57,7 +57,7 @@ class ShedMessage extends Message
public function getPushJson(): string
{
$this->first = sprintf($this->first,$this->waitingTime);
$this->first = sprintf($this->first, $this->waitingTime);
return json_encode([
......@@ -75,7 +75,7 @@ class ShedMessage extends Message
'value' => $this->time,
'color' => '#333',
],
'remark' => [
'remark' => [
'value' => $this->remark,
'color' => '#333',
],
......@@ -182,5 +182,4 @@ class ShedMessage extends Message
}
}
\ No newline at end of file
......@@ -36,7 +36,7 @@ class StartMessage extends Message
/**
* @var string
*/
private $remark = '感谢你的使用';
private $remark = '感谢您的使用';
/**
* StartMessage constructor.
......
......@@ -24,7 +24,6 @@ class Port
/**
* 端口正在充电的订单号
* cdz:port:using:order_id:hash
*/
const USING_ORDER_ID = 'cdz:port:using:order_id:hash';
......@@ -263,5 +262,13 @@ class Port
$this->stationId = $stationId;
}
public function addUsingOrder(int $portId, int $orderId)
{
$redis = \Events::getRedis();
$redis->hSet(self::USING_ORDER_ID, $portId, $orderId);
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
namespace Workerman\Protocols;
use Lib\log\Log;
use Workerman\Connection\TcpConnection;
class Bintohex
......@@ -27,36 +28,32 @@ class Bintohex
$packageLength = strlen($data);
$packageLength/=2;
$packageLength /= 2;
//小于最小的头长度继续等待
if($packageLength < self::HEAD_LENGTH)
{
if ($packageLength < self::HEAD_LENGTH) {
return 0;
}
//数据非ffaa开头直接断开连接
if(substr($data,0,4) !== 'ffaa')
{
var_dump('111');
if (substr($data, 0, 4) !== 'ffaa') {
$tcpConnection->close();
}
$hexCount = hexdec(substr($data,8,4));
$hexCount = hexdec(substr($data, 8, 4));
if($packageLength >= $hexCount)
{
if ($packageLength >= $hexCount) {
return $hexCount;
}else{
} else {
return 0;
}
}
......@@ -67,6 +64,11 @@ class Bintohex
public static function encode($buffer)
{
if (strlen($buffer) % 2 === 1) {
Log::error('encode error');
return;
}
return pack('H*', bin2hex(hex2bin($buffer)));
}
......
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