Commit fe9bd023 by banice

save

parent 00fc2ae3
......@@ -11,14 +11,79 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Lib\borrow\ConfirmReturnDoor;
use Lib\borrow\ReturnDoorClose;
class TestController extends Controller
{
const COMMAND = [
'return_open_door' => 11,
'confirm_return_open_door' => 92,
'close_return_door' => 93,
];
public function update()
public function update(Request $request)
{
$payload = $request->get('payload');
$boxCode = $request->get('box_code');
$client = $payload['client'];
if ($client === 'box') {
$topic = $topic = $payload['hdg_topic'];
switch ($topic) {
case 'open_door' :
$operationType = $payload['doors']['operation_type'];
$command = $payload['command'];
$operation = $payload['doors']['operation'];
$door = $payload['doors'];
$outTradeNo = $door['order_id'];
switch ($command . $operation . $operationType) {
//归还电池舱门确认打开
case self::COMMAND['return_open_door'] . '11':
Log::debug('归还电池舱门打开确认');
$confirmReturnDoor = new ConfirmReturnDoor();
$confirmReturnDoor->setOutTradeNo($outTradeNo);
$confirmReturnDoor->setBoxCode($boxCode);
$confirmReturnDoor->setBoxId($door['box_id']);
$confirmReturnDoor->setDoorId($door['door_id']);
$confirmReturnDoor->handle();
break;
// 归还电池舱门已关闭
case self::COMMAND['return_open_door'] . '01':
$returnDoorClose = new ReturnDoorClose();
$returnDoorClose->setBoxCode($boxCode);
$returnDoorClose->setDoorId($door['door_id']);
$returnDoorClose->setOutTradeNo($outTradeNo);
$returnDoorClose->setBoxId($door['box_id']);
$returnDoorClose->setBatteryCode($door['battery_code']);
$returnDoorClose->handle();
break;
// 取走电池舱门确认打开
case self::COMMAND['return_open_door'] . '10':
Log::debug('取走电池舱门确认打开');
break;
// 取走电池舱门确认关闭
case self::COMMAND['return_open_door'] . '00':
break;
}
break;
default:
break;
}
}
}
public function test(Request $request)
......
......@@ -15,6 +15,8 @@ use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\command\ConfirmOpenDoor;
use Lib\state\ReturnWaiting;
use Lib\state\Update;
use Lib\web_socket\OpenDoorResult;
use Lib\web_socket\PushClient;
use Lib\web_socket\ReturnDoorOpen;
......@@ -39,6 +41,74 @@ class UpdateController extends Controller
'close_return_door' => 93,
];
public function updateState(Request $request, \Lib\state\Order $order)
{
$payload = $request->get('payload');
$boxCode = $request->get('box_code');
$client = $payload['client'];
if ($client === 'battery') {
/**
* |数据头|设备类型|命令(2bytes)|数据长度|数据|校验和(2bytes)|结束位(2bytes)|
* |GW|B/L|CMD|LEN|DATA|CHECKSUM|##|
*/
//todo 检查
$message = $payload['message'];
$message = substr($message, 0, strlen($message) - 4);
$command = substr($message, 3, 2);
$data = substr($message, 5);
switch ($command) {
default:
break;
}
Log::debug('battery');
//todo 电池上报数据
return;
}
//hdg
$topic = $payload['hdg_topic'];
switch ($topic) {
case 'open_door':
$operationType = $payload['doors']['operation_type'];
$command = $payload['command'];
$operation = $payload['doors']['operation'];
$door = $payload['doors'];
$outTradeNo = $door['order_id'];
$order->outTradeNo = $outTradeNo;
$orderUpdate = new Update($order);
$orderUpdate->getCache();
switch ($command . $operation . $operationType) {
//归还电池舱门确认打开
case self::COMMAND['return_open_door'] . '11':
$returnWaiting = new ReturnWaiting($order);
$returnWaiting->toReturnDoorConfirm();
break;
// 归还电池舱门已关闭
case self::COMMAND['return_open_door'] . '01':
break;
// 取走电池舱门确认打开
case self::COMMAND['return_open_door'] . '10':
break;
// 取走电池舱门确认关闭
case self::COMMAND['return_open_door'] . '00':
break;
default:
break;
}
}
}
public function update(Request $request, Order $order)
{
$payload = $request->get('payload');
......
......@@ -21,7 +21,8 @@ class InitController extends Controller
{
$validator = Validator::make($request->all(), [
'box_code' => 'required|exists:boxes,code',
'box_num' => 'required|integer'
'box_num' => 'required|integer',
'door_count' => 'required|integer',
]);
if ($validator->fails() === true) {
......@@ -49,7 +50,7 @@ class InitController extends Controller
'error_message' => $validator->errors()->first()
]);
}
$box->initDoor();
$box->initDoor($request->get('door_count'));
return response()->json([
'return_code' => 200,
......
......@@ -22,8 +22,6 @@ class MqttMiddleware
public function handle(Request $request, Closure $next)
{
Log::debug($request->all());
$validator = Validator::make($request->all(), [
'Message' => 'required',
......
......@@ -38,7 +38,7 @@ class Gateway
// public static $registerAddress = 'cdzregister.default.svc.cluster.local:1239';
public static $registerAddress = '100.123.0.68:1239';
public static $registerAddress = '100.124.0.5:1239';
//public static $registerAddress = '127.0.0.1:1236';
......
......@@ -72,7 +72,7 @@ class Client
/**
* @param string $method
* @in bool
* @return bool
*/
public function request($method = 'GET')
{
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午8:48
*/
namespace Lib\battery_state;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class BatteryBorrowState extends State
{
public function toBorrow()
{
Log::error('state error');
}
public function toReturn()
{
try {
DB::beginTransaction();
DB::table('box_rooms')->where('id', $this->roomId)->update([
'battery_code' => $this->batteryCode,
'updated_at' => date('Y-m-d H:i:s'),
]);
DB::table('batteries')->where('code', $this->batteryCode)->update([
'site_id' => $this->siteId,
'room_id' => $this->roomId,
'box_id' => $this->boxId,
'box_type' => $this->boxNum,
]);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午8:49
*/
namespace Lib\battery_state;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class BatteryReturnState extends State
{
public function toBorrow()
{
$date = date('Y-m-d H:i:s');
try {
DB::beginTransaction();
DB::table('box_rooms')->where('id', $this->roomId)->update([
'battery_code' => null,
'updated_at' => $date,
]);
DB::table('batteries')->where('code', $this->batteryCode)->update([
'site_id' => null,
'box_id' => null,
'room_id' => null,
'box_type' => null,
'guest_id' => $this->userId,
'updated_at' => $date,
]);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
}
public function toReturn()
{
Log::error('state error');
// TODO: Implement toReturn() method.
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午8:47
*/
namespace Lib\battery_state;
abstract class State
{
protected $boxNum = 0;
protected $siteId = 0;
protected $batteryCode = '';
protected $batteryId = 0;
protected $roomId = 0;
protected $boxId = 0;
protected $userId = 0;
abstract public function toBorrow();
abstract public function toReturn();
/**
* @return int
*/
public function getBatteryId(): int
{
return $this->batteryId;
}
/**
* @param int $batteryId
*/
public function setBatteryId(int $batteryId): void
{
$this->batteryId = $batteryId;
}
/**
* @return int
*/
public function getRoomId(): int
{
return $this->roomId;
}
/**
* @param int $roomId
*/
public function setRoomId(int $roomId): void
{
$this->roomId = $roomId;
}
/**
* @return int
*/
public function getBoxId(): int
{
return $this->boxId;
}
/**
* @param int $boxId
*/
public function setBoxId(int $boxId): void
{
$this->boxId = $boxId;
}
/**
* @return int
*/
public function getUserId(): int
{
return $this->userId;
}
/**
* @param int $userId
*/
public function setUserId(int $userId): void
{
$this->userId = $userId;
}
/**
* @return string
*/
public function getBatteryCode(): string
{
return $this->batteryCode;
}
/**
* @param string $batteryCode
*/
public function setBatteryCode(string $batteryCode): void
{
$this->batteryCode = $batteryCode;
}
/**
* @return int
*/
public function getSiteId(): int
{
return $this->siteId;
}
/**
* @param int $siteId
*/
public function setSiteId(int $siteId): void
{
$this->siteId = $siteId;
}
/**
* @return int
*/
public function getBoxNum(): int
{
return $this->boxNum;
}
/**
* @param int $boxNum
*/
public function setBoxNum(int $boxNum): void
{
$this->boxNum = $boxNum;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/30
* Time: 上午8:23
*/
namespace Lib\borrow;
use Illuminate\Support\Facades\DB;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\command\ConfirmOpenDoor;
use Lib\Gateway;
use Lib\state\Order;
use Src\box\Box;
use Illuminate\Support\Facades\Log;
class ConfirmReturnDoor
{
private $command = 92;
private $operationType = 1;
private $outTradeNo = '';
private $boxCode = '';
private $doorId = 0;
private $boxId = 0;
public function handle()
{
$orderInfo = DB::table('orders')->where('code', $this->outTradeNo)->first(['in_box_id', 'in_room_id', 'in_room_name', 'status', 'guest_id']);
if ($orderInfo->in_box_id == $this->boxId && $orderInfo->in_room_id == $this->doorId) {
if ($orderInfo->status != 10) {
Log::error('state not 10 ,can not change to 11');
return;
}
$userId = $orderInfo->guest_id;
Gateway::sendToUid('hdg' . $userId, json_encode(['command' => 'return_door_open']));
try {
//更新订单状态,更新门的状态
DB::beginTransaction();
DB::table('orders')->where('code', $this->outTradeNo)->update(
['status' => 11]
);
DB::table('box_rooms')->where('id', $this->doorId)->update(
[
'door_status' => Order::DOOR_STATE['open']
]
);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
$confirmOpenDoor = new ConfirmOpenDoor();
$confirmOpenDoor->setOrderId($this->outTradeNo);
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent($confirmOpenDoor->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($this->boxCode));
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
Log::debug('return door open success');
} else {
Log::error('confirm return door open success fail');
}
} else {
Log::error('return wrong door');
//todo websocket 推送用户归还电柜错误
}
}
/**
* @return int
*/
public function getCommand(): int
{
return $this->command;
}
/**
* @param int $command
*/
public function setCommand(int $command): void
{
$this->command = $command;
}
/**
* @return int
*/
public function getOperationType(): int
{
return $this->operationType;
}
/**
* @param int $operationType
*/
public function setOperationType(int $operationType): void
{
$this->operationType = $operationType;
}
/**
* @return string
*/
public function getOutTradeNo(): string
{
return $this->outTradeNo;
}
/**
* @param string $outTradeNo
*/
public function setOutTradeNo(string $outTradeNo): void
{
$this->outTradeNo = $outTradeNo;
}
/**
* @return string
*/
public function getBoxCode(): string
{
return $this->boxCode;
}
/**
* @param string $boxCode
*/
public function setBoxCode(string $boxCode): void
{
$this->boxCode = $boxCode;
}
/**
* @return int
*/
public function getDoorId(): int
{
return $this->doorId;
}
/**
* @param int $doorId
*/
public function setDoorId(int $doorId): void
{
$this->doorId = $doorId;
}
/**
* @return int
*/
public function getBoxId(): int
{
return $this->boxId;
}
/**
* @param int $boxId
*/
public function setBoxId(int $boxId): void
{
$this->boxId = $boxId;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/30
* Time: 上午10:46
*/
namespace Lib\borrow;
class ConfirmTakeDoor
{
public function handle()
{
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/30
* Time: 上午8:33
*/
namespace Lib\borrow;
use App\Http\model\BatteryModel;
use App\model\DoorModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\battery_state\BatteryBorrowState;
use Lib\command\OpenDoor;
use Src\box\Box;
class ReturnDoorClose
{
private $batteryCode = '';
private $outTradeNo = '';
private $doorId = 0;
private $boxId = 0;
private $boxCode = '';
public function handle()
{
Log::debug('归还舱门关闭');
$orderInfo = DB::table('orders')->where('code', $this->outTradeNo)->first([
'in_box_id',
'in_room_id',
'status',
'in_site_id',
'guest_id',
]);
if (blank($orderInfo)) {
Log::error('miss order', [$this->outTradeNo]);
return;
}
if ($orderInfo->in_box_id == $this->boxId && $orderInfo->in_room_id == $this->doorId && $orderInfo->status == 11) {
if ($this->batteryCode === '0000') {
//todo 提示未归还电池
Log::error('need return battery');
} else {
$userId = $orderInfo->guest_id;
$batteryBorrow = new BatteryBorrowState();
$batteryBorrow->setBatteryCode($this->batteryCode);
$batteryBorrow->setBoxId($this->boxId);
$batteryBorrow->setRoomId($this->doorId);
$batteryBorrow->setSiteId($orderInfo->in_site_id);
$batteryBorrow->toReturn();
DB::table('orders')->where('code', $this->outTradeNo)->update(
[
'status' => 12,
'in_battery_code' => $this->batteryCode,
]
);
DB::table('box_rooms')->where('id', $this->boxId)->update(
[
'door_status' => 2,
'battery_code' => $this->batteryCode,
'updated_at' => Carbon::now(),
]
);
//todo 查找电柜下电量最大的舱门,打开该舱门
$maxBatteryPower = DB::table('box_rooms')->where([
['box_code', $this->boxCode],
['door_status', 2],
['reserve_status', 1]
])->max('battery_power');
$takeDoorId = DoorModel::where([
['box_code', $this->boxCode],
['door_status', 2],
['reserve_status', 1],
['battery_power', $maxBatteryPower],
])->first(['id']);
$takeDoorId = $takeDoorId->id;
Log::debug('take door id ' . $takeDoorId);
$takeDoor = DB::table('box_rooms')->where('id', $takeDoorId)->first(['box_id', 'site_id', 'id', 'name', 'battery_power', 'battery_code']);
$outTradeNo = time() . rand(1000, 9999) . $takeDoor->box_id . $takeDoor->id;
DB::table('orders')->insert([
'out_box_id' => $takeDoor->box_id,
'out_site_id' => $takeDoor->site_id,
'out_room_id' => $takeDoor->id,
'out_battery_code' => $takeDoor->battery_code,
'out_room_name' => $takeDoor->name,
'code' => $outTradeNo,
'guest_id' => $userId,
'status' => 13,
'in_site_id' => $takeDoor->site_id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
$openDoorCommand = new OpenDoor();
$openDoorCommand->setOrderId($outTradeNo);
$openDoorCommand->setOperationType(0);
$openDoorCommand->setBoxNum('0');
$openDoorCommand->setDoorNum($takeDoor->name);
$openDoorCommand->setDoorId($takeDoor->id);
$openDoorCommand->setBoxId($takeDoor->box_id);
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent($openDoorCommand->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($this->boxCode));
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
Log::debug('send success');
} else {
Log::error('send open take door fail');
}
}
}
}
/**
* @return string
*/
public function getBatteryCode(): string
{
return $this->batteryCode;
}
/**
* @param string $batteryCode
*/
public function setBatteryCode(string $batteryCode): void
{
$this->batteryCode = $batteryCode;
}
/**
* @return string
*/
public function getOutTradeNo(): string
{
return $this->outTradeNo;
}
/**
* @param string $outTradeNo
*/
public function setOutTradeNo(string $outTradeNo): void
{
$this->outTradeNo = $outTradeNo;
}
/**
* @return int
*/
public function getDoorId(): int
{
return $this->doorId;
}
/**
* @param int $doorId
*/
public function setDoorId(int $doorId): void
{
$this->doorId = $doorId;
}
/**
* @return int
*/
public function getBoxId(): int
{
return $this->boxId;
}
/**
* @param int $boxId
*/
public function setBoxId(int $boxId): void
{
$this->boxId = $boxId;
}
/**
* @return string
*/
public function getBoxCode(): string
{
return $this->boxCode;
}
/**
* @param string $boxCode
*/
public function setBoxCode(string $boxCode): void
{
$this->boxCode = $boxCode;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/30
* Time: 上午10:46
*/
namespace Lib\borrow;
class TakeDoorClose
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午9:09
*/
namespace Lib\order_state;
class OrderBorrowState
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午9:09
*/
namespace Lib\order_state;
class OrderReturnState
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午9:09
*/
namespace Lib\order_state;
class State
{
}
\ No newline at end of file
......@@ -12,6 +12,17 @@ namespace Lib\state;
class Order
{
const DOOR_STATE = [
'open' => 1,
'close' => 2,
];
private $userId = 0;
private $returnSideId = 0;
private $returnSiteName = '';
private $state = 0;
private $outTradeNo = '';
......@@ -24,8 +35,31 @@ class Order
private $returnBoxNum = 0;
private $returnBatteryCode = '';
private $boxCode = '';
private $takeBoxId = 0;
private $takeBoxCode = '';
private $takeBoxNum = 0;
private $takeDoorId = 0;
private $takeDoorNum = 0;
private $takeSiteId = 0;
private $takeSiteName = '';
private $takeBatteryCode = '';
private $returnBoxCode = '';
private $takeOutTradeNo = '';
public function __get($name)
{
return $this->$name;
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/23
* Time: 下午9:02
*/
namespace Lib\state;
use App\model\OrderModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\command\OpenDoor;
use Src\box\Box;
use Src\pay_rule\CashPay;
class ReturnOpen
{
const STATE = 3;
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function toReturnDoorClose()
{
//todo 判断电池是否归还? 电池仓状态改变,电池归还成功更新电池状态属性,结算租金,查找电量最大电池,下发打开舱门,状态转换到取出舱门等待
$date = date('Y-m-d H:i:s');
if ($this->order->returnBatteryCode === 'ffffffff') {
//todo 提示电池未归还;
return;
}
$orderModel = OrderModel::where('code', $this->order->outTradeNo)->first();
$cashRule = new CashPay();
$cashRule->setOrderModel($orderModel);
$payAmount = $cashRule->getPayAmount();
try {
DB::beginTransaction();
DB::table('box_rooms')->where('id', $this->order->returnDoorId)->update([
'status' => Order::DOOR_STATE['close'],
'battery_code' => $this->order->returnBatteryCode,
'update' => date('Y-m-d H:i:s'),
]);//更新舱门状态
DB::table('batteries')->where('code', $this->order->returnBatteryCode)->update(
[
'room_id' => $this->order->returnDoorId,
'site_id' => $this->order->returnSideId,
'box_id' => $this->order->returnBoxId,
'box_type' => $this->order->returnBoxNum,
'room_name' => $this->order->returnDoorNum,
]
);//电池状态改变
DB::table('orders')->where('code', $this->order->outTradeNo)->update([
'in_date' => $date,
'in_box_id' => $this->order->returnBoxId,
'in_room_id' => $this->order->returnDoorId,
'in_room_name' => $this->order->returnDoorNum,
'in_battery_code' => $this->order->returnBatteryCode,
'rent_money' => $payAmount,
'use_money' => $payAmount,
'in_site_name' => $this->order->returnSideName,
'in_site_id' => $this->order->returnSideId,
'status' => 1,
]);//结算租金
DB::table('orders')->where('code', $this->order->outTradeNo)->update(
['status' => self::STATE + 1]
);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
//todo 更新订单状态
//查找电量最大电池舱门
return $this;
}
public function toTakeDoorOpen()
{
$maxPower = DB::table('box_rooms')->where('box_code', $this->order->returnBoxCode)->where('reserve_status', 1)->max('battery_power');
$takeInfo = DB::table('box_rooms')->where('battery_power', $maxPower)->first(['name', 'id', 'box_type', 'box_id', 'box_code', 'battery_code']);
//下发打开舱门命令
$this->order->takeDoorNum = $takeInfo->name;
$this->order->takeDoorId = $takeInfo->id;
$this->order->takeBoxId = $takeInfo->box_id;
$this->order->takeBoxNum = $takeInfo->box_type;
$this->order->takeBoxCode = $takeInfo->box_code;
$this->order->takeBatterycode = $takeInfo->battery_code;
$this->order->takeOutTradeNo = time() . $this->order->returnBoxId . $this->order->returnDoorId . rand(1000, 9999);
$openDoorCommand = new OpenDoor();
$openDoorCommand->setOrderId($this->order->takeOutTradeNo);
$openDoorCommand->setBoxNum($this->order->takeBoxNum);
$openDoorCommand->setDoorNum($this->order->takeDoorNum);//fixme debug
$openDoorCommand->setDoorId($this->order->takeDoorId);
$openDoorCommand->setBoxId($this->order->takeBoxId);
$openDoorCommand->setOperationType(0);
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent($openDoorCommand->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($this->order->takeBoxCode));
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request()) {
Log::debug('send open take door success');
} else {
Log::error('send open take door fail');
return;
}
//todo 生成新的取走订单
DB::table('orders')->insert([
'guest_id' => $this->order->userId,
'code' => $this->order->takeOutTradeNo,
'box_code' => $this->order->takeBoxCode,
'out_box_id' => $this->order->takeBoxId,
'out_room_id' => $this->order->takeDoorId,
'out_room_name' => $this->order->takeDoorName,
'out_battery_code' => $this->order->takeBatteryCode,
'out_date' => Carbon::now(),
'status' => -1,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
//todo 电池状态改变
$this->order->status = -1;
$orderUpdate = new Update($this->order);
}
}
\ No newline at end of file
......@@ -9,9 +9,12 @@
namespace Lib\state;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\command\ConfirmOpenDoor;
use Src\box\door\Door;
class ReturnWaiting
{
......@@ -26,15 +29,35 @@ class ReturnWaiting
$this->order = $order;
}
/**
* 确认门已经打开
*/
public function toReturnDoorConfirm()
{
$doorStateOpen = 1;
$this->order->state = self::STATE;
$update = new Update($this->order);
$update->update(['state']);
//更新舱门状态
try {
//todo 推送websocket消息 确认门已打开
//todo 回复确认门已打开消息
DB::beginTransaction();
DB::table('orders')->where('code', $this->order->outTradeNo)->update(
['status' => self::STATE + 1]
);
DB::table('box_rooms')->where('id', $this->order->doorId)->update(
[
'door_status' => Order::DOOR_STATE['open']
]
);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
//todo 推送websocket消息 确认门已打开
$confirmOpenDoor = new ConfirmOpenDoor();
$confirmOpenDoor->setOrderId($this->order->outTradeNo);
$publishToTopic = new PublishToTopic();
......@@ -44,6 +67,11 @@ class ReturnWaiting
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
Log::debug('return door open success');
} else {
Log::error('confirm return door open success fail');
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午8:41
*/
namespace Lib\state;
class TakeDoorWaiting
{
/**
* 确认门已经打开
*/
public function toTakeDoorConfirm()
{
$doorStateOpen = 1;
$this->order->state = self::STATE;
$update = new Update($this->order);
$update->update(['state']);
//更新舱门状态
try {
DB::beginTransaction();
DB::table('orders')->where('code', $this->order->outTradeNo)->update(
['status' => self::STATE + 1]
);
DB::table('box_rooms')->where('id', $this->order->doorId)->update(
[
'door_status' => Order::DOOR_STATE['open']
]
);
DB::commit();
} catch (\Exception $exception) {
Log::error($exception->getMessage());
DB::rollBack();
}
//todo 推送websocket消息 确认门已打开
$confirmOpenDoor = new ConfirmOpenDoor();
$confirmOpenDoor->setOrderId($this->order->outTradeNo);
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent($confirmOpenDoor->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($this->order->boxCode));
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
Log::debug('return door open success');
} else {
Log::error('confirm return door open success fail');
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/24
* Time: 上午9:50
*/
namespace Lib\state;
class TakeWaiting
{
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function toTakeDoorConfirm()
{
}
}
\ No newline at end of file
......@@ -32,4 +32,18 @@ class Update
}
}
public function getCache()
{
$redis = app('Redis');
if ($redis instanceof \Redis) {
$data = $redis->hGetAll($this->order->getCacheKey());
foreach ($data as $key => $value) {
$this->order->$key = $value;
}
}
}
}
\ No newline at end of file
......@@ -26,7 +26,9 @@ $router->group(['middleware' => ['mqtt', 'message_exist']], function () use ($ro
$router->post('door/state', 'door\StateController@state');
$router->post('ac/alarm/info', 'ac\AlarmController@post');
$router->post('update', 'UpdateController@update');
//fixme
// $router->post('update', 'UpdateController@update');
$router->post('update', 'TestController@update');
$router->post('battery/update', 'BatteryController@update');
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 上午11:15
*/
namespace src\battery\client;
abstract class Handle
{
protected $map = [];
protected $data;
abstract public function handle();
public function __construct(string $data)
{
$this->data = $data;
$start = 0;
foreach ($this->map as $key => $value) {
if (is_array($value)) {
$value = $value[0];
$this->key = hexdec(substr($data, $start, $value));
}
$this->key = substr($data, $start, $value);
$start += $value;
}
}
public function __set($name, $value)
{
$this->$name = $value;
}
public function __get($name)
{
return $this->$name;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 下午4:28
*/
namespace src\battery\client;
use Illuminate\Support\Facades\DB;
class Info extends Handle
{
protected $map = [
'code' => 8,
];
public function handle()
{
$batteryCode = $this->code;
$info = DB::table('batteries')->where('code', $batteryCode)->first([
'sensor_num',
'battery_pack_no'
]);
$data = substr($this->data, 15);
$batteryNum = $info->battery_pack_no;
$tempNum = $info->sensor_num;
$voltage = substr($data, 0, $batteryNum * 4);
$temp = substr($data, $batteryNum * 4);
$voltage = str_split($voltage, 4);
$temp = str_split($temp, 2);
$temp = array_map(function ($value) {
return hexdec($value);
}, $temp);
$voltage = array_map(function ($value) {
return hexdec($value);
}, $voltage);
DB::table('batteries')->where('code', $batteryCode)->update([
'battery_all_V' => serialize($voltage),
'battery_all_temp' => serialize($temp),
]);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 上午11:15
*/
namespace src\battery\client;
use App\Http\model\BatteryModel;
use Illuminate\Support\Facades\DB;
/**
* 电池上报电池规格
* Class Standard
* @package src\battery\client
*/
class Standard extends Handle
{
protected $map = [
'battery_code' => 8,
'year' => [2],
'month' => [2],
'day' => [2],
'battery_capacity' => [4],
'temp_count' => [2],
'battery_type' => 1,
'transfer_type' => 1,
'icc_id' => 20,
];
public function __construct(string $data)
{
parent::__construct($data);
}
public function handle()
{
if (BatteryModel::where('code', $this->battery_code)->exists() === false) {
DB::table('batteries')->insert([
'code' => $this->battery_code,
'manufacture' => '20' . $this->day . '-' . sprintf('%02d', $this->month) . '-' . sprintf('%02d', $this->day),
'iccid' => $this->icc_id,
'battery_type' => $this->battery_type,
'link_type' => $this->transfer_type,
'sensor_num' => $this->temp_count,
]);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/29
* Time: 上午11:38
*/
namespace src\battery\client;
use Illuminate\Support\Facades\DB;
class State extends Handle
{
protected $map = [
'code' => 8,
'soc' => [2],
'bms' => 1,
'bms_fail_code' => 8,
'total_voltage' => [4],
'total_current' => [4],
'single_max_voltage' => [4],
'single_min_current' => [4],
'single_avg_voltage' => [4],
'max_temp' => [2],
'min_temp' => [2],
'out_mos_temp' => [2],
'in_mos_temp' => [2],
'avg_resistance_temp' => [2],
'in_mos_state' => 1,
'out_mos_state' => 1,
'latitude_sign' => 1,
'latitude' => [8],
'longitude_sign' => 1,
'longitude' => [8],
'location_type' => 1,
];
public function __construct(string $data)
{
parent::__construct($data);
}
public function handle()
{
// TODO: Implement handle() method.
if ($this->latitude_sign == 'S') {
$this->latitude = 0 - (int)$this->latitude;
}
$this->latitude /= 1000000;
if ($this->longitude_sign == 'W') {
$this->longitude = 0 - (int)$this->longitude;
}
$this->longitude /= 1000000;
DB::table('batteries')->where('code', $this->code)->update(
[
'soc' => $this->soc,
'bms' => $this->bms,
'alarm_name' => $this->bms_fail_code,
'battery_V' => $this->total_voltage,
'battery_A' => $this->total_current,
'battery_max_V' => $this->single_max_voltage,
'battery_min_current' => $this->single_min_current,
'mean_V' => $this->single_avg_voltage,
'battery_max_temp' => $this->max_temp,
'battery_min_temp' => $this->min_temp,
'out_mos_temp' => $this->out_mos_temp,
'in_mos_temp' => $this->in_mos_temp,
'mean_temp' => $this->avg_resistance_temp,
'in_mos_status' => $this->in_mos_state,
'out_mos_status' => $this->out_mos_state,
'longitude' => $this->longitude,
'latitude' => $this->latitude,
]
);
}
}
\ 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