Device.php
8.36 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
<?php
namespace domain\device;
use Yii;
use common\helpers\Utils;
use domain\device\models\Device as DeviceModel;
use stdClass;
class Device
{
/**
* @param $item
* @return DeviceModel
*/
static function create($item)
{
$newDevice = new DeviceModel();
$newDevice->serial_no = $item['serial_no'];
$newDevice->mac = $item['mac'];
$newDevice->device_id = $item['device_id'];
$newDevice->batch_id = $item['batch_id'];
$newDevice->status = $item['status'];
$newDevice->has_re_auth = $item['has_re_auth'];
$newDevice->apply_at = $item['apply_at'];
$newDevice->auth_at = $item['auth_at'];
$newDevice->save();
return $newDevice;
}
/**
* @param $batchId
* @param $batchNo
* @param $deviceId
* @param $applyAt
* @param $hasReAuth
* @return DeviceModel
*/
static function createWithMacSerialNo($batchId, $batchNo, $deviceId, $applyAt, $hasReAuth, $status)
{
$exitDeviceModel = DeviceModel::find();
$exitDeviceModel->where(['batch_id' => $batchId]);
$exitDeviceModel->orderBy('serial_no desc');
$exitDevice = $exitDeviceModel->one();
$serialNo = '';
if ($exitDevice) {
$numNo = mb_substr($exitDevice->serial_no, -4);
$no1 = hexdec($numNo);
$no = ($no1 * 1) + 1;
$newNo = sprintf('%04X', $no);
$serialNo = $batchNo.$newNo;
}
$macAddress = Utils::macGenerate();
$item = [
'serial_no' => strtoupper($serialNo),
'mac' => $macAddress,
'device_id' => $deviceId,
'batch_id' => $batchId,
'status' => $status,
'has_re_auth' => $hasReAuth,
'apply_at' => $applyAt,
'auth_at' => null,
];
return self::create($item);
}
/**
* 序列号前缀,也是batchNo
* @param $manufacture
* @param $project
* @param $model
* @param $production
* @return string
*/
static function getBatchNo($manufacture, $project, $model, $production)
{
return $manufacture. $project. $model. $production;
}
static function explodeBarcode($barcode)
{
//建议格式: 0001 0001 0001 0001
//供参考 厂商ID 项目ID 型号ID 生产日期ID
$len = mb_strlen($barcode);
if ($len != 16) {
return null;
}
$manufactureNo = mb_substr($barcode, 0, 4, 'utf-8');
$projectNo = mb_substr($barcode, 4, 4, 'utf-8');
$modelNo = mb_substr($barcode, 8, 4, 'utf-8');
$productionNo = mb_substr($barcode, 12, 4, 'utf-8');
return [$manufactureNo, $projectNo, $modelNo, $productionNo];
}
/**
* @param $deviceId
* @param $manufactureNo
* @param $projectNo
* @param $modelNo
* @param $productionNo
* @return stdClass
*/
static function authDevice($deviceId, $manufactureNo, $projectNo, $modelNo, $productionNo)
{
$e = new stdClass();
$e->success = false;
$e->message = '';
$e->serial_no = '';
$e->mac = '';
$e->status = 0;
$tt = time();
$batchNo = self::getBatchNo($manufactureNo, $projectNo, $modelNo, $productionNo);
$batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
if (empty($batchModel)) {
$e->message = '没有该批次';
$e->status = 3;
$item = [
'manufacture_no' => $manufactureNo,
'project_no' => $projectNo,
'model_no' => $modelNo,
'production_no' => $productionNo,
'device_id' => $deviceId,
];
DeviceAuthFail::create($item);
return $e;
}
$batchId = $batchModel->id;
$deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]);
if ($deviceModel) {
if (DeviceStatus::HAS_AUTH == $deviceModel->status) {
$e->mac = $deviceModel->mac;
$e->serial_no = $deviceModel->serial_no;
$e->message = '授权成功, 重复授权';
$e->success = true;
$e->status = 4;
return $e;
}
$deviceModel->status = DeviceStatus::HAS_AUTH;
$deviceModel->auth_at = $tt;
if ($deviceModel->save()) {
$e->mac = $deviceModel->mac;
$e->serial_no = $deviceModel->serial_no;
$e->message = '授权成功, 重复授权';
$e->success = true;
$e->status = 4;
} else {
$e->message = '设备已经存在,授权失败';
$e->success = false;
$e->status = 5;
}
return $e;
}
$needRecord = DeviceRepository::checkNeedRecordFailRecord($batchId, $batchModel->num);
if ($needRecord) {
// 超过了限制数,记录到另外一个表里面
$failRecord = DeviceAuthFailRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]);
if (!$failRecord) {
$item = [
'manufacture_no' => $manufactureNo,
'project_no' => $projectNo,
'model_no' => $modelNo,
'production_no' => $productionNo,
'device_id' => $deviceId,
];
DeviceAuthFail::create($item);
}
$e->status = 6;
$e->message = '授权失败,超过厂商设定的批次数量';
return $e;
}
if (empty($deviceModel)) {
$needGen = DeviceRepository::checkNeedAutoGen($batchId, $batchModel->num);
if ($needGen) {
$genDeviceModel = Device::createWithMacSerialNo($batchId, $batchNo, $deviceId, $tt, 0, DeviceStatus::HAS_AUTH);
if ($genDeviceModel) {
$e->message = '授权成功';
$e->success = true;
$e->serial_no = $genDeviceModel->serial_no;
$e->mac = $genDeviceModel->mac;
} else {
$e->status = 7;
$e->message = '授权失败, 下次重试授权!';
}
return $e;
}
$e = self::updateRecord($batchId, $deviceId);
return $e;
}
return $e;
}
/**
* @param $batchId
* @param $deviceId
* @return stdClass
*/
private static function updateRecord($batchId, $deviceId)
{
$e = new stdClass();
$e->success = false;
$e->message = '';
$e->serial_no = '';
$e->mac = '';
$e->status = 0;
$tt = time();
// 找到未空白未绑定的设备序列号
$newDeviceModel = DeviceRepository::findOne(['device_id' => null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
if (empty($newDeviceModel)) {
$e->status = 8;
$e->message = '授权失败,系统异常';
return $e;
}
$status = DeviceStatus::HAS_AUTH;
// 测试代码
/*
if (strpos($deviceId, 'FAILDEVICE') === false) {
$status = DeviceStatus::HAS_AUTH;
} else {
$status = DeviceStatus::FAIL_AUTH;
}*/
$newDeviceModel->device_id = $deviceId;
$newDeviceModel->status = $status;
$newDeviceModel->apply_at = $tt;
$newDeviceModel->auth_at = $tt;
if ($newDeviceModel->save()) {
$e->message = '授权成功';
$e->success = true;
$e->serial_no = $newDeviceModel->serial_no;
$e->mac = $newDeviceModel->mac;
} else {
$e->message = '授权失败,系统异常';
$e->status = 8; //系统异常
}
/* 测试代码*/
/*
if (DeviceStatus::FAIL_AUTH == $status) {
$e->message = '授权失败,系统异常';
$e->success = false;
$e->serial_no = '';
$e->status = 8;
$e->mac = '';
}
*/
return $e;
}
}