1
0
Fork 0

linkos 设备离线、上线通知通知

develop
李静 2023-04-28 17:40:20 +08:00
parent 0eef056851
commit 7b641539cd
4 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,47 @@
<?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();
}
}
}
}

View File

@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware
* @var array<int, string>
*/
protected $except = [
//
'callback/*'
];
}

View File

@ -13,4 +13,10 @@ class DeviceLog extends Model
'data' => 'json',
'reported_at' => 'datetime',
];
protected $fillable = [
'device_id',
'data',
'reported_at',
];
}

View File

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