支持现场服务器地址配置
This commit is contained in:
72
lib/core/env/env_config.dart
vendored
72
lib/core/env/env_config.dart
vendored
@@ -1,3 +1,9 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'server_configuration.dart';
|
||||
|
||||
class EnvConfig {
|
||||
static const String environment = String.fromEnvironment(
|
||||
'ENV',
|
||||
@@ -14,18 +20,62 @@ class EnvConfig {
|
||||
|
||||
static const bool isProduction = environment == 'prod';
|
||||
|
||||
// ─── 服务器 IP(测试/生产共用同一台机器) ───
|
||||
static const String serverHost = '1.95.137.212';
|
||||
static const _serverConfigurationKey = 'server_configuration_$environment';
|
||||
static ServerConfiguration _configuration = ServerConfiguration(
|
||||
production: isProduction,
|
||||
);
|
||||
|
||||
// ─── TCP 控制长连接 ───
|
||||
static String get tcpIp => serverHost;
|
||||
static int get tcpPort => isProduction ? 9001 : 59004;
|
||||
static ServerConfiguration get serverConfiguration => _configuration;
|
||||
|
||||
// ─── HTTP 接口 ───
|
||||
static int get httpPort => isProduction ? 8081 : 59003;
|
||||
static String get baseUrl => 'http://$serverHost:$httpPort';
|
||||
static void loadServerConfiguration(SharedPreferences preferences) {
|
||||
try {
|
||||
final saved = preferences.getString(_serverConfigurationKey);
|
||||
if (saved != null) {
|
||||
_configuration = ServerConfiguration.fromJson(
|
||||
jsonDecode(saved) as Map<String, dynamic>,
|
||||
production: isProduction,
|
||||
);
|
||||
return;
|
||||
}
|
||||
} on FormatException {
|
||||
// Malformed preferences fall back to the build's endpoints.
|
||||
} on TypeError {
|
||||
// Older or invalid preference values may have a different type.
|
||||
}
|
||||
_configuration = ServerConfiguration(production: isProduction);
|
||||
}
|
||||
|
||||
// ─── MQTT(一方后端:任务消息 / 割草机实时状态,prod=1883 / test=59007)───
|
||||
// 注:第三方无人机 OSD(droneOsd,WebSocket 8083)不纳入环境管理,保持硬编码
|
||||
static int get mqttPort => isProduction ? 1883 : 59007;
|
||||
static Future<void> saveServerConfiguration(
|
||||
SharedPreferences preferences,
|
||||
ServerConfiguration configuration,
|
||||
) async {
|
||||
final saved = await preferences.setString(
|
||||
_serverConfigurationKey,
|
||||
jsonEncode(configuration.toJson()),
|
||||
);
|
||||
if (!saved) throw StateError('Unable to save server configuration');
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
static Uri address(ServerService service) => _configuration.address(service);
|
||||
|
||||
static String baseAddress(ServerService service) =>
|
||||
address(service).toString().replaceFirst(RegExp(r'/+$'), '');
|
||||
|
||||
static String get tcpIp => address(ServerService.tcp).host;
|
||||
static int get tcpPort => address(ServerService.tcp).port;
|
||||
static String get baseUrl => baseAddress(ServerService.http);
|
||||
|
||||
static String get webRtcStreamAuthority =>
|
||||
Uri(scheme: 'webrtc', host: address(ServerService.webRtc).host).authority;
|
||||
|
||||
static Uri webRtcPlayAddress(Uri stream) {
|
||||
final configured = address(ServerService.webRtc);
|
||||
return stream.host == configured.host
|
||||
? configured
|
||||
: ServerService.webRtc.defaultAddress(
|
||||
production: isProduction,
|
||||
commonHost: stream.host,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user