1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';
const String brokerHostname = 'sia.xengineering.eu';
const int brokerPort = 1883;
const String topicPrefix = 'sia';
enum MachineState {
init, // user does not want connection MQTT not connected
disconnected, // user wants connection but MQTT not (yet) connected
}
enum MachineEvent {
connect, // user wants to connect
disconnect, // user does not want an connection anymore
}
class AppState with ChangeNotifier {
static const Map<MachineState, Map<MachineEvent, MachineState>> machine = <MachineState, Map<MachineEvent, MachineState>> {
MachineState.init: <MachineEvent, MachineState> {
MachineEvent.connect: MachineState.disconnected,
},
MachineState.disconnected: <MachineEvent, MachineState> {
MachineEvent.disconnect: MachineState.init,
},
};
MachineState state = MachineState.init;
Map<String, bool> contacts = <String, bool>{};
late final MqttServerClient _client;
bool _brokerConnected = false;
bool get brokerConnected => _brokerConnected;
bool _serverConnected = false;
bool get serverConnected => _serverConnected;
AppState() {
_initMqtt();
}
void process(MachineEvent event) {
Map<MachineEvent, MachineState>? ruleset = machine[state];
if (ruleset == null) {
return;
}
MachineState? nextMachineState = ruleset[event];
if (nextMachineState == null) {
return;
}
state = nextMachineState;
notifyListeners();
}
@override
void dispose() {
_client.disconnect();
super.dispose();
}
Future<void> _initMqtt() async {
_client = MqttServerClient(
brokerHostname,
'sia_app_${DateTime.now().millisecondsSinceEpoch}',
);
_client.port = brokerPort;
_client.keepAlivePeriod = 2;
_client.autoReconnect = true;
_client.disconnectOnNoResponsePeriod = 1;
_client.logging(on: false);
_client.onConnected = _onConnected;
_client.onDisconnected = _onDisconnected;
_client.onAutoReconnect = _onAutoReconnect;
_client.onAutoReconnected = _onAutoReconnected;
try {
await _client.connect();
} catch (e) {
_client.disconnect();
return;
}
_client.updates?.listen(_onMessage);
}
void _onConnected() {
_client.subscribe('$topicPrefix/contact/+/state', MqttQos.exactlyOnce);
_client.subscribe('$topicPrefix/server/health', MqttQos.exactlyOnce);
_brokerConnected = true;
notifyListeners();
}
void _onDisconnected() {
_brokerConnected = false;
notifyListeners();
}
void _onAutoReconnect() {
_brokerConnected = false;
notifyListeners();
}
void _onAutoReconnected() {
_brokerConnected = true;
notifyListeners();
}
void _onMessage(List<MqttReceivedMessage<MqttMessage>> messages) {
for (final MqttReceivedMessage<MqttMessage> message in messages) {
final String topic = message.topic;
final MqttPublishMessage payloadMessage =
message.payload as MqttPublishMessage;
final String payload =
MqttPublishPayload.bytesToStringAsString(payloadMessage.payload.message);
if (topic == '$topicPrefix/server/health') {
if (payload == 'good') {
_serverConnected = true;
notifyListeners();
}
if (payload == 'bad') {
_serverConnected = false;
notifyListeners();
}
}
// format <prefix>/contacts/<address>/state
final List<String> parts = topic.split('/');
if (parts.length != 4 || parts[1] != 'contact' || parts[3] != 'state') {
continue;
}
final String address = parts[2];
final bool? parsedState = _parseBool(payload);
if (parsedState != null) {
contacts[address] = parsedState;
notifyListeners();
}
}
}
bool? _parseBool(String payload) {
switch (payload.toLowerCase()) {
case 'open':
return true;
case 'closed':
return false;
default:
return null;
}
}
}
|