Merge branch 'develop' of gitea.hmily.club:liutk/internet-everythings-agricultural into develop
commit
22d9248cd3
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware
|
|||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
'callback/*'
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,19 +22,20 @@ class HttpClient
|
|||
* @param string $deviceId
|
||||
* @param \Illuminate\Support\Carbon $start
|
||||
* @param \Illuminate\Support\Carbon $end
|
||||
* @param array $pageable
|
||||
* @param int $page
|
||||
* @param int $perPage
|
||||
* @return array
|
||||
*/
|
||||
public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, array $pageable = []): array
|
||||
public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, int $page = 1, int $perPage = 50): array
|
||||
{
|
||||
$result = $this->post('/api/deviceFlow/v1/list', [
|
||||
'device_id' => $deviceId,
|
||||
'start_time' => $start->unix() * 1000,
|
||||
'end_time' => $end->unix() * 1000,
|
||||
'pageable' => array_merge([
|
||||
'page' => 0,
|
||||
'size' => 20,
|
||||
], $pageable),
|
||||
'pageable' => [
|
||||
'page' => $page - 1,
|
||||
'size' => $perPage,
|
||||
],
|
||||
]);
|
||||
|
||||
return $result['data'];
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Device extends Model
|
||||
{
|
||||
|
|
@ -16,6 +17,10 @@ class Device extends Model
|
|||
public const TYPE_WATER_QUALITY = 3; //水质设备
|
||||
public const TYPE_METEOROLOGICAL = 4; //气象设备
|
||||
|
||||
public const STATE_DISABLED = 0;
|
||||
public const STATE_ONLINE = 1;
|
||||
public const STATE_OFFLINE = 2;
|
||||
public const STATE_FAULT = 3;
|
||||
|
||||
protected $fillable = [
|
||||
'name', 'sn', 'powered_by', 'type', 'model_sn', 'state', 'extends',
|
||||
|
|
@ -26,6 +31,11 @@ class Device extends Model
|
|||
return $date->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
public function logs(): HasMany
|
||||
{
|
||||
return $this->hasMany(DeviceLog::class);
|
||||
}
|
||||
|
||||
public static function typeMap()
|
||||
{
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DeviceLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'data' => 'json',
|
||||
'reported_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'device_id',
|
||||
'data',
|
||||
'reported_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('device_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id');
|
||||
$table->json('data')->nullable();
|
||||
$table->timestamp('reported_at');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['device_id', 'reported_at']);
|
||||
$table->index('reported_at');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('device_logs');
|
||||
}
|
||||
};
|
||||
|
|
@ -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');
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue