1
0
Fork 0
vine_liutk 2023-05-04 17:18:35 +08:00
commit 0d48e59c15
1 changed files with 27 additions and 15 deletions

View File

@ -10,8 +10,14 @@ class LinkosCallbackController extends Controller
{ {
public function __invoke(Request $request) public function __invoke(Request $request)
{ {
logger()->info('linkos callback parameters -------->', $request->input());
if ($request->filled('notify_type')) { 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']); return response()->json(['code' => 0, 'msg' => 'ok']);
@ -22,25 +28,31 @@ class LinkosCallbackController extends Controller
*/ */
protected function handleDeviceStateNotify(Request $request): void protected function handleDeviceStateNotify(Request $request): void
{ {
if ($request->notify_type !== 'online_state_change') {
return;
}
foreach ($request->input('data', []) as $item) { foreach ($request->input('data', []) as $item) {
if (is_null($device = Device::where('sn', $item['device_id'])->first())) { if (! isset($item['device_id'])) {
continue; continue;
} }
if ($request->filled('online_state')) { $device = Device::where('sn', $item['device_id'])->first();
$state = match ((int) $request->online_state) {
0 => Device::STATE_OFFLINE,
1 => Device::STATE_ONLINE,
default => $device->state,
};
$device->forceFill([ if ($device === null) {
'state' => $state, continue;
])->save(); }
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;
}
} }
} }
} }