263 lines
9.0 KiB
C#
263 lines
9.0 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using PetWashControl.Models;
|
||
using PetWashControl.Services;
|
||
using System.Collections.ObjectModel;
|
||
using System.Text.Json;
|
||
using System.Windows;
|
||
|
||
namespace PetWashControl.ViewModels;
|
||
|
||
public partial class MainViewModel : ObservableObject
|
||
{
|
||
private readonly ApiService _apiService;
|
||
private readonly MqttClientService _mqttService;
|
||
private readonly ConfigurationService _config;
|
||
private readonly LogService _logger;
|
||
|
||
[ObservableProperty]
|
||
private ObservableCollection<Package> _packages = new();
|
||
|
||
[ObservableProperty]
|
||
private Package? _selectedPackage;
|
||
|
||
[ObservableProperty]
|
||
private Order? _currentOrder;
|
||
|
||
[ObservableProperty]
|
||
private string _statusMessage = "请选择套餐";
|
||
|
||
[ObservableProperty]
|
||
private bool _isDoorOpen;
|
||
|
||
[ObservableProperty]
|
||
private bool _isWashing;
|
||
|
||
[ObservableProperty]
|
||
private bool _isConnected;
|
||
|
||
public MainViewModel()
|
||
{
|
||
_config = new ConfigurationService();
|
||
_logger = new LogService();
|
||
_apiService = new ApiService(_config);
|
||
_mqttService = new MqttClientService(_config);
|
||
_mqttService.MessageReceived += OnMqttMessageReceived;
|
||
}
|
||
|
||
public async Task InitializeAsync()
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInfo("开始初始化系统...");
|
||
|
||
await _mqttService.ConnectAsync();
|
||
IsConnected = _mqttService.IsConnected;
|
||
_logger.LogInfo("MQTT连接成功");
|
||
|
||
await LoadPackagesAsync();
|
||
StatusMessage = "系统就绪,请选择套餐";
|
||
_logger.LogInfo("系统初始化完成");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("初始化失败", ex);
|
||
StatusMessage = $"初始化失败: {ex.Message}";
|
||
MessageBox.Show($"系统初始化失败,请检查后端服务是否启动。\n\n错误: {ex.Message}",
|
||
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task LoadPackagesAsync()
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInfo("加载套餐列表...");
|
||
var packages = await _apiService.GetPackagesAsync();
|
||
Packages.Clear();
|
||
foreach (var package in packages)
|
||
{
|
||
Packages.Add(package);
|
||
}
|
||
_logger.LogInfo($"成功加载 {packages.Count} 个套餐");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("加载套餐失败", ex);
|
||
StatusMessage = $"加载套餐失败: {ex.Message}";
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task CreateOrderAsync()
|
||
{
|
||
if (SelectedPackage == null)
|
||
{
|
||
MessageBox.Show("请先选择套餐", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
_logger.LogInfo($"创建订单,套餐ID: {SelectedPackage.Id}");
|
||
CurrentOrder = await _apiService.CreateOrderAsync(SelectedPackage.Id);
|
||
StatusMessage = $"订单创建成功,订单号: {CurrentOrder?.Id},请支付 ¥{SelectedPackage.Price}";
|
||
_logger.LogInfo($"订单创建成功,订单ID: {CurrentOrder?.Id}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("创建订单失败", ex);
|
||
StatusMessage = $"创建订单失败: {ex.Message}";
|
||
MessageBox.Show($"创建订单失败\n\n{ex.Message}", "错误",
|
||
MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task SimulatePaymentAsync()
|
||
{
|
||
if (CurrentOrder == null)
|
||
{
|
||
MessageBox.Show("请先创建订单", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
_logger.LogInfo($"模拟支付,订单ID: {CurrentOrder.Id}");
|
||
CurrentOrder = await _apiService.ConfirmPaymentAsync(CurrentOrder.Id);
|
||
StatusMessage = "支付成功!设备门已打开,请将宠物放入";
|
||
IsDoorOpen = true;
|
||
_logger.LogInfo("支付成功,门已打开");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("支付失败", ex);
|
||
StatusMessage = $"支付失败: {ex.Message}";
|
||
MessageBox.Show($"支付失败\n\n{ex.Message}", "错误",
|
||
MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private async Task CloseDoorAsync()
|
||
{
|
||
if (CurrentOrder == null || !IsDoorOpen)
|
||
{
|
||
MessageBox.Show("门未打开", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
_logger.LogInfo($"关门,订单ID: {CurrentOrder.Id}");
|
||
|
||
// 通过MQTT发送关门状态
|
||
await _mqttService.PublishAsync("device/status", new
|
||
{
|
||
status = "door_closed",
|
||
orderId = CurrentOrder.Id,
|
||
timestamp = DateTime.Now
|
||
});
|
||
|
||
// 更新订单状态
|
||
CurrentOrder = await _apiService.UpdateOrderStatusAsync(CurrentOrder.Id, OrderStatus.DoorClosed);
|
||
|
||
IsDoorOpen = false;
|
||
StatusMessage = "门已关闭,清洗即将开始...";
|
||
_logger.LogInfo("门已关闭,等待清洗开始");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("关门失败", ex);
|
||
StatusMessage = $"关门失败: {ex.Message}";
|
||
MessageBox.Show($"关门失败\n\n{ex.Message}", "错误",
|
||
MessageBoxButton.OK, MessageBoxImage.Error);
|
||
}
|
||
}
|
||
|
||
private void OnMqttMessageReceived(string topic, string payload)
|
||
{
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInfo($"收到MQTT消息 - Topic: {topic}, Payload: {payload}");
|
||
var message = JsonSerializer.Deserialize<JsonElement>(payload);
|
||
|
||
if (topic == "device/command")
|
||
{
|
||
var command = message.GetProperty("command").GetString();
|
||
|
||
if (command == "open_door")
|
||
{
|
||
IsDoorOpen = true;
|
||
StatusMessage = "设备门已打开,请将宠物放入后点击关门";
|
||
_logger.LogInfo("收到开门指令");
|
||
}
|
||
else if (command == "start_wash")
|
||
{
|
||
IsWashing = true;
|
||
var duration = message.GetProperty("durationMinutes").GetInt32();
|
||
StatusMessage = $"清洗已开始,预计 {duration} 分钟完成";
|
||
_logger.LogInfo($"收到开始清洗指令,时长: {duration}分钟");
|
||
|
||
// 模拟清洗完成
|
||
Task.Delay(TimeSpan.FromSeconds(_config.WashSimulationSeconds)).ContinueWith(async _ =>
|
||
{
|
||
await SimulateWashCompleteAsync();
|
||
});
|
||
}
|
||
}
|
||
else if (topic == "device/status")
|
||
{
|
||
var status = message.GetProperty("status").GetString();
|
||
StatusMessage = $"设备状态: {status}";
|
||
_logger.LogInfo($"设备状态更新: {status}");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("处理MQTT消息失败", ex);
|
||
StatusMessage = $"处理消息失败: {ex.Message}";
|
||
}
|
||
});
|
||
}
|
||
|
||
private async Task SimulateWashCompleteAsync()
|
||
{
|
||
if (CurrentOrder == null) return;
|
||
|
||
await Application.Current.Dispatcher.InvokeAsync(async () =>
|
||
{
|
||
try
|
||
{
|
||
_logger.LogInfo($"清洗完成,订单ID: {CurrentOrder.Id}");
|
||
|
||
// 发送清洗完成状态
|
||
await _mqttService.PublishAsync("device/status", new
|
||
{
|
||
status = "completed",
|
||
orderId = CurrentOrder.Id,
|
||
timestamp = DateTime.Now
|
||
});
|
||
|
||
CurrentOrder = await _apiService.UpdateOrderStatusAsync(CurrentOrder.Id, OrderStatus.Completed);
|
||
|
||
IsWashing = false;
|
||
StatusMessage = "清洗完成!请取出宠物";
|
||
|
||
MessageBox.Show("清洗完成!感谢使用", "完成",
|
||
MessageBoxButton.OK, MessageBoxImage.Information);
|
||
_logger.LogInfo("订单流程完成");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError("完成流程失败", ex);
|
||
StatusMessage = $"完成流程失败: {ex.Message}";
|
||
}
|
||
});
|
||
}
|
||
}
|