Commit df104c15 by banice

测试版

parent 2abe7fc5
...@@ -28,6 +28,8 @@ MESSAGE_POWER_EXCEED=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU ...@@ -28,6 +28,8 @@ MESSAGE_POWER_EXCEED=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU
MESSAGE_FINISH_USER=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8 MESSAGE_FINISH_USER=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8
MESSAGE_PORT_SHED=C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8 MESSAGE_PORT_SHED=C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8
MESSAGE_CHARGER_TIMEOUT=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8 MESSAGE_CHARGER_TIMEOUT=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8
MESSAGE_RELOAD=t_PyOGCKN_fmD0blw0EuIQs-8sS3qOTmpQay12haPZY
MESSAGE_SHED_TIMEOUT=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU
MESSAGE_CHARGER_SHED=C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8
LOW_POWER_LIMIT=30 LOW_POWER_LIMIT=30
...@@ -2,7 +2,7 @@ REDIS_HOST=redis.test.senthink.com ...@@ -2,7 +2,7 @@ REDIS_HOST=redis.test.senthink.com
REGISTER_IP=127.0.0.1 REGISTER_IP=127.0.0.1
REGISTER_PORT=1239 REGISTER_PORT=1239
GATEWAY_PORT=8282 GATEWAY_PORT=8283
GATEWAY_IP=127.0.0.1 GATEWAY_IP=127.0.0.1
GATEWAY_COUNT=1 GATEWAY_COUNT=1
GATEWAY_START_PORT=2130 GATEWAY_START_PORT=2130
...@@ -25,5 +25,11 @@ MESSAGE_API_URL=https://cdzback2.test.senthink.com ...@@ -25,5 +25,11 @@ MESSAGE_API_URL=https://cdzback2.test.senthink.com
MESSAGE_START=L9SEWl3X73v3PBYHtFBZlTlJeBMdO9DDNIwRgHX6SrY MESSAGE_START=L9SEWl3X73v3PBYHtFBZlTlJeBMdO9DDNIwRgHX6SrY
MESSAGE_FAIL=0al-a6kkNsdqFyXPEe97m4jjtbfOXjEC3eCjEsMHJdw MESSAGE_FAIL=0al-a6kkNsdqFyXPEe97m4jjtbfOXjEC3eCjEsMHJdw
MESSAGE_POWER_EXCEED=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU MESSAGE_POWER_EXCEED=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU
MESSAGE_FINISH_USER=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8
MESSAGE_PORT_SHED=C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8
MESSAGE_CHARGER_TIMEOUT=f0DDXSxvRTbHyzkCnwnYijtgteFTdfdUR7J8REwTYJ8
MESSAGE_RELOAD=t_PyOGCKN_fmD0blw0EuIQs-8sS3qOTmpQay12haPZY
MESSAGE_SHED_TIMEOUT=rdzTNQ7t4uRXkP0SkuwW10WH77A_CIgYFiJykXJ0utU
MESSAGE_CHARGER_SHED=C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8
LOW_POWER_LIMIT=30 LOW_POWER_LIMIT=30
...@@ -27,6 +27,9 @@ class Events ...@@ -27,6 +27,9 @@ class Events
*/ */
private static $redis; private static $redis;
/**
*
*/
public static function onWorkerStart() public static function onWorkerStart()
{ {
...@@ -46,7 +49,6 @@ class Events ...@@ -46,7 +49,6 @@ class Events
static::$redis->select('4'); static::$redis->select('4');
\Lib\log\Log::bootLog(); \Lib\log\Log::bootLog();
...@@ -72,7 +74,6 @@ class Events ...@@ -72,7 +74,6 @@ class Events
$crc = substr($message, -2); $crc = substr($message, -2);
var_dump($message);
$crcConfirm = sprintf('%08x', Crc::crc(substr($message, 0, strlen($message) - 2))); $crcConfirm = sprintf('%08x', Crc::crc(substr($message, 0, strlen($message) - 2)));
if ($crc != mb_strtoupper(substr($crcConfirm, 0, 2))) { if ($crc != mb_strtoupper(substr($crcConfirm, 0, 2))) {
...@@ -80,6 +81,7 @@ class Events ...@@ -80,6 +81,7 @@ class Events
return; return;
} }
Log::debug($message);
$command = substr($message, 6, 2); $command = substr($message, 6, 2);
$api = new Api(); $api = new Api();
...@@ -110,37 +112,90 @@ class Events ...@@ -110,37 +112,90 @@ class Events
} }
/**
* @param $clientId
*/
public static function onClose($clientId) public static function onClose($clientId)
{ {
var_dump('close'); 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 === '') {
Log::error('client close can not find station_id or gateway_id');
return;
}
$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'],
'type' => 1,
]);
}
Events::pipe();
$gatewayId = $_SESSION['gateway_id'];
$gateway = new Gateway(); $gateway = new Gateway();
//网关状态设置为离线 //网关状态设置为离线
$gateway->setState($gatewayId, 0); $gateway->setState($gatewayId, 0);
if (isset($_SESSION['port_mac_id']) === false) {
Log::error('gateway close can not find port_mac_id in session');
return;
}
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = $_SESSION['port_mac_id'];
$portsId = array_values($portsMacId); $portsId = array_values($portsMacId);
$port = new Port(); $port = new Port();
$exceptionModel = new \Application\model\ExceptionModel();
$date = date('Y-m-d H:i:s');
foreach ($portsId as $portId) { foreach ($portsId as $portId) {
//所有端口状态设置为离线
$port->setPortId($portId); $port->setPortId($portId);
$port->setState(10); $port->setGatewayId($gatewayId);
$port->setStationId($stationId);
$port->setState(\Lib\StateCode::PORT_OFFLINE);
$port->updatePortState(); $port->updatePortState();
} $exceptionModel->insertRow([
//所有端口状态设置为离线 'gateway_id' => $gatewayId,
Events::exec(); 'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'port_id' => $portId,
//端口当前订单记录离线时间 'message' => '口子离线',
'created_at' => $date,
]);
$portUsingOrderId = $port->getPortUsingOrderId();
//端口当前订单记录离线时间
if ($portUsingOrderId != false && $portUsingOrderId != '') {
$order = new \Lib\cdz\order\Order($portUsingOrderId);
if ($order->getOrderCache(static::$redis) != false) {
$order->setOfflineTime(time());
$saveOrder = new \Lib\cdz\order\save\OrderSaveRedis($order);
$saveOrder->save(['offlineTime']);
unset($saveOrder);
}
}
}
} }
......
...@@ -174,6 +174,11 @@ class Controller ...@@ -174,6 +174,11 @@ class Controller
public function __destruct() public function __destruct()
{ {
if($this->command === '00')
{
return ;
}
$logWriter = new GatewayLogWriter(); $logWriter = new GatewayLogWriter();
if($this->response->getResponseMessage() !== '') if($this->response->getResponseMessage() !== '')
......
...@@ -40,5 +40,7 @@ class HeartController extends Controller ...@@ -40,5 +40,7 @@ class HeartController extends Controller
$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()));
} }
} }
\ No newline at end of file
...@@ -12,10 +12,13 @@ namespace Application\controller\accept; ...@@ -12,10 +12,13 @@ namespace Application\controller\accept;
use Application\controller\Controller; use Application\controller\Controller;
use Application\model\DeviceModel; use Application\model\DeviceModel;
use Application\model\GatewayModel; use Application\model\GatewayModel;
use Application\model\ParkModel;
use Application\model\StationModel;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\state\OfflineNetState; use Lib\cdz\order\state\OfflineNetState;
use Lib\cdz\order\state\OfflinePowerState; use Lib\cdz\order\state\OfflinePowerState;
use Lib\Gateway; use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
use Src\route\Route; use Src\route\Route;
...@@ -67,9 +70,7 @@ class AccessController extends Controller ...@@ -67,9 +70,7 @@ class AccessController extends Controller
'id', 'id',
], 'mac_address = ' . "'$mac'"); ], 'mac_address = ' . "'$mac'");
var_dump($gatewayModelResult); if ($gatewayModelResult == false) {
if($gatewayModelResult == false)
{
Gateway::closeClient($this->clientId); Gateway::closeClient($this->clientId);
return; return;
} }
...@@ -81,6 +82,20 @@ class AccessController extends Controller ...@@ -81,6 +82,20 @@ class AccessController extends Controller
$this->logRecord->setGatewayId($gatewayId); $this->logRecord->setGatewayId($gatewayId);
$this->logRecord->setStationId($stationId); $this->logRecord->setStationId($stationId);
$stationModel = new StationModel();
$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;
}
}
$deviceModel = new DeviceModel(); $deviceModel = new DeviceModel();
...@@ -152,9 +167,8 @@ class AccessController extends Controller ...@@ -152,9 +167,8 @@ class AccessController extends Controller
} }
if(isset($_SESSION['port_mac_id']) == false) if (isset($_SESSION['port_mac_id']) == false) {
{ return;
return ;
} }
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = $_SESSION['port_mac_id'];
...@@ -164,7 +178,15 @@ class AccessController extends Controller ...@@ -164,7 +178,15 @@ class AccessController extends Controller
$port = new Port(); $port = new Port();
$portsLogicId = []; $portsLogicId = [];
$portsId = array_values($portsMacId); // $portsId = array_values($portsMacId);
$portsId = [];
foreach ($portMacState as $mac => $state) {
if (isset($portsMacId[$mac])) {
array_push($portsId, $portsMacId[$mac]);
}
}
foreach ($portMacState as $mac => $state) { foreach ($portMacState as $mac => $state) {
$portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false; $portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false;
...@@ -178,17 +200,19 @@ class AccessController extends Controller ...@@ -178,17 +200,19 @@ class AccessController extends Controller
$port->setState($state); $port->setState($state);
$port->setGatewayId($_SESSION['gateway_id']); $port->setGatewayId($_SESSION['gateway_id']);
$port->setStationId($_SESSION['station_id']); $port->setStationId($_SESSION['station_id']);
$port->updatePortState(); $port->updatePortState();
$portsLogicId[$portId] = $logicStart++; $portsLogicId[$logicStart++] = $portId;
} }
\Events::exec(); \Events::exec();
$deviceModel = new DeviceModel(); $deviceModel = new DeviceModel();
$_SESSION['port_logic_id'] = $portsLogicId; if (isset($_SESSION['port_logic_id'])) {
$_SESSION['port_logic_id'] = $_SESSION['port_logic_id'] + $portsLogicId;
} else {
$_SESSION['port_logic_id'] = $portsLogicId;
}
//更新逻辑地址 //更新逻辑地址
foreach ($portsLogicId as $portId => $logicId) { foreach ($portsLogicId as $portId => $logicId) {
...@@ -229,6 +253,7 @@ class AccessController extends Controller ...@@ -229,6 +253,7 @@ class AccessController extends Controller
$remainTime = $orderState->getRemainChargerTime(); $remainTime = $orderState->getRemainChargerTime();
if ($remainTime > 0) { if ($remainTime > 0) {
$logicNum = sprintf('%02x', $order->getPortLogicId()); $logicNum = sprintf('%02x', $order->getPortLogicId());
$orderId = sprintf('%012x', $order->getId()); $orderId = sprintf('%012x', $order->getId());
...@@ -246,9 +271,6 @@ class AccessController extends Controller ...@@ -246,9 +271,6 @@ class AccessController extends Controller
$totalCount = sprintf('%02x', $totalCount); $totalCount = sprintf('%02x', $totalCount);
$orderCount = sprintf('%02x', count($responseOrder)); $orderCount = sprintf('%02x', count($responseOrder));
var_dump($logicStart);
$this->response->send('03', $logicStart . $count . $totalCount . $orderCount . implode('', $responseOrder));; $this->response->send('03', $logicStart . $count . $totalCount . $orderCount . implode('', $responseOrder));;
......
...@@ -62,14 +62,12 @@ class CertifyController extends Controller ...@@ -62,14 +62,12 @@ class CertifyController extends Controller
$clients = Gateway::getclientIdByUid($mac); $clients = Gateway::getclientIdByUid($mac);
foreach ($clients as $client) { foreach ($clients as $client) {
var_dump('hello');
Gateway::closeClient($client); Gateway::closeClient($client);
} }
} }
Gateway::bindUid($this->clientId,$mac); Gateway::bindUid($this->clientId, $mac);
$this->response->send('01', $sign); $this->response->send('01', $sign);
......
...@@ -10,12 +10,16 @@ namespace Application\controller\business\charger; ...@@ -10,12 +10,16 @@ namespace Application\controller\business\charger;
use Application\controller\Controller; use Application\controller\Controller;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel; use Application\model\OrderModel;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\state\ChargerState; use Lib\cdz\order\state\ChargerState;
use Lib\cdz\order\state\ShedState; use Lib\cdz\order\state\ShedState;
use Lib\cdz\order\state\WaitingState; use Lib\cdz\order\state\WaitingState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\route\Route; use Src\route\Route;
class OverController extends Controller class OverController extends Controller
...@@ -47,20 +51,25 @@ class OverController extends Controller ...@@ -47,20 +51,25 @@ class OverController extends Controller
public function over() public function over()
{ {
$type = $this->request->get('type'); $type = $this->request->get('type');
$logicId = $this->request->get('logic_id'); $logicId = hexdec($this->request->get('logic_id'));
$state = hexdec($this->request->get('state')); $state = hexdec($this->request->get('state'));
$orderId = hexdec($this->request->get('order_id')); $orderId = hexdec($this->request->get('order_id'));
$capacity = hexdec($this->request->get('capacity')); $capacity = hexdec($this->request->get('capacity'));
$this->logRecord->setOrderId($orderId); $this->logRecord->setOrderId($orderId);
$order = new Order($orderId);
$order = new Order();
if ($order->getOrderCache(\Events::getRedis()) != false) { if ($order->getOrderCache(\Events::getRedis()) != false) {
$orderStateCode = $order->getStateCode(); $orderStateCode = $order->getStateCode();
$order->setEndCapacity($capacity); $order->setEndCapacity($capacity);
$order->setEndCode($state); $order->setEndCode($state);
if (Gateway::isUidOnline($order->getUserId()) === 1) {
Gateway::sendToUid($order->getUserId(), json_encode(['code' => 10000]));
}
Log::debug('order state code ' . $orderStateCode);
switch ($orderStateCode) { switch ($orderStateCode) {
case StateCode::WAITING: case StateCode::WAITING:
$orderState = new WaitingState($order); $orderState = new WaitingState($order);
...@@ -68,7 +77,7 @@ class OverController extends Controller ...@@ -68,7 +77,7 @@ class OverController extends Controller
case StateCode::PORT_CHARGER: case StateCode::PORT_CHARGER:
$orderState = new ChargerState($order); $orderState = new ChargerState($order);
break; break;
case StateCode::CHARGER_SHED: case StateCode::PORT_SHED:
$orderState = new ShedState($order); $orderState = new ShedState($order);
break; break;
default: default:
...@@ -78,24 +87,55 @@ class OverController extends Controller ...@@ -78,24 +87,55 @@ class OverController extends Controller
if (isset($orderState)) { if (isset($orderState)) {
switch ($state) { switch ($state) {
case StateCode::FINISH_USER: //用户主动结束 case StateCode::FINISH_USER: //用户主动结束
$this->logRecord->setDesc('用户主动结束充电');
$orderState->toUserFinish(); $orderState->toUserFinish();
break; break;
case StateCode::FINISH_SHED_TIMEOUT: //端口脱落超时 case StateCode::FINISH_SHED_TIMEOUT: //端口脱落超时
$this->logRecord->setDesc('端口脱落超时结束');
$orderState->toShedTimeoutState(); $orderState->toShedTimeoutState();
break; break;
case StateCode::FINISH_REFUND: //充电失败退款 case StateCode::FINISH_REFUND: //充电失败退款
$this->logRecord->setDesc('充电失败退款结束');
$orderState->toRefundFinish(); $orderState->toRefundFinish();
break; break;
case StateCode::FINISH_EXCEED: //功率超限 case StateCode::FINISH_EXCEED: //功率超限
$this->logRecord->setDesc('功率超限充电结束');
$orderState->toPowerExceedState(); $orderState->toPowerExceedState();
break; break;
case StateCode::FINISH_FULL: //充满自停 case StateCode::FINISH_FULL: //充满自停
$this->logRecord->setDesc('充满自停结束');
$orderState->toPowerFullState(); $orderState->toPowerFullState();
break; break;
case StateCode::FINISH_TIME_END: //充电时间结束 case StateCode::FINISH_TIME_END: //充电时间结束
$this->logRecord->setDesc('充电时间完结束');
$orderState->toTimeoutState(); $orderState->toTimeoutState();
break; break;
case StateCode::CHANGE_PORT:
if (isset($_SESSION['port_logic_id'])) {
$portId = isset($_SESSION['port_logic_id'][$logicId]) ? $_SESSION['port_logic_id'][$logicId] : 0;
$port = new Port($portId);
$port->setState(StateCode::PORT_ONLINE);
$port->setGatewayId($order->getGatewayId());
$port->setStationId($order->getStationId());
$port->freePort();
$orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([
'order_id' => $orderId,
'created_at' => date('Y-m-d H:i:s'),
'state' => '-1' . StateCode::CHANGE_PORT,
'desc' => '更换口子,' . $portId . '关闭成功',
]);
$this->logRecord->setDesc('更换口子,' . $portId . '关闭成功');
}
$this->response->send('05', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
return;
default: default:
break; break;
...@@ -117,7 +157,7 @@ class OverController extends Controller ...@@ -117,7 +157,7 @@ class OverController extends Controller
} }
$this->response->send('05', $logicId . sprintf('%12x', $orderId)); $this->response->send('05', sprintf('%02x', $logicId) . sprintf('%012x', $orderId));
} }
......
...@@ -12,6 +12,8 @@ namespace Application\controller\business\charger; ...@@ -12,6 +12,8 @@ namespace Application\controller\business\charger;
use Application\controller\Controller; use Application\controller\Controller;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\state\WaitingState; use Lib\cdz\order\state\WaitingState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode; use Lib\StateCode;
class StartController extends Controller class StartController extends Controller
...@@ -21,9 +23,9 @@ class StartController extends Controller ...@@ -21,9 +23,9 @@ class StartController extends Controller
'04' => [ '04' => [
'type' => 1, 'type' => 1,
'logic_id' => 1, 'logic_id' => 1,
'state' => 1, 'state' => 1,
'order_id' => 6, 'order_id' => 6,
'capacity' => 4, 'capacity' => 4,
], ],
...@@ -43,11 +45,44 @@ class StartController extends Controller ...@@ -43,11 +45,44 @@ class StartController extends Controller
$logic = $this->getRequest()->get('logic_id'); $logic = $this->getRequest()->get('logic_id');
$state = hexdec($this->getRequest()->get('state')); $state = hexdec($this->getRequest()->get('state'));
$orderId = hexdec($this->getRequest()->get('order_id')); $orderId = hexdec($this->getRequest()->get('order_id'));
$this->logRecord->setOrderId($orderId);
$capacity = hexdec($this->getRequest()->get('capacity')); $capacity = hexdec($this->getRequest()->get('capacity'));
if ($type == 1) { if ($type == 1) {
$waitingState = new WaitingState(new Order($orderId));
$order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) == false) {
$this->response->send('04', sprintf('%012x', $orderId));
return;
}
$order->setStartCapacity($capacity);
if (Gateway::isUidOnline($order->getUserId()) === 1) {
Gateway::sendToUid($order->getUserId(), json_encode(['code' => 10000]));
}
if (isset($_SESSION['agent_id'])) {
Log::debug('agent_id' . $_SESSION['agent_id']);
Log::debug(Gateway::isUidOnline($_SESSION['agent_id']));
if (Gateway::isUidOnline($_SESSION['agent_id'])) {
$clientsId = Gateway::getClientIdByUid($_SESSION['agent_id']);
$clientId = $clientsId[0];
$uIds = Gateway::getUidByClientId($clientId);
var_dump($uIds);
Gateway::sendToUid($_SESSION['agent_id'], json_encode($order->getOrderCache(\Events::getRedis())));
} else {
Log::debug('agent is offline');
}
}
$waitingState = new WaitingState($order);
$portId = $waitingState->getOrder()->getPortId();
$this->logRecord->setPortId($portId);
switch ($state) { switch ($state) {
case StateCode::PORT_CHARGER: case StateCode::PORT_CHARGER:
...@@ -69,7 +104,7 @@ class StartController extends Controller ...@@ -69,7 +104,7 @@ class StartController extends Controller
} }
} }
$this->response->send('04',sprintf('%012x',$orderId)); $this->response->send('04', sprintf('%012x', $orderId));
} }
......
...@@ -10,6 +10,7 @@ namespace Application\controller\business\port; ...@@ -10,6 +10,7 @@ namespace Application\controller\business\port;
use Application\controller\Controller; use Application\controller\Controller;
use Application\model\ExceptionModel;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
...@@ -29,7 +30,9 @@ class OfflineController extends Controller ...@@ -29,7 +30,9 @@ class OfflineController extends Controller
} }
/** /**
* * 口子离线
* 更新口子状态
* 记录离线记录
*/ */
public function offline() public function offline()
{ {
...@@ -38,30 +41,47 @@ class OfflineController extends Controller ...@@ -38,30 +41,47 @@ class OfflineController extends Controller
$portsLogicMac = []; $portsLogicMac = [];
$str = substr($this->message,$this->plc); $str = substr($this->message, $this->plc);
$tmp = str_split($str,18); $tmp = str_split($str, 18);
foreach ($tmp as $item) { foreach ($tmp as $item) {
$portsLogicMac[hexdec(substr($item,0,2))] = substr($item,2); $portsLogicMac[hexdec(substr($item, 0, 2))] = substr($item, 2);
} }
if (isset($_SESSION['port_mac_id'])) {
return;
}
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = $_SESSION['port_mac_id'];
$port = new Port(); $port = new Port();
foreach ($portsLogicMac as $portLogic => $portMac) { $exceptionModel = new ExceptionModel();
if (!(isset($_SESSION['station_id']) && isset($_SESSION['gateway_id']))) {
return;
}
if(isset($portsMacId[$portMac])) foreach ($portsLogicMac as $portLogic => $portMac) {
{ if (isset($portsMacId[$portMac])) {
$port->setPortId($portsMacId[$portMac]); $portId = $portsMacId[$portMac];
$port->setPortId($portId);
$port->setStationId($_SESSION['station_id']);
$port->setGatewayId($_SESSION['gateway_id']);
$port->setState(StateCode::PORT_OFFLINE); $port->setState(StateCode::PORT_OFFLINE);
$port->updatePortState(); $port->updatePortState();
//todo 记录口子上线 // 记录口子离线
} $exceptionModel->insertRow([
'message' => '口子离线',
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['stationg_id']) ? $_SESSION['station_id'] : '',
'port_id' => $portId,
'created_at' => date('Y-m-d H:i:s'),
]);
}
} }
} }
......
...@@ -10,6 +10,7 @@ namespace Application\controller\business\port; ...@@ -10,6 +10,7 @@ namespace Application\controller\business\port;
use Application\controller\Controller; use Application\controller\Controller;
use Application\model\ExceptionModel;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
...@@ -36,28 +37,48 @@ class OnlineController extends Controller ...@@ -36,28 +37,48 @@ class OnlineController extends Controller
$portsLogicMac = []; $portsLogicMac = [];
$str = substr($this->message,$this->plc); $str = substr($this->message, $this->plc);
$tmp = str_split($str,18); $tmp = str_split($str, 18);
foreach ($tmp as $item) { foreach ($tmp as $item) {
$portsLogicMac[hexdec(substr($item,0,2))] = substr($item,2); $portsLogicMac[hexdec(substr($item, 0, 2))] = substr($item, 2);
} }
if (isset($_SESSION['port_mac_id']) === false) {
return;
}
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = $_SESSION['port_mac_id'];
$port = new Port(); $port = new Port();
$exceptionModel = new ExceptionModel();
if (!(isset($_SESSION['station_id']) && isset($_SESSION['gateway_id']))) {
return;
}
foreach ($portsLogicMac as $portLogic => $portMac) { foreach ($portsLogicMac as $portLogic => $portMac) {
if (isset($portsMacId[$portMac])) {
if(isset($portsMacId[$portMac])) $portId = $portsMacId[$portMac];
{ $port->setPortId($portId);
$port->setPortId($portsMacId[$portMac]); $port->setStationId($_SESSION['station_id']);
$port->setGatewayId($_SESSION['gateway_id']);
$port->setState(StateCode::PORT_ONLINE); $port->setState(StateCode::PORT_ONLINE);
$port->updatePortState(); $port->updatePortState();
//todo 记录口子上线
// 记录口子上线
$exceptionModel->insertRow([
'port_id' => $portId,
'gateway_id' => isset($_SESSION['gateway_id']) ? $_SESSION['gateway_id'] : '',
'station_id' => isset($_SESSION['station_id']) ? $_SESSION['station_id'] : '',
'created_at' => date('Y-m-d H:i:s'),
]);
} }
} }
......
...@@ -15,6 +15,9 @@ use Lib\cdz\order\state\ChargerState; ...@@ -15,6 +15,9 @@ use Lib\cdz\order\state\ChargerState;
use Lib\cdz\order\state\ExceptionState; use Lib\cdz\order\state\ExceptionState;
use Lib\cdz\order\state\PowerExceedState; use Lib\cdz\order\state\PowerExceedState;
use Lib\cdz\order\state\ShedState; use Lib\cdz\order\state\ShedState;
use Lib\cdz\order\state\WaitingState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
...@@ -23,7 +26,7 @@ class StateController extends Controller ...@@ -23,7 +26,7 @@ class StateController extends Controller
protected $parameterMap = [ protected $parameterMap = [
'0b' => [ '0B' => [
'port_logic' => 1, 'port_logic' => 1,
'mac' => 8, 'mac' => 8,
'state' => 1, 'state' => 1,
...@@ -39,23 +42,36 @@ class StateController extends Controller ...@@ -39,23 +42,36 @@ class StateController extends Controller
{ {
$portLogic = $this->request->get('port_logic'); $portLogic = $this->request->get('port_logic');
$mac = $this->request->get('mac'); $mac = $this->request->get('mac');
$state = $this->request->get('state'); $state = hexdec($this->request->get('state'));
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = $_SESSION['port_mac_id'];
if (isset($portsMacId[$mac]) === false) {
return;
}
$portId = $portsMacId[$mac]; $portId = $portsMacId[$mac];
$port = new Port($portId); $port = new Port($portId);
$usingOrder = $port->getPortUsingOrderId(); $usingOrderId = $port->getPortUsingOrderId();
if ($usingOrder == false || $usingOrder = '') { if ($usingOrderId == false || $usingOrderId == '') {
$port->setState($state); $port->setState($state);
$port->updatePortState(); if (isset($_SESSION['gateway_id']) && isset($_SESSION['station_id'])) {
$port->setGatewayId($_SESSION['gateway_id']);
$port->setStationId($_SESSION['station_id']);
$port->updatePortState();
} else {
Log::error('can not find station_id or gateway_id from session (change state)');
}
} else { } else {
$order = new Order($usingOrder); $this->logRecord->setOrderId($usingOrderId);
$this->logRecord->setPortId($portId);
$order = new Order((int)$usingOrderId);
if ($order->getOrderCache(\Events::getRedis()) == false) { if ($order->getOrderCache(\Events::getRedis()) == false) {
$port->setState($state); $port->setState($state);
...@@ -63,12 +79,17 @@ class StateController extends Controller ...@@ -63,12 +79,17 @@ class StateController extends Controller
return; return;
} }
// if (Gateway::isUidOnline($order->getUserId()) === 1) {
// Gateway::sendToUid($order->getUserId(), json_encode(['code' => 10000]));
// }
Log::debug('order last state ' . $order->getStateCode());
switch ($order->getStateCode()) { switch ($order->getStateCode()) {
case StateCode::PORT_CHARGER: case StateCode::PORT_CHARGER:
$orderState = new ChargerState($order); $orderState = new ChargerState($order);
break; break;
case StateCode::CHARGER_SHED: case StateCode::PORT_SHED:
$orderState = new ShedState($order); $orderState = new ShedState($order);
break; break;
case StateCode::FINISH_EXCEED: case StateCode::FINISH_EXCEED:
...@@ -79,11 +100,15 @@ class StateController extends Controller ...@@ -79,11 +100,15 @@ class StateController extends Controller
case StateCode::PORT_EXCEPTION: case StateCode::PORT_EXCEPTION:
$orderState = new ExceptionState($order); $orderState = new ExceptionState($order);
break; break;
case StateCode::WAITING:
$orderState = new WaitingState($order);
break;
default: default:
return; break;
} }
Log::debug('now state' . $state);
switch ($state) { switch ($state) {
case StateCode::PORT_CHARGER: case StateCode::PORT_CHARGER:
$orderState->toChargerState(); $orderState->toChargerState();
...@@ -92,6 +117,11 @@ class StateController extends Controller ...@@ -92,6 +117,11 @@ class StateController extends Controller
$orderState->toPowerExceedState(); $orderState->toPowerExceedState();
break; break;
case StateCode::PORT_SHED:
$orderState->toShedState();
break;
default : default :
break; break;
} }
......
...@@ -14,6 +14,7 @@ use Application\model\OrderModel; ...@@ -14,6 +14,7 @@ use Application\model\OrderModel;
use Application\model\OrderPowerModel; use Application\model\OrderPowerModel;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis; use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
class PowerController extends Controller class PowerController extends Controller
...@@ -48,13 +49,14 @@ class PowerController extends Controller ...@@ -48,13 +49,14 @@ class PowerController extends Controller
$capacity = hexdec($this->request->get('capacity')); $capacity = hexdec($this->request->get('capacity'));
if (isset($_SESSION['port_id_logic']) == false) {
if (isset($_SESSION['port_logic_id']) == false) {
return; return;
} }
$portsLogicId = array_flip($_SESSION['port_id_logic']);
$portsLogicId = $_SESSION['port_logic_id'];
if (isset($portsLogicId[$logicId])) { if (isset($portsLogicId[$logicId])) {
$portId = $portsLogicId[$logicId]; $portId = $portsLogicId[$logicId];
$port = new Port($portId); $port = new Port($portId);
...@@ -63,6 +65,8 @@ class PowerController extends Controller ...@@ -63,6 +65,8 @@ class PowerController extends Controller
if ($orderId == false || $orderId == '') { if ($orderId == false || $orderId == '') {
return; return;
} }
$this->logRecord->setOrderId($orderId);
$this->logRecord->setPortId($portId);
$order = new Order($orderId); $order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) { if ($order->getOrderCache(\Events::getRedis()) != false) {
...@@ -80,6 +84,7 @@ class PowerController extends Controller ...@@ -80,6 +84,7 @@ class PowerController extends Controller
} }
$orderPowerModel = new OrderPowerModel(); $orderPowerModel = new OrderPowerModel();
$orderPowerModel->insertRow([ $orderPowerModel->insertRow([
'order_id' => $orderId, 'order_id' => $orderId,
'capacity' => $capacity, 'capacity' => $capacity,
...@@ -89,6 +94,10 @@ class PowerController extends Controller ...@@ -89,6 +94,10 @@ class PowerController extends Controller
]); ]);
} else {
Log::debug('no port_id');
} }
...@@ -97,6 +106,88 @@ class PowerController extends Controller ...@@ -97,6 +106,88 @@ class PowerController extends Controller
public function plural() public function plural()
{ {
$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/28
* Time: 上午9:56
*/
namespace Application\controller\card;
use Application\controller\Controller;
use Application\model\CardModel;
class BalanceController extends Controller
{
protected $parameterMap = [
'32' => [
'card_id' => 6
]
];
/**
* 获取卡的余额
*/
public function get()
{
$cardId = $this->request->get('card_id');
$cardModel = new CardModel();
$balance = $cardModel->getColumn('balance', 'card_number = ' . $cardId);
$balance = isset($balance) ? $balance : 0;
$balance = sprintf('%04x', $balance);
var_dump($balance);
$this->response->send('32', $cardId . $balance);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/28
* Time: 上午9:52
*/
namespace Application\controller\card;
use Application\controller\Controller;
use Application\model\BillTmpModel;
use GatewayWorker\Lib\Gateway;
class PayRuleController extends Controller
{
protected $parameterMap = [
'31' => [
]
];
/**
* 获取扣费套餐
*/
public function get()
{
$billingTmp = new BillTmpModel();
if (isset($_SESSION['station_id']) == false) {
Gateway::closeClient($this->clientId);
}
$rule = $billingTmp->getColumn('context', 'id = ' . $_SESSION['station_id']);
if (isset($rule)) {
$rule = json_decode($rule, true);
$back = [];
$count = count($rule);
foreach ($rule as $item) {
$time = sprintf('%02x', $item['time']);
$price = sprintf('%02x', $item['price']);
array_push($back, $time . $price);
}
$this->response->send('31', sprintf('%02x', $count) . implode($back));
}
}
}
\ No newline at end of file
...@@ -18,7 +18,7 @@ class SettingController extends Controller ...@@ -18,7 +18,7 @@ class SettingController extends Controller
protected $parameterMap = [ protected $parameterMap = [
'0a' => [] '0A' => []
]; ];
public function __construct(string $clientId, string $message, string $command) public function __construct(string $clientId, string $message, string $command)
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/28
* Time: 上午9:54
*/
namespace Application\model;
class BillTmpModel extends Model
{
protected $table = 'billing_tmps';
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/28
* Time: 上午9:57
*/
namespace Application\model;
class CardModel extends Model
{
protected $table = 'cards';
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/24
* Time: 上午10:15
*/
namespace Application\model;
class ExceptionModel extends Model
{
protected $table = 'exception';
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/23
* Time: 下午7:46
*/
namespace Application\model;
class ParkModel extends Model
{
protected $table = 'parks';
}
\ No newline at end of file
...@@ -14,4 +14,5 @@ class StationModel extends Model ...@@ -14,4 +14,5 @@ class StationModel extends Model
protected $table = 'stations'; protected $table = 'stations';
} }
\ No newline at end of file
...@@ -22,7 +22,7 @@ class Api ...@@ -22,7 +22,7 @@ class Api
$this->add('04','business\charger\StartController@start'); $this->add('04','business\charger\StartController@start');
$this->add('05','business\charger\OverController@over'); $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('07','business\power\PowerController@plural');
$this->add('17','business\power\PowerController@one'); $this->add('17','business\power\PowerController@one');
...@@ -33,8 +33,8 @@ class Api ...@@ -33,8 +33,8 @@ class Api
$this->add('08','business\port\OnlineController@online'); $this->add('08','business\port\OnlineController@online');
$this->add('09','business\port\OfflineController@offline'); $this->add('09','business\port\OfflineController@offline');
$this->add('0f','business\power\ShutdownController@shutdown'); $this->add('0F','business\power\ShutdownController@shutdown');
$this->add('0a','setting\SettingController@get'); $this->add('0A','setting\SettingController@get');
} }
......
...@@ -52,5 +52,7 @@ class StateCode ...@@ -52,5 +52,7 @@ class StateCode
const FINISH_EXCEPTION_REFUND = 44; const FINISH_EXCEPTION_REFUND = 44;
const CHANGE_PORT = 47;
} }
\ No newline at end of file
...@@ -33,32 +33,34 @@ class Order ...@@ -33,32 +33,34 @@ class Order
/** /**
* @var int * @var int
*/ */
private $purchaseTime; private $purchaseTime = 0;
/** /**
* @var string * @var string
*/ */
private $startTime; private $startTime = '';
/** /**
* @var string * @var string
*/ */
private $finishTime; private $finishTime = '';
/** /**
* @var string * @var string
*/ */
private $shedTime; private $shedTime = '';
/** /**
* @var string * @var string
*/ */
private $offlineTime; private $offlineTime = '0';
/** /**
* @var string * @var string
*/ */
private $onlineTime; private $onlineTime = '';
/** /**
* 订单状态 * 订单状态
...@@ -80,7 +82,7 @@ class Order ...@@ -80,7 +82,7 @@ class Order
/** /**
* @var int * @var int
*/ */
private $portId; private $portId = 0;
/** /**
...@@ -120,6 +122,11 @@ class Order ...@@ -120,6 +122,11 @@ class Order
/** /**
* @var int * @var int
*/ */
private $stationId;
/**
* @var int
*/
private $startCapacity = -1; private $startCapacity = -1;
...@@ -154,7 +161,8 @@ class Order ...@@ -154,7 +161,8 @@ class Order
return isset($this->$name) ? $this->$name : ''; return isset($this->$name) ? $this->$name : '';
} }
public function __construct(int $orderId = 0) { public function __construct(int $orderId = 0)
{
$this->id = $orderId; $this->id = $orderId;
...@@ -182,8 +190,10 @@ class Order ...@@ -182,8 +190,10 @@ class Order
$this->orderCache = $redis->hGetAll($keyName); $this->orderCache = $redis->hGetAll($keyName);
var_dump($this->orderCache); if (count($this->orderCache) < 5) {
return false;
}
foreach ($this->orderCache as $key => $value) { foreach ($this->orderCache as $key => $value) {
$this->$key = $value; $this->$key = $value;
...@@ -196,6 +206,16 @@ class Order ...@@ -196,6 +206,16 @@ class Order
} }
/** /**
* 删除订单的缓存
* @param \Redis $redis
*/
public function delCache(\Redis $redis)
{
$keyName = $this->getCacheKeyName();
$redis->del($keyName);
}
/**
* @param \Redis $redis * @param \Redis $redis
* @return array|bool * @return array|bool
*/ */
...@@ -592,7 +612,21 @@ class Order ...@@ -592,7 +612,21 @@ class Order
$this->endCode = $endCode; $this->endCode = $endCode;
} }
/**
* @return int
*/
public function getStationId(): int
{
return $this->stationId;
}
/**
* @param int $stationId
*/
public function setStationId(int $stationId): void
{
$this->stationId = $stationId;
}
} }
\ No newline at end of file
...@@ -46,6 +46,9 @@ class OrderSaveRedis ...@@ -46,6 +46,9 @@ class OrderSaveRedis
$this->redis->hMset($this->order->getCacheKeyName(), $update); $this->redis->hMset($this->order->getCacheKeyName(), $update);
$this->redis->expire($this->order->getCacheKeyName(), 60 * 60 * 24);
} }
......
...@@ -13,9 +13,11 @@ use Application\model\OrderExceptionModel; ...@@ -13,9 +13,11 @@ use Application\model\OrderExceptionModel;
use Application\model\OrderModel; use Application\model\OrderModel;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis; use Lib\cdz\order\save\OrderSaveRedis;
use Lib\Gateway;
use Lib\log\Log; use Lib\log\Log;
use Lib\rabbitmq\PublishPortShed; use Lib\rabbitmq\PublishPortShed;
use Lib\remote\api\message\ChargerFullMessage; use Lib\remote\api\message\ChargerFullMessage;
use Lib\remote\api\message\ChargerShedMessage;
use Lib\remote\api\message\ChargerTimeoutMessage; use Lib\remote\api\message\ChargerTimeoutMessage;
use Lib\remote\api\message\FinishByUserMessage; use Lib\remote\api\message\FinishByUserMessage;
use Lib\remote\api\message\PowerExceedMessage; use Lib\remote\api\message\PowerExceedMessage;
...@@ -23,6 +25,7 @@ use Lib\remote\api\message\SendMessage; ...@@ -23,6 +25,7 @@ use Lib\remote\api\message\SendMessage;
use Lib\remote\api\message\ShedMessage; use Lib\remote\api\message\ShedMessage;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
use Src\BroadcastToAgent;
class ChargerState extends State class ChargerState extends State
{ {
...@@ -42,23 +45,29 @@ class ChargerState extends State ...@@ -42,23 +45,29 @@ class ChargerState extends State
$orderModel->update([ $orderModel->update([
'is_stop' => $this->order->getEndCode(), 'is_stop' => $this->order->getEndCode(),
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'updated_at' => date('Y-m-d H:i:s'), 'updated_at' => $this->nowDate,
'end_time' => $this->nowDate,
'state' => 1, 'state' => 1,
], 'id = ' . $this->order->getId()); ], 'id = ' . $this->order->getId());
$port = new Port(); $port = new Port();
$port->setState(StateCode::PORT_ONLINE);
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
$port->setGatewayId($this->order->getGatewayId());
$port->freePort(); $port->setStationId($this->order->getStationId());
//释放口子状态
$port->freePort($this->order->getId());
//删除订单的缓存
$this->order->delCache(\Events::getRedis());
//用户主动结束充电推送 //用户主动结束充电推送
$finishByUserMessage = new FinishByUserMessage(); $finishByUserMessage = new FinishByUserMessage();
$finishByUserMessage->setOrderId($this->order->getId()); $finishByUserMessage->setOrderId($this->order->getId());
$finishByUserMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName()); $finishByUserMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName());
$chargerTime = time() - $this->order->getStartTime(); $finishByUserMessage->setUserId($this->order->getUserId());
$chargerTime = (int)time() - (int)$this->order->getStartTime();
$chargerTime /= 60; $chargerTime /= 60;
$chargerTime = (int)$chargerTime; $chargerTime = (int)$chargerTime;
$finishByUserMessage->setChargerTime($chargerTime); $finishByUserMessage->setChargerTime($chargerTime);
$finishByUserMessage->setEndTime($this->nowDate); $finishByUserMessage->setEndTime($this->nowDate);
...@@ -71,7 +80,6 @@ class ChargerState extends State ...@@ -71,7 +80,6 @@ class ChargerState extends State
$orderException = new OrderExceptionModel(); $orderException = new OrderExceptionModel();
$orderException->insertRow([ $orderException->insertRow([
'order_id' => $this->order->getId(), 'order_id' => $this->order->getId(),
'state' => StateCode::PORT_CHARGER . StateCode::FINISH_USER, 'state' => StateCode::PORT_CHARGER . StateCode::FINISH_USER,
'desc' => '用户主动结束充电', 'desc' => '用户主动结束充电',
...@@ -96,6 +104,7 @@ class ChargerState extends State ...@@ -96,6 +104,7 @@ class ChargerState extends State
public function toExceptionState() public function toExceptionState()
{ {
// TODO: Implement toExceptionState() method. // TODO: Implement toExceptionState() method.
} }
...@@ -105,6 +114,7 @@ class ChargerState extends State ...@@ -105,6 +114,7 @@ class ChargerState extends State
* 推送消息 * 推送消息
* 更新口子状态释放订单 * 更新口子状态释放订单
* 记录订单异常 * 记录订单异常
* 推送给agentId
* *
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
*/ */
...@@ -116,7 +126,7 @@ class ChargerState extends State ...@@ -116,7 +126,7 @@ class ChargerState extends State
$orderModel->update([ $orderModel->update([
'is_stop' => StateCode::FINISH_EXCEED, 'is_stop' => StateCode::FINISH_EXCEED,
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s', $this->nowTimeStamp), 'end_time' => $this->nowDate,
'state' => 1, 'state' => 1,
], 'id = ' . $this->order->getId()); ], 'id = ' . $this->order->getId());
...@@ -144,7 +154,14 @@ class ChargerState extends State ...@@ -144,7 +154,14 @@ class ChargerState extends State
$port = new Port(); $port = new Port();
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
$port->freePort(); $port->setState(StateCode::PORT_ONLINE);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
$port->freePort($this->order->getId());
$broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::FINISH_EXCEED]);
} }
...@@ -187,7 +204,15 @@ class ChargerState extends State ...@@ -187,7 +204,15 @@ class ChargerState extends State
} }
$port = new Port(); $port = new Port();
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
$port->freePort(); $port->setState(StateCode::PORT_ONLINE);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
$port->freePort($this->order->getId());
$broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::FINISH_FULL]);
} }
/** /**
...@@ -196,6 +221,7 @@ class ChargerState extends State ...@@ -196,6 +221,7 @@ class ChargerState extends State
* 推送用户信息 * 推送用户信息
* 发布延时任务十分钟后检测端口 * 发布延时任务十分钟后检测端口
* *
*
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
* @throws \AMQPConnectionException * @throws \AMQPConnectionException
* @throws \AMQPExchangeException * @throws \AMQPExchangeException
...@@ -230,13 +256,21 @@ class ChargerState extends State ...@@ -230,13 +256,21 @@ class ChargerState extends State
'created_at' => $this->nowDate, 'created_at' => $this->nowDate,
]); ]);
$shedMessage = new ShedMessage(); //更新端口状态和订单状态
$shedMessage->setOrderId($this->order->getId()); $port = new Port($this->order->getPortId());
$shedMessage->setPortName($this->order->getPortName()); $port->setState(StateCode::PORT_SHED);
$shedMessage->setStationName($this->order->getStationName()); $port->setGatewayId($this->order->getGatewayId());
$shedMessage->setTime($this->nowDate); $port->setStationId($this->order->getStationId());
$port->updatePortState($this->order->getId());
$chargerShedMessage = new ChargerShedMessage();
$chargerShedMessage->setStationName($this->order->getStationName());
$chargerShedMessage->setPortName($this->order->getPortName());
$chargerShedMessage->setUserId($this->order->getUserId());
$chargerShedMessage->setDate($this->nowDate);
$sendMessage = new SendMessage(); $sendMessage = new SendMessage();
if ($sendMessage->request($shedMessage) == false) { if ($sendMessage->request($chargerShedMessage) == false) {
Log::error('send port shed message fail'); Log::error('send port shed message fail');
} }
...@@ -249,7 +283,11 @@ class ChargerState extends State ...@@ -249,7 +283,11 @@ class ChargerState extends State
$publish->publishOrder([ $publish->publishOrder([
'order_id' => $this->order->getId(), 'order_id' => $this->order->getId(),
'shed_time' => $this->nowTimeStamp, 'shed_time' => $this->nowTimeStamp,
'port_id' => $this->order->getPortId() 'port_id' => $this->order->getPortId(),
'gateway_mac' => $this->order->getGatewayMac(),
'check_times' => 1,
'gateway_id' => $this->order->getGatewayId(),
'station_id' => $_SESSION['station_id'],
]); ]);
} catch (\Exception $exception) { } catch (\Exception $exception) {
...@@ -257,6 +295,9 @@ class ChargerState extends State ...@@ -257,6 +295,9 @@ class ChargerState extends State
} }
$broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::PORT_SHED]);
} }
...@@ -280,18 +321,24 @@ class ChargerState extends State ...@@ -280,18 +321,24 @@ class ChargerState extends State
$orderModel->update([ $orderModel->update([
'is_stop' => StateCode::FINISH_TIME_END, 'is_stop' => StateCode::FINISH_TIME_END,
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->nowDate,
'updated_at' => $this->nowDate, 'updated_at' => $this->nowDate,
'state' => 1,
], 'id = ' . $this->order->getId()); ], 'id = ' . $this->order->getId());
$port = new Port(); $port = new Port();
$port->setPortId($this->order->getId()); $port->setPortId($this->order->getPortId());
$port->freePort(); $port->setState(StateCode::PORT_ONLINE);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
$port->freePort($this->order->getId());
$chargerTimeoutMessage = new ChargerTimeoutMessage(); $chargerTimeoutMessage = new ChargerTimeoutMessage();
$chargerTimeoutMessage->setStationName($this->order->getStationName()); $chargerTimeoutMessage->setStationName($this->order->getStationName());
$chargerTimeoutMessage->setPortName($this->order->getPortName()); $chargerTimeoutMessage->setPortName($this->order->getPortName());
$chargerTimeoutMessage->setOrderId($this->order->getId()); $chargerTimeoutMessage->setOrderId($this->order->getId());
$chargerTimeoutMessage->setEndTime($this->nowDate); $chargerTimeoutMessage->setEndTime($this->nowDate);
$chargerTimeoutMessage->setUserId($this->order->getUserId());
$chargerTime = $this->nowTimeStamp - $this->order->getStartTime(); $chargerTime = $this->nowTimeStamp - $this->order->getStartTime();
$chargerTime /= 60; $chargerTime /= 60;
...@@ -306,6 +353,14 @@ class ChargerState extends State ...@@ -306,6 +353,14 @@ class ChargerState extends State
} }
/**
* 关闭失败
*/
public function stopFail()
{
}
} }
......
...@@ -65,5 +65,10 @@ class ExceptionState extends State ...@@ -65,5 +65,10 @@ class ExceptionState extends State
// TODO: Implement toTimeoutState() method. // TODO: Implement toTimeoutState() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -11,6 +11,7 @@ namespace Lib\cdz\order\state; ...@@ -11,6 +11,7 @@ namespace Lib\cdz\order\state;
use Lib\cdz\order\NoticePortShed; use Lib\cdz\order\NoticePortShed;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\log\Log;
class OfflineNetState extends State class OfflineNetState extends State
{ {
...@@ -27,11 +28,27 @@ class OfflineNetState extends State ...@@ -27,11 +28,27 @@ class OfflineNetState extends State
$startTime = $this->order->getStartTime(); $startTime = $this->order->getStartTime();
$purchaseTime = $this->order->getPurchaseTime(); 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($purchaseTime * 60);
$this->remainChargerTime = (int)($this->remainChargerTime/60); Log::debug($this->order->getOfflineTime());
Log::debug($this->order->getStartTime());
$this->remainChargerTime = $purchaseTime * 60 - ((int)$this->order->getOnlineTime() - $startTime);
Log::debug('---');
Log::debug($this->remainChargerTime);
$this->remainChargerTime = (int)($this->remainChargerTime / 60);
Log::debug($this->remainChargerTime);
}
} }
...@@ -49,15 +66,13 @@ class OfflineNetState extends State ...@@ -49,15 +66,13 @@ class OfflineNetState extends State
public function toChargerState() public function toChargerState()
{ {
if($this->order->getIsRefund() === 1) if ($this->order->getIsRefund() === 1) {
{
//已经退款 下发结束充电命令 //已经退款 下发结束充电命令
$this->order->stop($this->order,$_SESSION['client_id'],43); $this->order->stop($this->order, $_SESSION['client_id'], 43);
return; return;
} }
} }
public function toExceptionState() public function toExceptionState()
...@@ -81,8 +96,7 @@ class OfflineNetState extends State ...@@ -81,8 +96,7 @@ class OfflineNetState extends State
*/ */
public function toShedState() public function toShedState()
{ {
if($this->remainChargerTime > 0) if ($this->remainChargerTime > 0) {
{
$this->publishPortShed($this->order); $this->publishPortShed($this->order);
} }
...@@ -115,6 +129,10 @@ class OfflineNetState extends State ...@@ -115,6 +129,10 @@ class OfflineNetState extends State
$this->remainChargerTime = $remainChargerTime; $this->remainChargerTime = $remainChargerTime;
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -27,7 +27,7 @@ class OfflinePowerState extends State ...@@ -27,7 +27,7 @@ class OfflinePowerState extends State
/** /**
* @var int * @var int
*/ */
private $remainChargerTime =0; private $remainChargerTime = 0;
public function toWaitingState() public function toWaitingState()
{ {
...@@ -36,8 +36,7 @@ class OfflinePowerState extends State ...@@ -36,8 +36,7 @@ class OfflinePowerState extends State
public function toChargerState() public function toChargerState()
{ {
if($this->order->getIsRefund() === 1) if ($this->order->getIsRefund() === 1) {
{
//已经退款 下发结束充电命令 //已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43); //$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0; $this->remainChargerTime = 0;
...@@ -49,9 +48,9 @@ class OfflinePowerState extends State ...@@ -49,9 +48,9 @@ class OfflinePowerState extends State
$startTime = $this->order->getStartTime(); $startTime = $this->order->getStartTime();
$purchaseTime = $this->order->getPurchaseTime(); $purchaseTime = $this->order->getPurchaseTime();
$this->remainChargerTime = $purchaseTime *60- ($offlineTime - $startTime); $this->remainChargerTime = $purchaseTime * 60 - ($offlineTime - $startTime);
$this->remainChargerTime = (int)($this->remainChargerTime/60); $this->remainChargerTime = (int)($this->remainChargerTime / 60);
} }
...@@ -85,5 +84,10 @@ class OfflinePowerState extends State ...@@ -85,5 +84,10 @@ class OfflinePowerState extends State
// TODO: Implement toTimeoutState() method. // TODO: Implement toTimeoutState() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -64,5 +64,10 @@ class PowerExceedState extends State ...@@ -64,5 +64,10 @@ class PowerExceedState extends State
// TODO: Implement toTimeoutState() method. // TODO: Implement toTimeoutState() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -64,5 +64,10 @@ class PowerFullState extends State ...@@ -64,5 +64,10 @@ class PowerFullState extends State
// TODO: Implement toUserFinish() method. // TODO: Implement toUserFinish() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -9,8 +9,18 @@ ...@@ -9,8 +9,18 @@
namespace Lib\cdz\order\state; namespace Lib\cdz\order\state;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order; 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 class ShedState extends State
{ {
...@@ -19,50 +29,245 @@ class ShedState extends State ...@@ -19,50 +29,245 @@ class ShedState extends State
parent::__construct($order); parent::__construct($order);
} }
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toUserFinish() public function toUserFinish()
{ {
// TODO: Implement toUserFinish() method. $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() public function toWaitingState()
{ {
// TODO: Implement toWaitingState() method. Log::error('shed state can not change to waiting state');
} }
/**
* 更新订单状态
* 更新口子状态
* 推送微信消息
* 记录订单异常
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toChargerState() public function toChargerState()
{ {
// TODO: Implement toChargerState() method. $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() public function toExceptionState()
{ {
// TODO: Implement toExceptionState() method.
//todo
} }
public function toPowerExceedState() public function toPowerExceedState()
{ {
// TODO: Implement toPowerExceedState() method. Log::error('shed state can not change to power exceed state');
} }
public function toPowerFullState() public function toPowerFullState()
{ {
// TODO: Implement toPowerFullState() method. Log::error('shed state can not change to power full state');
} }
public function toShedState() public function toShedState()
{ {
// TODO: Implement toShedState() method. Log::error('shed state can not change to shed');
} }
/**
* 记录订单异常
* 释放口子
* 推送用户消息
* 更新订单记录,记录结束用电量
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function toShedTimeoutState() public function toShedTimeoutState()
{ {
// TODO: Implement toShedTimeoutState() method. $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() public function toTimeoutState()
{ {
// TODO: Implement toTimeoutState() method. $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.
} }
......
...@@ -63,5 +63,10 @@ class ShedTimeoutState extends State ...@@ -63,5 +63,10 @@ class ShedTimeoutState extends State
// TODO: Implement toUserFinish() method. // TODO: Implement toUserFinish() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -62,5 +62,25 @@ abstract class State ...@@ -62,5 +62,25 @@ abstract class State
public abstract function toUserFinish(); 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
...@@ -64,5 +64,10 @@ class TimeoutState extends State ...@@ -64,5 +64,10 @@ class TimeoutState extends State
// TODO: Implement toUserFinish() method. // TODO: Implement toUserFinish() method.
} }
public function stopFail()
{
// TODO: Implement stopFail() method.
}
} }
\ No newline at end of file
...@@ -58,7 +58,15 @@ class WaitingState extends State ...@@ -58,7 +58,15 @@ class WaitingState extends State
'is_stop' => StateCode::FINISH_REFUND, 'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s'), 'end_time' => date('Y-m-d H:i:s'),
],'id = '.$this->order->getId()); ], '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());
//释放端口 //释放端口
} }
...@@ -76,8 +84,7 @@ class WaitingState extends State ...@@ -76,8 +84,7 @@ class WaitingState extends State
public function toChargerState(): void public function toChargerState(): void
{ {
//订单已经退款则直接结束 //订单已经退款则直接结束
if($this->order->getIsRefund() == 1) if ($this->order->getIsRefund() == 1) {
{
//todo 下发结束充电命令 //todo 下发结束充电命令
$orderId = $this->order->getId(); $orderId = $this->order->getId();
$portLogic = $this->order->getPortLogicId(); $portLogic = $this->order->getPortLogicId();
...@@ -97,7 +104,7 @@ class WaitingState extends State ...@@ -97,7 +104,7 @@ class WaitingState extends State
$this->order->setStateCode(12); $this->order->setStateCode(12);
\Events::pipe(); \Events::pipe();
$orderSaveRedis = new OrderSaveRedis($this->order); $orderSaveRedis = new OrderSaveRedis($this->order);
$orderSaveRedis->save(['stateCode', 'startTime']); $orderSaveRedis->save(['stateCode', 'startTime', 'startCapacity']);
//更新口子状态 //更新口子状态
$port = new Port(); $port = new Port();
...@@ -105,7 +112,7 @@ class WaitingState extends State ...@@ -105,7 +112,7 @@ class WaitingState extends State
$port->setState(StateCode::PORT_CHARGER); $port->setState(StateCode::PORT_CHARGER);
$port->setGatewayId($this->order->getGatewayId()); $port->setGatewayId($this->order->getGatewayId());
$port->setStationId($_SESSION['station_id']); $port->setStationId($_SESSION['station_id']);
$port->updatePortState(); $port->updatePortState($this->order->getId());
\Events::exec(); \Events::exec();
...@@ -114,8 +121,7 @@ class WaitingState extends State ...@@ -114,8 +121,7 @@ class WaitingState extends State
$startMessage->setUserId($this->order->getUserId()); $startMessage->setUserId($this->order->getUserId());
$startMessage->setAmount($this->order->getPayAmount()); $startMessage->setAmount($this->order->getPayAmount());
$startMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName()); $startMessage->setAddress($this->order->getStationName() . '--' . $this->order->getPortName());
$startMessage->setRemark($startMessage->getRemark() . sprintf('%012x', $this->order->getId())); $startMessage->setOrderId($this->order->getOutTradeNo());
$sendMessage = new SendMessage(); $sendMessage = new SendMessage();
if ($sendMessage->request($startMessage) === false) { if ($sendMessage->request($startMessage) === false) {
...@@ -128,7 +134,6 @@ class WaitingState extends State ...@@ -128,7 +134,6 @@ class WaitingState extends State
'is_charger' => StateCode::PORT_CHARGER, 'is_charger' => StateCode::PORT_CHARGER,
'start_time' => date('Y-m-d H:i:s', $this->order->getStartTime()), 'start_time' => date('Y-m-d H:i:s', $this->order->getStartTime()),
'start_capacity' => $this->order->getStartCapacity(), 'start_capacity' => $this->order->getStartCapacity(),
]; ];
$orderModel = new OrderModel(); $orderModel = new OrderModel();
...@@ -140,9 +145,9 @@ class WaitingState extends State ...@@ -140,9 +145,9 @@ class WaitingState extends State
//添加订单异常 //添加订单异常
$orderExceptionModel->insertRow([ $orderExceptionModel->insertRow([
'order_id' => $this->order->getId(), 'order_id' => $this->order->getId(),
'state' => '-1' . StateCode::PORT_CHARGER, 'state' => '-1' . StateCode::PORT_CHARGER,
'desc' => '开始充电', 'desc' => '开始充电',
'created_at' => date('Y-m-d H:i:s', $this->order->getStartTime()), 'created_at' => date('Y-m-d H:i:s', $this->order->getStartTime()),
]); ]);
...@@ -165,9 +170,9 @@ class WaitingState extends State ...@@ -165,9 +170,9 @@ class WaitingState extends State
$orderExceptionModel = new OrderExceptionModel(); $orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([ $orderExceptionModel->insertRow([
'order_id' => $this->order->getId(), 'order_id' => $this->order->getId(),
'state' => '-1' . StateCode::PORT_EXCEPTION, 'state' => '-1' . StateCode::PORT_EXCEPTION,
'desc' => '打开口子失败', 'desc' => '打开口子失败',
'created_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s'),
]); ]);
//推送消息 //推送消息
...@@ -186,7 +191,7 @@ class WaitingState extends State ...@@ -186,7 +191,7 @@ class WaitingState extends State
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_EXCEPTION); $port->setState(StateCode::PORT_EXCEPTION);
$port->updatePortState(); $port->updatePortState($this->order->getId());
// TODO: Implement toExceptionState() method. // TODO: Implement toExceptionState() method.
} }
...@@ -196,37 +201,37 @@ class WaitingState extends State ...@@ -196,37 +201,37 @@ class WaitingState extends State
*/ */
public function toPowerExceedState() public function toPowerExceedState()
{ {
Log::error('waiting state can not change to power exceed state');
// TODO: Implement toPowerExceedState() method.
} }
public function toPowerFullState() public function toPowerFullState()
{ {
Log::error('waiting state can not change to power full state'); Log::error('waiting state can not change to power full state');
// TODO: Implement toPowerFullState() method.
} }
public function toShedState() public function toShedState()
{ {
// TODO: Implement toShedState() method.
Log::error('waiting state can not change to shed state');
} }
public function toShedTimeoutState() public function toShedTimeoutState()
{ {
Log::error('waiting state can not change to shed timeout'); Log::error('waiting state can not change to shed timeout');
// TODO: Implement toShedTimeoutState() method.
} }
public function toTimeoutState() public function toTimeoutState()
{ {
Log::error('waiting state can not change to timeout state'); Log::error('waiting state can not change to timeout state');
// TODO: Implement toTimeoutState() method. }
public function stopFail()
{
// TODO: Implement stopFail() method.
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/6
* Time: 上午9:45
*/
namespace Lib\remote\api\message;
class ChargerReloadMessage extends Message
{
protected $templateId = 't_PyOGCKN_fmD0blw0EuIQs-8sS3qOTmpQay12haPZY';
private $first = '尊敬的用户,检测到插头插入,您的爱车已开始继续充电';
private $stationName = '';
private $portName = '';
private $time = '';
private $remark = '';
public function __construct()
{
$this->templateId = getenv('MESSAGE_RELOAD');
}
public function getPushJson(): string
{
return json_encode([
'first' => [
'value' => $this->first,
'color' => '#333',
],
'keyword1' => [
'value' => $this->stationName,
'color' => '#333',
],
'keyword2' => [
'value' => $this->portName,
'color' => '#333',
],
'keyword3' => [
'value' => date('Y-m-d H:i:s'),
'color' => '#333',
],
'remark' => [
'value' => '感谢你的使用',
'color' => '#333',
],
]);
// TODO: Implement getPushJson() method.
}
/**
* @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 getStationName(): string
{
return $this->stationName;
}
/**
* @param string $stationName
*/
public function setStationName(string $stationName): void
{
$this->stationName = $stationName;
}
/**
* @return string
*/
public function getPortName(): string
{
return $this->portName;
}
/**
* @param string $portName
*/
public function setPortName(string $portName): void
{
$this->portName = $portName;
}
/**
* @return string
*/
public function getTime(): string
{
return $this->time;
}
/**
* @param string $time
*/
public function setTime(string $time): void
{
$this->time = $time;
}
/**
* @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
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/27
* Time: 下午5:04
*/
namespace Lib\remote\api\message;
/**
* Class ChargerShedMessage
* @package Lib\remote\api\message
*/
class ChargerShedMessage extends Message
{
protected $templateId = 'C0LvfETe-1rCR_IhOydDGPTUwwYkrWZxgfwMsETrhw8';
private $first = '尊敬的 户,充电端未检测到插头,请您在10分钟内接入。';
private $stationName = '';
private $portName = '';
private $date = '';
private $remark = '感谢您的使用。';
public function __construct()
{
$this->templateId = getenv('MESSAGE_CHARGER_SHED');
}
public function getPushJson(): string
{
return json_encode([
'first' => [
'value' => $this->first,
'color' => '#333',
],
'keyword1' => [
'value' => $this->stationName . '--' . $this->portName,
'color' => '#333',
],
'keyword2' => [
'value' => $this->date,
'color' => '#333',
],
'remark' => [
'value' => '感谢你的使用',
'color' => '#333',
],
]);
}
/**
* @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 getStationName(): string
{
return $this->stationName;
}
/**
* @param string $stationName
*/
public function setStationName(string $stationName): void
{
$this->stationName = $stationName;
}
/**
* @return string
*/
public function getPortName(): string
{
return $this->portName;
}
/**
* @param string $portName
*/
public function setPortName(string $portName): void
{
$this->portName = $portName;
}
/**
* @return string
*/
public function getDate(): string
{
return $this->date;
}
/**
* @param string $date
*/
public function setDate(string $date): void
{
$this->date = $date;
}
/**
* @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
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/19
* Time: 下午3:23
*/
namespace Lib\remote\api\message;
class ChargerShedTimeOutMessage extends Message
{
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 __construct()
{
$this->templateId = getenv('MESSAGE_SHED_TIMEOUT');
}
public function getPushJson(): 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,11 +24,6 @@ class FailMessage extends Message ...@@ -24,11 +24,6 @@ class FailMessage extends Message
/** /**
* @var string * @var string
*/ */
private $orderId = '';
/**
* @var string
*/
private $address = ''; private $address = '';
/** /**
...@@ -71,7 +66,7 @@ class FailMessage extends Message ...@@ -71,7 +66,7 @@ class FailMessage extends Message
'value' => number_format($this->amount / 100.0, 2), 'value' => number_format($this->amount / 100.0, 2),
'color' => '#333', 'color' => '#333',
], ],
'remark' => [ 'remark' => [
'value' => $this->remark, 'value' => $this->remark,
'color' => '#333', 'color' => '#333',
], ],
...@@ -80,7 +75,6 @@ class FailMessage extends Message ...@@ -80,7 +75,6 @@ class FailMessage extends Message
} }
/** /**
* @return string * @return string
*/ */
...@@ -97,21 +91,6 @@ class FailMessage extends Message ...@@ -97,21 +91,6 @@ class FailMessage extends Message
$this->first = $first; $this->first = $first;
} }
/**
* @return string
*/
public function getOrderId(): string
{
return $this->orderId;
}
/**
* @param string $orderId
*/
public function setOrderId(string $orderId): void
{
$this->orderId = $orderId;
}
/** /**
* @return string * @return string
......
...@@ -37,23 +37,25 @@ class SendMessage ...@@ -37,23 +37,25 @@ class SendMessage
{ {
$client = new Client([ $client = new Client([
'base_uri' => $this->uri, 'base_uri' => $this->uri,
'http_errors' => false, 'http_errors' => false,
]); ]);
$response = $client->request('POST', $this->uri, [ $response = $client->request('POST', $this->path, [
'form_params' => [ 'form_params' => [
'userId' => $message->getUserId(), 'userId' => $message->getUserId(),
'templateId' => $message->getTemplateId(), 'templateId' => $message->getTemplateId(),
'data' => $message->getPushJson(), 'data' => $message->getPushJson(),
], ],
]); ]);
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
Log::debug('发送模版消息'); Log::debug('发送模版消息');
} else {
Log::error('send wx message fail ', ['http_code' => $response->getStatusCode()]);
} }
......
...@@ -77,8 +77,8 @@ class StartMessage extends Message ...@@ -77,8 +77,8 @@ class StartMessage extends Message
'value' => number_format($this->amount / 100, 2), 'value' => number_format($this->amount / 100, 2),
'color' => '#333', 'color' => '#333',
], ],
'remark' => [ 'remark' => [
'value' => $this->remark . '\n' . "($this->orderId)", 'value' => $this->remark . "($this->orderId)",
'color' => '#333', 'color' => '#333',
], ],
]); ]);
...@@ -135,6 +135,4 @@ class StartMessage extends Message ...@@ -135,6 +135,4 @@ class StartMessage extends Message
} }
} }
\ No newline at end of file
...@@ -20,12 +20,12 @@ class Gateway ...@@ -20,12 +20,12 @@ class Gateway
* @param int $state * @param int $state
* @return int * @return int
*/ */
public function setState(int $gatewayId,int $state) public function setState(int $gatewayId, int $state)
{ {
$redis = \Events::getRedis(); $redis = \Events::getRedis();
return $redis->zAdd(self::GATEWAY_STATE,$state,$gatewayId); return $redis->zAdd(self::GATEWAY_STATE, $state, $gatewayId);
} }
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
namespace Lib\station\gateway\port; namespace Lib\station\gateway\port;
use Application\model\DeviceModel;
use Lib\log\Log;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\Gateway; use Lib\station\gateway\Gateway;
use Lib\station\Station; use Lib\station\Station;
...@@ -22,13 +24,14 @@ class Port ...@@ -22,13 +24,14 @@ class Port
/** /**
* 端口正在充电的订单号 * 端口正在充电的订单号
* cdz:port:using:order_id:hash
*/ */
const USING_ORDER_ID = 'cdz:port:using:order_id:hash'; const USING_ORDER_ID = 'cdz:port:using:order_id:hash';
/** /**
* 端口正在使用的用户号 * 端口正在使用的用户号
*/ */
const USING_USER_ID = 'cdz:port:using_user_id:zset'; const USING_USER_ID = 'cdz:port:using:user_id:zset';
/** /**
* 端口状态有序集合 * 端口状态有序集合
...@@ -58,12 +61,12 @@ class Port ...@@ -58,12 +61,12 @@ class Port
/** /**
* @var int * @var int
*/ */
private $gatewayId; private $gatewayId = 0;
/** /**
* @var int * @var int
*/ */
private $stationId; private $stationId = 0;
public function __construct(int $portId = 0) public function __construct(int $portId = 0)
{ {
...@@ -73,17 +76,36 @@ class Port ...@@ -73,17 +76,36 @@ class Port
/** /**
* 更新端口状态 * 更新端口状态
* @param int $orderId
*/ */
public function updatePortState() public function updatePortState(int $orderId = 0)
{ {
$redis = \Events::getRedis(); $redis = \Events::getRedis();
$redis->zAdd(self::PORT_STATE, $this->state, $this->portId); $redis->zAdd(self::PORT_STATE, $this->state, $this->portId);
if ($this->stationId === 0 || $this->gatewayId === 0) {
if (isset($_SESSION['gateway_id']) && isset($_SESSION['station_id'])) {
$this->stationId = $_SESSION['station_id'];
$this->gatewayId = $_SESSION['gateway_id'];
} else {
Log::error('can not find gateway_id or station_id from session');
return;
}
}
$redis->zAdd(sprintf(Gateway::GATEWAY_PORT_STATE, $this->gatewayId), $this->state, $this->portId); $redis->zAdd(sprintf(Gateway::GATEWAY_PORT_STATE, $this->gatewayId), $this->state, $this->portId);
$redis->zAdd(sprintf(Station::STATION_PORT_STATE, $this->stationId), $this->state, $this->portId); $redis->zAdd(sprintf(Station::STATION_PORT_STATE, $this->stationId), $this->state, $this->portId);
$portModel = new DeviceModel();
$portModel->update([
'state' => $this->state,
'updated_at' => date('Y-m-d H:i:s')
], 'id = ' . $this->portId);
if ($orderId !== 0) {
$redis->zAdd('cdz:order:state:zset', $this->state, $orderId);
}
} }
...@@ -100,8 +122,9 @@ class Port ...@@ -100,8 +122,9 @@ class Port
/** /**
* 释放口子,更新口子状态为空闲 * 释放口子,更新口子状态为空闲
* @param int $orderId
*/ */
public function freePort() public function freePort(int $orderId = 0)
{ {
\Events::pipe(); \Events::pipe();
...@@ -109,8 +132,12 @@ class Port ...@@ -109,8 +132,12 @@ class Port
$this->state = StateCode::PORT_ONLINE; $this->state = StateCode::PORT_ONLINE;
$this->updatePortState(); $this->updatePortState($orderId);
if ($orderId != 0) {
$redis->zRem('cdz:order:state:zset', $orderId);
$redis->del('cdz:order:state:' . $orderId);
}
$redis->zDelete(self::USING_USER_ID, $this->portId); $redis->zDelete(self::USING_USER_ID, $this->portId);
$redis->hDel(self::USING_ORDER_ID, $this->portId); $redis->hDel(self::USING_ORDER_ID, $this->portId);
...@@ -120,6 +147,7 @@ class Port ...@@ -120,6 +147,7 @@ class Port
} }
/** /**
* 释放端口的订单,布更新端口的状态 * 释放端口的订单,布更新端口的状态
*/ */
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/23
* Time: 下午7:55
*/
namespace Src;
use Lib\Gateway;
class BroadcastToAgent
{
private $agentId = '';
public function __construct()
{
if (isset($_SESSION['agent_id'])) {
$this->agentId = $_SESSION['agent_id'];
}
}
public function broadcast(array $data)
{
if ($this->agentId !== '') {
Gateway::sendToUid($this->agentId, json_encode($data));
return true;
} else {
return false;
}
}
}
\ No newline at end of file
...@@ -15,8 +15,6 @@ class Test ...@@ -15,8 +15,6 @@ class Test
public function __destruct() public function __destruct()
{ {
var_dump('i am test');
// TODO: Implement __destruct() method. // TODO: Implement __destruct() method.
} }
} }
\ No newline at end of file
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