diff --git a/app/Console/Commands/RotateTextAd.php b/app/Console/Commands/RotateTextAd.php index 5492837..c90be08 100644 --- a/app/Console/Commands/RotateTextAd.php +++ b/app/Console/Commands/RotateTextAd.php @@ -35,38 +35,39 @@ class RotateTextAd extends Command return; } - $nextTime = Cache::get('text_ad_next_time'); - - // 還沒到下一次播放時間就跳出 - if ($nextTime && now()->lt($nextTime)) { - $this->info("⏳ 尚未到下一次播放時間(下次播放:{$nextTime})"); - return; - } - - $index = Cache::get('text_ad_current_index', 0) % $ads->count(); - $ad = $ads[$index]; - $rooms = Room::where('type', '!=', 'svr') ->where('is_online', 1) - ->get(['internal_ip', 'port']); - // 📨 發送 TCP 廣告 - try { - $prefix = "全部({$ad->color->labels()})-"; - $content = $prefix . $ad->content; - foreach ($rooms as $room) { + ->get(['id', 'name', 'internal_ip', 'port']); + + foreach ($rooms as $room) { + $nextTimeKey = "text_ad_next_time_{$room->id}"; + $indexKey = "text_ad_index_{$room->id}"; + + $nextTime = Cache::get($nextTimeKey); + + // 還沒到時間,就跳過這個房間 + if ($nextTime && now()->lt($nextTime)) { + $this->info("⏳ 房間 {$room->id} 尚未到下一次播放時間(下次播放:{$nextTime})"); + continue; + } + + $index = Cache::get($indexKey, 0) % $ads->count(); + $ad = $ads[$index]; + $roomCode = str_pad($room->name, 4, '0', STR_PAD_LEFT); + $content = "{$roomCode}({$ad->color->labels()})-" . $ad->content; + + try { $client = new TcpSocketClient($room->internal_ip, $room->port); $response = $client->send($content); - $this->info("✅ 已推送廣告 #{$ad->id} 至 {$room->internal_ip}:{$room->port}"); + $this->info("✅ 廣告 #{$ad->id} 已推送到房間 {$room->id}:{$room->internal_ip}:{$room->port}"); + } catch (\Throwable $e) { + $this->error("❌ 房間 {$room->id} 發送失敗:{$e->getMessage()}"); } - } catch (\Throwable $e) { - $this->error("❌ 發送失敗:{$room->internal_ip}:{$room->port} - " . $e->getMessage()); + + // 更新此房間的播放狀態 + Cache::put($nextTimeKey, now()->addMinutes($ad->duration)); + Cache::put($indexKey, ($index + 1) % $ads->count()); } - - // 🕒 更新下次播放時間 - Cache::put('text_ad_next_time', now()->addMinutes($ad->duration)); - - // 📌 更新下次播放 index - Cache::put('text_ad_current_index', ($index + 1) % $ads->count()); } } diff --git a/app/Livewire/Forms/TextAdTestForm.php b/app/Livewire/Forms/TextAdTestForm.php new file mode 100644 index 0000000..d08d72e --- /dev/null +++ b/app/Livewire/Forms/TextAdTestForm.php @@ -0,0 +1,81 @@ +roomOptions = Room::where('type', '!=', 'svr')->get()->map(fn ($room) => [ + 'name' => $room->type->value.$room->name, + 'value' => $room->id, + ])->toArray(); + } + + public function openModal($id = null) + { + $textAd=TextAd::findOrFail($id); + $this->prefix = "({$textAd->color->labels()})-測試:"; + $this->content = $textAd->content; + $this->showModal = true; + } + public function closeModal() + { + $this->textAd=null; + $this->resetFields(); + $this->showModal = false; + } + + public function send() + { + + $room = Room::find($this->roomId); + $roomCode = str_pad($room->name, 4, '0', STR_PAD_LEFT); + try { + + $client = new TcpSocketClient($room->internal_ip, $room->port); + $client->send($roomCode.$this->prefix.$this->content); + + $this->notification()->send([ + 'icon' => 'success', + 'title' => '成功', + 'description' => "✅ 已送出至房間 {$room->name}", + ]); + } catch (\Throwable $e) { + $this->notification()->send([ + 'icon' => 'error', + 'title' => '失敗', + 'description' => "❌ 發送失敗:{$e->getMessage()}", + ]); + } + $this->resetFields(); + $this->showModal = false; + $this->dispatch('pg:eventRefresh-text-ads-table'); + } + public function resetFields() + { + $this->content=''; + $this->roomId=null; + } + + public function render() + { + return view('livewire.forms.text-ad-test-form'); + } +} \ No newline at end of file diff --git a/app/Livewire/Forms/Tables/TextAdsTable.php b/app/Livewire/Tables/TextAdsTable.php similarity index 95% rename from app/Livewire/Forms/Tables/TextAdsTable.php rename to app/Livewire/Tables/TextAdsTable.php index d8ab476..db23dfa 100644 --- a/app/Livewire/Forms/Tables/TextAdsTable.php +++ b/app/Livewire/Tables/TextAdsTable.php @@ -1,6 +1,6 @@ slot('測試') + ->icon('solid-cog') + ->class('inline-flex items-center gap-1 px-3 py-1 rounded bg-amber-200 text-black') + ->dispatchTo('forms.text-ad-test-form', 'openModal', ['id' => $row->id]); if ($this->canEdit) { $actions[]=Button::add('edit') diff --git a/resources/views/livewire/admin/text-ads.blade.php b/resources/views/livewire/admin/text-ads.blade.php index 63617fa..112ecea 100644 --- a/resources/views/livewire/admin/text-ads.blade.php +++ b/resources/views/livewire/admin/text-ads.blade.php @@ -1,6 +1,7 @@ - + + \ No newline at end of file diff --git a/resources/views/livewire/forms/text-ad-test-form.blade.php b/resources/views/livewire/forms/text-ad-test-form.blade.php new file mode 100644 index 0000000..bd83aa5 --- /dev/null +++ b/resources/views/livewire/forms/text-ad-test-form.blade.php @@ -0,0 +1,24 @@ +
+ + +
+
將發送內容:
+
{{ $content }}
+
+ + + +
+ + +
+
+
+
\ No newline at end of file