Commit 8447bd66 by banice

route放弃使用静态变量

parent b891742b
...@@ -45,6 +45,12 @@ class Events ...@@ -45,6 +45,12 @@ class Events
} }
static::$redis->select('4'); static::$redis->select('4');
\Application\route\Api::boot();
var_dump('route boot ');
\Lib\log\Log::bootLog();
} }
...@@ -65,7 +71,6 @@ class Events ...@@ -65,7 +71,6 @@ class Events
*/ */
public static function onMessage($clientId, $message) public static function onMessage($clientId, $message)
{ {
var_dump($message);
$crc = substr($message, -2); $crc = substr($message, -2);
...@@ -81,9 +86,13 @@ class Events ...@@ -81,9 +86,13 @@ class Events
if (isset(Api::$route[$command])) { if (isset(Api::$route[$command])) {
$route = Api::$route[$command]; $route = Api::$route[$command];
var_dump(spl_object_hash($route));
if ($route instanceof Route) { if ($route instanceof Route) {
$reflection = $route->getController(); $reflection = $route->getController();
var_dump('1');
$controller = $reflection->newInstanceArgs([$clientId, $message, $route]); $controller = $reflection->newInstanceArgs([$clientId, $message, $route]);
$method = $route->getMethod(); $method = $route->getMethod();
...@@ -96,6 +105,14 @@ class Events ...@@ -96,6 +105,14 @@ class Events
} }
unset($controller);
var_dump('unset controller');
unset($route);
var_dump('unset route');
} }
} }
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
namespace Application\controller; namespace Application\controller;
use Src\log\record\Record;
use Src\log\GatewayLogWriter;
use Src\request\Request; use Src\request\Request;
use Src\response\Response; use Src\response\Response;
use Src\route\Route; use Src\route\Route;
...@@ -15,6 +17,11 @@ use Src\route\Route; ...@@ -15,6 +17,11 @@ use Src\route\Route;
class Controller class Controller
{ {
/** /**
* @var Record
*/
protected $logRecord;
/**
* 数据帧解析 * 数据帧解析
* @var array * @var array
*/ */
...@@ -118,11 +125,22 @@ class Controller ...@@ -118,11 +125,22 @@ class Controller
*/ */
protected $plc = 0; protected $plc = 0;
protected $route; protected $route;
/**
* Controller constructor.
* @param string $clientId
* @param string $message
* @param Route $route
*/
public function __construct(string $clientId, string $message, Route $route) public function __construct(string $clientId, string $message, Route $route)
{ {
var_dump('construct');
var_dump(spl_object_hash($route));
$this->clientId = $clientId; $this->clientId = $clientId;
$this->message = $message; $this->message = $message;
...@@ -137,6 +155,31 @@ class Controller ...@@ -137,6 +155,31 @@ class Controller
$this->encrypt = $this->headerFlag === '05' ? true : false; $this->encrypt = $this->headerFlag === '05' ? true : false;
$this->logRecord = new Record();
$this->logRecord->setBody($this->message);
$this->logRecord->setCommand($route->getCommand());
if (isset($_SESSION['station_id'])) {
$this->logRecord->setStationId($_SESSION['station_id']);
}
if (isset($_SESSION['gateway_id'])) {
$this->logRecord->setGatewayId($_SESSION['gateway_id']);
}
}
public function __destruct()
{
$logWriter = new GatewayLogWriter();
$logWriter->write($this->logRecord);
var_dump('controller destruct');
} }
/** /**
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 下午7:09
*/
namespace Application\controller;
class TestController
{
public function __construct()
{
var_dump('test construct');
}
public function __destruct()
{
var_dump('test destruct');
}
}
\ No newline at end of file
...@@ -36,9 +36,16 @@ class AccessController extends Controller ...@@ -36,9 +36,16 @@ class AccessController extends Controller
public function __construct(string $clientId, string $message, Route $route) public function __construct(string $clientId, string $message, Route $route)
{ {
parent::__construct($clientId, $message, $route); parent::__construct($clientId, $message, $route);
} }
public function __destruct()
{
parent::__destruct(); // TODO: Change the autogenerated stub
}
/** /**
* 请求入网 * 请求入网
* 网关下的所有端口id和mac写如session * 网关下的所有端口id和mac写如session
...@@ -69,6 +76,11 @@ class AccessController extends Controller ...@@ -69,6 +76,11 @@ class AccessController extends Controller
$gatewayId = $gatewayModelResult['id']; $gatewayId = $gatewayModelResult['id'];
//日志记录
$this->logRecord->setGatewayId($gatewayId);
$this->logRecord->setStationId($stationId);
$deviceModel = new DeviceModel(); $deviceModel = new DeviceModel();
$deviceModelResult = $deviceModel->getAllColumns([ $deviceModelResult = $deviceModel->getAllColumns([
......
...@@ -32,9 +32,17 @@ class CertifyController extends Controller ...@@ -32,9 +32,17 @@ class CertifyController extends Controller
public function __construct(string $clientId, string $message, Route $route) public function __construct(string $clientId, string $message, Route $route)
{ {
var_dump('certify construct');
parent::__construct($clientId, $message, $route); parent::__construct($clientId, $message, $route);
} }
public function __destruct()
{
var_dump('certify destruct');
parent::__destruct();
}
public function certify() public function certify()
{ {
......
...@@ -10,9 +10,20 @@ namespace Application\controller\business\charger; ...@@ -10,9 +10,20 @@ namespace Application\controller\business\charger;
use Application\controller\Controller; use Application\controller\Controller;
use Src\route\Route;
class OverController extends Controller class OverController extends Controller
{ {
public function __construct(string $clientId, string $message, Route $route)
{
parent::__construct($clientId, $message, $route);
}
public function over()
{
}
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午9:57
*/
namespace Application\controller\business\port;
use Application\controller\Controller;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
class OfflineController extends Controller
{
protected $parameterMap = [
'09' => [
'count' => 1,
]
];
/**
*
*/
public function offline()
{
$count = $this->request->get('count');
$portsLogicMac = [];
$str = substr($this->message,$this->plc);
$tmp = str_split($str,18);
foreach ($tmp as $item) {
$portsLogicMac[hexdec(substr($item,0,2))] = substr($item,2);
}
$portsMacId = $_SESSION['port_mac_id'];
$port = new Port();
foreach ($portsLogicMac as $portLogic => $portMac) {
if(isset($portsMacId[$portMac]))
{
$port->setPortId($portsMacId[$portMac]);
$port->setState(StateCode::PORT_OFFLINE);
$port->updatePortState();
//todo 记录口子上线
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午9:57
*/
namespace Application\controller\business\port;
use Application\controller\Controller;
use Lib\StateCode;
use Lib\station\gateway\port\Port;
class OnlineController extends Controller
{
protected $parameterMap = [
'08' => [
'count' => 1,
]
];
/**
* 设备上线
*/
public function online()
{
$count = $this->request->get('count');
$portsLogicMac = [];
$str = substr($this->message,$this->plc);
$tmp = str_split($str,18);
foreach ($tmp as $item) {
$portsLogicMac[hexdec(substr($item,0,2))] = substr($item,2);
}
$portsMacId = $_SESSION['port_mac_id'];
$port = new Port();
foreach ($portsLogicMac as $portLogic => $portMac) {
if(isset($portsMacId[$portMac]))
{
$port->setPortId($portsMacId[$portMac]);
$port->setState(StateCode::PORT_ONLINE);
$port->updatePortState();
//todo 记录口子上线
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午10:50
*/
namespace Application\controller\business\power;
use Application\controller\Controller;
class CapacityController extends Controller
{
protected $parameterMap = [
'capacity' => 4,
];
public function capacity()
{
$gatewayId = $_SESSION['gateway_id'];
$capacity = hexdec($this->request->get('capacity'));
}
}
\ No newline at end of file
...@@ -43,6 +43,11 @@ class PowerController extends Controller ...@@ -43,6 +43,11 @@ class PowerController extends Controller
$capacity = hexdec($this->request->get('capacity')); $capacity = hexdec($this->request->get('capacity'));
if (isset($_SESSION['port_id_logic']) == false) {
return;
}
$portsLogicId = array_flip($_SESSION['port_id_logic']); $portsLogicId = array_flip($_SESSION['port_id_logic']);
if (isset($portsLogicId[$logicId])) { if (isset($portsLogicId[$logicId])) {
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 下午3:23
*/
namespace Application\model;
class GatewayLogModel extends Model
{
protected $table = 'gateway_log';
}
\ No newline at end of file
...@@ -16,10 +16,25 @@ class Api ...@@ -16,10 +16,25 @@ class Api
{ {
static::add('01', 'accept\CertifyController@certify'); static::add('01', 'accept\CertifyController@certify');
static::add('00','HeartController@break'); // static::add('00','HeartController@break');
static::add('02','accept\AccessController@request'); // static::add('02','accept\AccessController@request');
static::add('03','accept\AccessController@syncPort'); // static::add('03','accept\AccessController@syncPort');
static::add('0b','business\port\StateController@change'); //
// static::add('04','business\charger\StartController@start');
// static::add('05','business\charger\OverController@over');
//
// static::add('0b','business\port\StateController@change');
//
// static::add('07','business\power\PowerController@plural');
// static::add('17','business\power\PowerController@one');
//
// static::add('06','business\power\CapacityController@capacity');
//
//
// static::add('08','business\port\OnlineController@online');
// static::add('09','business\port\OfflineController@offline');
} }
......
...@@ -11,7 +11,7 @@ $worker = new \GatewayWorker\BusinessWorker(); ...@@ -11,7 +11,7 @@ $worker = new \GatewayWorker\BusinessWorker();
$worker->name = 'cdz_business'; $worker->name = 'cdz_business';
$worker->count = 4; $worker->count = 1;
$worker->registerAddress = getenv('REGISTER_IP') . ':' . getenv('REGISTER_PORT'); $worker->registerAddress = getenv('REGISTER_IP') . ':' . getenv('REGISTER_PORT');
......
/storage/ /storage/
/.env /.env/
/.idea/ /.idea
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 下午4:40
*/
namespace Src;
class Test
{
public function __destruct()
{
var_dump('i am test');
// TODO: Implement __destruct() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午11:09
*/
namespace Src\exception\order;
use Application\model\OrderExceptionModel;
class OrderException
{
/**
* @var int
*/
private $state = 0;
/**
* @var string
*/
private $desc = '';
/**
* @var int
*/
private $orderId = 0;
/**
* @var string
*/
private $time = '';
/**
* @var string
*/
private $data = '{}';
public function __construct()
{
$this->time = date('Y-m-d H:i:s');
}
/**
* 记录订单异常
* @return mixed
*/
public function addException()
{
$orderExceptionModel = new OrderExceptionModel();
return $orderExceptionModel->insertRow([
'state' => $this->state,
'desc' => $this->desc,
'order_id' => $this->orderId,
'created_at' => $this->time,
'data' => $this->data,
]);
}
/**
* @return int
*/
public function getState(): int
{
return $this->state;
}
/**
* @param int $state
*/
public function setState(int $state): void
{
$this->state = $state;
}
/**
* @return string
*/
public function getDesc(): string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function setDesc(string $desc): void
{
$this->desc = $desc;
}
/**
* @return int
*/
public function getOrderId(): int
{
return $this->orderId;
}
/**
* @param int $orderId
*/
public function setOrderId(int $orderId): void
{
$this->orderId = $orderId;
}
/**
* @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 getData(): string
{
return $this->data;
}
/**
* @param string $data
*/
public function setData(string $data): void
{
$this->data = $data;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午11:17
*/
namespace Src\log;
use Application\model\GatewayLogModel;
use Src\log\record\Record;
class GatewayLogWriter
{
private $gatewayLogModel;
public function __construct()
{
$this->gatewayLogModel = new GatewayLogModel();
}
public function write(Record $record)
{
$this->gatewayLogModel->insertRow([
'command' => $record->getCommand(),
'port_id' => $record->getPortId(),
'order_id' => $record->getOrderId(),
'body' => $record->getBody(),
'gateway_id' => $record->getGatewayId(),
'station_id' => $record->getStationId(),
'created_at' => date('Y-m-d H:i:s', $record->getTime()),
]);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 上午11:18
*/
namespace Src\log\record;
class Record
{
/**
* @var int
*/
protected $stationId = 0;
/**
* @var int
*/
protected $gatewayId = 0;
/**
* @var string
*/
protected $command = '';
/**
* @var string
*/
protected $body = '';
/**
* @var int
*/
protected $portId = 0;
/**
* @var int
*/
protected $orderId = 0;
/**
* @var int
*/
protected $time = 0;
public function __construct()
{
$this->time = time();
}
public function __destruct()
{
var_dump('log destruct');
}
/**
* @return int
*/
public function getPortId(): int
{
return $this->portId;
}
/**
* @param int $portId
*/
public function setPortId(int $portId): void
{
$this->portId = $portId;
}
/**
* @return int
*/
public function getOrderId(): int
{
return $this->orderId;
}
/**
* @param int $orderId
*/
public function setOrderId(int $orderId): void
{
$this->orderId = $orderId;
}
/**
* @return int
*/
public function getStationId(): int
{
return $this->stationId;
}
/**
* @param int $stationId
*/
public function setStationId(int $stationId): void
{
$this->stationId = $stationId;
}
/**
* @return int
*/
public function getGatewayId(): int
{
return $this->gatewayId;
}
/**
* @param int $gatewayId
*/
public function setGatewayId(int $gatewayId): void
{
$this->gatewayId = $gatewayId;
}
/**
* @return string
*/
public function getCommand(): string
{
return $this->command;
}
/**
* @param string $command
*/
public function setCommand(string $command): void
{
$this->command = $command;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* @return int
*/
public function getTime(): int
{
return $this->time;
}
/**
* @param int $time
*/
public function setTime(int $time): void
{
$this->time = $time;
}
}
\ No newline at end of file
...@@ -17,6 +17,8 @@ use \ReflectionClass; ...@@ -17,6 +17,8 @@ use \ReflectionClass;
class Route class Route
{ {
/** /**
* 命令 * 命令
* *
...@@ -53,6 +55,20 @@ class Route ...@@ -53,6 +55,20 @@ class Route
private $middleware = []; private $middleware = [];
public function __construct()
{
var_dump('route');
}
public function __destruct()
{
var_dump(spl_object_hash($this));
var_dump('route destruct');
}
/** /**
* Get 中间件验证 * Get 中间件验证
* *
......
...@@ -41,8 +41,9 @@ foreach (glob(__DIR__.'/application/start*.php') as $startFile) { ...@@ -41,8 +41,9 @@ foreach (glob(__DIR__.'/application/start*.php') as $startFile) {
} }
} }
Application\route\Api::boot(); //Application\route\Api::boot();
\Lib\log\Log::bootLog(); //
//\Lib\log\Log::bootLog();
\Workerman\Worker::runAll(); \Workerman\Worker::runAll();
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/21
* Time: 下午6:57
*/
class A{
static $x;
public function __destruct()
{
var_dump('destruct');
}
public function __construct()
{
var_dump('construct');
}
}
$reflection = new \ReflectionClass('A');
var_dump('1');
$x = &$reflection;
//$a = $reflection->newInstanceArgs();
A::$x = new A();
$a1 = A::$x;
var_dump(spl_object_hash($a1));
$a2 = A::$x;
var_dump(spl_object_hash($a2));
unset($a1);
unset($a2);
var_dump('------------------');
//unset($a);
var_dump('sss');
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