AesController.php
10.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Created by PhpStorm.
* User: ruidiudiu
* Date: 2018/11/12
* Time: 11:02
*/
namespace app\portal\controller;
use aesutil\AESUtil;
use cmf\controller\HomeBaseController;
use think\Db;
/**
* @title 蓝牙锁相关接口
* @description 蓝牙锁相关接口
* @group 蓝牙锁相关接口
*/
class AesController extends HomeBaseController{
public function index(){
$strKey='2222222222222222';
$message='ff0ca23403de4f4aaf5500ef';
$aes=new AESUtil($strKey);
dump($aes->encryption($message));
dump($aes->decryption('2857153e26d8dda190834c3a1a3f8f3c'));
}
// /**
// * @title 开锁(加密)
// * @description 获取加密后的“开锁”命令
// * @author 董瑞恩
// * @url /portal/Aes/secretLock
// * @method GET
// *
// * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
// *
// *
// * @return lockKey:加密的开锁指令(数组)
// */
public function secretLock(){
$MACAddress=$this->request->param('MACAddress');
$strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
if (!empty($strKey)){
$key="ff0ca2".$MACAddress."5500ef";
$aes=new AESUtil($strKey['key']);
$lockKey=$aes->encryption($key);
$this->apiResponse(200,'success',$lockKey);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
}
/**
* @title 开锁(不加密)
* @description 获取“开锁”命令
* @author 董瑞恩
* @url /portal/Aes/lock
* @method GET
*
* @param name:name type:String require:1 default:无 other: desc:设备名称
*
*
* @return lockKey:加密的开锁指令(数组)
*/
public function lock(){
$name=$this->request->param('name');
$strKey=Db::name('equipment')->where('name',$name)->find();
if (!empty($strKey)){
if ($strKey['electricity']==0){
$this->apiResponse(302,'设备电量低');
}
if ($strKey['use']==1){
$this->apiResponse(303,'当前设备正在使用');
}
$strMac=$this->macToStr($strKey['mac_address']);
$key="ff0ca2".$strMac."5500ef";
$lockKey=$this->ToArray($key);
$this->apiResponse(200,'success',$lockKey);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
}
public function macToStr($mac){
$mac_address=explode(":",$mac);
$strMac="";
foreach ($mac_address as $k=>$v){
$strMac.=$v;
}
return $strMac;
}
// /**
// * @title 获取电压、开关状态(加密)
// * @description 获得加密后“获取电压、开关状态”的命令
// * @author 董瑞恩
// * @url /portal/Aes/getSecretState
// * @method GET
// *
// * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
// *
// *
// * @return lockKey:获取电压、开关状态的指令(数组)
// */
public function getSecretState(){
$MACAddress=$this->request->param('MACAddress');
$strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
if (!empty($strKey)){
$key="ff05a301ef";
$aes=new AESUtil($strKey['key']);
$StateKey=$aes->encryption($key);
$this->apiResponse(200,'success',$StateKey);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
}
/**
* @title 获取电压、开关状态(不加密)
* @description 获得加密后“获取电压、开关状态”的命令
* @author 董瑞恩
* @url /portal/Aes/getState
* @method GET
*
* @param name:name type:String require:1 default:无 other: desc:设备名称
*
*
* @return lockKey:获取电压、开关状态的指令(数组)
*/
public function getState(){
$name=$this->request->param('name');
$equipment=Db::name('equipment')->where('mac_address',$name)->find();
if (!empty($equipment)){
$key="ff05a301ef";
$StateKey=$this->ToArray($key);
$this->apiResponse(200,'success',$StateKey);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
}
// /**
// * @title 命令解密
// * @description 将设备的十六进制码进行解码,获得设备的返回信息
// * @author 董瑞恩
// * @url /portal/Aes/decryption
// * @method GET
// *
// * @param name:decryKey type:String require:1 default:无 other: desc:设备返回状态(加密)
// * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
// * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
// *
// * @return code:1锁已开,0位锁已关,2为数据异常
// */
public function decryption(){
$MACAddress=$this->request->param('MACAddress');
$decryKey=$this->request->param('decryKey');
$type=$this->request->param('type');
$strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
if (!empty($strKey)){
$aes=new AESUtil($strKey['key']);
$key=$aes->decryption($decryKey);
$this->state($MACAddress,$key,$type);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
}
/**
* @title 设备状态判断
* @description 解析设备返回值,获取设备状态
* @author 董瑞恩
* @url /portal/Aes/state
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @param name:name type:String require:1 default:无 other: desc:设备名称
* @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
* @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
*
* @return state:1成功 0失败
* @return message:错误类型
* @return error:错误描述
*/
public function state($users_id,$name,$key,$type){
$key=$this->ToArray($key);
if ($type==1){
//判断命令是否合法
if($key[0]=='0xff' && $key[4]=='0xef'){
$this->open_deal_with($users_id,$name,$key);
}else{
$data=[
'state'=>0,
'message'=>'返回指令错误',
'error' =>''
];
$this->apiResponse(200,'success',$data);
}
}else if($type==2){
//判断命令是否合法
if($key[0]=='0xff' && $key[7]=='0xef'){
$this->lock_deal_with($users_id,$name,$key);
}else{
$data=[
'state'=>0,
'message'=>'返回指令错误',
'error' =>''
];
$this->apiResponse(200,'success',$data);
}
}
}
//开锁状态处理
public function open_deal_with($users_id,$name,$key){
if ($key[3]=='0x01'){
//修改数据库设备状态
try{
Db::startTrans();
//用户状态
Db::name('users')->where('id',$users_id)->update(['is_use'=>1]);
Db::name('equipment')->where('name',$name)->update(['use'=>1]);
}catch (\Exception $exception){
Db::rollback();
$data=[
'state'=>0,
'message'=>'数据库操作错误',
'error' => $exception->getMessage()
];
$this->apiResponse(200,'success',$data);
}
//生成订单
$order=new OrderController();
$res=$order->createOrder($users_id,$name);
if ($res['state']==true){
Db::commit();
$data=[
'state'=>1,
'name'=>$name,
'order_no' => $res['order_no']
];
$this->apiResponse(200,'success',$data);
}else{
Db::rollback();
$data=[
'state'=>0,
'message'=>'订单生成错误',
'error' => $res['message']
];
$this->apiResponse(200,'success',$data);
}
}else if($key[3]=='0x00'){
$data=[
'state'=>0,
'message'=>'开锁失败',
'error' =>''
];
$this->apiResponse(200,'success',$data);
}
}
//关锁
public function lock_deal_with($users_id,$name,$key){
//查看数据库中设备状态
$use=Db::name('equipment')->where('name',$name)->find()['use'];
if ($use==0){
$data=[
'state'=>0,
'message'=>'该设备为关闭状态,不能还锁',
'error' =>''
];
$this->apiResponse(200,'success',$data);
}
//电量过低,修改数据库状态
if ($key[5]=='0x01'){
Db::name('equipment')->where('name',$name)->update(['electricity'=>0]);
}
if ($key[6]=='0x01'){
//修改数据库设备状态
$data=[
'state'=>0,
'message'=>'未还床',
'error' =>''
];
$this->apiResponse(200,'success',$data);
}else if($key[6]=='0x00'){
//修改数据库设备状态
$order=new OrderController();
$order->order($users_id,$name);
}
}
//转成数组
private function ToArray($data){
$array = array();
for ($i = 0; $i < strlen($data); $i += 2) {
$array[]='0x'.substr($data, $i, 2);
}
return $array;
}
}