-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sayan Paul <[email protected]>
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import 'dart:convert'; | ||
import 'dart:ffi'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:mqtt_client/mqtt_client.dart'; | ||
import 'package:mqtt_client/mqtt_server_client.dart'; | ||
|
||
class MqttHandler with ChangeNotifier { | ||
final ValueNotifier<String> data = ValueNotifier<String>(""); | ||
late MqttServerClient client; | ||
|
||
Future<Object> connect() async { | ||
client = MqttServerClient.withPort( | ||
'broker.hivemq.com', 'smartpump239834893453920938', 1883); | ||
client.logging(on: true); | ||
client.onConnected = onConnected; | ||
client.onDisconnected = onDisconnected; | ||
client.onUnsubscribed = onUnsubscribed; | ||
client.onSubscribed = onSubscribed; | ||
client.onSubscribeFail = onSubscribeFail; | ||
client.pongCallback = pong; | ||
client.keepAlivePeriod = 60; | ||
client.logging(on: true); | ||
|
||
/// Set the correct MQTT protocol for mosquito | ||
client.setProtocolV311(); | ||
|
||
final connMessage = MqttConnectMessage() | ||
.withWillTopic('willtopic') | ||
.withWillMessage('Will message') | ||
.startClean() | ||
.withWillQos(MqttQos.atLeastOnce); | ||
|
||
print('MQTT_LOGS::Mosquitto client connecting....'); | ||
|
||
client.connectionMessage = connMessage; | ||
try { | ||
await client.connect(); | ||
} catch (e) { | ||
print('Exception: $e'); | ||
client.disconnect(); | ||
} | ||
|
||
if (client.connectionStatus!.state == MqttConnectionState.connected) { | ||
print('MQTT_LOGS::Mosquitto client connected'); | ||
} else { | ||
print( | ||
'MQTT_LOGS::ERROR Mosquitto client connection failed - disconnecting, status is ${client.connectionStatus}'); | ||
client.disconnect(); | ||
return -1; | ||
} | ||
|
||
//wil be used to determine water tank status | ||
print('MQTT_LOGS::Subscribing to the pump/current topic'); | ||
const topic = 'pump/current'; | ||
client.subscribe(topic, MqttQos.atMostOnce); | ||
|
||
client.updates!.listen((List<MqttReceivedMessage<MqttMessage?>>? c) { | ||
final recMess = c![0].payload as MqttPublishMessage; | ||
final pt = | ||
MqttPublishPayload.bytesToStringAsString(recMess.payload.message); | ||
|
||
data.value = pt; | ||
notifyListeners(); | ||
print( | ||
'MQTT_LOGS:: New data arrived: topic is <${c[0].topic}>, payload is $pt'); | ||
print(''); | ||
}); | ||
|
||
return client; | ||
} | ||
|
||
void onConnected() { | ||
print('MQTT_LOGS:: Connected'); | ||
} | ||
|
||
void onDisconnected() { | ||
print('MQTT_LOGS:: Disconnected'); | ||
} | ||
|
||
void onSubscribed(String topic) { | ||
print('MQTT_LOGS:: Subscribed topic: $topic'); | ||
} | ||
|
||
void onSubscribeFail(String topic) { | ||
print('MQTT_LOGS:: Failed to subscribe $topic'); | ||
} | ||
|
||
void onUnsubscribed(String? topic) { | ||
print('MQTT_LOGS:: Unsubscribed topic: $topic'); | ||
} | ||
|
||
void pong() { | ||
print('MQTT_LOGS:: Ping response client callback invoked'); | ||
} | ||
|
||
void pumpTrigger(bool state) { | ||
const pubTopic = 'trigger/pump'; | ||
final builder = MqttClientPayloadBuilder(); | ||
builder.addBool(val: state); | ||
if (client.connectionStatus?.state == MqttConnectionState.connected) { | ||
client.publishMessage(pubTopic, MqttQos.atMostOnce, builder.payload!); | ||
} | ||
} | ||
|
||
void updateConfig(String nextRun, UnsignedInt duration, bool auto) { | ||
const pubTopic = 'update/config'; | ||
final builder = MqttClientPayloadBuilder(); | ||
builder.addString(jsonEncode({ | ||
"next_run": nextRun, | ||
"interval": duration, | ||
"auto": auto, | ||
})); | ||
if (client.connectionStatus?.state == MqttConnectionState.connected) { | ||
client.publishMessage(pubTopic, MqttQos.atMostOnce, builder.payload!); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters