| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- import 'dart:async';
- import 'dart:core';
- import 'dart:typed_data';
- import 'package:app_business/app_config.dart';
- import 'package:app_business/generated/base.pb.dart' as pb;
- import 'package:app_business/generated/google/protobuf/duration.pb.dart' as pb;
- import 'package:app_business/generated/google/protobuf/timestamp.pb.dart' as pb;
- import 'package:app_business/generated/track_offical.pbgrpc.dart' as pb;
- import 'package:app_business/service/app.dart';
- import 'package:grpc/grpc.dart';
- import 'package:track_common/model.dart';
- import 'package:track_common/track_common.dart';
- import 'abase.dart';
- export 'package:app_business/generated/base.pb.dart'
- show DefaultRequest, IdRequest, IdArrRequest;
- export 'package:app_business/generated/google/protobuf/timestamp.pb.dart';
- export 'package:app_business/generated/track_offical.pbgrpc.dart'
- show ToMatchRegusterAddRequest, ToMatchRegusterListRequest;
- export 'package:fixnum/fixnum.dart' show Int64;
- typedef SmsType = pb.SmsType;
- extension _ImageExt on pb.NetImage {
- NetImage toModel() {
- return NetImage(url, md5);
- }
- }
- extension DateTimeExt on DateTime? {
- pb.Timestamp toPb() {
- if (this != null) {
- return pb.Timestamp.fromDateTime(this!.toUtc());
- } else {
- return pb.Timestamp();
- }
- }
- }
- extension TSExt on pb.Timestamp {
- DateTime toModel() {
- return toDateTime(toLocal: true);
- }
- }
- extension DurationExt on pb.Duration {
- Duration toModel() {
- final microseconds = hasNanos() ? nanos / 1000 : 0;
- return Duration(
- seconds: seconds.toInt(), microseconds: microseconds.toInt());
- }
- }
- extension PositionExt on pb.Position {
- Position toModel() {
- return Position(latitude: latitude, longitude: longitude)
- ..altitude = altitude;
- }
- }
- extension PBToControlPointExt on pb.ToControlPoint {
- ControlPoint toModel() {
- final one = ControlPoint()
- ..areaId = sn
- ..intId = id
- ..isSuccess = isCheckSuccess
- ..sn = orderNo.toString()
- ..checkAfterPrev = checkAfterLast.toModel()
- ..checkAfterStart = checkAfterStart.toModel()
- ..checkDistanceAfterPrev = disAfterLast.meter
- ..distanceStraightToPrev = disStraightAfterLast.meter
- ..paceAfterPrev = Pace.perKm(paceAfterLast.seconds)
- ..paceAfterStart = Pace.perKm(paceAfterStart.seconds)
- ..position = ciPosition.toModel();
- if (cType == pb.CType.BeginType) {
- one.isStart = true;
- }
- if (cType == pb.CType.EndType) {
- one.isFinish = true;
- }
- return one;
- }
- }
- class ApiService extends IService {
- ClientChannel? channel;
- @override
- Future<void> init() async {
- channel = _newChannel();
- }
- ClientChannel _newChannel() {
- return ClientChannel(
- AppConfig.apiHost,
- port: AppConfig.apiPort,
- options: const ChannelOptions(credentials: ChannelCredentials.secure()
- // ChannelCredentials.insecure()
- ),
- );
- }
- pb.ApiToClient get stub {
- return _newStub(timeout: 10.seconds);
- }
- pb.ApiToClient _newStub({Duration? timeout, ClientChannel? channel}) {
- if (this.channel == null) {
- throw Exception('$runtimeType 未初始化');
- }
- final metadata = <String, String>{};
- metadata['version'] = _appVersion;
- if (token != null) {
- metadata['token'] = token!;
- }
- // debug("token: $token");
- return pb.ApiToClient(channel ?? this.channel!,
- options: CallOptions(
- metadata: metadata,
- timeout: timeout,
- ));
- }
- String get _appVersion => Get.find<AppService>().appVersion;
- set token(String? v) {
- Get.find<AppService>().token.val = v ?? '';
- }
- String? get token {
- final app = Get.find<AppService>();
- final token = app.token.val;
- if (token.isEmpty) {
- return null;
- }
- return token;
- }
- // 获取短信验证码
- Future<void> authSendCodeToPhone(String phone, SmsType smsType) async {
- info('authSendCodeToPhone [$phone]');
- await stub.toSendCodeToPhoneV2(pb.ToSendCodeToPhoneRequestV2()
- ..phone = phone
- ..smsType = smsType);
- }
- // 场控端_登录
- Future<void> signIn(String userCode, String password, String ip) async {
- final r = await stub.toSignInV2(pb.ToSignInRequestV2()
- ..userCode = userCode
- ..password = password
- ..ip = ip);
- token = r.token;
- debug('sign in success: $token');
- }
- // 场控端_登出
- void signOut() {
- stub.toSignOutV2(pb.DefaultRequest());
- token = null;
- }
- Future<Duration> getSmsSendLeftTime(String phone) async {
- final r = await stub
- .toGetSmsSendLeftTimeV2(pb.GetSmsSendLeftTimeRequest()..phone = phone);
- info('getSmsSendLeftTime: $phone - ${r.second}s');
- return r.second.seconds;
- }
- Future<List<MapInfo>> getMapList(int limit, int offset) async {
- final r = await stub.toMapListV2(pb.MapListRequestV2()
- ..limit = limit
- ..offset = offset);
- return r.list
- .map((e) => MapInfo(e.mapId, e.name, e.distance.meter, e.description,
- e.mapScaleNumber, e.image.toModel()))
- .toList();
- }
- Future<BinReader> getBinReaderByMd5(Uint8List md5) async {
- final stream =
- stub.toGetBinaryByMd5(pb.ToGetBinaryByMd5Request()..md5 = md5);
- final controller = StreamController<List<int>>();
- controller.onCancel = () {
- stream.cancel();
- };
- Future<void> rcv() async {
- try {
- await for (final one in stream) {
- controller.add(one.data);
- }
- } finally {
- controller.close();
- stream.cancel();
- }
- }
- rcv();
- stream.headers.then((value) => debug(value));
- final headers = await stream.headers;
- final lenStr = headers['all-length']!;
- final length = int.parse(lenStr);
- final nonce = headers['nonce']!;
- final ext = headers['ext']!;
- return BinReader(
- data: controller.stream, length: length, ext: ext, nonce: nonce);
- }
- Future<void> eventEdit(int id, EventRegisterInfo event) async {
- await stub.toMatchRegusterEdit(pb.ToMatchRegusterEditRequest()
- ..id = id
- ..regName = event.name
- ..startAt = event.startAt.toPb()
- ..stopAt = event.stopAt.toPb()
- ..isQueryPwd = event.password != null
- ..queryPasswd = event.password ?? '');
- }
- Future<void> eventUserAllocRoute(int checkId, int routeId) async {
- await stub.toCourseChackMatch(pb.ToCourseChackMatchRequest()
- ..id = checkId
- ..cId = routeId);
- }
- Future<void> eventUserAllocRouteAll(Iterable<int> checkIdList) async {
- await stub
- .allCourseChackMatch(pb.IdArrRequest()..idArr.addAll(checkIdList));
- }
- Future<void> eventUserStartAll(Iterable<int> checkIdList) async {
- await stub
- .allMatchChackInsStart(pb.IdArrRequest()..idArr.addAll(checkIdList));
- }
- Future<void> eventUserRestartAll(Iterable<int> checkIdList) async {
- await stub.allMatchChackInsForceResume(
- pb.IdArrRequest()..idArr.addAll(checkIdList));
- }
- Future<void> eventUserDelAll(Iterable<int> checkIdList) async {
- await stub
- .allMatchChackInsDel(pb.IdArrRequest()..idArr.addAll(checkIdList));
- }
- Future<void> eventSettingsRulesSave(Iterable<Rule> rules) async {
- await stub.toActivityRulesSave(pb.ToActivityRulesSaveRequest()
- ..list.addAll(rules.map((e) {
- final one = pb.ToActivityRulesSaveInfo()..arrId = e.id;
- final vt = e.value;
- if (vt is RuleValueBool) {
- one.arValue = vt.value ? 'true' : 'false';
- }
- if (vt is RuleValueArrStr) {
- one.arValue = vt.value;
- }
- return one;
- })));
- }
- Stream<CooperationInfo> cooperationInfo() async* {
- while (true) {
- await 1.seconds.delay();
- try {
- final r = await stub.toCooperationQuery(pb.DefaultRequest());
- yield CooperationInfo()
- ..name = r.name
- ..leftCount = r.leftMatchNum;
- } catch (e) {
- warn('获取可用数错误', e);
- }
- }
- }
- }
- class CooperationInfo {
- var name = '';
- // 可用数
- var leftCount = 0;
- }
- class EventRegisterInfo {
- var id = 0;
- var name = '';
- var startAt = DateTime.now();
- var stopAt = DateTime.now();
- String? password;
- }
- class Rule {
- var id = 0;
- var idx = 0;
- var name = '';
- RuleValue value = RuleValueBool();
- }
- class RuleValue {}
- class RuleValueBool extends RuleValue {
- var value = false;
- }
- class RuleValueArrStr extends RuleValue {
- var value = '';
- var options = <RuleValueArrStrOption>[];
- }
- class RuleValueArrStrOption {
- var show = '';
- var value = '';
- }
|