修复 Linkos 设备上线、离线通知
parent
8f8d5ff8ef
commit
177b2cee24
|
|
@ -11,7 +11,11 @@ class LinkosCallbackController extends Controller
|
|||
public function __invoke(Request $request)
|
||||
{
|
||||
if ($request->filled('notify_type')) {
|
||||
$this->handleDeviceStateNotify($request);
|
||||
switch ($request->notify_type) {
|
||||
case 'online_state_change':
|
||||
$this->handleDeviceStateNotify($request);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['code' => 0, 'msg' => 'ok']);
|
||||
|
|
@ -22,25 +26,31 @@ class LinkosCallbackController extends Controller
|
|||
*/
|
||||
protected function handleDeviceStateNotify(Request $request): void
|
||||
{
|
||||
if ($request->notify_type !== 'online_state_change') {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($request->input('data', []) as $item) {
|
||||
if (is_null($device = Device::where('sn', $item['device_id'])->first())) {
|
||||
if (! isset($item['device_id'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($request->filled('online_state')) {
|
||||
$state = match ((int) $request->online_state) {
|
||||
0 => Device::STATE_OFFLINE,
|
||||
1 => Device::STATE_ONLINE,
|
||||
default => $device->state,
|
||||
};
|
||||
$device = Device::where('sn', $item['device_id'])->first();
|
||||
|
||||
$device->forceFill([
|
||||
'state' => $state,
|
||||
])->save();
|
||||
if ($device === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($item['online_state'])) {
|
||||
switch ($item['online_state']) {
|
||||
case 0:
|
||||
$device->forceFill([
|
||||
'state' => Device::STATE_OFFLINE,
|
||||
])->save();
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$device->forceFill([
|
||||
'state' => Device::STATE_ONLINE,
|
||||
])->save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue