123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- import 'dart:convert';
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'platform_model.dart';
- // ignore: depend_on_referenced_packages
- import 'package:collection/collection.dart';
- class Peer {
- final String id;
- String hash; // personal ab hash password
- String password; // shared ab password
- String username; // pc username
- String hostname;
- String platform;
- String alias;
- List<dynamic> tags;
- bool forceAlwaysRelay = false;
- String rdpPort;
- String rdpUsername;
- bool online = false;
- String loginName; //login username
- String device_group_name;
- bool? sameServer;
- String getId() {
- if (alias != '') {
- return alias;
- }
- return id;
- }
- Peer.fromJson(Map<String, dynamic> json)
- : id = json['id'] ?? '',
- hash = json['hash'] ?? '',
- password = json['password'] ?? '',
- username = json['username'] ?? '',
- hostname = json['hostname'] ?? '',
- platform = json['platform'] ?? '',
- alias = json['alias'] ?? '',
- tags = json['tags'] ?? [],
- forceAlwaysRelay = json['forceAlwaysRelay'] == 'true',
- rdpPort = json['rdpPort'] ?? '',
- rdpUsername = json['rdpUsername'] ?? '',
- loginName = json['loginName'] ?? '',
- device_group_name = json['device_group_name'] ?? '',
- sameServer = json['same_server'];
- Map<String, dynamic> toJson() {
- return <String, dynamic>{
- "id": id,
- "hash": hash,
- "password": password,
- "username": username,
- "hostname": hostname,
- "platform": platform,
- "alias": alias,
- "tags": tags,
- "forceAlwaysRelay": forceAlwaysRelay.toString(),
- "rdpPort": rdpPort,
- "rdpUsername": rdpUsername,
- 'loginName': loginName,
- 'device_group_name': device_group_name,
- 'same_server': sameServer,
- };
- }
- Map<String, dynamic> toCustomJson({required bool includingHash}) {
- var res = <String, dynamic>{
- "id": id,
- "username": username,
- "hostname": hostname,
- "platform": platform,
- "alias": alias,
- "tags": tags,
- };
- if (includingHash) {
- res['hash'] = hash;
- }
- return res;
- }
- Map<String, dynamic> toGroupCacheJson() {
- return <String, dynamic>{
- "id": id,
- "username": username,
- "hostname": hostname,
- "platform": platform,
- "login_name": loginName,
- "device_group_name": device_group_name,
- };
- }
- Peer({
- required this.id,
- required this.hash,
- required this.password,
- required this.username,
- required this.hostname,
- required this.platform,
- required this.alias,
- required this.tags,
- required this.forceAlwaysRelay,
- required this.rdpPort,
- required this.rdpUsername,
- required this.loginName,
- required this.device_group_name,
- this.sameServer,
- });
- Peer.loading()
- : this(
- id: '...',
- hash: '',
- password: '',
- username: '...',
- hostname: '...',
- platform: '...',
- alias: '',
- tags: [],
- forceAlwaysRelay: false,
- rdpPort: '',
- rdpUsername: '',
- loginName: '',
- device_group_name: '',
- );
- bool equal(Peer other) {
- return id == other.id &&
- hash == other.hash &&
- password == other.password &&
- username == other.username &&
- hostname == other.hostname &&
- platform == other.platform &&
- alias == other.alias &&
- tags.equals(other.tags) &&
- forceAlwaysRelay == other.forceAlwaysRelay &&
- rdpPort == other.rdpPort &&
- rdpUsername == other.rdpUsername &&
- device_group_name == other.device_group_name &&
- loginName == other.loginName;
- }
- Peer.copy(Peer other)
- : this(
- id: other.id,
- hash: other.hash,
- password: other.password,
- username: other.username,
- hostname: other.hostname,
- platform: other.platform,
- alias: other.alias,
- tags: other.tags.toList(),
- forceAlwaysRelay: other.forceAlwaysRelay,
- rdpPort: other.rdpPort,
- rdpUsername: other.rdpUsername,
- loginName: other.loginName,
- device_group_name: other.device_group_name,
- sameServer: other.sameServer);
- }
- enum UpdateEvent { online, load }
- typedef GetInitPeers = RxList<Peer> Function();
- class Peers extends ChangeNotifier {
- final String name;
- final String loadEvent;
- List<Peer> peers = List.empty(growable: true);
- final GetInitPeers? getInitPeers;
- UpdateEvent event = UpdateEvent.load;
- static const _cbQueryOnlines = 'callback_query_onlines';
- Peers(
- {required this.name,
- required this.getInitPeers,
- required this.loadEvent}) {
- peers = getInitPeers?.call() ?? [];
- 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) {
- int changedCount = 0;
- evt['onlines'].split(',').forEach((online) {
- for (var i = 0; i < peers.length; i++) {
- if (peers[i].id == online) {
- if (!peers[i].online) {
- changedCount += 1;
- peers[i].online = true;
- }
- }
- }
- });
- evt['offlines'].split(',').forEach((offline) {
- for (var i = 0; i < peers.length; i++) {
- if (peers[i].id == offline) {
- if (peers[i].online) {
- changedCount += 1;
- peers[i].online = false;
- }
- }
- }
- });
- if (changedCount > 0) {
- event = UpdateEvent.online;
- notifyListeners();
- }
- }
- void _updatePeers(Map<String, dynamic> evt) {
- final onlineStates = _getOnlineStates();
- if (getInitPeers != null) {
- peers = getInitPeers?.call() ?? [];
- } else {
- 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);
- return peers.map((peer) {
- return Peer.fromJson(peer as Map<String, dynamic>);
- }).toList();
- } catch (e) {
- debugPrint('peers(): $e');
- }
- return [];
- }
- }
|