pile_nav_new/lib/pages/real/real_controller.dart

280 lines
9.2 KiB
Dart
Raw Normal View History

2024-11-18 18:02:15 +08:00
import 'dart:math';
2024-11-20 13:12:51 +08:00
import 'dart:developer' as developer;
2024-08-27 18:08:05 +08:00
import 'dart:typed_data';
import 'dart:ui';
2024-11-18 14:48:54 +08:00
import 'package:cpnav/pages/setting/child_pages/XyChange/xy_change.dart';
2024-08-27 18:08:05 +08:00
import 'package:get/get.dart';
2024-11-18 14:48:54 +08:00
import 'package:scence_map/controllers/controller.dart';
2024-11-20 13:12:51 +08:00
import 'package:scence_map/record_entity.dart';
2024-11-06 17:23:29 +08:00
// import 'package:flutter_webrtc/flutter_webrtc.dart';
2024-08-27 18:08:05 +08:00
2024-11-18 14:48:54 +08:00
import '../../models/pilePoint/coord_trans.dart';
import '../setting/child_pages/XyChange/coor_trans.dart';
import '../setting/setting_controller.dart';
2024-08-27 18:08:05 +08:00
import 'process.dart';
2024-11-18 14:48:54 +08:00
// pile_cm
// class RealController extends GetxController {
// var sightOffset1 = const Offset(3, 3).obs;
// var sightOffsetInit1 = const Offset(0, 0).obs;
// var sightInit1 = const Offset(0, 0).obs;
// var initDx = 0.0.obs;
// reset() {
// sightOffset1.value = const Offset(3, 3);
// sightOffsetInit1.value = const Offset(0, 0);
// sightInit1.value = const Offset(0, 0);
// initDx.value = 0.0;
// update();
// }
// // RTCPeerConnection? peerConnection;
// // RTCDataChannel? dataChannel;
// final speed = 0.0.obs;
// final depth = 0.0.obs;
// final tiltX = 0.0.obs;
// final tiltY = 0.0.obs;
// final centerX = 0.0.obs;
// final centerY = 0.0.obs;
// final current1 = 0.0.obs;
// final current2 = 0.0.obs;
// final totalFlow2 = 0.0.obs;
// final totalFlow1 = 0.0.obs;
// final flow10cm1 = 0.0.obs;
// final flow10cm2 = 0.0.obs;
// final subtotalFlow2 = 0.0.obs;
// final subtotalFlow1 = 0.0.obs;
// final recvTime = ''.obs;
// final alt = 0.0.obs;
// final lng = 0.0.obs;
// final lat = 0.0.obs;
// final pileId = ''.obs;
// final time = "0".obs;
// final startIndex = 0.obs;
// // 折线数组
// final processList = <ProcessEntity>[].obs;
// var isDataVisible = false.obs;
// void updateProcessList(List<ProcessEntity> newList) {
// processList.assignAll(newList); // 使用 assignAll 方法更新 RxList
// }
// DateTime? sliderTime;
// updateSlider(double newValue, [isHand = true]) {
// if (isHand) {
// sliderTime = DateTime.now();
// }
// startIndex.value = newValue.toInt();
// update();
// }
// @override
// void onInit() {
// super.onInit();
// // 定时器每秒更新时间
// // Timer.periodic(const Duration(milliseconds: 1000), (_) {
// // time.value = DateTime.now();
// // });
// initializePeerConnection();
// }
// void initializePeerConnection() async {
// // final configuration = <String, dynamic>{
// // 'iceServers': [
// // {'url': 'stun:stun.l.google.com:19302'},
// // ],
// // };
// // peerConnection = await createPeerConnection(configuration);
// // initializeDataChannel();
// }
// // void initializeDataChannel() async {
// // // 创建数据通道
// // dataChannel = await peerConnection!
// // .createDataChannel('dataChannel', RTCDataChannelInit()..id = 1);
// // // 监听数据通道的消息事件
// // dataChannel!.onMessage = (RTCDataChannelMessage message) {
// // // 处理接收到的消息
// // if (message.isBinary) {
// // onBleData(message.binary);
// // }
// // };
// // }
// onBleData(Uint8List data) {
// ByteData byteData = data.buffer.asByteData();
// int tag = byteData.getUint8(0);
// // int length = byteData.getUint8(1);
// if (tag == 161) {
// speed.value = byteData.getInt16(2, Endian.little) * 0.1;
// int timeInt = byteData.getUint16(4, Endian.little);
// // log(timeInt);
// time.value = convertSecondsToMinSec(timeInt);
// depth.value = byteData.getInt16(6, Endian.little) * 0.001;
// pileId.value = byteData.getUint16(8, Endian.little).toString();
// subtotalFlow1.value = byteData.getInt16(10, Endian.little) * 0.01;
// flow10cm1.value = byteData.getUint16(12, Endian.little) * 0.01;
// totalFlow1.value = byteData.getInt32(14, Endian.little) * 0.01;
// subtotalFlow2.value = byteData.getInt16(18, Endian.little) * 0.01;
// flow10cm2.value = byteData.getUint16(20, Endian.little) * 0.01;
// totalFlow2.value = byteData.getInt32(22, Endian.little) * 0.01;
// current1.value = byteData.getUint16(26, Endian.little) * 0.01;
// current2.value = byteData.getUint16(28, Endian.little) * 0.01;
// // current3.value = byteData.getUint16(30, Endian.little) * 0.01;
// int tag1 = byteData.getUint8(34);
// // int length = byteData.getUint8(35);
// if (tag1 == 162) {
// // var gpsStatus = byteData.getUint8(36);
// // var gpsView = byteData.getUint8(37);
// // var gpsUse = byteData.getUint8(38);
// // var reg = byteData.getUint8(39);
// // var utc = byteData.getUint32(40, Endian.little);
// // 中心坐标
// centerX.value = byteData.getFloat64(44, Endian.little);
// centerY.value = byteData.getFloat64(52, Endian.little);
// // log("中心---$centerX,$centerY");
// // tiltX.value = byteData.getFloat64(44, Endian.little);
// // tiltY.value = byteData.getFloat64(52, Endian.little);
// // var direction = byteData.getFloat64(60, Endian.little);
// // var pitch = byteData.getFloat64(68, Endian.little);
// // log("$gpsUse,$gpsView,$utc,$direction,$pitch");
// }
// }
// }
// String convertSecondsToMinSec(int totalSeconds) {
// int minutes = totalSeconds ~/ 60;
// int seconds = totalSeconds % 60;
// String formattedTime = '$minutes:${seconds.toString().padLeft(2, '0')}';
// return formattedTime;
// }
// @override
// void dispose() {
// // dataChannel?.close(); // 关闭数据通道
// // peerConnection?.close(); // 关闭对等连接
// super.dispose();
// }
// }
final ScenceMapController mapController = Get.find<ScenceMapController>();
SettingController settingController = Get.find<SettingController>();
XYChangeController xyChangeController = Get.put(XYChangeController());
2024-08-27 18:08:05 +08:00
class RealController extends GetxController {
var sightOffset1 = const Offset(3, 3).obs;
var sightOffsetInit1 = const Offset(0, 0).obs;
var sightInit1 = const Offset(0, 0).obs;
var initDx = 0.0.obs;
2024-11-18 14:48:54 +08:00
var processdata = ProcessEntity(
tid: 0,
speed: 0,
alt: 0,
lng: 0,
lat: 0,
tp_id: 0,
utc: DateTime.now().millisecond,
impact_force: []).obs;
var dataCount = 0.obs;
CoordTrans? coordTrans;
2024-11-18 18:02:15 +08:00
var status = 0.obs;
var sa = 0.obs;
var sv = 0.obs;
2024-08-27 18:08:05 +08:00
reset() {
sightOffset1.value = const Offset(3, 3);
sightOffsetInit1.value = const Offset(0, 0);
sightInit1.value = const Offset(0, 0);
initDx.value = 0.0;
update();
}
var isDataVisible = false.obs;
onBleData(Uint8List data) {
2024-11-18 14:48:54 +08:00
String str = String.fromCharCodes(data);
2024-11-18 18:02:15 +08:00
String first = str.split("#POSA")[1];
List<String> list = first.split(",");
double baseLine = double.parse(list[7]);
double heading = double.parse(list[8]);
double pitch = double.parse(list[9]);
status.value = int.parse(list[1]);
sa.value = int.parse(list[10]);
List<String> svStr = list[11].trim().split(" ");
sv.value = int.parse(svStr[0]);
ProcessEntity process = ProcessEntity.fromList(list);
processdata.update((value) {
// value.speed = process.speed;
value?.lat = process.lat;
value?.lng = process.lng;
value?.alt = process.height;
value?.utc = process.utc;
});
2024-11-18 14:48:54 +08:00
dataCount.value++;
CoorTransModel trans = xyChangeController.coorTrans.value;
coordTrans ??= CoordTrans(TransOptions(
L0: trans.L0,
// dstEllipsoid: trans.dstEllipsoid,
// srcEllipsoid: trans.srcEllipsoid,
dx: trans.dx,
dy: trans.dy,
type: trans.type));
var xyh = coordTrans!
.d2p(CoordBLH(B: process.lat, L: process.lng, H: process.alt));
Offset offset = Offset(xyh.X, xyh.Y);
if ((mapController.centerXY - offset).distance > 40) {
mapController.centerXY = offset;
}
2024-11-20 13:12:51 +08:00
RecordEntity record = RecordEntity(
tid: 0,
tpId: -1,
id: 0,
startTime: DateTime.now(),
endTime: DateTime.now(),
lat: process.lat,
lng: process.lng,
name: "实时",
times: 1,
x: xyh.X,
y: xyh.Y,
strongDrop: 0,
);
if (mapController.recordList.isEmpty) {
mapController.recordList.add(record);
} else {
int index = mapController.recordList
.lastIndexWhere((element) => element.tpId == -1);
if (index == -1) {
mapController.recordList.add(record);
} else {
mapController.recordList[index] = record;
}
}
mapController.updateCount++;
2024-11-18 14:48:54 +08:00
var centerDev =
mapController.deviceList[settingController.currentDevice.value?.tid];
if (centerDev != null) {
// log("center${xyh.X},${xyh.Y}");
2024-11-18 18:02:15 +08:00
2024-11-18 14:48:54 +08:00
centerDev.update.value++;
2024-11-18 18:02:15 +08:00
double pitchRad = pitch * pi / 180;
2024-11-20 13:12:51 +08:00
double headingRad = (heading) * pi / 180; //(270-)
centerDev.rotation.value = (heading + 90) * pi / 180;
2024-11-18 18:02:15 +08:00
// 计算水平投影距离
double dXY = baseLine * cos(pitchRad);
// 计算副天线的坐标
double carX = xyh.X + dXY * cos(headingRad);
double carY = xyh.Y + dXY * sin(headingRad);
2024-11-20 13:12:51 +08:00
2024-11-18 18:02:15 +08:00
centerDev.x.value = carX;
centerDev.y.value = carY;
2024-11-20 13:12:51 +08:00
developer.log("carX:$carX,carY:$carY");
2024-08-27 18:08:05 +08:00
}
}
}