diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d2a4d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub +.packages +.pub/ +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ diff --git a/lib/mqtt_client.dart b/lib/mqtt_client.dart index b3ad7e7..68c3dbe 100644 --- a/lib/mqtt_client.dart +++ b/lib/mqtt_client.dart @@ -11,6 +11,9 @@ class MqttClient { var onSubscribeData = null; var onConnectionLost = null; Map _messagesToCompleteMap; + + String userName; + String userPassword; var _liveTimer; @@ -20,7 +23,7 @@ class MqttClient { /** * MqttClient constructor */ - MqttClient(E mqttConnection, {String clientID: '', num qos: 0x0, bool cleanSession:true} ) + MqttClient(E mqttConnection, {String clientID: '', num qos: 0x0, bool cleanSession:true, String this.userName, String this.userPassword} ) : _mqttConnection = mqttConnection, _clientID = clientID, _qos = qos, _cleanSession = cleanSession, debugMessage = false, _will = null; /** @@ -40,8 +43,11 @@ class MqttClient { * An optional callback can be provided to be called when * the connection to the mqtt broker has been lost */ - Future connect([onConnectionLostCallback]) { + Future connect([onConnectionLostCallback, String uName, String uPassword]) { _connack = new Completer(); + + userName = uName != null ? uName : userName; + userPassword = uPassword != null ? uPassword : userPassword; if (onConnectionLostCallback != null) onConnectionLost = onConnectionLostCallback; @@ -164,6 +170,10 @@ class MqttClient { void _openSession() { print("Opening session"); MqttMessageConnect m = new MqttMessageConnect.setOptions(_clientID, _qos, _cleanSession); + + m._userName= userName; + m._password = userPassword; + m.setUserNameAndPassword(userName, userPassword); // set will m.setWill(_will); @@ -190,7 +200,11 @@ class MqttClient { * * Return the data that has not been processed */ - List _processMqttMessage(data) { + List _processMqttMessage(dynamic details) { + var data = details; + + if (details.runtimeType.toString() != 'Uint8List') data = details.asInt8List(); + num type = data[0] >> 4; int msgProcessedLength = data.length; diff --git a/lib/mqtt_message_suback.dart b/lib/mqtt_message_suback.dart index b6f92b2..7864ea4 100644 --- a/lib/mqtt_message_suback.dart +++ b/lib/mqtt_message_suback.dart @@ -22,7 +22,7 @@ class MqttMessageSuback extends MqttMessageAssured { * byte 2 - Message ID LSB */ num decodeVariableHeader(List data) { - assert(data.length == 3); + // assert(data.length == 3); messageID = 256 * data[0] + data[1]; diff --git a/lib/mqtt_shared.dart b/lib/mqtt_shared.dart index 973c4b2..a2911fa 100644 --- a/lib/mqtt_shared.dart +++ b/lib/mqtt_shared.dart @@ -5,6 +5,7 @@ import 'mqtt_version_v3.dart' ; import "package:ini/ini.dart"; import 'dart:io'; import 'dart:convert'; +import 'dart:typed_data'; part 'mqtt_client.dart'; part 'mqtt_connection_shared.dart';