Commit 7bc74168 by banice

添加下发消息结果通知,添加设置网关为离线接口

parent 4b5de055
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/8
* Time: 下午2:28
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Lib\Session;
use Lib\station\gateway\Gateway;
use Lib\station\gateway\port\Port;
class GatewayController extends Controller
{
public function offline(Request $request)
{
$gatewaysId = $request->get('gateways');
$gateway = new Gateway();
$port = new Port();
$redis = app('Redis');
if ($redis instanceof \Redis) {
$redis->multi(2);
}
foreach ($gatewaysId as $gatewayId) {
$deviceId = DB::table('gateways')->where('id', $gatewayId)->first([
'nb_device_id'
]);
if (blank($deviceId)) {
continue;
}
$gateway->offline($gatewayId);
}
$redis->exec();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/5
* Time: 上午9:37
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Lib\iot\IotSend;
use Lib\nb_log\NbLog;
class SendController extends Controller
{
const RESULT_CODE = [
'DELIVERED' => 1,
'TIMEOUT' => 2
];
public function sendResult(Request $request)
{
Log::debug('send message back_message');
Log::debug($request->all());
$validator = Validator::make($request->all(), [
'commandId' => 'required',
'deviceId' => 'required',
'result' => 'required'
]);
if ($validator->fails()) {
Log::error($validator->errors()->first());
return;
}
$result = $request->get('result');
$commandId = $request->get('commandId');
$resultCode = $result['resultCode'];
if (isset(self::RESULT_CODE[$resultCode])) {
DB::table('nb_log')->where('command_id', $commandId)->update([
'send_result' => self::RESULT_CODE[$resultCode],
'notify_date' => date('Y-m-d H:i:s'),
]);
$nbLog = new NbLog();
$nbLog->del($commandId);
}
Log::debug('send result');
Log::debug($request->all());
}
public function send(Request $request)
{
$validator = Validator::make($request->all(), [
'key' => 'required',
'content' => 'required',
'device_id' => 'required',
]);
if ($request->get('key') != '6DEWXswackIHb8OnftJgCz5AolrMFKp3u9iUdZ7R24SLmBT10xyNGqvPjehQYV') {
abort(400);
}
if ($validator->fails()) {
Log::error($validator->errors()->first());
return;
}
Log::debug($request->all());
$deviceId = $request->get('device_id');
$content = $request->get('content');
$iotSend = new IotSend();
$iotSend->setDeviceId($deviceId);
$iotSend->setMessageBody($content);
$iotSend->setCallBackUrl(route('sendSendResult'));
/**
* 发送消息
* commandId 存储值redis
* 发送消息存储至数据库
*/
if ($iotSend->send()) {
return response()->json([
'code' => 200,
]);
} else {
Log::debug('send fail');
abort(500);
return response()->json([
'code' => 500,
]);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/12
* Time: 上午10:16
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Lib\Session;
class SessionController extends Controller
{
public function getSession(Request $request)
{
$deviceId = $request->get('id');
$session = Session::all($deviceId);
dd($session);
dd(json_decode($session));
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/6
* Time: 下午5:37
*/
namespace App\Http\Controllers\device;
use App\Http\Controllers\Controller;
use Application\model\GatewayModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Lib\station\gateway\Gateway;
class StateController extends Controller
{
public function state(Request $request)
{
Log::debug($request->all());
}
public function offline(Request $request)
{
$validator = Validator::make($request->all(), [
'ids' => 'required|array'
]);
if ($validator->fails()) {
Log::error($validator->errors()->first());
abort(400, $validator->errors()->first());
}
$gatewayIds = $request->get('ids');
Log::debug($gatewayIds);
$gateway = new Gateway();
$redis = app('Redis');
if ($redis instanceof \Redis) {
$redis->multi(2);
}
foreach ($gatewayIds as $gatewayId) {
$gatewayDeviceId = DB::table('gateways')->where('id', $gatewayId)->first(['nb_device_id']);
if (filled($gatewayDeviceId)) {
$request->merge(['deviceId' => $gatewayDeviceId->nb_device_id]);
$gateway->offline($gatewayId);
}
}
$redis->exec();
}
}
\ No newline at end of file
...@@ -33,6 +33,12 @@ class Gateway ...@@ -33,6 +33,12 @@ class Gateway
} }
public static function isUidOnline(string $clientId)
{
return 1;
}
public static function bindUid(string $clientId, string $uid) public static function bindUid(string $clientId, string $uid)
{ {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Application\controller; namespace Application\controller;
use Lib\Session;
use Src\log\record\Record; use Src\log\record\Record;
use Src\log\GatewayLogWriter; use Src\log\GatewayLogWriter;
use Src\request\Request; use Src\request\Request;
...@@ -159,15 +160,14 @@ class Controller ...@@ -159,15 +160,14 @@ class Controller
$this->logRecord->setCommand($command); $this->logRecord->setCommand($command);
if (filled(Session::get('station_id'))) {
$this->logRecord->setStationId(Session::get('station_id'));
// if (isset($_SESSION['station_id'])) { }
// $this->logRecord->setStationId($_SESSION['station_id']);
// }
// if (isset($_SESSION['gateway_id'])) {
// $this->logRecord->setGatewayId($_SESSION['gateway_id']);
//
// }
if (filled(Session::get('gateway_id'))) {
$this->logRecord->setGatewayId(Session::get('gateway_id'));
}
} }
......
...@@ -156,7 +156,6 @@ class AccessController extends Controller ...@@ -156,7 +156,6 @@ class AccessController extends Controller
*/ */
public function syncPort(): void public function syncPort(): void
{ {
Log::debug('sync port port');
$logicStart = hexdec($this->request->get('logic_start')); $logicStart = hexdec($this->request->get('logic_start'));
...@@ -181,6 +180,9 @@ class AccessController extends Controller ...@@ -181,6 +180,9 @@ class AccessController extends Controller
} }
if (filled(Session::get('port_mac_id')) == false) { if (filled(Session::get('port_mac_id')) == false) {
Log::error('can not get port_mac_id');
return; return;
} }
...@@ -198,6 +200,7 @@ class AccessController extends Controller ...@@ -198,6 +200,7 @@ class AccessController extends Controller
} }
$logicStartTmp = $logicStart; $logicStartTmp = $logicStart;
foreach ($portMacState as $mac => $state) { foreach ($portMacState as $mac => $state) {
$portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false; $portId = isset($portsMacId[$mac]) ? $portsMacId[$mac] : false;
...@@ -232,7 +235,6 @@ class AccessController extends Controller ...@@ -232,7 +235,6 @@ class AccessController extends Controller
$deviceModel->update(['logic_num' => $logicId], 'id=' . $portId); $deviceModel->update(['logic_num' => $logicId], 'id=' . $portId);
} }
Log::debug('update');
$responseOrder = []; $responseOrder = [];
...@@ -253,7 +255,7 @@ class AccessController extends Controller ...@@ -253,7 +255,7 @@ class AccessController extends Controller
$order->setId($orderId); $order->setId($orderId);
if ($order->getOrderCache(\Events::getRedis()) == false) { if ($order->getOrderCache(app('Redis')) == false) {
continue; continue;
} }
$order->setOnlineTime(time()); $order->setOnlineTime(time());
...@@ -264,30 +266,29 @@ class AccessController extends Controller ...@@ -264,30 +266,29 @@ class AccessController extends Controller
$orderState = new OfflinePowerState($order); $orderState = new OfflinePowerState($order);
} }
Log::debug('update');
$remainTime = $orderState->getRemainChargerTime(); $remainTime = $orderState->getRemainChargerTime();
if ($remainTime > 0) { if ($remainTime > 0) {
$logicNum = sprintf(' % 02x', $order->getPortLogicId()); $logicNum = sprintf('%02x', $order->getPortLogicId());
$orderId = sprintf(' % 012x', $order->getId()); $orderId = sprintf('%012x', $order->getId());
$time = sprintf(' % 04x', $remainTime); $time = sprintf('%04x', $remainTime);
array_push($responseOrder, $logicNum . $orderId . $time); array_push($responseOrder, $logicNum . $orderId . $time);
} }
$port->setPortId($portId); $port->setPortId($portId);
$port->setGatewayId($_SESSION['gateway_id']); $port->setGatewayId(Session::get('gateway_id'));
$port->setStationId($_SESSION['station_id']); $port->setStationId(Session::get('station_id'));
$port->setState(StateCode::PORT_CHARGER);
$port->updatePortState();
// $port->setState(StateCode::PORT_CHARGER);
$port->setState($order->getStateCode());
$port->updatePortState();
unset($order); unset($order);
} }
Log::debug('update');
$logicStart = sprintf('%02x', $logicStart); $logicStart = sprintf('%02x', $logicStart);
$count = sprintf('%02x', $count); $count = sprintf('%02x', $count);
......
...@@ -12,6 +12,7 @@ namespace Application\controller\accept; ...@@ -12,6 +12,7 @@ namespace Application\controller\accept;
use Application\controller\Controller; use Application\controller\Controller;
use Application\model\DeviceModel; use Application\model\DeviceModel;
use Application\model\GatewayModel; use Application\model\GatewayModel;
use Illuminate\Support\Facades\DB;
use Lib\Aes; use Lib\Aes;
use Lib\Session; use Lib\Session;
use Src\route\Route; use Src\route\Route;
...@@ -47,11 +48,14 @@ class CertifyController extends Controller ...@@ -47,11 +48,14 @@ class CertifyController extends Controller
public function certify() public function certify()
{ {
Session::clear();
$nonce = $this->getNonce(); $nonce = $this->getNonce();
$mac = $this->request->get('mac'); $mac = $this->request->get('mac');
$gatewayModel = new GatewayModel(); $gatewayModel = new GatewayModel();
$gatewayModelResult = $gatewayModel->getColumns([ $gatewayModelResult = $gatewayModel->getColumns([
...@@ -62,10 +66,15 @@ class CertifyController extends Controller ...@@ -62,10 +66,15 @@ class CertifyController extends Controller
], 'mac_address=' . $mac); ], 'mac_address=' . $mac);
if ($gatewayModelResult == false) { if ($gatewayModelResult == false) {
Log::error('no this gateway hahah'); Log::error('no this gateway');
return; return;
} }
DB::table('gateways')->where('mac_address', $mac)->update([
'nb_device_id' => $this->clientId,
]);
$stationId = $gatewayModelResult['station_id']; $stationId = $gatewayModelResult['station_id'];
$gatewayId = $gatewayModelResult['id']; $gatewayId = $gatewayModelResult['id'];
Session::update('station_id', $stationId); Session::update('station_id', $stationId);
......
...@@ -18,6 +18,7 @@ use Lib\cdz\order\state\ShedState; ...@@ -18,6 +18,7 @@ use Lib\cdz\order\state\ShedState;
use Lib\cdz\order\state\WaitingState; use Lib\cdz\order\state\WaitingState;
use Lib\Gateway; use Lib\Gateway;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Lib\Session;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
use Src\route\Route; use Src\route\Route;
...@@ -59,11 +60,21 @@ class OverController extends Controller ...@@ -59,11 +60,21 @@ class OverController extends Controller
$this->logRecord->setOrderId($orderId); $this->logRecord->setOrderId($orderId);
$order = new Order($orderId); $order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) { if ($order->getOrderCache(app('Redis')) != false) {
$orderStateCode = $order->getStateCode(); $orderStateCode = $order->getStateCode();
$order->setEndCapacity($capacity); $order->setEndCapacity($capacity);
$order->setEndCode($state); $order->setEndCode($state);
$portId = isset($_SESSION['port_logic_id'][$logicId]) ? $_SESSION['port_logic_id'][$logicId] : 0; if (filled(Session::get('port_logic_id'))) {
$portLogicId = Session::get('port_logic_id');
$portId = isset($portLogicId[$logicId]) ? $portLogicId[$logicId] : 0;
} else {
Log::error('miss port_logic_id' . __CLASS__);
return;
}
if ($portId !== 0) { if ($portId !== 0) {
$this->logRecord->setPortId($portId); $this->logRecord->setPortId($portId);
} }
...@@ -108,7 +119,7 @@ class OverController extends Controller ...@@ -108,7 +119,7 @@ class OverController extends Controller
'is_stop' => StateCode::FINISH_REFUND, 'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $order->getEndCapacity(), 'end_capacity' => $order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s'), 'end_time' => date('Y-m-d H:i:s'),
], 'id = ' . $order->getId()); ], 'id=' . $order->getId());
break; break;
case StateCode::FINISH_USER: //用户主动结束 case StateCode::FINISH_USER: //用户主动结束
...@@ -138,8 +149,9 @@ class OverController extends Controller ...@@ -138,8 +149,9 @@ class OverController extends Controller
case StateCode::CHANGE_PORT: case StateCode::CHANGE_PORT:
if (isset($_SESSION['port_logic_id'])) { if (filled(Session::get('port_logic_id'))) {
$portId = isset($_SESSION['port_logic_id'][$logicId]) ? $_SESSION['port_logic_id'][$logicId] : 0; $portLogicId = Session::get('port_logic_id');
$portId = isset($portLogicId[$logicId]) ? $portLogicId[$logicId] : 0;
$port = new Port($portId); $port = new Port($portId);
$port->setState(StateCode::PORT_ONLINE); $port->setState(StateCode::PORT_ONLINE);
......
...@@ -14,6 +14,7 @@ use Lib\cdz\order\Order; ...@@ -14,6 +14,7 @@ use Lib\cdz\order\Order;
use Lib\cdz\order\state\ChargerState; use Lib\cdz\order\state\ChargerState;
use Lib\cdz\order\state\WaitingState; use Lib\cdz\order\state\WaitingState;
use Lib\Gateway; use Lib\Gateway;
use Lib\Session;
use Lib\StateCode; use Lib\StateCode;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
...@@ -49,17 +50,21 @@ class StartController extends Controller ...@@ -49,17 +50,21 @@ class StartController extends Controller
$this->logRecord->setOrderId($orderId); $this->logRecord->setOrderId($orderId);
$capacity = hexdec($this->getRequest()->get('capacity')); $capacity = hexdec($this->getRequest()->get('capacity'));
Log::debug('order_id-->' . $orderId);
Log::debug('capacity-->' . $capacity);
if ($type == 1) { if ($type == 1) {
$order = new Order($orderId); $order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) == false) { if ($order->getOrderCache(app('Redis')) == false) {
$this->response->send('14', sprintf('%012x', $orderId)); $this->response->send('14', sprintf('%012x', $orderId));
return; return;
} else { } else {
$orderLogicId = $order->getPortLogicId(); $orderLogicId = $order->getPortLogicId();
$orderGatewayId = $order->getGatewayId();
if ($orderGatewayId != Session::get('gateway_id')) {
Log::error('port changed cause gateway_id not equal order cache');
$this->response->send('14', sprintf('%012x', $orderId));
return;
}
//logicId 与订单中的实际不相同则已更换口子 //logicId 与订单中的实际不相同则已更换口子
if ($orderLogicId != hexdec($logic)) { if ($orderLogicId != hexdec($logic)) {
...@@ -73,13 +78,13 @@ class StartController extends Controller ...@@ -73,13 +78,13 @@ class StartController extends Controller
} }
if (isset($_SESSION['agent_id'])) { if (filled(Session::get('agent_id'))) {
if (Gateway::isUidOnline($_SESSION['agent_id'])) { if (Gateway::isUidOnline(Session::get('agent_id'))) {
$clientsId = Gateway::getClientIdByUid($_SESSION['agent_id']);
$clientsId = Gateway::getClientIdByUid(Session::get('agent_id'));
$clientId = $clientsId[0]; $clientId = $clientsId[0];
$uIds = Gateway::getUidByClientId($clientId); $uIds = Gateway::getUidByClientId($clientId);
Gateway::sendToUid($_SESSION['agent_id'], json_encode($order->getOrderCache(\Events::getRedis()))); Gateway::sendToUid(Session::get('agent_id'), json_encode($order->getOrderCache(app('Redis'))));
} else { } else {
Log::debug('agent is offline'); Log::debug('agent is offline');
} }
......
...@@ -19,6 +19,7 @@ use Lib\cdz\order\state\ShedState; ...@@ -19,6 +19,7 @@ use Lib\cdz\order\state\ShedState;
use Lib\cdz\order\state\WaitingState; use Lib\cdz\order\state\WaitingState;
use Lib\Gateway; use Lib\Gateway;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Lib\Session;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
...@@ -48,9 +49,9 @@ class StateController extends Controller ...@@ -48,9 +49,9 @@ class StateController extends Controller
$mac = $this->request->get('mac'); $mac = $this->request->get('mac');
$state = hexdec($this->request->get('state')); $state = hexdec($this->request->get('state'));
if (isset($_SESSION['port_mac_id']) !== true) { if (filled(Session::get('port_mac_id')) === true) {
$deviceModel = new DeviceModel(); $deviceModel = new DeviceModel();
$portId = $deviceModel->getColumn('id', 'device_mac = ' . $mac); $portId = $deviceModel->getColumn('id', 'device_mac=' . $mac);
if (isset($portId) === false || $portId == '') { if (isset($portId) === false || $portId == '') {
return; return;
...@@ -58,7 +59,7 @@ class StateController extends Controller ...@@ -58,7 +59,7 @@ class StateController extends Controller
} else { } else {
$portsMacId = $_SESSION['port_mac_id']; $portsMacId = Session::get('port_mac_id');
if (isset($portsMacId[$mac]) === false) { if (isset($portsMacId[$mac]) === false) {
return; return;
...@@ -83,7 +84,7 @@ class StateController extends Controller ...@@ -83,7 +84,7 @@ class StateController extends Controller
$order = new Order((int)$usingOrderId); $order = new Order((int)$usingOrderId);
if ($order->getOrderCache(\Events::getRedis()) == false) { if ($order->getOrderCache(app('Redis')) == false) {
$port->setState($state); $port->setState($state);
$port->updatePortState(); $port->updatePortState();
......
...@@ -15,6 +15,7 @@ use Application\model\OrderPowerModel; ...@@ -15,6 +15,7 @@ use Application\model\OrderPowerModel;
use Lib\cdz\order\Order; use Lib\cdz\order\Order;
use Lib\cdz\order\save\OrderSaveRedis; use Lib\cdz\order\save\OrderSaveRedis;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Lib\Session;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
class PowerController extends Controller class PowerController extends Controller
...@@ -56,8 +57,9 @@ class PowerController extends Controller ...@@ -56,8 +57,9 @@ class PowerController extends Controller
} }
$portsLogicId = $_SESSION['port_logic_id']; $portsLogicId = Session::get('port_logic_id');
if (isset($portsLogicId[$logicId])) {
if (filled($portsLogicId)) {
$portId = $portsLogicId[$logicId]; $portId = $portsLogicId[$logicId];
$port = new Port($portId); $port = new Port($portId);
$orderId = $port->getPortUsingOrderId(); $orderId = $port->getPortUsingOrderId();
...@@ -69,13 +71,13 @@ class PowerController extends Controller ...@@ -69,13 +71,13 @@ class PowerController extends Controller
$this->logRecord->setPortId($portId); $this->logRecord->setPortId($portId);
$order = new Order($orderId); $order = new Order($orderId);
if ($order->getOrderCache(\Events::getRedis()) != false) { if ($order->getOrderCache(app('Redis')) != false) {
if ($order->getStartCapacity() === -1) { if ($order->getStartCapacity() === -1) {
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$startCapacity = $orderModel->getColumn('start_capacity', 'id = ' . $orderId); $startCapacity = $orderModel->getColumn('start_capacity', 'id=' . $orderId);
if ($startCapacity == -1) { if ($startCapacity == -1) {
$orderModel->update(['start_capacity' => $capacity], 'id = ' . $orderId); $orderModel->update(['start_capacity' => $capacity], 'id=' . $orderId);
$saveOrderRedis = new OrderSaveRedis($order); $saveOrderRedis = new OrderSaveRedis($order);
$saveOrderRedis->save(['startCapacity']); $saveOrderRedis->save(['startCapacity']);
...@@ -83,7 +85,7 @@ class PowerController extends Controller ...@@ -83,7 +85,7 @@ class PowerController extends Controller
} else { } else {
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$orderModel->update(['end_capacity' => $capacity], 'id = ' . $orderId); $orderModel->update(['end_capacity' => $capacity], 'id=' . $orderId);
} }
} }
...@@ -141,7 +143,7 @@ class PowerController extends Controller ...@@ -141,7 +143,7 @@ class PowerController extends Controller
return; return;
} }
$portsLogicId = $_SESSION['port_logic_id']; $portsLogicId = Session::get('port_logic_id');
if (isset($portsLogicId[$logicId])) { if (isset($portsLogicId[$logicId])) {
$portId = $portsLogicId[$logicId]; $portId = $portsLogicId[$logicId];
...@@ -176,7 +178,7 @@ class PowerController extends Controller ...@@ -176,7 +178,7 @@ class PowerController extends Controller
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$orderModel->update([ $orderModel->update([
'end_capacity' => hexdec($capacitys[$i]), 'end_capacity' => hexdec($capacitys[$i]),
], 'id = ' . $orderId); ], 'id=' . $orderId);
} }
} }
} else { } else {
......
...@@ -44,6 +44,8 @@ class SettingController extends Controller ...@@ -44,6 +44,8 @@ class SettingController extends Controller
} }
$invertalTime = $idConfigsValue['5']; //设备上报数据间隔单位 秒 $invertalTime = $idConfigsValue['5']; //设备上报数据间隔单位 秒
$invertalTime = 60 * 20;
if (filled(Session::get('station_id')) === false) { if (filled(Session::get('station_id')) === false) {
Log::error('setting miss station_id'); Log::error('setting miss station_id');
...@@ -57,6 +59,7 @@ class SettingController extends Controller ...@@ -57,6 +59,7 @@ class SettingController extends Controller
$getPriceConfig = $idConfigsValue['6']; $getPriceConfig = $idConfigsValue['6'];
$powerSaturateInvertalTime = $idConfigsValue['7']; $powerSaturateInvertalTime = $idConfigsValue['7'];
$extraTime = $idConfigsValue['8']; $extraTime = $idConfigsValue['8'];
$powerLowerLimit = getenv('LOW_POWER_LIMIT'); $powerLowerLimit = getenv('LOW_POWER_LIMIT');
$detectTime = 5; $detectTime = 5;
$afterFormat = $this->adaptor([ $afterFormat = $this->adaptor([
......
...@@ -12,4 +12,6 @@ return [ ...@@ -12,4 +12,6 @@ return [
'password' => 'IoM@1234', 'password' => 'IoM@1234',
'host' => 'https://180.101.147.89:8743/', 'host' => 'https://180.101.147.89:8743/',
'app_key' => 'tWVEY6ruJIYnQ1XdadMZHU9IR0sa', 'app_key' => 'tWVEY6ruJIYnQ1XdadMZHU9IR0sa',
'log_hash_key' => 'cdz:nb:log:command_id_gateway_id:hash',
'log_zset_key' => 'cdz:nb:log:command_id_timestamp:zset',
]; ];
...@@ -35,7 +35,9 @@ class Gateway ...@@ -35,7 +35,9 @@ class Gateway
* *
* @var string * @var string
*/ */
public static $registerAddress = 'webapppush.default:1239'; // public static $registerAddress = 'webapppush.default:1239';
public static $registerAddress = '100.124.0.5:1239';
//public static $registerAddress = '127.0.0.1:1236'; //public static $registerAddress = '127.0.0.1:1236';
......
...@@ -20,6 +20,22 @@ class Session ...@@ -20,6 +20,22 @@ class Session
public static $deviceId = ''; public static $deviceId = '';
public static function all(string $deviceId)
{
return Cache::get($deviceId);
}
public static function clear()
{
$request = app('request');
if ($request instanceof Request) {
$deviceId = $request->get('deviceId');
Cache::forget($deviceId);
}
}
public static function update($key, $value) public static function update($key, $value)
{ {
static::init(); static::init();
...@@ -45,6 +61,12 @@ class Session ...@@ -45,6 +61,12 @@ class Session
} }
} }
public static function set(string $key, $value)
{
static::$session[$key] = $value;
}
public static function get($key = '') public static function get($key = '')
{ {
static::init(); static::init();
......
...@@ -29,7 +29,7 @@ class OrderSaveRedis ...@@ -29,7 +29,7 @@ class OrderSaveRedis
public function __construct(Order $order) public function __construct(Order $order)
{ {
$this->redis = app('Redis')(); $this->redis = app('Redis');
$this->order = $order; $this->order = $order;
......
...@@ -59,7 +59,7 @@ class ChargerState extends State ...@@ -59,7 +59,7 @@ class ChargerState extends State
//释放口子状态 //释放口子状态
$port->freePort($this->order->getId()); $port->freePort($this->order->getId());
//删除订单的缓存 //删除订单的缓存
$this->order->delCache(\Events::getRedis()); $this->order->delCache(app('Redis'));
//用户主动结束充电推送 //用户主动结束充电推送
$finishByUserMessage = new FinishByUserMessage(); $finishByUserMessage = new FinishByUserMessage();
...@@ -129,7 +129,7 @@ class ChargerState extends State ...@@ -129,7 +129,7 @@ class ChargerState extends State
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'end_time' => $this->nowDate, 'end_time' => $this->nowDate,
'state' => 1, 'state' => 1,
], 'id = ' . $this->order->getId()); ], 'id=' . $this->order->getId());
$orderExceptionModel = new OrderExceptionModel(); $orderExceptionModel = new OrderExceptionModel();
$orderExceptionModel->insertRow([ $orderExceptionModel->insertRow([
...@@ -174,7 +174,7 @@ class ChargerState extends State ...@@ -174,7 +174,7 @@ class ChargerState extends State
$broadcastToAgent = new BroadcastToAgent(); $broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::FINISH_EXCEED]); $broadcastToAgent->broadcast($this->order->getOrderCache(app('Redis')) + ['notice_exception_code' => StateCode::FINISH_EXCEED]);
} }
...@@ -227,7 +227,7 @@ class ChargerState extends State ...@@ -227,7 +227,7 @@ class ChargerState extends State
$broadcastToAgent = new BroadcastToAgent(); $broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::FINISH_FULL]); $broadcastToAgent->broadcast($this->order->getOrderCache(app('Redis')) + ['notice_exception_code' => StateCode::FINISH_FULL]);
} }
...@@ -260,7 +260,7 @@ class ChargerState extends State ...@@ -260,7 +260,7 @@ class ChargerState extends State
$orderModel->update([ $orderModel->update([
'updated_at' => $this->nowDate, 'updated_at' => $this->nowDate,
'shed_time' => $this->nowDate, 'shed_time' => $this->nowDate,
], 'id = ' . $this->order->getId()); ], 'id=' . $this->order->getId());
$orderExceptionModel = new OrderExceptionModel(); $orderExceptionModel = new OrderExceptionModel();
...@@ -323,7 +323,7 @@ class ChargerState extends State ...@@ -323,7 +323,7 @@ class ChargerState extends State
$broadcastToAgent = new BroadcastToAgent(); $broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast($this->order->getOrderCache(\Events::getRedis()) + ['notice_exception_code' => StateCode::PORT_SHED]); $broadcastToAgent->broadcast($this->order->getOrderCache(app('Redis')) + ['notice_exception_code' => StateCode::PORT_SHED]);
} }
...@@ -350,7 +350,7 @@ class ChargerState extends State ...@@ -350,7 +350,7 @@ class ChargerState extends State
'end_time' => $this->nowDate, 'end_time' => $this->nowDate,
'updated_at' => $this->nowDate, 'updated_at' => $this->nowDate,
'state' => 1, 'state' => 1,
], 'id = ' . $this->order->getId()); ], 'id=' . $this->order->getId());
$port = new Port(); $port = new Port();
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
......
...@@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Log; ...@@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Log;
use Lib\remote\api\message\FailMessage; use Lib\remote\api\message\FailMessage;
use Lib\remote\api\message\SendMessage; use Lib\remote\api\message\SendMessage;
use Lib\remote\api\message\StartMessage; use Lib\remote\api\message\StartMessage;
use Lib\Session;
use Lib\StateCode; use Lib\StateCode;
use Lib\station\gateway\port\Port; use Lib\station\gateway\port\Port;
use Src\BroadcastToAgent; use Src\BroadcastToAgent;
...@@ -54,12 +55,14 @@ class WaitingState extends State ...@@ -54,12 +55,14 @@ class WaitingState extends State
{ {
//更新订单状态 //更新订单状态
Log::debug('refund order');
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$orderModel->update([ $orderModel->update([
'is_stop' => StateCode::FINISH_REFUND, 'is_stop' => StateCode::FINISH_REFUND,
'end_capacity' => $this->order->getEndCapacity(), 'end_capacity' => $this->order->getEndCapacity(),
'end_time' => date('Y-m-d H:i:s'), 'end_time' => date('Y-m-d H:i:s'),
], 'id = ' . $this->order->getId()); ], 'id=' . $this->order->getId());
$port = new Port(); $port = new Port();
$port->setState(StateCode::PORT_ONLINE); $port->setState(StateCode::PORT_ONLINE);
...@@ -97,7 +100,7 @@ class WaitingState extends State ...@@ -97,7 +100,7 @@ class WaitingState extends State
$stopChargerCommand->setPortLogic($portLogic); $stopChargerCommand->setPortLogic($portLogic);
$stopChargerCommand->setOutTradeNo($orderId); $stopChargerCommand->setOutTradeNo($orderId);
$sendCommand = new SendCommand($stopChargerCommand); $sendCommand = new SendCommand($stopChargerCommand);
$sendCommand->send($_SESSION['client_id']); $sendCommand->send(Session::get('client_id'));
return; return;
} }
...@@ -105,7 +108,6 @@ class WaitingState extends State ...@@ -105,7 +108,6 @@ class WaitingState extends State
//更新订单cache //更新订单cache
$this->order->setStartTime($this->nowTimeStamp); $this->order->setStartTime($this->nowTimeStamp);
$this->order->setStateCode(12); $this->order->setStateCode(12);
\Events::pipe();
$orderSaveRedis = new OrderSaveRedis($this->order); $orderSaveRedis = new OrderSaveRedis($this->order);
$orderSaveRedis->save(['stateCode', 'startTime', 'startCapacity']); $orderSaveRedis->save(['stateCode', 'startTime', 'startCapacity']);
...@@ -114,9 +116,8 @@ class WaitingState extends State ...@@ -114,9 +116,8 @@ class WaitingState extends State
$port->setPortId($this->order->getPortId()); $port->setPortId($this->order->getPortId());
$port->setState(StateCode::PORT_CHARGER); $port->setState(StateCode::PORT_CHARGER);
$port->setGatewayId($this->order->getGatewayId()); $port->setGatewayId($this->order->getGatewayId());
$port->setStationId($_SESSION['station_id']); $port->setStationId(Session::get('station_id'));
$port->updatePortState($this->order->getId()); $port->updatePortState($this->order->getId());
\Events::exec();
//推送消息 //推送消息
...@@ -143,7 +144,7 @@ class WaitingState extends State ...@@ -143,7 +144,7 @@ class WaitingState extends State
} }
$orderModel = new OrderModel(); $orderModel = new OrderModel();
$orderModel->update($update, 'id = ' . $this->order->getId()); $orderModel->update($update, 'id=' . $this->order->getId());
$orderExceptionModel = new OrderExceptionModel(); $orderExceptionModel = new OrderExceptionModel();
...@@ -203,7 +204,7 @@ class WaitingState extends State ...@@ -203,7 +204,7 @@ class WaitingState extends State
$broadcastToAgent = new BroadcastToAgent(); $broadcastToAgent = new BroadcastToAgent();
$broadcastToAgent->broadcast([ $broadcastToAgent->broadcast([
'data' => $this->order->getOrderCache(\Events::getRedis()), 'data' => $this->order->getOrderCache(app('Redis')),
'notice_exception_code' => StateCode::PORT_EXCEPTION, 'notice_exception_code' => StateCode::PORT_EXCEPTION,
]); ]);
...@@ -240,7 +241,7 @@ class WaitingState extends State ...@@ -240,7 +241,7 @@ class WaitingState extends State
$orderModel->update([ $orderModel->update([
'start_capacity' => (int)$this->order->getStartCapacity(), 'start_capacity' => (int)$this->order->getStartCapacity(),
], 'id = ' . $this->order->getId()); ], 'id=' . $this->order->getId());
Log::debug('update shed start_capacity' . $this->order->getId()); Log::debug('update shed start_capacity' . $this->order->getId());
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/5
* Time: 上午9:55
*/
namespace Lib\iot;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Lib\iot\AccessToken;
use Illuminate\Support\Facades\Log;
use Lib\nb_log\NbLog;
class IotSend
{
protected $expireTime = 0;
protected $maxRetransmit = 2;
private $deviceId = '';
private $messageBody = '';
private $callBackUrl = '';
public function send()
{
$headers = [
'app_key' => env('IOT_APP_ID'),
'Authorization' => 'Bearer ' . AccessToken::getAccessTokenCache(),
'Content-Type' => 'application/json'
];
$request = [
'verify' => false,
'cert' => [
config('iot.cert_path'),
'IoM@1234',
]
];
$request['headers'] = $headers;
$client = new Client();
$serviceId = time() . rand(100000, 999999);
$request['json'] = [
'deviceId' => $this->deviceId,
'command' => [
'serviceId' => 'Transmission',
'method' => 'Cmd_Res',
'paras' => ['downData' => strtoupper($this->messageBody)],
],
'expireTime' => $this->expireTime,
// 'callbackUrl' => $this->callBackUrl,
'callbackUrl' => 'http://cdz-nb.test.senthink.com:80/message/result',
'maxRetransmit' => $this->maxRetransmit,
];
$url = 'iocm/app/cmd/v1.4.0/deviceCommands';
$response = $client->request('POST', config('iot.host') . $url, $request);
$body = $response->getBody()->getContents();
$body = json_decode($body, true);
$this->nbLog($this->deviceId, $this->messageBody, $body['commandId']);
return $response->getStatusCode() === 201;
}
/**
* @return string
*/
public function getDeviceId(): string
{
return $this->deviceId;
}
/**
* @param string $deviceId
*/
public function setDeviceId(string $deviceId): void
{
$this->deviceId = $deviceId;
}
/**
* @return string
*/
public function getMessageBody(): string
{
return $this->messageBody;
}
/**
* @param string $messageBody
*/
public function setMessageBody(string $messageBody): void
{
$this->messageBody = $messageBody;
}
/**
* @return string
*/
public function getCallBackUrl(): string
{
return $this->callBackUrl;
}
/**
* @param string $callBackUrl
*/
public function setCallBackUrl(string $callBackUrl): void
{
$this->callBackUrl = $callBackUrl;
}
private function nbLog(string $deviceId, string $content, string $commandId)
{
$gateway = DB::table('gateways')->where('nb_device_id', $deviceId)->first(['id']);
if (filled($gateway)) {
DB::table('nb_log')->insert([
'content' => $content,
'command_id' => $commandId,
'gateway_id' => $gateway->id,
'created_at' => date('Y-m-d H:i:s')
]);
}
$nbLog = new NbLog();
$nbLog->log($commandId, $gateway->id);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/11/8
* Time: 上午11:02
*/
namespace Lib\nb_log;
class NbLog
{
private $redis;
public function __construct()
{
$this->redis = app('Redis');
}
public function log(string $commandId, int $gatewayId)
{
if ($this->redis instanceof \Redis) {
$this->redis->multi(2);
$this->redis->hSet(config('iot.log_hash_key'), $commandId, $gatewayId);
$this->redis->zAdd(config('iot.log_zset_key'), time(), $commandId);
$this->redis->exec();
}
}
public function del(string $commandId)
{
if ($this->redis instanceof \Redis) {
$this->redis->multi(2);
$this->redis->hDel(config('iot.log_hash_key'), $commandId);
$this->redis->zDelete(config('iot.log_zset_key'), $commandId);
$this->redis->exec();
}
}
}
...@@ -9,6 +9,11 @@ ...@@ -9,6 +9,11 @@
namespace Lib\station\gateway; namespace Lib\station\gateway;
use Lib\Session;
use Lib\station\gateway\port\Port;
use Illuminate\Support\Facades\Log;
class Gateway class Gateway
{ {
const GATEWAY_PORT_STATE = 'cdz:gateway:port:state:%s:zset'; const GATEWAY_PORT_STATE = 'cdz:gateway:port:state:%s:zset';
...@@ -24,9 +29,33 @@ class Gateway ...@@ -24,9 +29,33 @@ class Gateway
{ {
$redis = app('Redis'); $redis = app('Redis');
return $redis->zAdd(self::GATEWAY_STATE, $state, $gatewayId); return $redis->zAdd(self::GATEWAY_STATE, $state, $gatewayId);
}
public function offline(int $gatewayId)
{
$portsMacId = Session::get('port_mac_id');
Log::debug($portsMacId);
$portsId = array_values($portsMacId);
$redis = app('Redis');
if ($redis instanceof \Redis) {
$redis->zAdd(self::GATEWAY_STATE, 0, $gatewayId);
$port = new Port();
foreach ($portsId as $portId) {
$port->setPortId($portId);
$port->setState(10);
$port->updatePortState();
}
} }
}
} }
\ No newline at end of file
...@@ -80,6 +80,7 @@ class Port ...@@ -80,6 +80,7 @@ class Port
*/ */
public function updatePortState(int $orderId = 0) public function updatePortState(int $orderId = 0)
{ {
$redis = app('Redis'); $redis = app('Redis');
$redis->zAdd(self::PORT_STATE, $this->state, $this->portId); $redis->zAdd(self::PORT_STATE, $this->state, $this->portId);
...@@ -127,7 +128,6 @@ class Port ...@@ -127,7 +128,6 @@ class Port
*/ */
public function freePort(int $orderId = 0) public function freePort(int $orderId = 0)
{ {
\Events::pipe();
$redis = app('Redis'); $redis = app('Redis');
...@@ -143,8 +143,6 @@ class Port ...@@ -143,8 +143,6 @@ class Port
$redis->hDel(self::USING_ORDER_ID, $this->portId); $redis->hDel(self::USING_ORDER_ID, $this->portId);
\Events::exec();
} }
......
...@@ -34,3 +34,20 @@ $router->get('device/update', 'device\MessageController@receiver'); ...@@ -34,3 +34,20 @@ $router->get('device/update', 'device\MessageController@receiver');
$router->post('message/register', 'TestController@test'); $router->post('message/register', 'TestController@test');
$router->post('message/sender', 'SendController@send');
$router->post('device/state', 'device\StateController@state');
$router->post('message/result', ['uses' => 'SendController@sendResult', 'as' => 'sendSendResult']);
$router->post('gateway/offline', 'GatewayController@offline');
$router->get('gateway', 'SessionController@getSession');
$router->post('gateway/offline', 'device\StateController@offline');
...@@ -6,6 +6,8 @@ use Application\controller\Controller; ...@@ -6,6 +6,8 @@ use Application\controller\Controller;
use Src\request\Request; use Src\request\Request;
use Lib\Crc; use Lib\Crc;
use Application\Gateway; use Application\Gateway;
use Illuminate\Support\Facades\Log;
class Response class Response
{ {
...@@ -102,8 +104,12 @@ class Response ...@@ -102,8 +104,12 @@ class Response
$exceptCrc = self::PREAMBLE . $this->headerFlag . $this->headerCtrl . $this->length . $this->nonce . $this->body; $exceptCrc = self::PREAMBLE . $this->headerFlag . $this->headerCtrl . $this->length . $this->nonce . $this->body;
Log::debug('exception' . $exceptCrc);
$crc = substr(sprintf('%08x', Crc::crc($exceptCrc)), 0, 2); $crc = substr(sprintf('%08x', Crc::crc($exceptCrc)), 0, 2);
Log::debug('crc' . $crc);
return $this->responseMessage = $exceptCrc . $crc; return $this->responseMessage = $exceptCrc . $crc;
} }
......
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