Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
fa44297e
IoTSharp
项目概览
IoTSharp
/
IoTSharp
8 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
fa44297e
编写于
6月 30, 2019
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove mqtt client service
上级
ca0e4d5e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
0 addition
and
89 deletion
+0
-89
IoTSharp/Extensions/MqttExtension.cs
IoTSharp/Extensions/MqttExtension.cs
+0
-3
IoTSharp/Services/MqttClientService.cs
IoTSharp/Services/MqttClientService.cs
+0
-86
未找到文件。
IoTSharp/Extensions/MqttExtension.cs
浏览文件 @
fa44297e
...
...
@@ -101,15 +101,12 @@ namespace IoTSharp
public
static
void
AddMqttClient
(
this
IServiceCollection
services
,
MqttClientSetting
setting
)
{
services
.
AddSingleton
(
options
=>
new
MQTTnet
.
MqttFactory
().
CreateMqttClient
());
services
.
AddTransient
(
options
=>
new
MqttClientOptionsBuilder
()
.
WithClientId
(
setting
.
MqttBroker
)
.
WithTcpServer
((
setting
.
MqttBroker
==
"built-in"
||
string
.
IsNullOrEmpty
(
setting
.
MqttBroker
))
?
"127.0.0.1"
:
setting
.
MqttBroker
,
setting
.
Port
)
.
WithCredentials
(
setting
.
UserName
,
setting
.
Password
)
.
WithCleanSession
()
//.WithProtocolVersion (MQTTnet.Formatter.MqttProtocolVersion.V500)
.
Build
());
services
.
AddHostedService
<
MqttClientService
>();
}
public
static
void
UseIotSharpSelfCollecting
(
this
IApplicationBuilder
app
)
...
...
IoTSharp/Services/MqttClientService.cs
已删除
100644 → 0
浏览文件 @
ca0e4d5e
using
IoTSharp.Data
;
using
IoTSharp.Handlers
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Hosting
;
using
Microsoft.Extensions.Logging
;
using
MQTTnet.Client
;
using
MQTTnet.Client.Connecting
;
using
MQTTnet.Client.Disconnecting
;
using
MQTTnet.Client.Options
;
using
MQTTnet.Client.Receiving
;
using
System
;
using
System.Collections.Generic
;
using
System.Threading
;
using
System.Threading.Tasks
;
namespace
IoTSharp.Services
{
public
class
MqttClientService
:
IHostedService
{
private
readonly
ILogger
_logger
;
private
readonly
IMqttClient
_mqtt
;
private
readonly
IMqttClientOptions
_clientOptions
;
private
ApplicationDbContext
_dbContext
;
private
IServiceScope
_serviceScope
;
public
MqttClientService
(
ILogger
<
MqttClientService
>
logger
,
IServiceScopeFactory
scopeFactor
,
IMqttClient
mqtt
,
IMqttClientOptions
clientOptions
)
{
_logger
=
logger
;
_mqtt
=
mqtt
;
_clientOptions
=
clientOptions
;
mqtt
.
ApplicationMessageReceivedHandler
=
new
MqttApplicationMessageReceivedHandlerDelegate
(
args
=>
Mqtt_ApplicationMessageReceived
(
mqtt
,
args
));
mqtt
.
ConnectedHandler
=
new
MqttClientConnectedHandlerDelegate
(
args
=>
Mqtt_ConnectedAsync
(
mqtt
,
args
));
mqtt
.
DisconnectedHandler
=
new
MqttClientDisconnectedHandlerDelegate
(
args
=>
Mqtt_DisconnectedAsync
(
mqtt
,
args
));
_serviceScope
=
scopeFactor
.
CreateScope
();
_dbContext
=
_serviceScope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>();
}
private
async
void
Mqtt_DisconnectedAsync
(
object
sender
,
MqttClientDisconnectedEventArgs
e
)
{
_logger
.
LogInformation
(
$"DISCONNECTED FROM SERVER ClientWasConnected:
{
e
.
ClientWasConnected
}
, Exception=
{
e
.
Exception
?.
Message
}
"
);
try
{
await
_mqtt
.
ConnectAsync
(
_clientOptions
);
_logger
.
LogInformation
(
"RECONNECT AGAIN"
);
}
catch
(
Exception
exception
)
{
_logger
.
LogError
(
"CONNECTING FAILED"
,
exception
);
}
}
private
void
Mqtt_ConnectedAsync
(
object
sender
,
MqttClientConnectedEventArgs
e
)
{
_logger
.
LogInformation
(
$"CONNECTED IsSessionPresent:
{
e
.
AuthenticateResult
.
IsSessionPresent
}
ResultCode:
{
e
.
AuthenticateResult
.
ResultCode
}
"
);
}
Dictionary
<
string
,
Device
>
Devices
=>
MQTTServerHandler
.
Devices
;
private
void
Mqtt_ApplicationMessageReceived
(
object
sender
,
MQTTnet
.
MqttApplicationMessageReceivedEventArgs
e
)
{
_logger
.
LogInformation
(
$"Received :
{
e
.
ApplicationMessage
.
Topic
}
"
);
}
public
Task
StartAsync
(
CancellationToken
cancellationToken
)
{
return
Task
.
Run
(()
=>
{
try
{
_mqtt
.
ConnectAsync
(
_clientOptions
);
_logger
.
LogInformation
(
"CONNECTED"
);
}
catch
(
Exception
exception
)
{
_logger
.
LogError
(
"CONNECTING FAILED"
,
exception
);
}
});
}
public
Task
StopAsync
(
CancellationToken
cancellationToken
)
{
_mqtt
.
DisconnectedHandler
=
null
;
return
_mqtt
.
DisconnectAsync
();
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录