From a037f82a5fef4060990e2dd8077f36fda3a36850 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Wed, 30 Aug 2023 12:12:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=99=AB=E6=83=85=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E5=92=8C=E8=99=AB=E6=83=85=E7=85=A7=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/BiAng/DeviceLogSyncCommand.php | 31 ++++++++--- .../BiAng/WormStatisticsSyncCommand.php | 2 +- app/Http/Controllers/DeviceController.php | 55 +++++-------------- app/Http/Resources/WormPhotoResource.php | 17 ++++++ app/Models/WormPhoto.php | 14 +++++ 5 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 app/Http/Resources/WormPhotoResource.php diff --git a/app/Console/Commands/BiAng/DeviceLogSyncCommand.php b/app/Console/Commands/BiAng/DeviceLogSyncCommand.php index 64b58bd..b90a0ab 100644 --- a/app/Console/Commands/BiAng/DeviceLogSyncCommand.php +++ b/app/Console/Commands/BiAng/DeviceLogSyncCommand.php @@ -7,10 +7,11 @@ use App\Enums\DeviceType; use App\Iot\BiAng\HttpClient; use App\Models\Device; use App\Models\DeviceLog; -use App\Models\InsecticidalLampReport; use App\Models\WormPhoto; use Illuminate\Console\Command; use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Storage; use RuntimeException; use Throwable; @@ -135,16 +136,28 @@ class DeviceLogSyncCommand extends Command case DeviceType::Worm: case DeviceType::InsectSexLure: + $dir = "worm-photos/{$device->id}"; + + $disk = Storage::disk('public'); + $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'], - // ]); - // } + foreach ($data['imgUrl'] as $item) { + // 下载图片 + $name = md5($item['url']); + if ($ext = pathinfo($item['url'], PATHINFO_EXTENSION)) { + $name .= ".{$ext}"; + } + $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([ 'status' => count($data['imgUrl'] ?? []) > 0 ? DeviceStatus::Online : DeviceStatus::Offline, diff --git a/app/Console/Commands/BiAng/WormStatisticsSyncCommand.php b/app/Console/Commands/BiAng/WormStatisticsSyncCommand.php index 9b6c04b..29c05e2 100644 --- a/app/Console/Commands/BiAng/WormStatisticsSyncCommand.php +++ b/app/Console/Commands/BiAng/WormStatisticsSyncCommand.php @@ -53,7 +53,7 @@ class WormStatisticsSyncCommand extends Command foreach ($devices as $device) { $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); diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index ee54770..146ee9c 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -8,6 +8,7 @@ use App\Enums\OperationType; use App\Helpers\Paginator; use App\Http\Requestes\DeviceRequest; use App\Http\Resources\DeviceResource; +use App\Http\Resources\WormPhotoResource; use App\Models\AgriculturalBase; use App\Models\Device; use App\Models\InsecticidalLampDailyReport; @@ -671,19 +672,22 @@ class DeviceController extends Controller ]); } - $device = Device::findOrFail($id); - - $wormReports = WormReport::where('device_id', $device->id) + $wormReports = WormReport::where('device_id', $id) ->whereBetween('reported_at', [$startTime->toDateString(), $endTime->toDateString()]) ->pluck('worm_num', 'reported_at'); $data = []; - do { - $key = $startTime->toDateString(); - $data[$key] = $wormReports->get($key); - $startTime->addDay(); - } while ($startTime->lte($endTime)); + if ($startTime->lte($endTime)) { + do { + // 日期 + $date = $startTime->toDateString(); + + $data[$date] = $wormReports->get($date); + + $startTime->addDay(); + } while ($startTime->lte($endTime)); + } return $this->json($data); } @@ -714,40 +718,11 @@ class DeviceController extends Controller fn () => $endTime->copy()->startOfDay(), ); - if ($startTime->gt($endTime)) { - 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) + $wormPhotos = WormPhoto::where('device_id', $id) ->whereBetween('uploaded_at', [$startTime, $endTime]) ->latest('uploaded_at') - ->get(); + ->paginate($request->input('per_page', 20)); - $data = $wormPhotos->map(fn ($item) => [ - 'id' => $item->id, - 'url' => $item->url, - 'time' => $item->created_at->toDateTimeString(), - ]); - break; - } - - return $this->json($data); + return WormPhotoResource::collection($wormPhotos); } } diff --git a/app/Http/Resources/WormPhotoResource.php b/app/Http/Resources/WormPhotoResource.php new file mode 100644 index 0000000..773bd26 --- /dev/null +++ b/app/Http/Resources/WormPhotoResource.php @@ -0,0 +1,17 @@ + $this->id, + 'url' => $this->image_url, + 'time' => $this->created_at->toDateTimeString(), + ]; + } +} diff --git a/app/Models/WormPhoto.php b/app/Models/WormPhoto.php index 2499bd8..574d2db 100644 --- a/app/Models/WormPhoto.php +++ b/app/Models/WormPhoto.php @@ -2,8 +2,10 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Storage; class WormPhoto extends Model { @@ -16,4 +18,16 @@ class WormPhoto extends Model protected $fillable = [ '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']); + }, + ); + } }