Commit df104c15 by banice

测试版

parent 2abe7fc5
......@@ -28,6 +28,8 @@ 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
......@@ -2,7 +2,7 @@ REDIS_HOST=redis.test.senthink.com
REGISTER_IP=127.0.0.1
REGISTER_PORT=1239
GATEWAY_PORT=8282
GATEWAY_PORT=8283
GATEWAY_IP=127.0.0.1
GATEWAY_COUNT=1
GATEWAY_START_PORT=2130
......@@ -25,5 +25,11 @@ MESSAGE_API_URL=https://cdzback2.test.senthink.com
MESSAGE_START=L9SEWl3X73v3PBYHtFBZlTlJeBMdO9DDNIwRgHX6SrY
MESSAGE_FAIL=0al-a6kkNsdqFyXPEe97m4jjtbfOXjEC3eCjEsMHJdw
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
......@@ -27,6 +27,9 @@ class Events
*/
private static $redis;
/**
*
*/
public static function onWorkerStart()
{
......@@ -46,7 +49,6 @@ class Events
static::$redis->select('4');
\Lib\log\Log::bootLog();
......@@ -72,7 +74,6 @@ class Events
$crc = substr($message, -2);
var_dump($message);
$crcConfirm = sprintf('%08x', Crc::crc(substr($message, 0, strlen($message) - 2)));
if ($crc != mb_strtoupper(substr($crcConfirm, 0, 2))) {
......@@ -80,6 +81,7 @@ class Events
return;
}
Log::debug($message);
$command = substr($message, 6, 2);
$api = new Api();
......@@ -110,37 +112,90 @@ class Events
}
/**
* @param $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->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'];
$portsId = array_values($portsMacId);
$port = new Port();
$exceptionModel = new \Application\model\ExceptionModel();
$date = date('Y-m-d H:i:s');
foreach ($portsId as $portId) {
//所有端口状态设置为离线
$port->setPortId($portId);
$port->setState(10);
$port->setGatewayId($gatewayId);
$port->setStationId($stationId);
$port->setState(\Lib\StateCode::PORT_OFFLINE);
$port->updatePortState();
}
//所有端口状态设置为离线
Events::exec();
$exceptionModel->insertRow([
'gateway_id' => $gatewayId,
'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
public function __destruct()
{
if($this->command === '00')
{
return ;
}
$logWriter = new GatewayLogWriter();
if($this->response->getResponseMessage() !== '')
......
......@@ -40,5 +40,7 @@ class HeartController extends Controller
$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;
use Application\controller\Controller;
use Application\model\DeviceModel;
use Application\model\GatewayModel;
use Application\model\ParkModel;
use Application\model\StationModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\state\OfflineNetState;
use Lib\cdz\order\state\OfflinePowerState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\route\Route;
......@@ -67,9 +70,7 @@ class AccessController extends Controller
'id',
], 'mac_address = ' . "'$mac'");
var_dump($gatewayModelResult);
if($gatewayModelResult == false)
{
if ($gatewayModelResult == false) {
Gateway::closeClient($this->clientId);
return;
}
......@@ -81,6 +82,20 @@ class AccessController extends Controller
$this->logRecord->setGatewayId($gatewayId);
$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();
......@@ -152,9 +167,8 @@ class AccessController extends Controller
}
if(isset($_SESSION['port_mac_id']) == false)
{
return ;
if (isset($_SESSION['port_mac_id']) == false) {
return;
}
$portsMacId = $_SESSION['port_mac_id'];
......@@ -164,7 +178,15 @@ class AccessController extends Controller
$port = new Port();
$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) {
$portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false;
......@@ -178,17 +200,19 @@ class AccessController extends Controller
$port->setState($state);
$port->setGatewayId($_SESSION['gateway_id']);
$port->setStationId($_SESSION['station_id']);
$port->updatePortState();
$portsLogicId[$portId] = $logicStart++;
$portsLogicId[$logicStart++] = $portId;
}
\Events::exec();
$deviceModel = new DeviceModel();
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) {
......@@ -229,6 +253,7 @@ class AccessController extends Controller
$remainTime = $orderState->getRemainChargerTime();
if ($remainTime > 0) {
$logicNum = sprintf('%02x', $order->getPortLogicId());
$orderId = sprintf('%012x', $order->getId());
......@@ -246,9 +271,6 @@ class AccessController extends Controller
$totalCount = sprintf('%02x', $totalCount);
$orderCount = sprintf('%02x', count($responseOrder));
var_dump($logicStart);
$this->response->send('03', $logicStart . $count . $totalCount . $orderCount . implode('', $responseOrder));;
......
......@@ -62,14 +62,12 @@ class CertifyController extends Controller
$clients = Gateway::getclientIdByUid($mac);
foreach ($clients as $client) {
var_dump('hello');
Gateway::closeClient($client);
}
}
Gateway::bindUid($this->clientId,$mac);
Gateway::bindUid($this->clientId, $mac);
$this->response->send('01', $sign);
......
......@@ -10,12 +10,16 @@ namespace Application\controller\business\charger;
use Application\controller\Controller;
use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
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\Gateway;
use Lib\log\Log;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\route\Route;
class OverController extends Controller
......@@ -47,20 +51,25 @@ class OverController extends Controller
public function over()
{
$type = $this->request->get('type');
$logicId = $this->request->get('logic_id');
$logicId = hexdec($this->request->get('logic_id'));
$state = hexdec($this->request->get('state'));
$orderId = hexdec($this->request->get('order_id'));
$capacity = hexdec($this->request->get('capacity'));
$this->logRecord->setOrderId($orderId);
$order = new Order();
$order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) {
$orderStateCode = $order->getStateCode();
$order->setEndCapacity($capacity);
$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) {
case StateCode::WAITING:
$orderState = new WaitingState($order);
......@@ -68,7 +77,7 @@ class OverController extends Controller
case StateCode::PORT_CHARGER:
$orderState = new ChargerState($order);
break;
case StateCode::CHARGER_SHED:
case StateCode::PORT_SHED:
$orderState = new ShedState($order);
break;
default:
......@@ -78,24 +87,55 @@ class OverController extends Controller
if (isset($orderState)) {
switch ($state) {
case StateCode::FINISH_USER: //用户主动结束
$this->logRecord->setDesc('用户主动结束充电');
$orderState->toUserFinish();
break;
case StateCode::FINISH_SHED_TIMEOUT: //端口脱落超时
$this->logRecord->setDesc('端口脱落超时结束');
$orderState->toShedTimeoutState();
break;
case StateCode::FINISH_REFUND: //充电失败退款
$this->logRecord->setDesc('充电失败退款结束');
$orderState->toRefundFinish();
break;
case StateCode::FINISH_EXCEED: //功率超限
$this->logRecord->setDesc('功率超限充电结束');
$orderState->toPowerExceedState();
break;
case StateCode::FINISH_FULL: //充满自停
$this->logRecord->setDesc('充满自停结束');
$orderState->toPowerFullState();
break;
case StateCode::FINISH_TIME_END: //充电时间结束
$this->logRecord->setDesc('充电时间完结束');
$orderState->toTimeoutState();
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:
break;
......@@ -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;
use Application\controller\Controller;
use Lib\cdz\order\Order;
use Lib\cdz\order\state\WaitingState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode;
class StartController extends Controller
......@@ -43,11 +45,44 @@ class StartController extends Controller
$logic = $this->getRequest()->get('logic_id');
$state = hexdec($this->getRequest()->get('state'));
$orderId = hexdec($this->getRequest()->get('order_id'));
$this->logRecord->setOrderId($orderId);
$capacity = hexdec($this->getRequest()->get('capacity'));
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) {
case StateCode::PORT_CHARGER:
......@@ -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;
use Application\controller\Controller;
use Application\model\ExceptionModel;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
......@@ -29,7 +30,9 @@ class OfflineController extends Controller
}
/**
*
* 口子离线
* 更新口子状态
* 记录离线记录
*/
public function offline()
{
......@@ -38,31 +41,48 @@ class OfflineController extends Controller
$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) {
$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'];
$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]))
{
$port->setPortId($portsMacId[$portMac]);
foreach ($portsLogicMac as $portLogic => $portMac) {
if (isset($portsMacId[$portMac])) {
$portId = $portsMacId[$portMac];
$port->setPortId($portId);
$port->setStationId($_SESSION['station_id']);
$port->setGatewayId($_SESSION['gateway_id']);
$port->setState(StateCode::PORT_OFFLINE);
$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'),
]);
}
}
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace Application\controller\business\port;
use Application\controller\Controller;
use Application\model\ExceptionModel;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
......@@ -36,28 +37,48 @@ class OnlineController extends Controller
$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) {
$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'];
$port = new Port();
$exceptionModel = new ExceptionModel();
if (!(isset($_SESSION['station_id']) && isset($_SESSION['gateway_id']))) {
return;
}
foreach ($portsLogicMac as $portLogic => $portMac) {
if (isset($portsMacId[$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_ONLINE);
$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;
use Lib\cdz\order\state\ExceptionState;
use Lib\cdz\order\state\PowerExceedState;
use Lib\cdz\order\state\ShedState;
use Lib\cdz\order\state\WaitingState;
use Lib\Gateway;
use Lib\log\Log;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
......@@ -23,7 +26,7 @@ class StateController extends Controller
protected $parameterMap = [
'0b' => [
'0B' => [
'port_logic' => 1,
'mac' => 8,
'state' => 1,
......@@ -39,23 +42,36 @@ class StateController extends Controller
{
$portLogic = $this->request->get('port_logic');
$mac = $this->request->get('mac');
$state = $this->request->get('state');
$state = hexdec($this->request->get('state'));
$portsMacId = $_SESSION['port_mac_id'];
if (isset($portsMacId[$mac]) === false) {
return;
}
$portId = $portsMacId[$mac];
$port = new Port($portId);
$usingOrder = $port->getPortUsingOrderId();
$usingOrderId = $port->getPortUsingOrderId();
if ($usingOrder == false || $usingOrder = '') {
if ($usingOrderId == false || $usingOrderId == '') {
$port->setState($state);
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 {
$order = new Order($usingOrder);
$this->logRecord->setOrderId($usingOrderId);
$this->logRecord->setPortId($portId);
$order = new Order((int)$usingOrderId);
if ($order->getOrderCache(\Events::getRedis()) == false) {
$port->setState($state);
......@@ -63,12 +79,17 @@ class StateController extends Controller
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()) {
case StateCode::PORT_CHARGER:
$orderState = new ChargerState($order);
break;
case StateCode::CHARGER_SHED:
case StateCode::PORT_SHED:
$orderState = new ShedState($order);
break;
case StateCode::FINISH_EXCEED:
......@@ -79,11 +100,15 @@ class StateController extends Controller
case StateCode::PORT_EXCEPTION:
$orderState = new ExceptionState($order);
break;
case StateCode::WAITING:
$orderState = new WaitingState($order);
break;
default:
return;
break;
}
Log::debug('now state' . $state);
switch ($state) {
case StateCode::PORT_CHARGER:
$orderState->toChargerState();
......@@ -92,6 +117,11 @@ class StateController extends Controller
$orderState->toPowerExceedState();
break;
case StateCode::PORT_SHED:
$orderState->toShedState();
break;
default :
break;
}
......
......@@ -14,6 +14,7 @@ use Application\model\OrderModel;
use Application\model\OrderPowerModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\log\Log;
use Lib\station\gateway\port\Port;
class PowerController extends Controller
......@@ -48,13 +49,14 @@ class PowerController extends Controller
$capacity = hexdec($this->request->get('capacity'));
if (isset($_SESSION['port_id_logic']) == false) {
if (isset($_SESSION['port_logic_id']) == false) {
return;
}
$portsLogicId = array_flip($_SESSION['port_id_logic']);
$portsLogicId = $_SESSION['port_logic_id'];
if (isset($portsLogicId[$logicId])) {
$portId = $portsLogicId[$logicId];
$port = new Port($portId);
......@@ -63,6 +65,8 @@ class PowerController extends Controller
if ($orderId == false || $orderId == '') {
return;
}
$this->logRecord->setOrderId($orderId);
$this->logRecord->setPortId($portId);
$order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) {
......@@ -80,6 +84,7 @@ class PowerController extends Controller
}
$orderPowerModel = new OrderPowerModel();
$orderPowerModel->insertRow([
'order_id' => $orderId,
'capacity' => $capacity,
......@@ -89,6 +94,10 @@ class PowerController extends Controller
]);
} else {
Log::debug('no port_id');
}
......@@ -97,6 +106,88 @@ class PowerController extends Controller
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;
}
}
}
}
<?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
protected $parameterMap = [
'0a' => []
'0A' => []
];
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
protected $table = 'stations';
}
\ No newline at end of file
......@@ -22,7 +22,7 @@ class Api
$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');
......@@ -33,8 +33,8 @@ class Api
$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');
}
......
<?php
namespace Lib;
use \Exception;
/**
* This file is part of workerman.
*
......@@ -19,7 +21,6 @@ use \Exception;
* 数据发送相关
* @version 3.0.10
*/
class Gateway
{
/**
......@@ -34,7 +35,7 @@ class Gateway
*
* @var string
*/
public static $registerAddress = 'webapppush.default:1240';
public static $registerAddress = 'webapppush.default:1239';
//public static $registerAddress = '127.0.0.1:1236';
......@@ -86,7 +87,7 @@ class Gateway
if ($client_id_array) {
if (!is_array($client_id_array)) {
echo new \Exception('bad $client_id_array:'.var_export($client_id_array, true));
echo new \Exception('bad $client_id_array:' . var_export($client_id_array, true));
return;
}
$data_array = array();
......@@ -120,7 +121,7 @@ class Gateway
if (static::$businessWorker) {
foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('exclude'=> $address_connection_array[$address])) : '';
json_encode(array('exclude' => $address_connection_array[$address])) : '';
/** @var TcpConnection $gateway_connection */
$gateway_connection->send($gateway_data);
}
......@@ -133,7 +134,7 @@ class Gateway
}
foreach ($all_addresses as $address) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('exclude'=> $address_connection_array[$address])) : '';
json_encode(array('exclude' => $address_connection_array[$address])) : '';
static::sendToGateway($address, $gateway_data);
}
}
......@@ -478,7 +479,7 @@ class Gateway
*/
public static function getUidByClientId($client_id)
{
$data = static::select(array('uid'), array('client_id'=>array($client_id)));
$data = static::select(array('uid'), array('client_id' => array($client_id)));
foreach ($data as $local_ip => $buffer_array) {
foreach ($buffer_array as $local_port => $items) {
//$items = ['connection_id'=>['uid'=>x, 'group'=>[x,x..], 'session'=>[..]], 'client_id'=>[..], ..];
......@@ -531,7 +532,6 @@ class Gateway
}
/**
* 获取所有分组uid在线列表
*
......@@ -539,7 +539,7 @@ class Gateway
*/
public static function getAllGroupUidList()
{
$data = static::select(array('uid','groups'));
$data = static::select(array('uid', 'groups'));
$group_uid_map = array();
foreach ($data as $local_ip => $buffer_array) {
foreach ($buffer_array as $local_port => $items) {
......@@ -550,7 +550,7 @@ class Gateway
}
$uid = $info['uid'];
foreach ($info['groups'] as $group_id) {
if(!isset($group_uid_map[$group_id])) {
if (!isset($group_uid_map[$group_id])) {
$group_uid_map[$group_id] = array();
}
$group_uid_map[$group_id][$uid] = $uid;
......@@ -579,7 +579,7 @@ class Gateway
}
$client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
foreach ($info['groups'] as $group_id) {
if(!isset($group_client_id_map[$group_id])) {
if (!isset($group_client_id_map[$group_id])) {
$group_client_id_map[$group_id] = array();
}
$group_client_id_map[$group_id][$client_id] = $client_id;
......@@ -613,7 +613,7 @@ class Gateway
* @param array $where
* @return array
*/
protected static function select($fields = array('session','uid','groups'), $where = array())
protected static function select($fields = array('session', 'uid', 'groups'), $where = array())
{
$t = microtime(true);
$gateway_data = GatewayProtocol::$empty;
......@@ -685,7 +685,7 @@ class Gateway
$auth_buffer = static::$secretKey ? static::generateAuthBuffer() : '';
foreach ($gateway_data_array as $address => $gateway_data) {
if ($auth_buffer) {
$gateway_buffer_array[$address] = $auth_buffer.GatewayProtocol::encode($gateway_data);
$gateway_buffer_array[$address] = $auth_buffer . GatewayProtocol::encode($gateway_data);
} else {
$gateway_buffer_array[$address] = GatewayProtocol::encode($gateway_data);
}
......@@ -986,7 +986,7 @@ class Gateway
}
// 分组发送,没有排除的client_id,直接发送
$default_ext_data_buffer = json_encode(array('group'=> $group, 'exclude'=> null));
$default_ext_data_buffer = json_encode(array('group' => $group, 'exclude' => null));
if (empty($exclude_client_id)) {
$gateway_data['ext_data'] = $default_ext_data_buffer;
return static::sendToAllGateway($gateway_data);
......@@ -1002,7 +1002,7 @@ class Gateway
if (static::$businessWorker) {
foreach (static::$businessWorker->gatewayConnections as $address => $gateway_connection) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
json_encode(array('group' => $group, 'exclude' => $address_connection_array[$address])) :
$default_ext_data_buffer;
/** @var TcpConnection $gateway_connection */
$gateway_connection->send($gateway_data);
......@@ -1016,7 +1016,7 @@ class Gateway
}
foreach ($addresses as $address) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
json_encode(array('group' => $group, 'exclude' => $address_connection_array[$address])) :
$default_ext_data_buffer;
static::sendToGateway($address, $gateway_data);
}
......@@ -1166,7 +1166,7 @@ class Gateway
}
$recv_len = strlen($all_buffer);
if (!$pack_len && $recv_len >= 4) {
$pack_len= current(unpack('N', $all_buffer));
$pack_len = current(unpack('N', $all_buffer));
}
// 回复的数据都是以\n结尾
if (($pack_len && $recv_len >= $pack_len + 4) || microtime(true) - $time_start > $timeout) {
......@@ -1316,7 +1316,7 @@ class Gateway
static $addresses_cache, $last_update, $last_register_address;
$time_now = time();
$expiration_time = 1;
if(empty($addresses_cache) || $time_now - $last_update > $expiration_time || $last_register_address !== static::$registerAddress) {
if (empty($addresses_cache) || $time_now - $last_update > $expiration_time || $last_register_address !== static::$registerAddress) {
$client = stream_socket_client('tcp://' . static::$registerAddress, $errno, $errmsg, static::$connectTimeout);
if (!$client) {
throw new Exception('Can not connect to tcp://' . static::$registerAddress . ' ' . $errmsg);
......@@ -1344,7 +1344,7 @@ class Gateway
protected static function isValidGroupId($group)
{
if (empty($group)) {
echo new \Exception('group('.var_export($group, true).') empty');
echo new \Exception('group(' . var_export($group, true) . ') empty');
return false;
}
return true;
......@@ -1402,8 +1402,7 @@ class Context
*/
public static function sessionEncode($session_data = '')
{
if($session_data !== '')
{
if ($session_data !== '') {
return serialize($session_data);
}
return '';
......@@ -1444,11 +1443,10 @@ class Context
*/
public static function clientIdToAddress($client_id)
{
if(strlen($client_id) !== 20)
{
if (strlen($client_id) !== 20) {
throw new \Exception("client_id $client_id is invalid");
}
return unpack('Nlocal_ip/nlocal_port/Nconnection_id' ,pack('H*', $client_id));
return unpack('Nlocal_ip/nlocal_port/Nconnection_id', pack('H*', $client_id));
}
}
......@@ -1556,6 +1554,7 @@ class GatewayProtocol
'ext_data' => '',
'body' => '',
);
/**
* 返回包长度
*
......@@ -1570,6 +1569,7 @@ class GatewayProtocol
$data = unpack("Npack_len", $buffer);
return $data['pack_len'];
}
/**
* 获取整个包的 buffer
*
......@@ -1592,6 +1592,7 @@ class GatewayProtocol
$data['flag'], $data['gateway_port'],
$ext_len) . $data['ext_data'] . $data['body'];
}
/**
* 从二进制数据转换为数组
*
......
......@@ -52,5 +52,7 @@ class StateCode
const FINISH_EXCEPTION_REFUND = 44;
const CHANGE_PORT = 47;
}
\ No newline at end of file
......@@ -33,32 +33,34 @@ class Order
/**
* @var int
*/
private $purchaseTime;
private $purchaseTime = 0;
/**
* @var string
*/
private $startTime;
private $startTime = '';
/**
* @var string
*/
private $finishTime;
private $finishTime = '';
/**
* @var string
*/
private $shedTime;
private $shedTime = '';
/**
* @var string
*/
private $offlineTime;
private $offlineTime = '0';
/**
* @var string
*/
private $onlineTime;
private $onlineTime = '';
/**
* 订单状态
......@@ -80,7 +82,7 @@ class Order
/**
* @var int
*/
private $portId;
private $portId = 0;
/**
......@@ -120,6 +122,11 @@ class Order
/**
* @var int
*/
private $stationId;
/**
* @var int
*/
private $startCapacity = -1;
......@@ -154,7 +161,8 @@ class Order
return isset($this->$name) ? $this->$name : '';
}
public function __construct(int $orderId = 0) {
public function __construct(int $orderId = 0)
{
$this->id = $orderId;
......@@ -182,8 +190,10 @@ class Order
$this->orderCache = $redis->hGetAll($keyName);
var_dump($this->orderCache);
if (count($this->orderCache) < 5) {
return false;
}
foreach ($this->orderCache as $key => $value) {
$this->$key = $value;
......@@ -196,6 +206,16 @@ class Order
}
/**
* 删除订单的缓存
* @param \Redis $redis
*/
public function delCache(\Redis $redis)
{
$keyName = $this->getCacheKeyName();
$redis->del($keyName);
}
/**
* @param \Redis $redis
* @return array|bool
*/
......@@ -592,7 +612,21 @@ class Order
$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
$this->redis->hMset($this->order->getCacheKeyName(), $update);
$this->redis->expire($this->order->getCacheKeyName(), 60 * 60 * 24);
}
......
......@@ -13,9 +13,11 @@ use Application\model\OrderExceptionModel;
use Application\model\OrderModel;
use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis;
use Lib\Gateway;
use Lib\log\Log;
use Lib\rabbitmq\PublishPortShed;
use Lib\remote\api\message\ChargerFullMessage;
use Lib\remote\api\message\ChargerShedMessage;
use Lib\remote\api\message\ChargerTimeoutMessage;
use Lib\remote\api\message\FinishByUserMessage;
use Lib\remote\api\message\PowerExceedMessage;
......@@ -23,6 +25,7 @@ use Lib\remote\api\message\SendMessage;
use Lib\remote\api\message\ShedMessage;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
use Src\BroadcastToAgent;
class ChargerState extends State
{
......@@ -42,23 +45,29 @@ class ChargerState extends State
$orderModel->update([
'is_stop' => $this->order->getEndCode(),
'end_capacity' => $this->order->getEndCapacity(),
'updated_at' => date('Y-m-d H:i:s'),
'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->freePort();
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
//释放口子状态
$port->freePort($this->order->getId());
//删除订单的缓存
$this->order->delCache(\Events::getRedis());
//用户主动结束充电推送
$finishByUserMessage = new FinishByUserMessage();
$finishByUserMessage->setOrderId($this->order->getId());
$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 = (int)$chargerTime;
$finishByUserMessage->setChargerTime($chargerTime);
$finishByUserMessage->setEndTime($this->nowDate);
......@@ -71,7 +80,6 @@ class ChargerState extends State
$orderException = new OrderExceptionModel();
$orderException->insertRow([
'order_id' => $this->order->getId(),
'state' => StateCode::PORT_CHARGER . StateCode::FINISH_USER,
'desc' => '用户主动结束充电',
......@@ -96,6 +104,7 @@ class ChargerState extends State
public function toExceptionState()
{
// TODO: Implement toExceptionState() method.
}
......@@ -105,6 +114,7 @@ class ChargerState extends State
* 推送消息
* 更新口子状态释放订单
* 记录订单异常
* 推送给agentId
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
......@@ -116,7 +126,7 @@ class ChargerState extends State
$orderModel->update([
'is_stop' => StateCode::FINISH_EXCEED,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s', $this->nowTimeStamp),
'end_time' => $this->nowDate,
'state' => 1,
], 'id = ' . $this->order->getId());
......@@ -144,7 +154,14 @@ class ChargerState extends State
$port = new Port();
$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
}
$port = new Port();
$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
* 推送用户信息
* 发布延时任务十分钟后检测端口
*
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \AMQPConnectionException
* @throws \AMQPExchangeException
......@@ -230,13 +256,21 @@ class ChargerState extends State
'created_at' => $this->nowDate,
]);
$shedMessage = new ShedMessage();
$shedMessage->setOrderId($this->order->getId());
$shedMessage->setPortName($this->order->getPortName());
$shedMessage->setStationName($this->order->getStationName());
$shedMessage->setTime($this->nowDate);
//更新端口状态和订单状态
$port = new Port($this->order->getPortId());
$port->setState(StateCode::PORT_SHED);
$port->setGatewayId($this->order->getGatewayId());
$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();
if ($sendMessage->request($shedMessage) == false) {
if ($sendMessage->request($chargerShedMessage) == false) {
Log::error('send port shed message fail');
}
......@@ -249,7 +283,11 @@ class ChargerState extends State
$publish->publishOrder([
'order_id' => $this->order->getId(),
'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) {
......@@ -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
$orderModel->update([
'is_stop' => StateCode::FINISH_TIME_END,
'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->nowDate,
'updated_at' => $this->nowDate,
'state' => 1,
], 'id = ' . $this->order->getId());
$port = new Port();
$port->setPortId($this->order->getId());
$port->freePort();
$port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_ONLINE);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($this->order->getStationId());
$port->freePort($this->order->getId());
$chargerTimeoutMessage = new ChargerTimeoutMessage();
$chargerTimeoutMessage->setStationName($this->order->getStationName());
$chargerTimeoutMessage->setPortName($this->order->getPortName());
$chargerTimeoutMessage->setOrderId($this->order->getId());
$chargerTimeoutMessage->setEndTime($this->nowDate);
$chargerTimeoutMessage->setUserId($this->order->getUserId());
$chargerTime = $this->nowTimeStamp - $this->order->getStartTime();
$chargerTime /= 60;
......@@ -306,6 +353,14 @@ class ChargerState extends State
}
/**
* 关闭失败
*/
public function stopFail()
{
}
}
......
......@@ -65,5 +65,10 @@ class ExceptionState extends State
// 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;
use Lib\cdz\order\NoticePortShed;
use Lib\cdz\order\Order;
use Lib\log\Log;
class OfflineNetState extends State
{
......@@ -27,11 +28,27 @@ class OfflineNetState extends State
$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($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
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;
}
}
public function toExceptionState()
......@@ -81,8 +96,7 @@ class OfflineNetState extends State
*/
public function toShedState()
{
if($this->remainChargerTime > 0)
{
if ($this->remainChargerTime > 0) {
$this->publishPortShed($this->order);
}
......@@ -115,6 +129,10 @@ class OfflineNetState extends State
$this->remainChargerTime = $remainChargerTime;
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
......@@ -27,7 +27,7 @@ class OfflinePowerState extends State
/**
* @var int
*/
private $remainChargerTime =0;
private $remainChargerTime = 0;
public function toWaitingState()
{
......@@ -36,8 +36,7 @@ class OfflinePowerState extends State
public function toChargerState()
{
if($this->order->getIsRefund() === 1)
{
if ($this->order->getIsRefund() === 1) {
//已经退款 下发结束充电命令
//$this->order->stop($this->order,$_SESSION['client_id'],43);
$this->remainChargerTime = 0;
......@@ -49,9 +48,9 @@ class OfflinePowerState extends State
$startTime = $this->order->getStartTime();
$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
// 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
// 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
// TODO: Implement toUserFinish() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
......@@ -9,8 +9,18 @@
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
{
......@@ -19,50 +29,245 @@ class ShedState extends State
parent::__construct($order);
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
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()
{
// TODO: Implement toWaitingState() method.
Log::error('shed state can not change to waiting state');
}
/**
* 更新订单状态
* 更新口子状态
* 推送微信消息
* 记录订单异常
* @throws \GuzzleHttp\Exception\GuzzleException
*/
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()
{
// TODO: Implement toExceptionState() method.
//todo
}
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
Log::error('shed state can not change to power exceed state');
}
public function toPowerFullState()
{
// TODO: Implement toPowerFullState() method.
Log::error('shed state can not change to power full state');
}
public function toShedState()
{
// TODO: Implement toShedState() method.
Log::error('shed state can not change to shed');
}
/**
* 记录订单异常
* 释放口子
* 推送用户消息
* 更新订单记录,记录结束用电量
* @throws \GuzzleHttp\Exception\GuzzleException
*/
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()
{
// 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
// TODO: Implement toUserFinish() method.
}
public function stopFail()
{
// TODO: Implement stopFail() method.
}
}
\ No newline at end of file
......@@ -62,5 +62,25 @@ abstract class State
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
// 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
'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $this->order->getEndCapacity(),
'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
public function toChargerState(): void
{
//订单已经退款则直接结束
if($this->order->getIsRefund() == 1)
{
if ($this->order->getIsRefund() == 1) {
//todo 下发结束充电命令
$orderId = $this->order->getId();
$portLogic = $this->order->getPortLogicId();
......@@ -97,7 +104,7 @@ class WaitingState extends State
$this->order->setStateCode(12);
\Events::pipe();
$orderSaveRedis = new OrderSaveRedis($this->order);
$orderSaveRedis->save(['stateCode', 'startTime']);
$orderSaveRedis->save(['stateCode', 'startTime', 'startCapacity']);
//更新口子状态
$port = new Port();
......@@ -105,7 +112,7 @@ class WaitingState extends State
$port->setState(StateCode::PORT_CHARGER);
$port->setGatewayId($this->order->getGatewayId());
$port->setStationId($_SESSION['station_id']);
$port->updatePortState();
$port->updatePortState($this->order->getId());
\Events::exec();
......@@ -114,8 +121,7 @@ class WaitingState extends State
$startMessage->setUserId($this->order->getUserId());
$startMessage->setAmount($this->order->getPayAmount());
$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();
if ($sendMessage->request($startMessage) === false) {
......@@ -128,7 +134,6 @@ class WaitingState extends State
'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();
......@@ -186,7 +191,7 @@ class WaitingState extends State
$port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_EXCEPTION);
$port->updatePortState();
$port->updatePortState($this->order->getId());
// TODO: Implement toExceptionState() method.
}
......@@ -196,37 +201,37 @@ class WaitingState extends State
*/
public function toPowerExceedState()
{
// TODO: Implement toPowerExceedState() method.
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');
// TODO: Implement toPowerFullState() method.
}
public function toShedState()
{
// TODO: Implement toShedState() method.
Log::error('waiting state can not change to shed state');
}
public function toShedTimeoutState()
{
Log::error('waiting state can not change to shed timeout');
// TODO: Implement toShedTimeoutState() method.
}
public function toTimeoutState()
{
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
/**
* @var string
*/
private $orderId = '';
/**
* @var string
*/
private $address = '';
/**
......@@ -80,7 +75,6 @@ class FailMessage extends Message
}
/**
* @return string
*/
......@@ -97,21 +91,6 @@ class FailMessage extends Message
$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
......
......@@ -41,7 +41,7 @@ class SendMessage
'http_errors' => false,
]);
$response = $client->request('POST', $this->uri, [
$response = $client->request('POST', $this->path, [
'form_params' => [
......@@ -54,6 +54,8 @@ class SendMessage
if ($response->getStatusCode() === 200) {
Log::debug('发送模版消息');
} else {
Log::error('send wx message fail ', ['http_code' => $response->getStatusCode()]);
}
......
......@@ -78,7 +78,7 @@ class StartMessage extends Message
'color' => '#333',
],
'remark' => [
'value' => $this->remark . '\n' . "($this->orderId)",
'value' => $this->remark . "($this->orderId)",
'color' => '#333',
],
]);
......@@ -135,6 +135,4 @@ class StartMessage extends Message
}
}
\ No newline at end of file
......@@ -20,12 +20,12 @@ class Gateway
* @param int $state
* @return int
*/
public function setState(int $gatewayId,int $state)
public function setState(int $gatewayId, int $state)
{
$redis = \Events::getRedis();
return $redis->zAdd(self::GATEWAY_STATE,$state,$gatewayId);
return $redis->zAdd(self::GATEWAY_STATE, $state, $gatewayId);
}
......
......@@ -9,6 +9,8 @@
namespace Lib\station\gateway\port;
use Application\model\DeviceModel;
use Lib\log\Log;
use Lib\StateCode;
use Lib\station\gateway\Gateway;
use Lib\station\Station;
......@@ -22,13 +24,14 @@ class Port
/**
* 端口正在充电的订单号
* 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
/**
* @var int
*/
private $gatewayId;
private $gatewayId = 0;
/**
* @var int
*/
private $stationId;
private $stationId = 0;
public function __construct(int $portId = 0)
{
......@@ -73,17 +76,36 @@ class Port
/**
* 更新端口状态
* @param int $orderId
*/
public function updatePortState()
public function updatePortState(int $orderId = 0)
{
$redis = \Events::getRedis();
$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(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
/**
* 释放口子,更新口子状态为空闲
* @param int $orderId
*/
public function freePort()
public function freePort(int $orderId = 0)
{
\Events::pipe();
......@@ -109,8 +132,12 @@ class Port
$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->hDel(self::USING_ORDER_ID, $this->portId);
......@@ -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
public function __destruct()
{
var_dump('i am test');
// 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