Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
d02e0d0d
IoTSharp
项目概览
IoTSharp
/
IoTSharp
12 个月 前同步成功
通知
15
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
IoTSharp
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d02e0d0d
编写于
2月 07, 2022
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改sdk
上级
5d6eee86
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
19 addition
and
24 deletion
+19
-24
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/IoTSharp.Sdk.MQTT.csproj
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/IoTSharp.Sdk.MQTT.csproj
+1
-1
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/MQTTClient.cs
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/MQTTClient.cs
+18
-22
IoTSharp/Extensions/MqttExtension.cs
IoTSharp/Extensions/MqttExtension.cs
+0
-1
未找到文件。
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/IoTSharp.Sdk.MQTT.csproj
浏览文件 @
d02e0d0d
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net4
5
2;netstandard2.0;netstandard2.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>net4
6
2;netstandard2.0;netstandard2.1;net5.0;net6.0</TargetFrameworks>
<Version>1.0.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
...
...
IoTSharp.SDKs/IoTSharp.Sdk.MQTT/MQTTClient.cs
浏览文件 @
d02e0d0d
using
MQTTnet
;
using
MQTTnet.Client
;
using
MQTTnet.Client.Connecting
;
using
MQTTnet.Client.Disconnecting
;
using
MQTTnet.Client.Options
;
using
MQTTnet.Client.Receiving
;
using
MQTTnet.Diagnostics
;
using
MQTTnet.Protocol
;
using
System
;
...
...
@@ -21,7 +17,7 @@ namespace IoTSharp.EdgeSdk.MQTT
public
string
DeviceId
{
get
;
set
;
}
=
string
.
Empty
;
public
Uri
BrokerUri
{
get
;
set
;
}
public
bool
IsConnected
=>
(
Client
?.
IsConnected
).
GetValueOrDefault
();
private
I
MqttClient
Client
{
get
;
set
;
}
private
MqttClient
Client
{
get
;
set
;
}
public
delegate
void
DLogError
(
string
message
,
Exception
exception
);
public
event
DLogError
LogError
;
public
delegate
void
DLogInformation
(
string
message
);
...
...
@@ -48,9 +44,14 @@ namespace IoTSharp.EdgeSdk.MQTT
.
WithTcpServer
(
uri
.
Host
,
uri
.
Port
)
.
WithCredentials
(
username
,
password
)
.
Build
();
Client
.
ApplicationMessageReceivedHandler
=
new
MqttApplicationMessageReceivedHandlerDelegate
(
args
=>
Client_ApplicationMessageReceived
(
Client
,
args
));
Client
.
ConnectedHandler
=
new
MqttClientConnectedHandlerDelegate
(
args
=>
Client_ConnectedAsync
(
Client
,
args
));
Client
.
DisconnectedHandler
=
new
MqttClientDisconnectedHandlerDelegate
(
async
e
=>
Client
.
ApplicationMessageReceivedAsync
+=
Client_ApplicationMessageReceived
;
Client
.
ConnectedAsync
+=
e
=>
{
Client
.
SubscribeAsync
(
$"/devices/
{
DeviceId
}
/rpc/request/+/+"
);
Client
.
SubscribeAsync
(
$"/devices/
{
DeviceId
}
/attributes/update/"
,
MqttQualityOfServiceLevel
.
ExactlyOnce
);
LogInformation
?.
Invoke
(
$"CONNECTED WITH SERVER "
);
return
Task
.
CompletedTask
;
};
Client
.
DisconnectedAsync
+=
async
e
=>
{
try
{
...
...
@@ -60,7 +61,7 @@ namespace IoTSharp.EdgeSdk.MQTT
{
LogError
?.
Invoke
(
"CONNECTING FAILED"
,
exception
);
}
}
)
;
};
try
{
...
...
@@ -80,16 +81,11 @@ namespace IoTSharp.EdgeSdk.MQTT
return
initok
;
}
private
void
Client_ConnectedAsync
(
object
sender
,
MqttClientConnectedEventArgs
e
)
{
Client
.
SubscribeAsync
(
$"/devices/
{
DeviceId
}
/rpc/request/+/+"
);
Client
.
SubscribeAsync
(
$"/devices/
{
DeviceId
}
/attributes/update/"
,
MqttQualityOfServiceLevel
.
ExactlyOnce
);
LogInformation
?.
Invoke
(
$"CONNECTED WITH SERVER "
);
}
private
void
Client_ApplicationMessageReceived
(
object
sender
,
MqttApplicationMessageReceivedEventArgs
e
)
private
Task
Client_ApplicationMessageReceived
(
MqttApplicationMessageReceivedEventArgs
e
)
{
LogDebug
?.
Invoke
(
$"ApplicationMessageReceived Topic
{
e
.
ApplicationMessage
.
Topic
}
QualityOfServiceLevel:
{
e
.
ApplicationMessage
.
QualityOfServiceLevel
}
Retain:
{
e
.
ApplicationMessage
.
Retain
}
"
);
try
...
...
@@ -118,12 +114,12 @@ namespace IoTSharp.EdgeSdk.MQTT
});
}
}
}
catch
(
Exception
ex
)
{
LogError
?.
Invoke
(
$"ClientId:
{
e
.
ClientId
}
Topic:
{
e
.
ApplicationMessage
.
Topic
}
,Payload:
{
e
.
ApplicationMessage
.
ConvertPayloadToString
()}
"
,
ex
);
}
return
Task
.
CompletedTask
;
}
private
void
ReceiveAttributes
(
MqttApplicationMessageReceivedEventArgs
e
)
...
...
@@ -156,21 +152,21 @@ namespace IoTSharp.EdgeSdk.MQTT
public
Task
UploadAttributeAsync
(
string
_devicename
,
object
obj
)
{
return
Client
.
PublishAsync
(
$"devices/
{
_devicename
}
/attributes"
,
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
obj
));
return
Client
.
PublishAsync
(
new
MqttApplicationMessageBuilder
().
WithTopic
(
$"devices/
{
_devicename
}
/attributes"
).
WithPayload
(
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
obj
)).
Build
(
));
}
public
Task
UploadTelemetryDataAsync
(
object
obj
)
=>
UploadTelemetryDataAsync
(
"me"
,
obj
);
public
Task
UploadTelemetryDataAsync
(
string
_devicename
,
object
obj
)
{
return
Client
.
PublishAsync
(
$"devices/
{
_devicename
}
/telemetry"
,
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
obj
));
return
Client
.
PublishAsync
(
new
MqttApplicationMessageBuilder
().
WithTopic
(
$"devices/
{
_devicename
}
/telemetry"
).
WithPayload
(
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
obj
)).
Build
(
));
}
public
Task
ResponseExecommand
(
RpcResponse
rpcResult
)
{
///IoTSharp/Clients/RpcClient.cs#L65 var responseTopic = $"/devices/{deviceid}/rpc/response/{methodName}/{rpcid}";
string
topic
=
$"/devices/
{
rpcResult
.
DeviceId
}
/rpc/response/
{
rpcResult
.
Method
.
ToString
()}
/
{
rpcResult
.
ResponseId
}
"
;
return
Client
.
PublishAsync
(
topic
,
rpcResult
.
Data
.
ToString
(),
MqttQualityOfServiceLevel
.
ExactlyOnce
);
return
Client
.
PublishAsync
(
new
MqttApplicationMessageBuilder
().
WithTopic
(
topic
).
WithPayload
(
rpcResult
.
Data
.
ToString
()).
WithQualityOfServiceLevel
(
MqttQualityOfServiceLevel
.
ExactlyOnce
).
Build
()
);
}
public
Task
RequestAttributes
(
params
string
[]
args
)
=>
RequestAttributes
(
"me"
,
false
,
args
);
public
Task
RequestAttributes
(
string
_device
,
params
string
[]
args
)
=>
RequestAttributes
(
_device
,
false
,
args
);
...
...
@@ -183,7 +179,7 @@ namespace IoTSharp.EdgeSdk.MQTT
Dictionary
<
string
,
string
>
keys
=
new
Dictionary
<
string
,
string
>();
keys
.
Add
(
anySide
?
"anySide"
:
"server"
,
string
.
Join
(
","
,
args
));
Client
.
SubscribeAsync
(
$"/devices/
{
_device
}
/attributes/response/
{
id
}
"
,
MqttQualityOfServiceLevel
.
ExactlyOnce
);
return
Client
.
PublishAsync
(
topic
,
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
keys
),
MqttQualityOfServiceLevel
.
ExactlyOnce
);
return
Client
.
Publish
String
Async
(
topic
,
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
keys
),
MqttQualityOfServiceLevel
.
ExactlyOnce
);
}
}
public
class
RpcRequest
...
...
IoTSharp/Extensions/MqttExtension.cs
浏览文件 @
d02e0d0d
...
...
@@ -17,7 +17,6 @@ using MQTTnet.Client.Options;
using
System.Diagnostics
;
using
System.Runtime.InteropServices
;
using
System.Threading
;
using
MQTTnet.AspNetCore.Extensions
;
using
MQTTnet.Diagnostics.Logger
;
using
System.Security.Cryptography.X509Certificates
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录