优化虫情统计和虫情照片
parent
a723e40d25
commit
a037f82a5f
|
|
@ -7,10 +7,11 @@ use App\Enums\DeviceType;
|
||||||
use App\Iot\BiAng\HttpClient;
|
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\WormPhoto;
|
use App\Models\WormPhoto;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
|
|
@ -135,16 +136,28 @@ class DeviceLogSyncCommand extends Command
|
||||||
|
|
||||||
case DeviceType::Worm:
|
case DeviceType::Worm:
|
||||||
case DeviceType::InsectSexLure:
|
case DeviceType::InsectSexLure:
|
||||||
|
$dir = "worm-photos/{$device->id}";
|
||||||
|
|
||||||
|
$disk = Storage::disk('public');
|
||||||
|
|
||||||
$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) {
|
foreach ($data['imgUrl'] as $item) {
|
||||||
// WormPhoto::firstOrCreate([
|
// 下载图片
|
||||||
// 'device_id' => $device->id,
|
$name = md5($item['url']);
|
||||||
// 'uploaded_at' => $item['time'],
|
if ($ext = pathinfo($item['url'], PATHINFO_EXTENSION)) {
|
||||||
// ], [
|
$name .= ".{$ext}";
|
||||||
// 'url' => $item['url'],
|
}
|
||||||
// ]);
|
$path = "{$dir}/{$name}";
|
||||||
// }
|
$disk->put($path, file_get_contents($item['url']));
|
||||||
|
|
||||||
|
WormPhoto::firstOrCreate([
|
||||||
|
'device_id' => $device->id,
|
||||||
|
'uploaded_at' => $item['time'],
|
||||||
|
], [
|
||||||
|
'url' => $path,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$device->update([
|
$device->update([
|
||||||
'status' => count($data['imgUrl'] ?? []) > 0 ? DeviceStatus::Online : DeviceStatus::Offline,
|
'status' => count($data['imgUrl'] ?? []) > 0 ? DeviceStatus::Online : DeviceStatus::Offline,
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ class WormStatisticsSyncCommand extends Command
|
||||||
foreach ($devices as $device) {
|
foreach ($devices as $device) {
|
||||||
$this->info('==================================');
|
$this->info('==================================');
|
||||||
|
|
||||||
$latestReportedAt = WormReport::Where('device_id', $device->id)->latest('reported_at')->value('reported_at');
|
$latestReportedAt = WormReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at');
|
||||||
|
|
||||||
$start = $latestReportedAt ? $latestReportedAt->copy() : $today->copy()->subDays(179);
|
$start = $latestReportedAt ? $latestReportedAt->copy() : $today->copy()->subDays(179);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use App\Enums\OperationType;
|
||||||
use App\Helpers\Paginator;
|
use App\Helpers\Paginator;
|
||||||
use App\Http\Requestes\DeviceRequest;
|
use App\Http\Requestes\DeviceRequest;
|
||||||
use App\Http\Resources\DeviceResource;
|
use App\Http\Resources\DeviceResource;
|
||||||
|
use App\Http\Resources\WormPhotoResource;
|
||||||
use App\Models\AgriculturalBase;
|
use App\Models\AgriculturalBase;
|
||||||
use App\Models\Device;
|
use App\Models\Device;
|
||||||
use App\Models\InsecticidalLampDailyReport;
|
use App\Models\InsecticidalLampDailyReport;
|
||||||
|
|
@ -671,19 +672,22 @@ class DeviceController extends Controller
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$device = Device::findOrFail($id);
|
$wormReports = WormReport::where('device_id', $id)
|
||||||
|
|
||||||
$wormReports = WormReport::where('device_id', $device->id)
|
|
||||||
->whereBetween('reported_at', [$startTime->toDateString(), $endTime->toDateString()])
|
->whereBetween('reported_at', [$startTime->toDateString(), $endTime->toDateString()])
|
||||||
->pluck('worm_num', 'reported_at');
|
->pluck('worm_num', 'reported_at');
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
|
||||||
|
if ($startTime->lte($endTime)) {
|
||||||
do {
|
do {
|
||||||
$key = $startTime->toDateString();
|
// 日期
|
||||||
$data[$key] = $wormReports->get($key);
|
$date = $startTime->toDateString();
|
||||||
|
|
||||||
|
$data[$date] = $wormReports->get($date);
|
||||||
|
|
||||||
$startTime->addDay();
|
$startTime->addDay();
|
||||||
} while ($startTime->lte($endTime));
|
} while ($startTime->lte($endTime));
|
||||||
|
}
|
||||||
|
|
||||||
return $this->json($data);
|
return $this->json($data);
|
||||||
}
|
}
|
||||||
|
|
@ -714,40 +718,11 @@ class DeviceController extends Controller
|
||||||
fn () => $endTime->copy()->startOfDay(),
|
fn () => $endTime->copy()->startOfDay(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($startTime->gt($endTime)) {
|
$wormPhotos = WormPhoto::where('device_id', $id)
|
||||||
throw ValidationException::withMessages([
|
|
||||||
'start_time' => ['开始时间不能大于结束时间'],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$device = Device::findOrFail($id);
|
|
||||||
|
|
||||||
$data = [];
|
|
||||||
|
|
||||||
switch ($device->supplier_key) {
|
|
||||||
case 'device-supplier-biang':
|
|
||||||
$result = $biAngDeviceService->getWormPhotos(
|
|
||||||
$device,
|
|
||||||
Carbon::parse($request->input('start_time')),
|
|
||||||
Carbon::parse($request->input('end_time')),
|
|
||||||
);
|
|
||||||
$data = $result['imgUrl'];
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
$wormPhotos = WormPhoto::where('device_id', $device->id)
|
|
||||||
->whereBetween('uploaded_at', [$startTime, $endTime])
|
->whereBetween('uploaded_at', [$startTime, $endTime])
|
||||||
->latest('uploaded_at')
|
->latest('uploaded_at')
|
||||||
->get();
|
->paginate($request->input('per_page', 20));
|
||||||
|
|
||||||
$data = $wormPhotos->map(fn ($item) => [
|
return WormPhotoResource::collection($wormPhotos);
|
||||||
'id' => $item->id,
|
|
||||||
'url' => $item->url,
|
|
||||||
'time' => $item->created_at->toDateTimeString(),
|
|
||||||
]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->json($data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class WormPhotoResource extends JsonResource
|
||||||
|
{
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'url' => $this->image_url,
|
||||||
|
'time' => $this->created_at->toDateTimeString(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class WormPhoto extends Model
|
class WormPhoto extends Model
|
||||||
{
|
{
|
||||||
|
|
@ -16,4 +18,16 @@ class WormPhoto extends Model
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'device_id', 'url', 'uploaded_at',
|
'device_id', 'url', 'uploaded_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function imageUrl(): Attribute
|
||||||
|
{
|
||||||
|
return Attribute::make(
|
||||||
|
get: function(mixed $value, array $attributes) {
|
||||||
|
if (preg_match('/^https?:\/\//', $attributes['url']) > 0) {
|
||||||
|
return $attributes['url'];
|
||||||
|
}
|
||||||
|
return Storage::disk('public')->url($attributes['url']);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue