feat:预定广播

This commit is contained in:
dzc committed 2025-04-21 11:32:09 +08:00
1 parent fa90df8d9d
commit 691fb68d53
13 files changed
+341 -1

No files matched your search

@@ -21,10 +21,14 @@ class SyncCheckTool {
Duration printInterval = const Duration(seconds: 60 * 10);
Duration broadcastInterval = const Duration(seconds: 60 * 10);
Timer? mqTimer;
Timer? printTimer;
Timer? broadcastTimer;
// **************************************************************************
// 定时器配置 end
// **************************************************************************
@@ -33,6 +37,7 @@ class SyncCheckTool {
if (_isRunning) return;
checkMq();
checkPrint();
checkBroadcast();
}
void dispose() {
@@ -94,4 +99,30 @@ class SyncCheckTool {
}
});
}
// 定时检查需不要广播
checkBroadcast() {
String logMsg = "定时检查需不要广播";
final inSeconds = broadcastInterval.inSeconds;
if (broadcastTimer != null && broadcastTimer!.isActive) {
broadcastTimer?.cancel();
broadcastTimer = null;
}
if (broadcastTimer != null) {
broadcastTimer?.cancel();
broadcastTimer = null;
}
broadcastTimer = Timer.periodic(broadcastInterval, (Timer timer) async {
final isNetwork = AppNetworkTool.isAutoNetwork();
if (!isNetwork) return;
logger.i("|$_tag|$logMsg,$inSeconds秒一次,开始执行");
try {
await SyncTakeFoodBroadcastTool.doCheckBroadcast();
} catch (e) {
logger.i("|$_tag|$logMsg,$inSeconds秒一次,执行失败,error:$e");
} finally {
logger.i("|$_tag|$logMsg,$inSeconds秒一次,执行完成");
}
});
}
}
@@ -0,0 +1,77 @@
import 'package:common_utils/common_utils.dart';
import 'package:get/get.dart';
import 'package:pj_supermarket_cloud/common/index.dart';
abstract class SyncTakeFoodBroadcastTool {
static const _tag = "sync订餐广播工具";
static bool _isBroadcast = false;
static Future<void> addTask(MqttTakeFoodBroadcastMsg msg) async {
final appC = Get.find<AppControl>();
final broadcastDao = appC.dataBase.getBroadcastDao;
final saveObj = BroadcastEntity(
content: msg.content,
broadcastMaxCount: msg.broadcastMaxCount,
isBroadcast: true,
productDesc: msg.productDesc,
serviceCode: msg.id,
userId: msg.userId,
username: msg.username,
userMobile: msg.userMobile,
payPrice: msg.payPrice,
createTime: DateTime.now(),
);
await broadcastDao.save(saveObj);
logger.i("|$_tag|添加到打印记录到本地数据库,json:|${saveObj.toJson()}|");
await SyncTakeFoodBroadcastTool.doCheckBroadcast();
}
// 检查广播
static Future<void> doCheckBroadcast() async {
if (_isBroadcast) {
logger.i("|$_tag|检查打印,正在广播中");
return;
}
_isBroadcast = true;
final appC = Get.find<AppControl>();
final broadcastDao = appC.dataBase.getBroadcastDao;
try {
final list = await broadcastDao.getAll();
if (ObjectUtil.isEmptyList(list)) {
_isBroadcast = false;
logger.i("|$_tag|检查广播,没有需要广播的订单信息");
return;
}
// 将列表分组,每组5个元素
final int groupSize = 5;
final int totalGroups = (list.length / groupSize).ceil();
for (int i = 0; i < totalGroups; i++) {
// 计算当前组的起始和结束索引
final int start = i * groupSize;
final int end = (start + groupSize) < list.length
? (start + groupSize)
: list.length;
// 获取当前组的元素
final currentGroup = list.sublist(start, end);
String msg = "请手机尾号,";
for (final v in currentGroup) {
msg += "${v.content},";
}
msg = msg.substring(0, msg.length - 1);
msg += "的用户,请尽快取餐";
// 播报当前组
await TtsManage.speak(msg);
// 如果不是最后一组,等待一段时间再播报下一组
if (i < totalGroups - 1) {
await Future.delayed(const Duration(seconds: 3));
}
}
} catch (e, stack) {
logger.e("|$_tag|检查广播,广播错误,error:|$e|stack:|$stack|");
} finally {
_isBroadcast = false;
}
}
}