rustdesk/flutter/lib/models/peer_model.dart

181 lines
4.2 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'platform_model.dart';
class Peer {
final String id;
String hash;
final String username;
final String hostname;
final String platform;
2022-10-08 15:53:03 +08:00
String alias;
List<dynamic> tags;
bool forceAlwaysRelay = false;
String rdpPort;
String rdpUsername;
bool online = false;
String getId() {
if (alias != '') {
return alias;
}
return id;
}
2022-09-21 21:20:19 +08:00
Peer.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
hash = json['hash'] ?? '',
2022-09-21 21:20:19 +08:00
username = json['username'] ?? '',
hostname = json['hostname'] ?? '',
platform = json['platform'] ?? '',
2022-09-21 21:20:19 +08:00
alias = json['alias'] ?? '',
tags = json['tags'] ?? [],
forceAlwaysRelay = json['forceAlwaysRelay'] == 'true',
rdpPort = json['rdpPort'] ?? '',
rdpUsername = json['rdpUsername'] ?? '';
2022-10-08 15:53:03 +08:00
Map<String, dynamic> toJson() {
return <String, dynamic>{
"id": id,
"hash": hash,
2022-10-08 15:53:03 +08:00
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
"forceAlwaysRelay": forceAlwaysRelay.toString(),
"rdpPort": rdpPort,
"rdpUsername": rdpUsername,
2022-10-08 15:53:03 +08:00
};
}
Map<String, dynamic> toAbUploadJson() {
return <String, dynamic>{
"id": id,
"hash": hash,
"username": username,
"hostname": hostname,
"platform": platform,
"tags": tags,
};
}
Peer({
required this.id,
required this.hash,
required this.username,
required this.hostname,
required this.platform,
2022-09-21 21:20:19 +08:00
required this.alias,
required this.tags,
required this.forceAlwaysRelay,
required this.rdpPort,
required this.rdpUsername,
});
Peer.loading()
: this(
id: '...',
hash: '',
username: '...',
hostname: '...',
platform: '...',
alias: '',
tags: [],
forceAlwaysRelay: false,
rdpPort: '',
rdpUsername: '',
);
}
enum UpdateEvent { online, load }
class Peers extends ChangeNotifier {
final String name;
final String loadEvent;
List<Peer> peers;
UpdateEvent event = UpdateEvent.load;
static const _cbQueryOnlines = 'callback_query_onlines';
Peers({required this.name, required this.peers, required this.loadEvent}) {
platformFFI.registerEventHandler(_cbQueryOnlines, name, (evt) async {
_updateOnlineState(evt);
});
platformFFI.registerEventHandler(loadEvent, name, (evt) async {
_updatePeers(evt);
});
}
@override
void dispose() {
platformFFI.unregisterEventHandler(_cbQueryOnlines, name);
platformFFI.unregisterEventHandler(loadEvent, name);
super.dispose();
}
Peer getByIndex(int index) {
if (index < peers.length) {
return peers[index];
} else {
return Peer.loading();
}
}
int getPeersCount() {
return peers.length;
}
void _updateOnlineState(Map<String, dynamic> evt) {
evt['onlines'].split(',').forEach((online) {
for (var i = 0; i < peers.length; i++) {
if (peers[i].id == online) {
peers[i].online = true;
}
}
});
evt['offlines'].split(',').forEach((offline) {
for (var i = 0; i < peers.length; i++) {
if (peers[i].id == offline) {
peers[i].online = false;
}
}
});
event = UpdateEvent.online;
notifyListeners();
}
void _updatePeers(Map<String, dynamic> evt) {
final onlineStates = _getOnlineStates();
peers = _decodePeers(evt['peers']);
for (var peer in peers) {
final state = onlineStates[peer.id];
peer.online = state != null && state != false;
}
event = UpdateEvent.load;
notifyListeners();
}
Map<String, bool> _getOnlineStates() {
var onlineStates = <String, bool>{};
for (var peer in peers) {
onlineStates[peer.id] = peer.online;
}
return onlineStates;
}
List<Peer> _decodePeers(String peersStr) {
try {
if (peersStr == "") return [];
List<dynamic> peers = json.decode(peersStr);
2022-09-21 21:20:19 +08:00
return peers.map((peer) {
return Peer.fromJson(peer as Map<String, dynamic>);
}).toList();
} catch (e) {
debugPrint('peers(): $e');
}
return [];
}
}