cpnav/lib/pages/pass_track/model/recievemodel.dart
2024-09-06 09:11:06 +08:00

146 lines
3.7 KiB
Dart

import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
class RecieveModel {
// int UTC;
// double LAT;
// double LNG;
// double ASL;
// double speed;
// int status;
// num HDG;
// num TP;
int type;
int TID;
// num count;
// String data;
// num? mile;
// int layer;
// DecodeDataObj? obj;
int x;
int y;
RecieveModel(
{
// required this.ASL, //
// required this.HDG, //
// required this.LAT, //
// required this.LNG, //
required this.TID,
// required this.TP,
// required this.UTC,
// required this.count,
// required this.data,
// required this.layer,
// required this.mile,
// required this.speed,
// required this.status,
required this.type,
required this.x,
required this.y,
// this.obj
});
factory RecieveModel.fromJson(Map<dynamic, dynamic> json) {
return RecieveModel(
// ASL: json['ASL']!.toDouble(),
// UTC: json['UTC'],
type: int.tryParse(json['type'])??1,
// TP: json['TP'],
// count: json['count'],
// data: json['data'],
// HDG: json['HDG'],
// LAT: json['LAT'],
// layer: json['layer'],
// LNG: json['LNG'],
// mile: json['mile'],
// speed: json['speed'],
// status: json['status'],
TID: json['TID'],
// obj: json['obj'],
x: json['x'] ?? 0,
y: json['y'] ?? 0);
}
}
class DecodeDataObj {
int x;
int y;
Uint16List data;
int dx;
int dy;
int w;
int h;
double rad;
DecodeDataObj(
{required this.data,
required this.dx,
required this.dy,
required this.h,
required this.w,
required this.x,
required this.y,
required this.rad});
factory DecodeDataObj.fromBase64(String base64) {
// base64 解码
Uint8List bytes = base64Decode(base64);
ByteData byteData = bytes.buffer.asByteData();
// 解码后解析为需要的格式返回出去
int x = byteData.getInt32(0, Endian.little); //readInt32LE(str, 0);
int y = byteData.getInt32(4, Endian.little); //readInt32LE(str, 4);
int v = byteData.getUint16(8, Endian.little); //readUInt16LE(str, 8);
int w = v & 0x3ff;
double rad = (v >>> 10) * 2 * pi / 64;
Uint16List data;
int length = bytes.length;
int h = 0;
h -= 1;
int dx;
int dy;
int i = 0;
if (w < 16) {
h = (length - 10 - 1);
data = Uint16List(h * 2);
for (i = 0; i < h; i++) {
int x = bytes[10 + i] & 0x0f;
int w = bytes[10 + i] >> 4;
data[i * 2] = x;
data[i * 2 + 1] = w;
}
dx = bytes[10 + i] & 0x0f;
dy = bytes[10 + i] >> 4;
} else if (w < 256) {
h = ((length - 10) / 2 - 1).toInt();
data = Uint16List(h * 2);
for (i = 0; i < h; i++) {
int x = bytes[10 + i * 2];
int w = bytes[10 + i * 2 + 1];
data[i * 2] = x;
data[i * 2 + 1] = w;
}
dx = bytes[10 + i * 2];
dy = bytes[10 + i * 2 + 1];
} else {
h = ((length - 10) / 3 - 1).toInt();
data = Uint16List(h * 2);
for (i = 0; i < h; i++) {
if (10 + i * 3 + 3 < h) {
int a = bytes[10 + i * 3];
int b = bytes[10 + i * 3 + 1];
int c = bytes[10 + i * 3 + 2];
int x = a | (b & 0x0f) << 8;
int w = (b & 0xf0) >> 4 | (c & 0x0f) << 4;
data[i * 2] = x;
data[i * 2 + 1] = w;
}
}
int a = bytes[10 + i * 3];
int b = bytes[10 + i * 3 + 1];
int c = bytes[10 + i * 3 + 2];
dx = a | (b & 0x0f) << 8;
dy = (b & 0xf0) >> 4 | (c & 0x0f) << 4;
}
return DecodeDataObj(
x: x, y: y, w: w, h: h, dx: dx, dy: dy, data: data, rad: rad);
}
}