Commit d22590f3 by BANice

部分功能调试完成

parent 5609ac12
......@@ -8,8 +8,8 @@ REGISTER_IP=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
START_FILES=start_businessworker
DB_USER=ctz
DB_NAME=lierda_ctz
DB_PASSWORD=ctz
DB_USER=cdz
DB_NAME=cdzpay
DB_PASSWORD=senthink123
DB_PORT=3306
DB_HOST=127.0.0.1
\ No newline at end of file
/storage/
.env
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData">
<serverData>
<paths name="cdz">
<serverdata>
<mappings>
<mapping deploy="/cdzbusiness" local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/vendor/psr/log" />
<path value="$PROJECT_DIR$/vendor/monolog/monolog" />
<path value="$PROJECT_DIR$/vendor/workerman/gateway-worker" />
<path value="$PROJECT_DIR$/vendor/workerman/mysql" />
<path value="$PROJECT_DIR$/vendor/workerman/workerman" />
<path value="$PROJECT_DIR$/vendor/composer" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.2" />
</project>
\ No newline at end of file
......@@ -126,7 +126,7 @@ class Controller
$this->clientId = $clientId;
$this->message = $message;
$this->message = mb_strtoupper($message);
$this->headerBody = $this->parse($message, $this->defaultHeader);
......
......@@ -11,7 +11,9 @@ namespace Application\Controller;
use Application\Controller;
use Application\Model\GatewayModel;
use Application\Model\PortModel;
use GatewayWorker\Lib\Gateway;
use Lib\port\Port;
class AccessController extends Controller
{
......@@ -19,12 +21,12 @@ class AccessController extends Controller
protected $parameterMap = [
'02' => [
'mac' => 6
'mac' => 6,
],
'03' => [
'start_num' => 1,
'count' => 1,
'total_count' => 1
'start_num' => 1,
'count' => 1,
'total_count' => 1,
],
];
......@@ -35,50 +37,156 @@ class AccessController extends Controller
*/
public function access()
{
if($_SESSION['certified'] === true)
{
if ($_SESSION['certified'] === true) {
$this->response->send('02');
$gatewayModel = new GatewayModel();
$mac = $this->get('mac');
$gatewayId = $gatewayModel->getColumn('id'," mac_address = '$mac' ");
$gatewayId = $gatewayModel->getColumn('id', " mac_address = '$mac' ");
v($gatewayId);
if ($gatewayId != false) {
if($gatewayId != false)
{
echo 'bind mac - '.$mac.PHP_EOL;
}
//todo gatewayId 查询所有的口子的id和mad 存入session
//tcp绑定uid
Gateway::bindUid($this->clientId, $mac);
$_SESSION['gateway_id'] = $gatewayId;
$portModel = new PortModel();
$portMacId = $portModel->getAllColumns(['mac_address', 'id'], 'gateway_id = ' . $gatewayId);
if (count($portMacId) !== 0) {
//网关下的端口id和mac注入到session当中,静态缓存
foreach ($portMacId as $item) {
$_SESSION['port_mac_id'][$item['mac_address']] = $item['id'];
}
}
$sessionId = '';
for ($i = 0; $i < 4; $i++) {
$hex = dechex(rand(0, 255));
$sessionId .= $hex;
}
$this->response->send('02', $sessionId);
}else{
} else {
Gateway::closeClient($this->clientId);
}
} else {
Gateway::closeClient($this->clientId);
}
}
/**
* command 03
* 端口同步
*/
public function syncPort()
{
$startLogicNum = $this->get('start_num');
$count = $this->get('count');
$count = hexdec($this->get('count'));
$totalCount = $this->get('total_count');
$portMacsState = substr($this->message,$this->plc);
$macs = substr($this->message, $this->plc, $count * 16);
var_dump($portMacsState);
$this->plc += $count * 16;
$macsState = substr($this->message, $this->plc);
}
$macsState = str_split($macsState, 2);
$macsStateArray = [];
foreach ($macsState as $value) {
array_push($macsStateArray, hexdec($value));
}
$macs = str_split($macs, 16);
$ids = [];
$onlineIds = [];
$offlineIds = [];
$i = 0;
//依据mac地址和session中存储的mac和id的数组获取到对应的id
//根据状态为设置端口id的状态
foreach ($macs as $mac) {
if (isset($_SESSION['port_mac_id'][$mac])) {
//$macIdArray[$mac] = $_SESSION['port_mac_id'][$mac];
$id = $_SESSION['port_mac_id'][$mac];
array_push($ids, $id);
//在线离线状态判断
if ($macsStateArray[$i++] == 1) {
array_push($onlineIds, $id);
} else {
array_push($offlineIds, $id);
}
}
}
$idLogicArray = [];
$logicId = $startLogicNum;
//端口id计算逻辑id
foreach ($ids as $id) {
$idLogicArray[$id] = $logicId;
//根据起始逻辑地址计算逻辑地址
$logicId = sprintf('%02x', hexdec($logicId) + 1);
}
$port = new Port();
$port->pipe();
$port->register($idLogicArray, $_SESSION['gateway_id']);
$port->online($onlineIds);
$port->offline($offlineIds);
$port->exec();
$count = sprintf('%02x',$count);
var_dump($count);
var_dump($totalCount);
$this->response->send('03', $startLogicNum . $count . $totalCount);
}
}
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/21
* Time: 上午8:58
*/
namespace Application\Controller;
use Application\Controller;
class CapacityController extends Controller
{
protected $parameterMap = [
'06' => [
'capacity'=> 4,
],
];
public function power()
{
$capacity = $this->get('capacity');
//todo 记录用电
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ namespace Application\Controller;
use Application\Controller;
use Lib\port\Port;
class PortController extends Controller
{
......@@ -26,20 +27,84 @@ class PortController extends Controller
];
/**
* 端口上线
*/
public function online()
{
$count = $this->get('count');
$logicNums = str_split(substr($this->message,$this->plc,$count),2);
$portsLogicNum = str_split(substr($this->message,$this->plc,$count),2);
$this->plc+=$count;
$macs = str_split(substr($this->message,$this->plc),16);
$logicMacArray = array_combine($logicNums,$macs);
$portsIdLogic = [];
for($i=0;$i<$count;$i++)
{
if(isset($_SESSION['port_mac_id'][$macs[$i]]))
{
$id = $_SESSION['port_mac_id'][$macs[$i]];
$portsIdLogic[$id] = $portsLogicNum[$i];
}
}
$port = new Port();
$port->pipe();
$port->register($portsIdLogic,$_SESSION['gateway_id']);
$port->online(array_keys($portsIdLogic));
$port->exec();
}
/**
* 端口离线
*/
public function offline()
{
$gatewayId = $_SESSION['gateway_id'];
$count = $this->get('count');
$logics = substr($this->message,$this->plc);
$portsLogic = str_split($logics,2);
$port = new Port();
$portsId = $port->getPortId($portsLogic,$gatewayId);
var_dump($portsId);
}
}
\ No newline at end of file
}
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/21
* Time: 上午9:50
*/
namespace Application\Controller;
use Application\Controller;
class PowerController extends Controller
{
protected $parameterMap = [
'04' => [
'type' => 1,
'logic' => 1,
'outTradeNo' => 6,
],
'05' => [
'outTradeNo' => 6,
'logic' => 1,
'state' => 1,
'capacity' => 4
],
'07' => [
'logic' => 1,
'startTime' => 4,
'intervalTime' => 1,
'count' => 1
],
];
public function charger()
{
$type = $this->get('type');
if($type == 1)
{
$logic = $this->get('logic');
$outTradeNo = $this->get('outTradeNo');
}
$this->response->send('14',$outTradeNo);
}
public function stop()
{
$outTradeNo = $this->get('outTradeNo');
$logic = $this->get('logic');
$state = $this->get('state');
$capacity = $this->get('capacity');
//todo 存储数据库
$this->response->send('15',$outTradeNo);
}
public function power()
{
$logic = $this->get('logic');
$startTime = $this->get('startTime');
$count = $this->get('count');
$powerData = substr($this->message,$this->plc,hexdec($count) * 4);
$powerData = str_split($powerData,4);
$this->plc+=hexdec($count) * 4;
$capacity = substr($this->message,$this->plc);
$capacity = str_split($capacity,8);
var_dump($powerData);
var_dump($capacity);
}
}
......@@ -63,6 +63,9 @@ class Events
public static function onMessage($clientId,$message)
{
//$message = mb_strtoupper($message);
var_dump($message);
$crc = substr($message,-2);
......
......@@ -24,12 +24,20 @@ class Model
protected $table;
/**
* Model constructor.
*/
public function __construct()
{
$this->db = \Events::getDb();
}
/**
* @param $column
* @param $where
* @return string
*/
public function getColumn($column,$where)
{
......@@ -38,6 +46,11 @@ class Model
}
/**
* @param array $columns
* @param $where
* @return array
*/
public function getColumns(Array $columns,$where)
{
$columns = implode(',',$columns);
......@@ -46,6 +59,20 @@ class Model
}
/**
* @param array $columns
* @param $where
* @return mixed
*/
public function getAllColumns(Array $columns,$where)
{
$columns = implode(',',$columns);
return $this->db->query("SELECT $columns FROM $this->table WHERE $where");
}
public function getRow($where)
{
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/21
* Time: 上午8:41
*/
namespace Application\Model;
use Application\Model;
class PortModel extends Model
{
protected $table = 'ports';
}
\ No newline at end of file
......@@ -96,6 +96,8 @@ class Response
$crc = substr(sprintf('%08x',Crc::crc($exceptCrc)),0,2);
var_dump($exceptCrc.$crc);
return $exceptCrc.$crc;
}
......
......@@ -32,13 +32,18 @@ class Port
}
public function register(array $logicMac)
/**
* 同步端口
* 端口的id和
* @param array $idLogic
* @param int $gatewayId
*/
public function register(array $idLogic,int $gatewayId)
{
$this->redis->hMset(RedisKey::PORT_MAC_LOGIC_HASH,array_flip($logicMac));
$this->redis->hMset(RedisKey::PORT_ID_LOGIC_HASH,$idLogic);
call_user_func_array([$this->redis,'zset'],)
$this->redis->zAdd()
$this->redis->hMset('gateway_id_'.":$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,array_flip($idLogic));
}
......@@ -49,6 +54,60 @@ class Port
}
/**
* 依据逻辑地址获取网关下的端口id
* @param array $logicId
* @param $gatewayId
* @return array
*/
public function getPortId(array $logicId,$gatewayId)
{
return $this->redis->hMGet('gateway_id_'."$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,$logicId);
}
/**
* 端口id在线
* @param array $portId
*/
public function online(array $portId)
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portId as $item) {
array_push($tmp,1);
array_push($tmp,$item);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
}
/**
* 端口id下线
* @param array $portId
*/
public function offline(array $portId)
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portId as $item) {
array_push($tmp,0);
array_push($tmp,$item);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
}
public function exec()
{
$this->redis->exec();
......
......@@ -20,9 +20,15 @@ class RedisKey
const PORT_MAC_LOGIC_HASH = 'port:mac_logic:hash';
const GATEWAY_PORT_MAC_LOGIC_HASH = ':port:mac_logic_hash';
//端口id和逻辑地址的hash
const PORT_ID_LOGIC_HASH = 'port:id_logic:hash';
const PORT_STATE_ZSET = 'port:state:zset';
const GATEWAY_PORT_LOGIC_ID_HASH = ':port:logic_id_hash';
//端口状态有续集合
const PORT_STATE_ZSET = 'port:id_state:zset';
......
......@@ -25,6 +25,12 @@ class Api
static::add('03','AccessController@syncPort');
static::add('04','PowerController@charger');
static::add('08','PortController@online');
static::add('09','PortController@offline');
}
public static function add(string $command, string $controllerAction,array $middleware =[])
......
[2018-05-18 16:30:48] error.ERROR: Using $this when not in object context [] []
......@@ -84,7 +84,7 @@ class Bintohex
$data = unpack("H*", $buffer);
return $data[1];
return mb_strtoupper($data[1]);
}
}
\ 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