Skip to content

Commit a20bc09

Browse files
committed
feat(upgrade): upgrade dependency packages and fix several bugs
1 parent b42106f commit a20bc09

75 files changed

Lines changed: 3243 additions & 1662 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

mqtt-client-C-paho/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.17)
2-
find_package(eclipse-paho-mqtt-c 1.3.9 REQUIRED)
2+
33
project(mqtt_c C)
44
include_directories(/usr/local/include)
55
link_directories(/usr/local/lib)

mqtt-client-C-paho/simple_test.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "stdlib.h"
2+
#include "string.h"
3+
#include "unistd.h"
4+
#include "MQTTClient.h"
5+
6+
#define ADDRESS "tcp://broker.emqx.io:1883"
7+
#define USERNAME "emqx"
8+
#define PASSWORD "public"
9+
#define CLIENTID "c-client-test"
10+
#define QOS 0
11+
#define TOPIC "emqx/c-test"
12+
#define TIMEOUT 10000L
13+
14+
int main(int argc, char *argv[]) {
15+
int rc;
16+
MQTTClient client;
17+
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
18+
19+
MQTTClient_create(&client, ADDRESS, CLIENTID, 0, NULL);
20+
conn_opts.username = USERNAME;
21+
conn_opts.password = PASSWORD;
22+
23+
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
24+
printf("Failed to connect, return code %d\n", rc);
25+
exit(-1);
26+
} else {
27+
printf("Connected to MQTT Broker!\n");
28+
}
29+
30+
MQTTClient_disconnect(client, 1000);
31+
MQTTClient_destroy(&client);
32+
return 0;
33+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
</PropertyGroup>
9-
10-
<ItemGroup>
11-
<PackageReference Include="MQTTnet" Version="4.2.0.706" />
12-
</ItemGroup>
13-
14-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="MQTTnet" Version="4.3.7.1364" />
12+
</ItemGroup>
13+
14+
</Project>

mqtt-client-Csharp/MqttNetTLSClient/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class Program
55

66
static void Main(string[] args)
77
{
8-
// 自签名证书单向认证
8+
// Self-signed certificate one-way authentication
99
// MqttClient.Client.TlsClientWithCA("E:/certs/cacert.pem").Wait();
1010

11-
// 双向认证
11+
// Two-way authentication
1212
MqttClient.Client.TlsClientWithCert(@"E:/certs/cacert.pem", @"E:/certs/client.pfx","123456").Wait();
1313
}
1414
}

mqtt-client-Csharp/publish/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using uPLibrary.Networking.M2Mqtt;
33

44
namespace publish
@@ -19,6 +19,7 @@ static MqttClient ConnectMQTT(string broker, int port, string clientId, string u
1919
}
2020
return client;
2121
}
22+
2223
static void Publish(MqttClient client, string topic)
2324
{
2425
int msg_count = 0;
@@ -31,6 +32,7 @@ static void Publish(MqttClient client, string topic)
3132
msg_count++;
3233
}
3334
}
35+
3436
static void Main(string[] args)
3537
{
3638
string broker = "broker.emqx.io";

mqtt-client-Csharp/publish/publish.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

mqtt-client-Csharp/subscribe/Program.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using uPLibrary.Networking.M2Mqtt;
33
using uPLibrary.Networking.M2Mqtt.Messages;
44

@@ -20,16 +20,20 @@ static MqttClient ConnectMQTT(string broker, int port, string clientId, string u
2020
}
2121
return client;
2222
}
23+
2324
static void Subscribe(MqttClient client, string topic)
2425
{
25-
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
26-
client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
27-
}
28-
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
29-
{
30-
string payload = System.Text.Encoding.Default.GetString(e.Message);
31-
Console.WriteLine("Received `{0}` from `{1}` topic", payload, e.Topic.ToString());
26+
client.MqttMsgPublishReceived += (sender, e) =>
27+
{
28+
Console.WriteLine($"Received `{System.Text.Encoding.UTF8.GetString(e.Message)}` from `{e.Topic}` topic");
29+
};
30+
31+
client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
32+
33+
Console.WriteLine($"Subscribed to topic: {topic}");
34+
Console.ReadLine(); // Keep the program running
3235
}
36+
3337
static void Main(string[] args)
3438
{
3539
string broker = "broker.emqx.io";
@@ -38,6 +42,7 @@ static void Main(string[] args)
3842
string clientId = Guid.NewGuid().ToString();
3943
string username = "emqx";
4044
string password = "public";
45+
4146
MqttClient client = ConnectMQTT(broker, port, clientId, username, password);
4247
Subscribe(client, topic);
4348
}

mqtt-client-Csharp/subscribe/subscribe.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
from . import mqtt
2-
mqtt.client.loop_start()
2+
3+
# Create and start the MQTT client
4+
client = mqtt.create_mqtt_client()
5+
client.loop_start()
6+

mqtt-client-Django/django_mqtt/mqtt.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,33 @@
22
from django.conf import settings
33

44

5-
def on_connect(mqtt_client, userdata, flags, rc):
6-
if rc == 0:
7-
print('Connected successfully')
8-
mqtt_client.subscribe('django/mqtt')
5+
def on_connect(mqtt_client, userdata, flags, reason_code, properties):
6+
if reason_code == 0:
7+
print("Connected successfully")
8+
mqtt_client.subscribe("django/mqtt")
99
else:
10-
print('Bad connection. Code:', rc)
10+
print("Bad connection. Code:", reason_code)
1111

1212

1313
def on_message(mqtt_client, userdata, msg):
14-
print(f'Received message on topic: {msg.topic} with payload: {msg.payload}')
14+
print(f"Received message on topic: {msg.topic} with payload: {msg.payload}")
1515

1616

17-
client = mqtt.Client()
18-
client.on_connect = on_connect
19-
client.on_message = on_message
20-
client.username_pw_set(settings.MQTT_USER, settings.MQTT_PASSWORD)
21-
client.connect(
22-
host=settings.MQTT_SERVER,
23-
port=settings.MQTT_PORT,
24-
keepalive=settings.MQTT_KEEPALIVE
25-
)
17+
def create_mqtt_client():
18+
"""Create and return an MQTT client configured for connection"""
19+
client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION2)
20+
client.on_connect = on_connect
21+
client.on_message = on_message
22+
23+
# Only set credentials if they are provided
24+
if settings.MQTT_USER and settings.MQTT_PASSWORD:
25+
client.username_pw_set(settings.MQTT_USER, settings.MQTT_PASSWORD)
26+
27+
client.connect(
28+
host=settings.MQTT_SERVER,
29+
port=settings.MQTT_PORT,
30+
keepalive=settings.MQTT_KEEPALIVE
31+
)
32+
33+
return client
34+

0 commit comments

Comments
 (0)