Commit 3ff47aa1 by BANice

0b命令

parent 1733bb7f
......@@ -42,25 +42,29 @@ class AccessController extends Controller
$mac = $this->get('mac');
$gatewayId = $gatewayModel->getColumn('id', " mac_address = '$mac' ");
$gateway = $gatewayModel->getColumns(['id','station_id'], " mac_address = '$mac' ");
if ($gatewayId != false) {
if (count($gateway) !== 0) {
$gatewayId = $gateway['id'];
Gateway::bindUid($this->clientId, $mac);
$_SESSION['gateway_id'] = $gatewayId;
$_SESSION['station_id'] = $gateway['station_id'];
$portModel = new PortModel();
//网关id下所有的口子mac地址和id
$portMacId = $portModel->getAllColumns(['mac_address', 'id'], 'gateway_id = ' . $gatewayId);
$portMacId = $portModel->getAllColumns(['device_mac', 'id'], 'gateway_id = ' . $gatewayId);
if (count($portMacId) !== 0) { //网关下的口子mac和id缓存到session中
//网关下的端口id和mac注入到session当中,静态缓存
foreach ($portMacId as $item) {
$_SESSION['port_mac_id'][$item['mac_address']] = $item['id'];
$_SESSION['port_mac_id'][$item['device_mac']] = $item['id'];
}
}
......@@ -68,10 +72,12 @@ class AccessController extends Controller
for ($i = 0; $i < 4; $i++) {
$hex = dechex(rand(0, 255));
$hex = sprintf('%02x',rand(0, 255));
$sessionId .= $hex;
}
$this->response->send('02', $sessionId);
} else {
......@@ -115,6 +121,8 @@ class AccessController extends Controller
$portMacId = isset($_SESSION['port_mac_id']) ? $_SESSION['port_mac_id'] : [];
var_dump($portMacId);
$port = new port();
$portIdState = [];
......@@ -126,7 +134,7 @@ class AccessController extends Controller
'state' => $stateArray[$i],
'logic_num' => $logic,
], 'mac_address = ' . "'$mac'");
], 'device_mac = ' . "'$mac'");
if(isset($portMacId[$mac]))
{
......@@ -136,6 +144,8 @@ class AccessController extends Controller
$i++;
}
var_dump($portIdState);
$port->setPortState($portIdState); //改变口子状态
......
......@@ -28,6 +28,10 @@ class CapacityController extends Controller
$capacity = $this->get('capacity');
$gatewayId = $_SESSION['gateway_id'];
//todo 记录用电
}
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/30
* Time: 下午2:53
*/
namespace Application\Controller;
use Application\Controller;
class HeartController extends Controller
{
protected $parameterMap = [
'00' => [
]
];
public function heart()
{
$time = sprintf('%08x',time());
var_dump($time);
$this->response->send('00',$time);
}
}
\ No newline at end of file
......@@ -24,6 +24,12 @@ class PortController extends Controller
'count' => 1,
],
'0b' => [
'logic' => 1,
'mac' => 8,
'state' => 1,
]
];
......@@ -112,6 +118,16 @@ class PortController extends Controller
$port->offline($offlinePortId);
}
public function portState()
{
$portMac = $this->get('mac');
$state = $this->get('state');
}
......
......@@ -11,6 +11,7 @@ namespace Application\Controller;
use Application\Controller;
use Application\Model\OrderModel;
use Lib\port\Port;
class PowerController extends Controller
{
......@@ -30,14 +31,21 @@ class PowerController extends Controller
'orderId' => 6,
'logic' => 1,
'state' => 1,
'capacity' => 4
'capacity' => 4,
],
'07' => [
'logic' => 1,
'startTime' => 4,
'intervalTime' => 1,
'count' => 1
'count' => 1,
],
'17' => [
'logic' => 1,
'power' => 2,
'capacity' => 2,
'state' => 1,
],
];
......@@ -52,29 +60,58 @@ class PowerController extends Controller
$orderId = $this->get('orderId');
if($type == 1)
{
if ($type == 1) {
$logic = $this->get('logic');
$power = $this->get('capacity');
$state = $this->get('state');
$state = hexdec($this->get('state'));
$orderModel = new OrderModel();
$id = hexdec($orderId);
$row = $orderModel->update(['is_charger' => $state,'start_capacity' => $power],'id = '.$id);
$_SESSION['current_order_id'] = $id;
switch ($state)
{
case 0:
$state = -1;
break;
case 1:
$state = 2;
break;
case 2:
$state = 6;
break;
default:
break;
}
$_SESSION['current_order_id'] = $id;
$_SESSION['order_state'] = $state;
$row = $orderModel->update(['is_charger' => $state, 'start_capacity' => $power], 'id = ' . $id);
$portId = $orderModel->getPortId($id);
$port = new Port();
if (hexdec($state) == 2) {
$port->setPortState([$portId => $state + 2]);
//todo websocket推送给前段开始充电信息,如果未发现当前订单id连接则存入消息队列当中
}
}
$this->response->send('14',$orderId);
$this->response->send('14', $orderId);
}
......@@ -99,39 +136,93 @@ class PowerController extends Controller
$orderModel = new OrderModel();
$orderModel->update(['is_stop' => $state, 'end_capacity' => $capacity],' id = '.$id);
// 存储数据库
$orderModel->update(['is_stop' => $state, 'end_capacity' => $capacity], ' id = ' . $id);
//todo 发送微信消息给用户。
//todo 充电结束时删除端口正在使用的hash表中的元素,删除充电用户正在充电订单中的本次订单
//todo 存储数据库
$this->response->send('15',$orderId);
$this->response->send('15', $orderId);
}
/**
*
*
* |0|计时结束|
* |1|远程关闭|
* |2|插头脱落|
* |3|充满自停|
* |4|功率超限|
* |5|关闭失败|
* |6|插头插上,正常充电|
* 上报充电功率
*/
public function power()
{
//todo 依据状态修改订单中的状态,本地缓存该笔订单的状态,状态不发生改变则不更新数据库
$logic = $this->get('logic');
$startTime = $this->get('startTime');
$count = $this->get('count');
$powerData = substr($this->message,$this->plc,hexdec($count) * 4);
$powerData = substr($this->message, $this->plc, hexdec($count) * 4);
$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);
$capacity = str_split($capacity, 8);
$capacity = str_split($capacity,8);
var_dump($powerData);
var_dump($capacity);
}
/**
* 上报功率
* command 17
*/
public function onePower()
{
$logicNum = $this->get('logic');
$powerData = $this->get('power');
$capacity = $this->get('capacity');
$state = hexdec($this->get('state'));
$orderLastState = $_SESSION['order_state'];
if($orderLastState != $state)
{
// $orderModel = new OrderModel();
$port = new Port();
// $port->setPortState([
//
// ])
$orderId = $_SESSION['current_order_id'];
// $row = $orderModel->update(['state' => $state] ,'id = '.$orderId);
}
}
......@@ -172,3 +263,16 @@ class PowerController extends Controller
......@@ -15,7 +15,7 @@ use Lib\Route;
use Lib\Log;
define('APP','Application\\Controller\\');
define('APP', 'Application\\Controller\\');
class Events
{
......@@ -48,12 +48,11 @@ class Events
static::$redis->select(4);
try{
try {
static::$db = new Connection(Env::get('DB_HOST'), Env::get('DB_PORT'), Env::get('DB_USER'), Env::get('DB_PASSWORD'), Env::get('DB_NAME'));
}catch (Exception $exception)
{
} catch (Exception $exception) {
var_dump($exception->getMessage());
}
......@@ -64,47 +63,43 @@ class Events
public static function onConnect($clientId)
{
Log::debug('connect',[
Log::debug('connect', [
'time' => date('H:i:s')
'time' => date('H:i:s'),
]);
}
public static function onMessage($clientId,$message)
public static function onMessage($clientId, $message)
{
var_dump($message);
$crc = substr($message,-2);
$crc = substr($message, -2);
$makeCrc = sprintf('%08x',(Crc::crc(substr($message,0,-2))));
$makeCrc = sprintf('%08x', (Crc::crc(substr($message, 0, -2))));
if($crc != substr($makeCrc,0,2))
{
if ($crc != substr($makeCrc, 0, 2)) {
Gateway::closeClient($clientId);
}else{
} else {
$command = substr($message,6,2);
$command = substr($message, 6, 2);
$route = Api::getRoute($command);
if( $route instanceof Route)
{
if ($route instanceof Route) {
$controllerName = $route->getController();
$method = $route->getMethod();
$controllerName = APP.$controllerName;
$controllerName = APP . $controllerName;
//去除crc字节
$message = substr($message,0,-2);
$message = substr($message, 0, -2);
$message = mb_strtoupper($message);
......@@ -113,16 +108,19 @@ class Events
foreach ($middleware as $item) {
if($item instanceof \Application\Middleware)
{
if($item->handle($clientId,$message) === false)
{
if ($item instanceof \Application\Middleware) {
if ($item->handle($clientId, $message) === false) {
try {
Gateway::closeCurrentClient();
} catch (Exception $e) {
}
}
}
}
$controller = new $controllerName($clientId,$message,$command);
$controller = new $controllerName($clientId, $message, $command);
$controller->$method();
......@@ -132,13 +130,30 @@ class Events
}
}
/**
*
* @param $clientId
*/
public static function onClose($clientId)
{
//fixme 网关离线将往下所有的口子全部设置为离线
if(!isset($_SESSION['port_mac_id']))
return;
$portMacIdArray = $_SESSION['port_mac_id'];
if(isset($portMacIdArray))
{
$port = new \Lib\port\Port();
$port->offline(array_values($portMacIdArray));
}
}
......
......@@ -14,9 +14,19 @@ use Application\Model;
class OrderModel extends Model
{
protected $table = 'wx_charger_order';
protected $table = 'wx_charger_orders';
/**
* @param int $orderId
* @return string
*/
public function getPortId(int $orderId)
{
return $this->getColumn('port_id', 'id = ' . $orderId);
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ use Application\Model;
class PortModel extends Model
{
protected $table = 'ports';
protected $table = 'devices';
......
......@@ -96,7 +96,7 @@ class Response
$crc = substr(sprintf('%08x',Crc::crc($exceptCrc)),0,2);
var_dump($exceptCrc.$crc);
var_dump('message:'.PHP_EOL.$exceptCrc.$crc);
return $exceptCrc.$crc;
......
......@@ -14,6 +14,7 @@ use Lib\redis\RedisKey;
class Port
{
private $mac;
private $id;
......@@ -38,13 +39,13 @@ class Port
* @param array $idLogic
* @param int $gatewayId
*/
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));
}
......@@ -61,29 +62,49 @@ class Port
* @param $gatewayId
* @return array
*/
public function getPortId(array $logicId,$gatewayId)
public function getPortId(array $logicId, $gatewayId)
{
return $this->redis->hMGet('gateway_id_'."$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,$logicId);
return $this->redis->hMGet('gateway_id_' . "$gatewayId" . RedisKey::GATEWAY_PORT_LOGIC_ID_HASH, $logicId);
}
/**
* 设置端口的状态
* 端口zset中,gateway的端口zset
* @param array $portIdState
*/
public function setPortState(array $portIdState)
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portIdState as $id => $state) {
array_push($tmp,$state);
array_push($tmp, $state);
array_push($tmp,$id);
array_push($tmp, $id);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
$this->pipe();
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::GATEWAY_PORT_STATE . $_SESSION['gateway_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::STATION_PORT_STATE . $_SESSION['station_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$this->exec();
}
/**
* 端口id在线
* @param array $portId
......@@ -94,13 +115,27 @@ class Port
foreach ($portId as $item) {
array_push($tmp,1);
array_push($tmp, 1);
array_push($tmp,$item);
array_push($tmp, $item);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
$this->pipe();
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::GATEWAY_PORT_STATE . $_SESSION['gateway_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::STATION_PORT_STATE . $_SESSION['station_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$this->exec();
}
......@@ -112,16 +147,28 @@ class Port
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portId as $item) {
array_push($tmp,0);
array_push($tmp, 0);
array_push($tmp,$item);
array_push($tmp, $item);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
$this->pipe();
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::GATEWAY_PORT_STATE . $_SESSION['gateway_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$tmp[0] = RedisKey::STATION_PORT_STATE . $_SESSION['station_id'];
call_user_func_array([$this->redis, 'zAdd'], $tmp);
$this->exec();
}
public function exec()
......
......@@ -30,6 +30,9 @@ class RedisKey
//端口状态有续集合
const PORT_STATE_ZSET = 'port:id_state:zset';
const GATEWAY_PORT_STATE = 'gateway:port:state:';
const STATION_PORT_STATE = 'station:port:state:';
}
\ No newline at end of file
......@@ -21,6 +21,8 @@ class Api
public static function boot()
{
static::add('00','HeartController@heart');
static::add('01','CertifyController@certify');
static::add('02','AccessController@access');
......@@ -33,6 +35,12 @@ class Api
static::add('09','PortController@offline',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('07','PowerController@power',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('17','PowerController@onePower',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('0b','PortController@portState',[new AuthMiddleware(),new HasMacMiddleware()]);
}
public static function add(string $command, string $controllerAction,array $middleware =[])
......
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