Commit 1733bb7f by BANice

将口子的逻辑地址保存至数据中,不再保存至redis中,口子状态redis维护不止在线和离线状态,异常等状态

parent ae14f02f
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="PublishConfigData"> <component name="PublishConfigData" serverName="cdz">
<serverData> <serverData>
<paths name="cdz"> <paths name="cdz">
<serverdata> <serverdata>
......
...@@ -340,6 +340,16 @@ class Controller ...@@ -340,6 +340,16 @@ class Controller
$this->plc = $plc; $this->plc = $plc;
} }
/**
* 获取网关下的口子mac地址和id的数组
* @return array
*/
protected function getPortMacId()
{
return isset($_SESSION['port_mac_id']) ? $_SESSION['port_mac_id'] : [];
}
} }
\ No newline at end of file
...@@ -37,54 +37,49 @@ class AccessController extends Controller ...@@ -37,54 +37,49 @@ class AccessController extends Controller
*/ */
public function access() public function access()
{ {
if ($_SESSION['certified'] === true) {
$gatewayModel = new GatewayModel();
$gatewayModel = new GatewayModel(); $mac = $this->get('mac');
$mac = $this->get('mac'); $gatewayId = $gatewayModel->getColumn('id', " mac_address = '$mac' ");
$gatewayId = $gatewayModel->getColumn('id', " mac_address = '$mac' "); if ($gatewayId != false) {
if ($gatewayId != false) { Gateway::bindUid($this->clientId, $mac);
//tcp绑定uid $_SESSION['gateway_id'] = $gatewayId;
Gateway::bindUid($this->clientId, $mac);
$_SESSION['gateway_id'] = $gatewayId; $portModel = new PortModel();
$portModel = new PortModel(); //网关id下所有的口子mac地址和id
$portMacId = $portModel->getAllColumns(['mac_address', 'id'], 'gateway_id = ' . $gatewayId);
$portMacId = $portModel->getAllColumns(['mac_address', 'id'], 'gateway_id = ' . $gatewayId); if (count($portMacId) !== 0) { //网关下的口子mac和id缓存到session中
if (count($portMacId) !== 0) { //网关下的端口id和mac注入到session当中,静态缓存
//网关下的端口id和mac注入到session当中,静态缓存 foreach ($portMacId as $item) {
foreach ($portMacId as $item) {
$_SESSION['port_mac_id'][$item['mac_address']] = $item['id']; $_SESSION['port_mac_id'][$item['mac_address']] = $item['id'];
}
} }
}
$sessionId = ''; $sessionId = '';
for ($i = 0; $i < 4; $i++) {
$hex = dechex(rand(0, 255));
$sessionId .= $hex; for ($i = 0; $i < 4; $i++) {
}
$this->response->send('02', $sessionId);
} else { $hex = dechex(rand(0, 255));
Gateway::closeClient($this->clientId); $sessionId .= $hex;
} }
$this->response->send('02', $sessionId);
} else { } else {
Gateway::closeClient($this->clientId); Gateway::closeClient($this->clientId);
} }
} }
/** /**
...@@ -96,94 +91,103 @@ class AccessController extends Controller ...@@ -96,94 +91,103 @@ class AccessController extends Controller
$startLogicNum = $this->get('start_num'); $startLogicNum = $this->get('start_num');
$count = hexdec($this->get('count')); $startLogicNumDec = hexdec($startLogicNum);
$totalCount = $this->get('total_count');
$macs = substr($this->message, $this->plc, $count * 16);
$this->plc += $count * 16; $count = $this->get('count');
$macsState = substr($this->message, $this->plc); $countDec = hexdec($count);
$macsState = str_split($macsState, 2); $totalCount = $this->get('total_count');
$macsStateArray = []; $macs = substr($this->message, $this->plc, $countDec * 16);
foreach ($macsState as $value) { $this->plc += $countDec * 16;
array_push($macsStateArray, hexdec($value));
}
$macs = str_split($macs, 16); $macsState = substr($this->message, $this->plc);
$ids = []; $stateArray = $this->parseState($macsState);
$onlineIds = []; $macLogicArray = $this->parseMacs($macs, $startLogicNumDec);
$offlineIds = []; $portModel = new PortModel();
$i = 0; $i = 0;
//依据mac地址和session中存储的mac和id的数组获取到对应的id $portMacId = isset($_SESSION['port_mac_id']) ? $_SESSION['port_mac_id'] : [];
//根据状态为设置端口id的状态
foreach ($macs as $mac) {
if (isset($_SESSION['port_mac_id'][$mac])) { $port = new port();
//$macIdArray[$mac] = $_SESSION['port_mac_id'][$mac];
$id = $_SESSION['port_mac_id'][$mac]; $portIdState = [];
array_push($ids, $id); foreach ($macLogicArray as $mac => $logic)//将端口状态和端口逻辑地址更新至数据库中
//在线离线状态判断 {
if ($macsStateArray[$i++] == 1) {
array_push($onlineIds, $id);
} else { $portModel->update([
array_push($offlineIds, $id); 'state' => $stateArray[$i],
} 'logic_num' => $logic,
], 'mac_address = ' . "'$mac'");
if(isset($portMacId[$mac]))
{
$portIdState[$portMacId[$mac]] = $stateArray[$i];
} }
$i++;
} }
$idLogicArray = []; $port->setPortState($portIdState); //改变口子状态
$logicId = $startLogicNum;
//端口id计算逻辑id if(isset($_SESSION['port_mac_id'])) {
foreach ($ids as $id) {
$portMacId = $_SESSION['port_mac_id'];
$idLogicArray[$id] = $logicId;
//根据起始逻辑地址计算逻辑地址
$logicId = sprintf('%02x', hexdec($logicId) + 1);
} }
$port = new Port(); return $this->response->send('03', $startLogicNum . $count . $totalCount);
}
$port->pipe(); /**
* @param string $macs
* @param int $logicStart
* @return array
*/
private function parseMacs(string $macs, int $logicStart)
{
$port->register($idLogicArray, $_SESSION['gateway_id']); $macs = str_split($macs, 16);
$port->online($onlineIds); $macLogic = [];
$port->offline($offlineIds); foreach ($macs as $mac) {
$port->exec(); $macLogic[$mac] = sprintf('%02x', $logicStart++);
$count = sprintf('%02x',$count); }
var_dump($count); return $macLogic;
var_dump($totalCount); }
$this->response->send('03', $startLogicNum . $count . $totalCount); /**
* 解析口子状态
* @param string $state
* @return array
*/
private function parseState(string $state)
{
$state = str_split($state, 2);
return array_map(function ($hex) {
return hexdec($hex);
}, $state);
} }
......
...@@ -28,41 +28,44 @@ class PortController extends Controller ...@@ -28,41 +28,44 @@ class PortController extends Controller
/** /**
* command 08
* 端口上线 * 端口上线
*/ */
public function online() public function online()
{ {
$count = $this->get('count'); $count = $this->get('count');
$portsLogicNum = str_split(substr($this->message,$this->plc,$count),2); $countDec = hexdec($count) ;
$this->plc+=$count; $portLogic =substr($this->message,$this->plc,$countDec*2);
$macs = str_split(substr($this->message,$this->plc),16); $this->plc += $countDec*2;
$portMac = substr($this->message,$this->plc);
$portsIdLogic = []; $portLogicArray = str_split($portLogic,2);
for($i=0;$i<$count;$i++) $portMacArray = str_split($portMac,16);
{
if(isset($_SESSION['port_mac_id'][$macs[$i]]))
{
$id = $_SESSION['port_mac_id'][$macs[$i]];
$portsIdLogic[$id] = $portsLogicNum[$i]; $portMacId = $this->getPortMacId();
}
}
$port = new Port(); $port = new Port();
$port->pipe(); $onlinePortId = [];
$port->register($portsIdLogic,$_SESSION['gateway_id']); foreach ($portMacArray as $mac)
{
$port->online(array_keys($portsIdLogic)); //todo 更新逻辑地址数据中
if(isset($portMacId[$mac]))
{
array_push($onlinePortId,$portMacId[$mac]);
}
$port->exec(); }
$port->online($onlinePortId);
} }
...@@ -72,19 +75,42 @@ class PortController extends Controller ...@@ -72,19 +75,42 @@ class PortController extends Controller
public function offline() public function offline()
{ {
$gatewayId = $_SESSION['gateway_id'];
$count = $this->get('count'); $count = $this->get('count');
$logics = substr($this->message,$this->plc); $countDec = hexdec($count) ;
$portLogic =substr($this->message,$this->plc,$countDec*2);
$this->plc += $countDec*2;
$portsLogic = str_split($logics,2); $portMac = substr($this->message,$this->plc);
$portLogicArray = str_split($portLogic,2);
$portMacArray = str_split($portMac,16);
$portMacId = $this->getPortMacId();
$port = new Port(); $port = new Port();
$portsId = $port->getPortId($portsLogic,$gatewayId); $offlinePortId = [];
foreach ($portMacArray as $mac)
{
//todo 更新逻辑地址数据中
if(isset($portMacId[$mac]))
{
array_push($onlinePortId,$portMacId[$mac]);
}
}
$port->offline($offlinePortId);
var_dump($portsId);
} }
......
...@@ -21,11 +21,13 @@ class PowerController extends Controller ...@@ -21,11 +21,13 @@ class PowerController extends Controller
'type' => 1, 'type' => 1,
'logic' => 1, 'logic' => 1,
'outTradeNo' => 6, 'orderId' => 6,
'capacity' => 4,
'state' => 1,
], ],
'05' => [ '05' => [
'outTradeNo' => 6, 'orderId' => 6,
'logic' => 1, 'logic' => 1,
'state' => 1, 'state' => 1,
'capacity' => 4 'capacity' => 4
...@@ -48,23 +50,31 @@ class PowerController extends Controller ...@@ -48,23 +50,31 @@ class PowerController extends Controller
$type = $this->get('type'); $type = $this->get('type');
$orderId = $this->get('orderId');
if($type == 1) if($type == 1)
{ {
$logic = $this->get('logic'); $logic = $this->get('logic');
$outTradeNo = $this->get('outTradeNo'); $power = $this->get('capacity');
//fixme 修改订单表中,订单的状态位 $state = $this->get('state');
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$row = $orderModel->update(['state' => 1],'out_trade_no = '.$outTradeNo); $id = hexdec($orderId);
$row = $orderModel->update(['is_charger' => $state,'start_capacity' => $power],'id = '.$id);
//todo websocket推送给前段开始充电信息,如果未发现当前订单id连接则存入消息队列当中
var_dump($row);
} }
$this->response->send('14',$outTradeNo); $this->response->send('14',$orderId);
} }
...@@ -77,7 +87,9 @@ class PowerController extends Controller ...@@ -77,7 +87,9 @@ class PowerController extends Controller
{ {
$outTradeNo = $this->get('outTradeNo'); $orderId = $this->get('orderId');
$id = hexdec($orderId);
$logic = $this->get('logic'); $logic = $this->get('logic');
...@@ -85,10 +97,12 @@ class PowerController extends Controller ...@@ -85,10 +97,12 @@ class PowerController extends Controller
$capacity = $this->get('capacity'); $capacity = $this->get('capacity');
$orderModel = new OrderModel();
$orderModel->update(['is_stop' => $state, 'end_capacity' => $capacity],' id = '.$id);
//todo 存储数据库 //todo 存储数据库
$this->response->send('15',$outTradeNo); $this->response->send('15',$orderId);
} }
...@@ -108,7 +122,6 @@ class PowerController extends Controller ...@@ -108,7 +122,6 @@ class PowerController extends Controller
$powerData = str_split($powerData,4); $powerData = str_split($powerData,4);
$this->plc+=hexdec($count) * 4; $this->plc+=hexdec($count) * 4;
$capacity = substr($this->message,$this->plc); $capacity = substr($this->message,$this->plc);
......
...@@ -93,11 +93,8 @@ class Events ...@@ -93,11 +93,8 @@ class Events
$command = substr($message,6,2); $command = substr($message,6,2);
var_dump($command);
$route = Api::getRoute($command); $route = Api::getRoute($command);
if( $route instanceof Route) if( $route instanceof Route)
{ {
$controllerName = $route->getController(); $controllerName = $route->getController();
...@@ -111,6 +108,20 @@ class Events ...@@ -111,6 +108,20 @@ class Events
$message = mb_strtoupper($message); $message = mb_strtoupper($message);
$middleware = $route->getMiddleware();
foreach ($middleware as $item) {
if($item instanceof \Application\Middleware)
{
if($item->handle($clientId,$message) === false)
{
Gateway::closeCurrentClient();
}
}
}
$controller = new $controllerName($clientId,$message,$command); $controller = new $controllerName($clientId,$message,$command);
$controller->$method(); $controller->$method();
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:24
*/
namespace Application;
abstract class Middleware
{
/**
* @param $clientId
* @param $message
* @return bool
*/
abstract public function handle($clientId,$message) : bool ;
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:35
*/
namespace Application\Middleware;
use Application\Middleware;
class AuthMiddleware extends Middleware
{
public function handle($clientId, $message): bool
{
return isset($_SESSION['certified']);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:37
*/
namespace Application\Middleware;
use Application\Middleware;
class HasMacMiddleware extends Middleware
{
public function handle($clientId, $message): bool
{
return isset($_SESSION['gateway_mac']);
}
}
\ No newline at end of file
This diff is collapsed. Click to expand it.
...@@ -41,6 +41,7 @@ class Port ...@@ -41,6 +41,7 @@ class Port
public function register(array $idLogic,int $gatewayId) public function register(array $idLogic,int $gatewayId)
{ {
$this->redis->hMset(RedisKey::PORT_ID_LOGIC_HASH,$idLogic); $this->redis->hMset(RedisKey::PORT_ID_LOGIC_HASH,$idLogic);
$this->redis->hMset('gateway_id_'.":$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,array_flip($idLogic)); $this->redis->hMset('gateway_id_'.":$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,array_flip($idLogic));
...@@ -68,6 +69,21 @@ class Port ...@@ -68,6 +69,21 @@ class Port
} }
public function setPortState(array $portIdState)
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portIdState as $id => $state) {
array_push($tmp,$state);
array_push($tmp,$id);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
}
/** /**
* 端口id在线 * 端口id在线
* @param array $portId * @param array $portId
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
namespace Route; namespace Route;
use Application\Middleware\AuthMiddleware;
use Application\Middleware\HasMacMiddleware;
use Lib\Route; use Lib\Route;
class Api class Api
...@@ -23,13 +25,13 @@ class Api ...@@ -23,13 +25,13 @@ class Api
static::add('02','AccessController@access'); static::add('02','AccessController@access');
static::add('03','AccessController@syncPort'); static::add('03','AccessController@syncPort',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('04','PowerController@charger'); static::add('04','PowerController@charger',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('08','PortController@online'); static::add('08','PortController@online',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('09','PortController@offline'); static::add('09','PortController@offline',[new AuthMiddleware(),new HasMacMiddleware()]);
} }
......
...@@ -25,8 +25,6 @@ class Bintohex ...@@ -25,8 +25,6 @@ class Bintohex
$data = $data[1]; $data = $data[1];
var_dump(['message' => $data]);
$packageLength = strlen($data); $packageLength = strlen($data);
$packageLength/=2; $packageLength/=2;
......
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