單機版 v.0.1.4 20250627

加入測試功能
This commit is contained in:
allen.yan 2025-06-27 17:13:43 +08:00
parent 40aa6c3901
commit ce58b1d9aa
5 changed files with 140 additions and 29 deletions

View File

@ -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;
->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->internal_ip}:{$room->port} - " . $e->getMessage());
$this->error("房間 {$room->id} 發送失敗:{$e->getMessage()}");
}
// 🕒 更新下次播放時間
Cache::put('text_ad_next_time', now()->addMinutes($ad->duration));
// 📌 更新下次播放 index
Cache::put('text_ad_current_index', ($index + 1) % $ads->count());
// 更新此房間的播放狀態
Cache::put($nextTimeKey, now()->addMinutes($ad->duration));
Cache::put($indexKey, ($index + 1) % $ads->count());
}
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Livewire\Forms;
use Livewire\Component;
use WireUi\Traits\WireUiActions;
use App\Models\Room;
use App\Models\TextAd;
use App\Services\TcpSocketClient;
class TextAdTestForm extends Component
{
use WireUiActions;
protected $listeners = ['openModal','closeModal'];
public bool $showModal = false;
public ?string $prefix ="";
public ?string $content = "";
public ?int $roomId = null;
public array $roomOptions =[];
public function mount()
{
$this->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');
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Livewire\Forms\Tables;
namespace App\Livewire\Tables;
use App\Models\TextAd;
use App\Enums\TextAdColors;
@ -189,7 +189,11 @@ final class TextAdsTable extends PowerGridComponent
public function actions(TextAd $row): array
{
$actions = [];
$actions[] = Button::add('text-ad-test')
->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')

View File

@ -1,6 +1,7 @@
<x-layouts.admin>
<x-wireui:notifications/>
<livewire:forms.tables.text-ads-table />
<livewire:tables.text-ads-table />
<livewire:forms.text-ads-form />
<livewire:forms.text-ad-test-form />
</x-layouts.admin>

View File

@ -0,0 +1,24 @@
<div class="p-4 space-y-4">
<x-wireui:modal-card title="測試" blur wire:model.defer="showModal">
<x-wireui:select
label="選擇房間"
wire:model.defer="roomId"
placeholder="請選擇房間"
:options="$roomOptions"
option-label="name"
option-value="value"
/>
<div class="text-gray-700 bg-gray-100 p-3 rounded shadow">
<div class="font-semibold mb-1">將發送內容:</div>
<div>{{ $content }}</div>
</div>
<x-slot name="footer">
<div class="flex justify-between w-full">
<x-wireui:button flat label="{{__('text_ads.cancel')}}" wire:click="closeModal" />
<x-wireui:button primary wire:click="send" primary label="立即發送" spinner />
</div>
</x-slot>
</x-wireui:modal-card>
</div>