支持现场服务器地址配置
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
177
lib/core/env/server_configuration.dart
vendored
Normal file
177
lib/core/env/server_configuration.dart
vendored
Normal file
@@ -0,0 +1,177 @@
|
||||
enum ServerService {
|
||||
http,
|
||||
tcp,
|
||||
mqtt,
|
||||
webRtc,
|
||||
droneMqtt,
|
||||
geoServer,
|
||||
pathPlanning,
|
||||
ai,
|
||||
update,
|
||||
stationWeb;
|
||||
|
||||
bool get isCore => this == http || this == tcp || this == mqtt;
|
||||
|
||||
Uri defaultAddress({required bool production, required String commonHost}) {
|
||||
return switch (this) {
|
||||
http => Uri(
|
||||
scheme: 'http',
|
||||
host: commonHost,
|
||||
port: production ? 8081 : 59003,
|
||||
),
|
||||
tcp => Uri(
|
||||
scheme: 'tcp',
|
||||
host: commonHost,
|
||||
port: production ? 9001 : 59004,
|
||||
),
|
||||
mqtt => Uri(
|
||||
scheme: 'mqtt',
|
||||
host: commonHost,
|
||||
port: production ? 1883 : 59007,
|
||||
),
|
||||
webRtc => Uri(
|
||||
scheme: 'http',
|
||||
host: commonHost,
|
||||
port: 1985,
|
||||
path: '/rtc/v1/play/',
|
||||
),
|
||||
droneMqtt => Uri.parse('ws://1.95.137.212:8083/mqtt'),
|
||||
geoServer => Uri.parse('http://1.95.137.212:7900/geoserver'),
|
||||
pathPlanning => Uri.parse('https://servicepathplan.satabot.com/api/path'),
|
||||
ai => Uri.parse('http://1.95.137.212:30009'),
|
||||
update => Uri.parse('http://8.159.134.0:8012/api/update/check'),
|
||||
stationWeb => Uri.parse('https://pvs.satabot.com/'),
|
||||
};
|
||||
}
|
||||
|
||||
/// A port uses the shared host. An explicit address can use another host.
|
||||
Uri resolve(
|
||||
String input, {
|
||||
required bool production,
|
||||
required String commonHost,
|
||||
}) {
|
||||
final fallback = defaultAddress(
|
||||
production: production,
|
||||
commonHost: commonHost,
|
||||
);
|
||||
if (input.isEmpty) return fallback;
|
||||
|
||||
final port = int.tryParse(input);
|
||||
if (port != null) {
|
||||
if (port < 1 || port > 65535) throw const FormatException('Invalid port');
|
||||
return fallback.replace(host: commonHost, port: port);
|
||||
}
|
||||
|
||||
final url = input.contains('://') ? input : '${fallback.scheme}://$input';
|
||||
final address = Uri.tryParse(url);
|
||||
final authority = url
|
||||
.substring(url.indexOf('://') + 3)
|
||||
.split(RegExp(r'[/#?]'))
|
||||
.first;
|
||||
if (address == null ||
|
||||
!ServerConfiguration.isHost(address.host) ||
|
||||
address.userInfo.isNotEmpty ||
|
||||
address.hasFragment ||
|
||||
address.hasQuery ||
|
||||
RegExp(r':0+$').hasMatch(authority)) {
|
||||
throw const FormatException('Invalid server address');
|
||||
}
|
||||
|
||||
final schemes = switch (this) {
|
||||
tcp => const ['tcp'],
|
||||
mqtt => const ['mqtt'],
|
||||
droneMqtt => const ['ws', 'wss'],
|
||||
_ => const ['http', 'https'],
|
||||
};
|
||||
if (!schemes.contains(address.scheme) ||
|
||||
((this == tcp || this == mqtt) && address.path.isNotEmpty)) {
|
||||
throw const FormatException('Invalid protocol or path');
|
||||
}
|
||||
|
||||
final effectivePort = address.hasPort || address.port != 0
|
||||
? address.port
|
||||
: switch (address.scheme) {
|
||||
'ws' => 80,
|
||||
'wss' => 443,
|
||||
_ => fallback.port,
|
||||
};
|
||||
if (effectivePort < 1 || effectivePort > 65535) {
|
||||
throw const FormatException('Invalid port');
|
||||
}
|
||||
return address.replace(
|
||||
port: effectivePort,
|
||||
path: address.path.isEmpty || address.path == '/'
|
||||
? fallback.path
|
||||
: address.path,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ServerConfiguration {
|
||||
static const defaultHost = '1.95.137.212';
|
||||
|
||||
final String sharedHost;
|
||||
final Map<ServerService, String> overrides;
|
||||
final Map<ServerService, Uri> _addresses;
|
||||
|
||||
ServerConfiguration._(this.sharedHost, this.overrides, this._addresses);
|
||||
|
||||
factory ServerConfiguration({
|
||||
required bool production,
|
||||
String sharedHost = '',
|
||||
Map<ServerService, String> overrides = const {},
|
||||
}) {
|
||||
final host = sharedHost.trim();
|
||||
if (host.isNotEmpty && !isHost(host)) {
|
||||
throw const FormatException('Invalid shared host');
|
||||
}
|
||||
final inputs = <ServerService, String>{
|
||||
for (final entry in overrides.entries)
|
||||
if (entry.value.trim().isNotEmpty) entry.key: entry.value.trim(),
|
||||
};
|
||||
final effectiveHost = host.isEmpty ? defaultHost : host;
|
||||
return ServerConfiguration._(host, Map.unmodifiable(inputs), {
|
||||
for (final service in ServerService.values)
|
||||
service: service.resolve(
|
||||
inputs[service] ?? '',
|
||||
production: production,
|
||||
commonHost: effectiveHost,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
factory ServerConfiguration.fromJson(
|
||||
Map<String, dynamic> json, {
|
||||
required bool production,
|
||||
}) => ServerConfiguration(
|
||||
production: production,
|
||||
sharedHost: json['host'] as String? ?? '',
|
||||
overrides: {
|
||||
for (final service in ServerService.values)
|
||||
if (json[service.name] case final String input) service: input,
|
||||
},
|
||||
);
|
||||
|
||||
Uri address(ServerService service) => _addresses[service]!;
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
if (sharedHost.isNotEmpty) 'host': sharedHost,
|
||||
for (final entry in overrides.entries) entry.key.name: entry.value,
|
||||
};
|
||||
|
||||
static bool isHost(String input) {
|
||||
if (RegExp(
|
||||
r'^[a-zA-Z0-9](?:[a-zA-Z0-9.-]*[a-zA-Z0-9.])?$',
|
||||
).hasMatch(input)) {
|
||||
return !input.contains('..');
|
||||
}
|
||||
try {
|
||||
final host = input.startsWith('[') && input.endsWith(']')
|
||||
? input.substring(1, input.length - 1)
|
||||
: input;
|
||||
return host.contains(':') && Uri.parseIPv6Address(host).length == 16;
|
||||
} on FormatException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user