feat: lots of fixes

This commit is contained in:
2024-03-14 20:50:30 +01:00
parent 5d617131ee
commit 48476f6fe4
11 changed files with 377 additions and 136 deletions

View File

@ -24,20 +24,33 @@ class LocationUpdateReceived extends LocationUpdateState {
class LocationSubscribeCubit extends Cubit<LocationUpdateState> {
Option<WebSocketClient> _wsClient = None;
String url = '';
String username = '';
String pass = '';
LocationSubscribeCubit() : super(LocationUpdateUnconnected());
subscribe(SettingsState settings) async {
// check if resubscribe is necessary (different URL)
if (settings.url == url && settings.username == username && settings.password == pass) {
return;
} else {
url = settings.url;
username = settings.username;
pass = settings.password;
}
await _wsConnectionEstablish();
}
if(_wsClient.isSome()) {
Future<void> _wsConnectionEstablish() async {
if (_wsClient.isSome()) {
await _wsClient.unwrap().close();
_wsClient = None;
}
var ws = await OwntracksApi(
baseUrl: settings.url,
username: settings.username,
pass: settings.password)
var ws = await OwntracksApi(baseUrl: url, username: username, pass: pass)
.createWebSocketConnection(
wsPath: 'last',
onMessage: (msg) {
@ -47,7 +60,7 @@ class LocationSubscribeCubit extends Cubit<LocationUpdateState> {
}
try {
final Map<String, dynamic> map = jsonDecode(msg);
if (map['_type'] == 'location') {
// filter points (only the ones for this device pls!)
final topic = (map['topic'] as String?)?.split('/');
@ -55,17 +68,20 @@ class LocationSubscribeCubit extends Cubit<LocationUpdateState> {
// couldn't reconstruct ID, bail
return;
}
// build device_id
final deviceId = (topic[1], topic[2]);
print(map);
// build point
final p = Point(
lat: map['lat'] as double,
lon: map['lon'] as double,
timestamp:
DateTime.fromMillisecondsSinceEpoch(map['tst'] as int));
timestamp: DateTime.fromMillisecondsSinceEpoch((map['tst'] as int) * 1000));
print(p);
emit(LocationUpdateReceived(p, deviceId));
}
} catch (e) {
@ -74,22 +90,25 @@ class LocationSubscribeCubit extends Cubit<LocationUpdateState> {
}
},
onStateChange: (sc) {
if (sc case WebSocketClientState$Open(:final url)) {
_wsClient.map((wsc) => wsc.add('LAST'));
switch (sc) {
case WebSocketClientState$Open(:final url):
_wsClient.map((wsc) => wsc.add('LAST'));
emit(LocationUpdateConnected());
break;
default:
emit(LocationUpdateUnconnected());
break;
}
print(sc);
},
);
_wsClient = Some(ws);
emit(LocationUpdateConnected());
_wsClient = ws.expect("Estabilshing Websocket Conenction failed").toOption();
}
@override
void onChange(Change<LocationUpdateState> change) {
print('loc_sub_cubit change: $change');
}
@override