48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Callback;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Device;
|
|
use Illuminate\Http\Request;
|
|
|
|
class LinkosCallbackController extends Controller
|
|
{
|
|
public function __invoke(Request $request)
|
|
{
|
|
if ($request->filled('notify_type')) {
|
|
$this->handleDeviceStateNotify($request);
|
|
}
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok']);
|
|
}
|
|
|
|
/**
|
|
* 设备状态通知
|
|
*/
|
|
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())) {
|
|
continue;
|
|
}
|
|
|
|
if ($request->filled('online_state')) {
|
|
$state = match ((int) $request->online_state) {
|
|
0 => Device::STATE_OFFLINE,
|
|
1 => Device::STATE_ONLINE,
|
|
default => $device->state,
|
|
};
|
|
|
|
$device->forceFill([
|
|
'state' => $state,
|
|
])->save();
|
|
}
|
|
}
|
|
}
|
|
}
|