1
0
Fork 0

修复 Linkos 设备上线、离线通知

develop
李静 2023-05-04 11:30:40 +08:00
parent 8f8d5ff8ef
commit 177b2cee24
1 changed files with 25 additions and 15 deletions

View File

@ -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;
}
}
}
}