summaryrefslogtreecommitdiff
path: root/lib/data.dart
blob: 415e61e734191d36832627814cf1e7695db1a1b6 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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 MqttServerClient _client;
  bool _brokerConnected = false;
  bool get brokerConnected => _brokerConnected;
  bool _serverConnected = false;
  bool get serverConnected => _serverConnected;

  AppState();

  void process(MachineEvent event) {
    MachineState lastState = state;

    Map<MachineEvent, MachineState>? transitions = machine[lastState];
    if (transitions == null) {
      return;
    }

    MachineState? nextState = transitions[event];
    if (nextState == null) {
      return;
    }

    if (nextState == lastState) {
      return;
    }

    if (lastState == MachineState.init) {
      _initMqtt();
    }

    if (nextState == MachineState.init) {
      _deinitMqtt();
    }

    state = nextState;
    notifyListeners();
  }

  @override
  void dispose() {
    _client.disconnect();
    super.dispose();
  }

  void _initMqtt() {
    _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 {
      _client.connect();
    } catch (e) {
      _client.disconnect();
      return;
    }

    _client.updates?.listen(_onMessage);
  }

  void _deinitMqtt() {
    _client.disconnect();
  }

  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;
    }
  }
}