2023-12-11 13:44:15 +08:00
|
|
|
|
import 'dart:async';
|
2023-12-21 10:21:25 +08:00
|
|
|
|
import 'package:audioplayers/audioplayers.dart';
|
2023-12-11 13:44:15 +08:00
|
|
|
|
import 'package:fast_gbk/fast_gbk.dart';
|
2024-03-29 14:38:51 +08:00
|
|
|
|
import 'package:flutter/services.dart';
|
2024-03-25 17:33:40 +08:00
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2023-12-11 13:44:15 +08:00
|
|
|
|
import 'package:get/get.dart';
|
2024-08-21 18:31:19 +08:00
|
|
|
|
import 'package:star_lock/login/login/entity/LoginData.dart';
|
2024-03-29 14:38:51 +08:00
|
|
|
|
import 'package:star_lock/main/lockDetail/monitoring/monitoring/lockMonitoring_state.dart';
|
2024-03-22 13:51:30 +08:00
|
|
|
|
import 'package:star_lock/talk/call/callTalk.dart';
|
2024-03-29 13:54:38 +08:00
|
|
|
|
import 'package:star_lock/tools/eventBusEventManage.dart';
|
2023-12-11 13:44:15 +08:00
|
|
|
|
import '../../appRouters.dart';
|
2024-04-26 15:38:59 +08:00
|
|
|
|
import '../../app_settings/app_settings.dart';
|
2023-12-11 13:44:15 +08:00
|
|
|
|
import '../../tools/storage.dart';
|
|
|
|
|
|
import 'udp_manage.dart';
|
|
|
|
|
|
|
2023-12-20 16:52:18 +08:00
|
|
|
|
class UDPTalkClass {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
|
|
|
|
|
|
factory UDPTalkClass() => _share();
|
|
|
|
|
|
|
|
|
|
|
|
UDPTalkClass._init();
|
2023-12-11 13:44:15 +08:00
|
|
|
|
static UDPTalkClass? _manager;
|
2023-12-20 16:52:18 +08:00
|
|
|
|
static UDPTalkClass _share() {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
_manager ??= UDPTalkClass._init();
|
|
|
|
|
|
return _manager!;
|
|
|
|
|
|
}
|
|
|
|
|
|
UDPTalkClass get manager => _share();
|
|
|
|
|
|
|
2023-12-21 13:56:48 +08:00
|
|
|
|
// var status = 0; //0:初始状态 1:等待监视 2: 3:监视中 4:呼叫成功 5:主角通话中 6:被叫通话 8:被叫通话中
|
|
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
2023-12-20 16:52:18 +08:00
|
|
|
|
var remoteEquid; // 手机号
|
2024-03-29 13:54:38 +08:00
|
|
|
|
late Timer talkTimer;
|
2023-12-11 13:44:15 +08:00
|
|
|
|
// 该字段是为了判断是否跳转到接听界面 挂断或者退出接听界面要记得变更状态
|
2024-08-21 18:31:19 +08:00
|
|
|
|
bool isBeCall = false;
|
|
|
|
|
|
bool isEndCall = false; //是否已经调用结束对讲
|
|
|
|
|
|
final AudioPlayer audioPlayer = AudioPlayer();
|
|
|
|
|
|
int playLocalAudioSecond = 0;
|
2024-04-01 16:21:47 +08:00
|
|
|
|
late Timer playLocalAudioTimer;
|
2023-12-11 13:44:15 +08:00
|
|
|
|
|
|
|
|
|
|
beCallW({List<int>? data, String? ip, int? port}) async {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
AppLog.log('beCall status:$status');
|
2023-12-11 13:44:15 +08:00
|
|
|
|
// if (await isCallMe(data)) {
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2023-12-20 16:52:18 +08:00
|
|
|
|
if (status == 0) {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
// 空闲
|
|
|
|
|
|
// 响铃
|
|
|
|
|
|
// [[Pub getApp] ring];
|
|
|
|
|
|
remoteEquid = getEquidFrombb(data, 9);
|
|
|
|
|
|
status = 6;
|
|
|
|
|
|
|
2023-12-18 16:03:05 +08:00
|
|
|
|
// 呼叫应答,呼叫成功
|
2023-12-11 13:44:15 +08:00
|
|
|
|
data![7] = 1;
|
|
|
|
|
|
data[8] = 4;
|
|
|
|
|
|
UDPManage().sendData(data);
|
|
|
|
|
|
|
2023-12-18 16:03:05 +08:00
|
|
|
|
// 门锁发送视频
|
2023-12-11 13:44:15 +08:00
|
|
|
|
data[7] = 1;
|
|
|
|
|
|
data[8] = 5;
|
|
|
|
|
|
UDPManage().sendData(data);
|
|
|
|
|
|
|
|
|
|
|
|
// Get.to(MaterialPageRoute(
|
|
|
|
|
|
// builder: (context) {
|
|
|
|
|
|
// return const LockMonitoringPage();
|
|
|
|
|
|
// },
|
|
|
|
|
|
// fullscreenDialog: true
|
|
|
|
|
|
// ));
|
|
|
|
|
|
|
2023-12-21 13:56:48 +08:00
|
|
|
|
if (isBeCall == false) {
|
|
|
|
|
|
isBeCall = true;
|
2023-12-18 16:03:05 +08:00
|
|
|
|
// 保持连接
|
2024-08-21 18:31:19 +08:00
|
|
|
|
talkTimer = Timer.periodic(1.seconds, (Timer timer) {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
data[7] = 1;
|
|
|
|
|
|
data[8] = 9;
|
|
|
|
|
|
UDPManage().sendData(data);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-08-21 18:31:19 +08:00
|
|
|
|
Get.toNamed(Routers.lockMonitoringPage, arguments: <String, String>{'lockId': '111'});
|
2023-12-11 13:44:15 +08:00
|
|
|
|
}
|
2024-03-25 17:33:40 +08:00
|
|
|
|
|
|
|
|
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
|
|
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
|
|
_showNotification(flutterLocalNotificationsPlugin);
|
2023-12-21 10:21:25 +08:00
|
|
|
|
playLocalAudio();
|
2024-03-23 16:12:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (isEndCall == true) {
|
2024-04-26 15:38:59 +08:00
|
|
|
|
AppLog.log('已经结束对讲1');
|
2024-03-23 16:12:17 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-08-21 18:31:19 +08:00
|
|
|
|
playLocalAudioTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
|
2024-03-23 16:12:17 +08:00
|
|
|
|
playLocalAudioSecond++;
|
2024-04-26 15:38:59 +08:00
|
|
|
|
// AppLog.log('还在工作么这个定时器$playLocalAudioSecond');
|
2024-03-23 16:12:17 +08:00
|
|
|
|
// 检查条件,如果达到60秒且未得到应答,则认为失败
|
|
|
|
|
|
if (playLocalAudioSecond >= 60) {
|
2024-04-06 17:00:36 +08:00
|
|
|
|
timer.cancel(); // 取消定时器
|
2024-04-01 16:21:47 +08:00
|
|
|
|
playLocalAudioSecond = 0;
|
2024-03-29 13:54:38 +08:00
|
|
|
|
// LockMonitoringLogic().initiateUdpHangUpAction(5);
|
|
|
|
|
|
callNoAnswer(4);
|
2024-03-23 16:12:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-12-20 16:52:18 +08:00
|
|
|
|
} else {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
// 忙
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-25 17:33:40 +08:00
|
|
|
|
//发送本地通知
|
|
|
|
|
|
Future<void> _showNotification(
|
|
|
|
|
|
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
|
|
|
|
|
|
const AndroidNotificationDetails androidPlatformChannelSpecifics =
|
2024-03-26 14:03:46 +08:00
|
|
|
|
AndroidNotificationDetails('1', 'flutter_channel',
|
2024-03-25 17:33:40 +08:00
|
|
|
|
importance: Importance.max,
|
|
|
|
|
|
priority: Priority.high,
|
2024-03-26 14:03:46 +08:00
|
|
|
|
playSound: true,
|
2024-03-25 17:33:40 +08:00
|
|
|
|
ticker: 'ticker');
|
|
|
|
|
|
const NotificationDetails platformChannelSpecifics =
|
|
|
|
|
|
NotificationDetails(android: androidPlatformChannelSpecifics);
|
2024-08-21 18:31:19 +08:00
|
|
|
|
String? getLockName = await Storage.getString(saveLockAlias);
|
2024-03-25 17:33:40 +08:00
|
|
|
|
await flutterLocalNotificationsPlugin.show(
|
2024-08-21 18:31:19 +08:00
|
|
|
|
0, '呼叫提醒'.tr, '${'收到来自'.tr}($getLockName)${'锁的呼叫'.tr}。', platformChannelSpecifics,
|
2024-03-25 17:33:40 +08:00
|
|
|
|
payload: 'item x');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-11 13:44:15 +08:00
|
|
|
|
// 判断是否是call的本人
|
|
|
|
|
|
Future<bool> isCallMe(List<int>? data) async {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
final LoginData? loginData = await Storage.getLoginData();
|
|
|
|
|
|
AppLog.log('getEquidFrombb(data, 1000):${getEquidFrombb(data, 12)}');
|
2023-12-20 16:52:18 +08:00
|
|
|
|
if (loginData!.mobile == getEquidFrombb(data, 12)) {
|
2023-12-11 13:44:15 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String getEquidFrombb(List<int>? bb, int pos) {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
String equid = '';
|
2023-12-11 13:44:15 +08:00
|
|
|
|
int equlen = 8;
|
|
|
|
|
|
|
2023-12-21 10:32:10 +08:00
|
|
|
|
if (bb![pos] == 77) {
|
|
|
|
|
|
//M
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 8;
|
2023-12-21 10:32:10 +08:00
|
|
|
|
} else if (bb[pos] == 87) {
|
|
|
|
|
|
//W
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 5;
|
2023-12-21 10:32:10 +08:00
|
|
|
|
} else if (bb[pos] == 72) {
|
|
|
|
|
|
//H
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 12;
|
2023-12-21 10:32:10 +08:00
|
|
|
|
} else if (bb[pos] == 83) {
|
|
|
|
|
|
//S
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 12;
|
2023-12-21 10:32:10 +08:00
|
|
|
|
} else if (bb[pos] == 0x50) {
|
|
|
|
|
|
//P
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 12;
|
2023-12-21 10:32:10 +08:00
|
|
|
|
} else if (bb[pos] == 0x54) {
|
|
|
|
|
|
//T
|
2023-12-11 13:44:15 +08:00
|
|
|
|
equlen = 16;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
equlen = 12;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<int> tempbb = bb.sublist(pos, pos + equlen);
|
|
|
|
|
|
equid = gbk.decode(tempbb);
|
|
|
|
|
|
return equid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-21 10:21:25 +08:00
|
|
|
|
//播放本地音频
|
|
|
|
|
|
void playLocalAudio() async {
|
|
|
|
|
|
audioPlayer.setReleaseMode(ReleaseMode.loop);
|
|
|
|
|
|
await audioPlayer.play(AssetSource('ring1.mp3'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 停止播放本地音频
|
|
|
|
|
|
void stopLocalAudio() async {
|
|
|
|
|
|
audioPlayer.setReleaseMode(ReleaseMode.loop);
|
|
|
|
|
|
await audioPlayer.stop();
|
2024-04-07 15:46:36 +08:00
|
|
|
|
if (playLocalAudioTimer != null) {
|
|
|
|
|
|
playLocalAudioTimer.cancel();
|
|
|
|
|
|
playLocalAudioSecond = 0;
|
|
|
|
|
|
}
|
2023-12-21 10:21:25 +08:00
|
|
|
|
}
|
2024-03-22 13:51:30 +08:00
|
|
|
|
|
|
|
|
|
|
//呼叫有响铃无应答处理
|
2024-03-29 13:54:38 +08:00
|
|
|
|
void callNoAnswer(int clickIndex) {
|
|
|
|
|
|
if (isEndCall == true) {
|
2024-04-26 15:38:59 +08:00
|
|
|
|
// AppLog.log('已经结束对讲2');
|
2024-03-29 13:54:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-04-26 15:38:59 +08:00
|
|
|
|
// AppLog.log('$clickIndex****无应答处理');
|
2024-03-23 16:12:17 +08:00
|
|
|
|
stopLocalAudio();
|
|
|
|
|
|
status = 0;
|
|
|
|
|
|
isBeCall = false;
|
2024-03-29 13:54:38 +08:00
|
|
|
|
// isEndCall = true;
|
|
|
|
|
|
// LockMonitoringState().isClickHangUp.value = false;
|
2024-04-15 14:53:14 +08:00
|
|
|
|
CallTalk().finishAVData();
|
2024-03-29 13:54:38 +08:00
|
|
|
|
eventBus.fire(GetUDPStatusRefreshUI(UDPTalkClass().status));
|
2024-03-29 14:38:51 +08:00
|
|
|
|
Get.back();
|
|
|
|
|
|
|
2024-03-29 13:54:38 +08:00
|
|
|
|
if (talkTimer != null) {
|
|
|
|
|
|
talkTimer.cancel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (playLocalAudioTimer != null) {
|
|
|
|
|
|
playLocalAudioTimer.cancel();
|
|
|
|
|
|
playLocalAudioSecond = 0;
|
|
|
|
|
|
}
|
2024-03-29 14:38:51 +08:00
|
|
|
|
|
|
|
|
|
|
if (LockMonitoringState().oneMinuteTimeTimer != null) {
|
|
|
|
|
|
LockMonitoringState().oneMinuteTimeTimer.cancel();
|
|
|
|
|
|
LockMonitoringState().oneMinuteTime.value = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
LockMonitoringState().listPhotoData.value = Uint8List(0);
|
|
|
|
|
|
if (LockMonitoringState().answerTimer != null) {
|
|
|
|
|
|
LockMonitoringState().answerTimer.cancel();
|
|
|
|
|
|
LockMonitoringState().answerSeconds.value = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (LockMonitoringState().hangUpTimer != null) {
|
|
|
|
|
|
LockMonitoringState().hangUpTimer.cancel();
|
|
|
|
|
|
LockMonitoringState().hangUpSeconds.value = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (LockMonitoringState().openDoorTimer != null) {
|
|
|
|
|
|
LockMonitoringState().openDoorTimer.cancel();
|
|
|
|
|
|
LockMonitoringState().openDoorSeconds.value = 0;
|
|
|
|
|
|
}
|
2024-03-22 13:51:30 +08:00
|
|
|
|
}
|
2023-12-21 10:32:10 +08:00
|
|
|
|
}
|