Erweiterungen

This commit is contained in:
Sven committed 2024-06-21 13:34:43 +02:00
1 parent 85e973c3dd
commit fbdd2a4338
14 files changed
+296 -116

No files matched your search

+16
View File
@@ -37,6 +37,22 @@ class MessageService {
if (entry.owner == owner) {
_callbackMaps[topic]!.remove(entry);
unsubscribe(topic, owner);
break;
}
}
}
}
unsubscribeAll<T>(dynamic owner) {
for (var callbackMapsEntry in _callbackMaps.entries) {
for (var entry in callbackMapsEntry.value.toList()) {
if (entry.owner == owner) {
_callbackMaps[callbackMapsEntry.key]!.remove(entry);
unsubscribeAll(owner);
break;
}
}
@@ -1,7 +1,11 @@
import 'package:mqtt_client/mqtt_client.dart';
class PublishPayloadMessage {
final String topic;
final Map<String, dynamic> message;
PublishPayloadMessage({required this.topic, required this.message});
final MqttQos qos;
PublishPayloadMessage({required this.topic, required this.message, required this.qos});
}
+17 -22
View File
@@ -119,31 +119,29 @@ class MqttCore {
_reconnectDelay = 0;
_setupMqtt();
await _setupMqtt();
_initSession();
} else {
Util.logInfo('Verbindung zum MQTT-Broker fehlgeschlagen!');
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged,
arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
}
} catch (ex) {
Util.logInfo('Verbindung zum MQTT-Broker fehlgeschlagen!: $ex');
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged,
arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
}
}
bool _publishText(String topic, String text, {bool enqueueOnError = true}) {
bool _publishText(String topic, String text, MqttQos qos, {bool enqueueOnError = true}) {
try {
var buffer = Uint8Buffer();
buffer.addAll(GZipEncoder().encode(const Utf8Encoder().convert(text)) as Iterable<int>);
var messageTopic = "$_toServerTopic$_clientId/$topic";
_mqttClient!.publishMessage(messageTopic, MqttQos.exactlyOnce, buffer);
_mqttClient!.publishMessage(messageTopic, qos, buffer);
Util.logInfo("message\n$text\nsend to $messageTopic");
Util.logInfo("size in bytes: ${buffer.length}");
@@ -158,7 +156,7 @@ class MqttCore {
return false;
}
void _setupMqtt() async {
Future<void> _setupMqtt() async {
Util.logInfo('_setupMqtt called');
_mqttClient!.updates?.listen((List<MqttReceivedMessage<MqttMessage>> messages) {
@@ -368,7 +366,7 @@ class MqttCore {
var json = jsonEncode(deviceLog.toJson());
_publishText("device_log", json);
_publishText("device_log", json, MqttQos.atMostOnce);
}
void _startBootstrapReconnectTimer() {
@@ -398,19 +396,17 @@ class MqttCore {
_startTrackTimer();
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged,
arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, arguments: ConnectionStatus(status: ConnectionStatusEnum.notConnected));
}
void _clientConnected() {
Util.logInfo("_clientDisconnected called");
Util.logInfo("_clientConnected called");
connectionState = ConnectionStateEnum.connected;
_initSession();
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged,
arguments: ConnectionStatus(status: ConnectionStatusEnum.connected));
MessageService().publish<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, arguments: ConnectionStatus(status: ConnectionStatusEnum.connected));
}
void _processBootstrap() async {
@@ -434,7 +430,6 @@ class MqttCore {
var jsonMap = handshakeMessage.toJson();
var json = Uri.encodeFull(jsonEncode(jsonMap));
//var uri = "https://test-app-server.assecutor.org/bootstrap?json=$json";
var uri = "https://bootstrap.assecutor.de/bootstrap?json=$json";
Util.logInfo("Bootstrap request: $uri");
@@ -454,7 +449,7 @@ class MqttCore {
_connectToBroker();
} catch (ex) {
Util.logException("bootstrap failed");
Util.logException("bootstrap failed: {$ex}");
_startBootstrapReconnectTimer();
}
@@ -464,7 +459,7 @@ class MqttCore {
var startSessionMessage = StartSessionMessage();
var jsonMap = startSessionMessage.toJson();
var json = jsonEncode(jsonMap);
_publishText("session", json);
_publishText("session", json, MqttQos.exactlyOnce);
}
_sendSyncMessage() async {
@@ -482,7 +477,7 @@ class MqttCore {
answer.sync.jobs.add(syncJob);
}
_publishText("sync", jsonEncode(answer.toJson()));
_publishText("sync", jsonEncode(answer.toJson()), MqttQos.exactlyOnce);
}
void _startTrackTimer() {
@@ -495,7 +490,7 @@ class MqttCore {
trackMessage.long = position.longitude;
trackMessage.acc = position.accuracy.toInt();
_publishText("track", jsonEncode(trackMessage.toJson()));
_publishText("track", jsonEncode(trackMessage.toJson()), MqttQos.atMostOnce);
}
});
}
@@ -522,7 +517,7 @@ class MqttCore {
if (enqueuedMessages.isNotEmpty) {
for (var enqueuedMessage in enqueuedMessages) {
try {
if (_publishText(enqueuedMessage.topic, enqueuedMessage.payload, enqueueOnError: false)) {
if (_publishText(enqueuedMessage.topic, enqueuedMessage.payload, MqttQos.exactlyOnce, enqueueOnError: false)) {
await MyApp.isarInstance.writeTxn(() async {
await MyApp.isarInstance.enqueuedMessages.delete(enqueuedMessage.id);
});
@@ -541,12 +536,12 @@ class MqttCore {
message.ver = job.ver;
message.utc = DateTime.now().millisecondsSinceEpoch;
_publishText('job_received', jsonEncode(message.toJson()));
_publishText('job_received', jsonEncode(message.toJson()), MqttQos.exactlyOnce);
}
void initMessages() {
MessageService().subscribe<PublishPayloadMessage>(Constants.messageTopicPublishPayload, this, (PublishPayloadMessage payloadMessage) {
_publishText(payloadMessage.topic, jsonEncode(payloadMessage.message));
_publishText(payloadMessage.topic, jsonEncode(payloadMessage.message), payloadMessage.qos);
});
MessageService().subscribe<DisconnectClientMessage>(Constants.messageTopicDisconnectClient, this,
+55 -35
View File
@@ -220,43 +220,63 @@ class Util {
}
}
Widget getDefaultButton(String text, Function() onPressed, {Image? image, double? buttonHeight}) {
return SizedBox(
height: buttonHeight ?? 50,
child: MaterialButton(
color: const Color.fromARGB(255, 220, 220, 220),
disabledColor: Colors.indigo[400],
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15),
Widget getDefaultButton(String text, Function() onPressed, {Image? image, double? buttonHeight, bool enabledTimerActive = true, int enabledTimerDelay = 0}) {
bool enabledState = enabledTimerActive;
return StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
if (!enabledState) {
Future.delayed(Duration(seconds: enabledTimerDelay), () {
enabledState = true;
if (context.mounted) {
setState(() {});
}
});
}
return SizedBox(
height: buttonHeight ?? 50,
child: MaterialButton(
color: const Color.fromARGB(255, 220, 220, 220),
disabledColor: const Color.fromARGB(255, 220, 220, 220),
disabledElevation: 5,
elevation: 5,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
onPressed: () {
if (enabledState) {
Util.logInfo("Button \"$text\" clicked");
onPressed();
}
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Visibility(
visible: image != null,
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: image,
),
),
Expanded(
child: Opacity(
opacity: enabledState ? 1.0 : 0.3,
child: Text(
text,
style: const TextStyle(color: Colors.black, fontSize: 14.0),
textAlign: TextAlign.center,
),
),
),
],
),
),
onPressed: () {
Util.logInfo("Button \"$text\" clicked");
onPressed();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Visibility(
visible: image != null,
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: image,
),
),
Expanded(
child: Text(
text,
style: const TextStyle(color: Colors.black, fontSize: 14.0),
textAlign: TextAlign.center,
),
),
],
),
),
);
);
});
}
String mapVehicleTypeToImageName(int vehType)