Update
This commit is contained in:
parent
5865aa445f
commit
ef3379f1d9
3 changed files with 33 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
enum StreamElementType { log, alert }
|
enum StreamElementType { log, warning, error, alert }
|
||||||
|
|
||||||
class StreamElement {
|
class StreamElement {
|
||||||
StreamElement(this.type, this.message) {
|
StreamElement(this.type, this.message) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Mailer {
|
||||||
];
|
];
|
||||||
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
|
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
|
||||||
static MailClient? _mailClient;
|
static MailClient? _mailClient;
|
||||||
|
static Timer? _timer;
|
||||||
|
|
||||||
/// High level mail API example
|
/// High level mail API example
|
||||||
static Future<bool> connect({
|
static Future<bool> connect({
|
||||||
|
@ -45,7 +46,8 @@ class Mailer {
|
||||||
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
||||||
await _onMessage(event, streamController, email, whitelist);
|
await _onMessage(event, streamController, email, whitelist);
|
||||||
});
|
});
|
||||||
await _mailClient!.startPolling();
|
await _mailClient!.startPolling(const Duration(seconds: 1));
|
||||||
|
_startDebugProfiler(streamController);
|
||||||
return true;
|
return true;
|
||||||
} on MailException catch (e) {
|
} on MailException catch (e) {
|
||||||
streamController.add(StreamElement(StreamElementType.log, e.toString()));
|
streamController.add(StreamElement(StreamElementType.log, e.toString()));
|
||||||
|
@ -59,6 +61,7 @@ class Mailer {
|
||||||
}) async {
|
}) async {
|
||||||
if (_mailClient == null) return;
|
if (_mailClient == null) return;
|
||||||
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
|
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
|
||||||
|
_stopDebugProfiler();
|
||||||
await _mailClient!.stopPolling();
|
await _mailClient!.stopPolling();
|
||||||
await _mailClient!.disconnect();
|
await _mailClient!.disconnect();
|
||||||
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
|
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
|
||||||
|
@ -103,4 +106,19 @@ class Mailer {
|
||||||
await Requests.sendAlert(message, type: AlertType.host);
|
await Requests.sendAlert(message, type: AlertType.host);
|
||||||
streamController.add(StreamElement(StreamElementType.alert, message));
|
streamController.add(StreamElement(StreamElementType.alert, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _startDebugProfiler(StreamController streamController) {
|
||||||
|
_timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
|
||||||
|
if (!_mailClient!.isPolling()) {
|
||||||
|
streamController.add(StreamElement(StreamElementType.warning,
|
||||||
|
'Mail client is not polling | Restart it'));
|
||||||
|
await _mailClient!.startPolling();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _stopDebugProfiler() {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,23 @@ class StreamElementCard extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
IconData icon;
|
IconData icon;
|
||||||
|
Color textColor;
|
||||||
switch (streamElement.type) {
|
switch (streamElement.type) {
|
||||||
case StreamElementType.log:
|
case StreamElementType.log:
|
||||||
icon = Icons.info;
|
icon = Icons.info;
|
||||||
|
textColor = Colors.white;
|
||||||
break;
|
break;
|
||||||
case StreamElementType.alert:
|
case StreamElementType.alert:
|
||||||
icon = Icons.notifications_active;
|
icon = Icons.notifications_active;
|
||||||
|
textColor = Colors.white;
|
||||||
|
break;
|
||||||
|
case StreamElementType.warning:
|
||||||
|
icon = Icons.warning_rounded;
|
||||||
|
textColor = Colors.yellow[600]!;
|
||||||
|
break;
|
||||||
|
case StreamElementType.error:
|
||||||
|
icon = Icons.error_rounded;
|
||||||
|
textColor = Colors.red[300]!;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return Card(
|
return Card(
|
||||||
|
@ -36,12 +47,13 @@ class StreamElementCard extends StatelessWidget {
|
||||||
Icon(
|
Icon(
|
||||||
icon,
|
icon,
|
||||||
size: 30,
|
size: 30,
|
||||||
|
color: textColor,
|
||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
streamElement.message,
|
streamElement.message,
|
||||||
style: const TextStyle(fontSize: 16),
|
style: TextStyle(fontSize: 16, color: textColor),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (streamElement.type == StreamElementType.alert)
|
if (streamElement.type == StreamElementType.alert)
|
||||||
|
|
Loading…
Reference in a new issue