diff --git a/app/Http/Controllers/Callback/LinkosCallbackController.php b/app/Http/Controllers/Callback/LinkosCallbackController.php new file mode 100644 index 0000000..e7f6f4a --- /dev/null +++ b/app/Http/Controllers/Callback/LinkosCallbackController.php @@ -0,0 +1,47 @@ +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(); + } + } + } +} diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 9e86521..331eef3 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware * @var array */ protected $except = [ - // + 'callback/*' ]; } diff --git a/app/Models/DeviceLog.php b/app/Models/DeviceLog.php index fe2bfd6..fff5f0d 100644 --- a/app/Models/DeviceLog.php +++ b/app/Models/DeviceLog.php @@ -13,4 +13,10 @@ class DeviceLog extends Model 'data' => 'json', 'reported_at' => 'datetime', ]; + + protected $fillable = [ + 'device_id', + 'data', + 'reported_at', + ]; } diff --git a/routes/web.php b/routes/web.php index b130397..16744b1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -13,6 +13,10 @@ use Illuminate\Support\Facades\Route; | */ +Route::prefix('callback')->group(function () { + Route::post('linkos', \App\Http\Controllers\Callback\LinkosCallbackController::class); +}); + Route::get('/', function () { return view('welcome'); });