Update
parent
c8ae7e78a9
commit
644b37da28
|
|
@ -8,6 +8,7 @@ use App\Iot\BiAng\HttpClient;
|
||||||
use App\Models\Device;
|
use App\Models\Device;
|
||||||
use App\Models\DeviceLog;
|
use App\Models\DeviceLog;
|
||||||
use App\Models\InsecticidalLampReport;
|
use App\Models\InsecticidalLampReport;
|
||||||
|
use App\Models\WormPhoto;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
@ -136,6 +137,15 @@ class DeviceLogSyncCommand extends Command
|
||||||
case DeviceType::InsectSexLure:
|
case DeviceType::InsectSexLure:
|
||||||
$data = $httpClient->getWormPhotos($device->sn, $now->copy()->subHours(24), $now);
|
$data = $httpClient->getWormPhotos($device->sn, $now->copy()->subHours(24), $now);
|
||||||
|
|
||||||
|
foreach ($data['imgUrl'] as $item) {
|
||||||
|
WormPhoto::firstOrCreate([
|
||||||
|
'device_id' => $device->id,
|
||||||
|
'uploaded_at' => $item['time'],
|
||||||
|
], [
|
||||||
|
'url' => $item['url'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$device->update([
|
$device->update([
|
||||||
'status' => count($data['imgUrl'] ?? []) > 0 ? DeviceStatus::Online : DeviceStatus::Offline,
|
'status' => count($data['imgUrl'] ?? []) > 0 ? DeviceStatus::Online : DeviceStatus::Offline,
|
||||||
]);
|
]);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ use App\Models\SoilMonitoringDailyLog;
|
||||||
use App\Models\SoilMonitoringLog;
|
use App\Models\SoilMonitoringLog;
|
||||||
use App\Models\WaterQualityMonitoringDailyLog;
|
use App\Models\WaterQualityMonitoringDailyLog;
|
||||||
use App\Models\WaterQualityMonitoringLog;
|
use App\Models\WaterQualityMonitoringLog;
|
||||||
|
use App\Models\WormPhoto;
|
||||||
use App\Models\WormReport;
|
use App\Models\WormReport;
|
||||||
use App\Services\BiAngDeviceService;
|
use App\Services\BiAngDeviceService;
|
||||||
use App\Services\OperationLogService;
|
use App\Services\OperationLogService;
|
||||||
|
|
@ -644,15 +645,27 @@ class DeviceController extends Controller
|
||||||
public function wormStatics($id, Request $request)
|
public function wormStatics($id, Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'start_time' => ['bail', 'required', 'date_format:Y-m-d'],
|
'start_time' => ['bail', 'required_with:end_time', 'date_format:Y-m-d'],
|
||||||
'end_time' => ['bail', 'required', 'date_format:Y-m-d'],
|
'end_time' => ['bail', 'required_with:start_time', 'date_format:Y-m-d'],
|
||||||
|
], [], [
|
||||||
|
'start_time' => '开始时间',
|
||||||
|
'end_time' => '结束时间',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$startAt = Carbon::parse($request->input('start_time'));
|
// 结束时间
|
||||||
|
$endTime = $request->whenFilled(
|
||||||
|
'end_time',
|
||||||
|
fn ($time) => Carbon::parse($time)->startOfDay(),
|
||||||
|
fn () => now()->startOfDay(),
|
||||||
|
);
|
||||||
|
// 开始时间
|
||||||
|
$startTime = $request->whenFilled(
|
||||||
|
'start_time',
|
||||||
|
fn ($time) => Carbon::parse($time)->startOfDay(),
|
||||||
|
fn () => $endTime->copy()->subDays(6),
|
||||||
|
);
|
||||||
|
|
||||||
$endAt = Carbon::parse($request->input('end_time'));
|
if ($startTime->gt($endTime)) {
|
||||||
|
|
||||||
if ($startAt->gt($endAt)) {
|
|
||||||
throw ValidationException::withMessages([
|
throw ValidationException::withMessages([
|
||||||
'start_time' => ['开始时间不能大于结束时间'],
|
'start_time' => ['开始时间不能大于结束时间'],
|
||||||
]);
|
]);
|
||||||
|
|
@ -661,16 +674,16 @@ class DeviceController extends Controller
|
||||||
$device = Device::findOrFail($id);
|
$device = Device::findOrFail($id);
|
||||||
|
|
||||||
$wormReports = WormReport::where('device_id', $device->id)
|
$wormReports = WormReport::where('device_id', $device->id)
|
||||||
->whereBetween('reported_at', [$startAt->toDateString(), $endAt->toDateString()])
|
->whereBetween('reported_at', [$startTime->toDateString(), $endTime->toDateString()])
|
||||||
->pluck('worm_num', 'reported_at');
|
->pluck('worm_num', 'reported_at');
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$key = $startAt->toDateString();
|
$key = $startTime->toDateString();
|
||||||
$data[$key] = $wormReports->get($key);
|
$data[$key] = $wormReports->get($key);
|
||||||
$startAt->addDay();
|
$startTime->addDay();
|
||||||
} while ($startAt->lte($endAt));
|
} while ($startTime->lte($endTime));
|
||||||
|
|
||||||
return $this->json($data);
|
return $this->json($data);
|
||||||
}
|
}
|
||||||
|
|
@ -681,10 +694,32 @@ class DeviceController extends Controller
|
||||||
public function wormPhotos($id, Request $request, BiAngDeviceService $biAngDeviceService)
|
public function wormPhotos($id, Request $request, BiAngDeviceService $biAngDeviceService)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'start_time' => ['bail', 'required', 'date_format:Y-m-d'],
|
'start_time' => ['bail', 'required_with:end_time', 'date_format:Y-m-d'],
|
||||||
'end_time' => ['bail', 'required', 'date_format:Y-m-d'],
|
'end_time' => ['bail', 'required_with:start_time', 'date_format:Y-m-d'],
|
||||||
|
], [], [
|
||||||
|
'start_time' => '开始时间',
|
||||||
|
'end_time' => '结束时间',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// 结束时间
|
||||||
|
$endTime = $request->whenFilled(
|
||||||
|
'end_time',
|
||||||
|
fn ($time) => Carbon::parse($time)->endOfDay(),
|
||||||
|
fn () => now()->endOfDay(),
|
||||||
|
);
|
||||||
|
// 开始时间
|
||||||
|
$startTime = $request->whenFilled(
|
||||||
|
'start_time',
|
||||||
|
fn ($time) => Carbon::parse($time)->startOfDay(),
|
||||||
|
fn () => $endTime->copy()->startOfDay(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($startTime->gt($endTime)) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'start_time' => ['开始时间不能大于结束时间'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$device = Device::findOrFail($id);
|
$device = Device::findOrFail($id);
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
|
@ -698,6 +733,19 @@ class DeviceController extends Controller
|
||||||
);
|
);
|
||||||
$data = $result['imgUrl'];
|
$data = $result['imgUrl'];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$wormPhotos = WormPhoto::where('device_id', $device->id)
|
||||||
|
->whereBetween('uploaded_at', [$startTime, $endTime])
|
||||||
|
->latest('uploaded_at')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$data = $wormPhotos->map(fn ($item) => [
|
||||||
|
'id' => $item->id,
|
||||||
|
'url' => $item->url,
|
||||||
|
'time' => $item->created_at->toDateTimeString(),
|
||||||
|
]);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->json($data);
|
return $this->json($data);
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ return new class extends Migration
|
||||||
$table->text('url')->nullable();
|
$table->text('url')->nullable();
|
||||||
$table->timestamp('uploaded_at')->comment('上传时间');
|
$table->timestamp('uploaded_at')->comment('上传时间');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index('uploaded_at');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue