Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
7d820139
IoTSharp
项目概览
IoTSharp
/
IoTSharp
9 个月 前同步成功
通知
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 搜索 >>
提交
7d820139
编写于
12月 23, 2021
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
调整会话处理
上级
a3fc2d9d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
55 addition
and
22 deletion
+55
-22
IoTSharp/Extensions/EventBusExtensions.cs
IoTSharp/Extensions/EventBusExtensions.cs
+1
-1
IoTSharp/Handlers/EventBusHandler.cs
IoTSharp/Handlers/EventBusHandler.cs
+31
-16
IoTSharp/Handlers/MQTTServerHandler.cs
IoTSharp/Handlers/MQTTServerHandler.cs
+23
-5
未找到文件。
IoTSharp/Extensions/EventBusExtensions.cs
浏览文件 @
7d820139
...
...
@@ -21,7 +21,7 @@ namespace IoTSharp.Extensions
}
public
static
void
PublishDeviceStatus
(
this
ICapPublisher
cap
,
Guid
devid
,
bool
status
)
{
cap
.
Publish
(
"iotsharp.services.datastream.devicestatus"
,
new
{
Device
=
devid
,
Status
=
status
}
);
cap
.
Publish
(
"iotsharp.services.datastream.devicestatus"
,
new
DeviceStatus
{
DeviceId
=
devid
,
Status
=
status
}
);
}
}
}
IoTSharp/Handlers/EventBusHandler.cs
浏览文件 @
7d820139
...
...
@@ -128,28 +128,43 @@ namespace IoTSharp.Handlers
[
CapSubscribe
(
"iotsharp.services.datastream.devicestatus"
)]
public
void
DeviceStatus
(
DeviceStatus
status
)
{
using
(
var
_scope
=
_scopeFactor
.
CreateScope
())
try
{
using
(
var
_
dbContext
=
_scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>
())
using
(
var
_
scope
=
_scopeFactor
.
CreateScope
())
{
var
dev
=
_dbContext
.
Device
.
Find
(
status
.
DeviceId
);
if
(
dev
.
Online
==
true
&&
status
.
Status
==
false
)
{
dev
.
Online
=
false
;
dev
.
LastActive
=
DateTime
.
Now
;
Task
.
Run
(()=>
RunRules
(
dev
.
Id
,
status
,
MountType
.
Online
));
//真正掉线
}
else
if
(
dev
.
Online
==
false
&&
status
.
Status
==
true
)
using
(
var
_dbContext
=
_scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>())
{
dev
.
Online
=
true
;
dev
.
LastActive
=
DateTime
.
Now
;
Task
.
Run
(()
=>
RunRules
(
dev
.
Id
,
status
,
MountType
.
Offline
));
//真正离线
var
dev
=
_dbContext
.
Device
.
FirstOrDefault
(
d
=>
d
.
Id
==
status
.
DeviceId
);
if
(
dev
!=
null
)
{
if
(
dev
.
Online
==
true
&&
status
.
Status
==
false
)
{
dev
.
Online
=
false
;
dev
.
LastActive
=
DateTime
.
Now
;
Task
.
Run
(()
=>
RunRules
(
dev
.
Id
,
status
,
MountType
.
Online
));
//真正掉线
}
else
if
(
dev
.
Online
==
false
&&
status
.
Status
==
true
)
{
dev
.
Online
=
true
;
dev
.
LastActive
=
DateTime
.
Now
;
Task
.
Run
(()
=>
RunRules
(
dev
.
Id
,
status
,
MountType
.
Offline
));
//真正离线
}
_dbContext
.
SaveChanges
();
}
else
{
_logger
.
LogWarning
(
$"未找到设备
{
status
.
DeviceId
}
,因此无法处理设备状态"
);
}
}
_dbContext
.
SaveChanges
();
}
}
catch
(
Exception
ex
)
{
_logger
.
LogError
(
ex
,
$"处理
{
status
.
DeviceId
}
的状态
{
status
.
Status
}
时遇到异常:
{
ex
.
Message
}
"
);
}
}
...
...
IoTSharp/Handlers/MQTTServerHandler.cs
浏览文件 @
7d820139
...
...
@@ -84,6 +84,7 @@ namespace IoTSharp.Handlers
string
topic
=
e
.
ApplicationMessage
.
Topic
.
ToLower
();
var
tpary
=
topic
.
Split
(
'/'
,
StringSplitOptions
.
RemoveEmptyEntries
);
var
_dev
=
await
FoundDevice
(
e
.
ClientId
);
if
(
tpary
.
Length
>=
3
&&
tpary
[
0
]
==
"devices"
&&
_dev
!=
null
)
{
Device
device
=
JudgeOrCreateNewDevice
(
tpary
,
_dev
);
...
...
@@ -166,7 +167,8 @@ namespace IoTSharp.Handlers
}
else
{
_logger
.
LogWarning
(
$"不支持
{
e
.
ClientId
}
的
{
e
.
ApplicationMessage
.
Topic
}
格式"
);
//tpary.Length >= 3 && tpary[0] == "devices" && _dev != null
_logger
.
LogWarning
(
$"不支持
{
e
.
ClientId
}
的
{
e
.
ApplicationMessage
.
Topic
}
格式,Length:
{
tpary
.
Length
}
,
{
tpary
[
0
]
}
,
{
_dev
!=
null
}
"
);
}
}
catch
(
Exception
ex
)
...
...
@@ -180,10 +182,12 @@ namespace IoTSharp.Handlers
private
void
ResetDeviceStatus
(
Device
device
,
bool
status
=
true
)
{
if
(
device
.
DeviceType
==
DeviceType
.
Device
&&
device
.
Owner
!=
null
&&
device
.
Owner
?.
Id
!=
null
)
//虚拟设备上线
_logger
.
LogInformation
(
$"重置状态
{
device
.
Id
}
{
device
.
Name
}
"
);
if
(
device
.
DeviceType
==
DeviceType
.
Device
&&
device
.
Owner
!=
null
&&
device
.
Owner
?.
Id
!=
null
&&
device
.
Owner
?.
Id
!=
Guid
.
Empty
)
//虚拟设备上线
{
_queue
.
PublishDeviceStatus
(
device
.
Id
,
status
);
_queue
.
PublishDeviceStatus
(
device
.
Owner
.
Id
,
status
);
_logger
.
LogInformation
(
$"重置网关状态
{
device
.
Owner
.
Id
}
{
device
.
Owner
.
Name
}
"
);
}
else
{
...
...
@@ -244,9 +248,23 @@ namespace IoTSharp.Handlers
private
async
Task
<
Device
>
FoundDevice
(
string
clientid
)
{
var
ss
=
await
_serverEx
.
GetSessionStatusAsync
();
var
_device
=
ss
.
FirstOrDefault
(
s
=>
s
.
ClientId
==
clientid
)?.
Items
?.
FirstOrDefault
(
k
=>
(
string
)
k
.
Key
==
nameof
(
Device
)).
Value
as
Device
;
return
_device
;
Device
device
=
null
;
var
clients
=
await
_serverEx
.
GetClientStatusAsync
();
var
client
=
clients
.
FirstOrDefault
(
c
=>
c
.
ClientId
==
clientid
);
if
(
client
!=
null
)
{
device
=
client
.
Session
?.
Items
?.
FirstOrDefault
(
k
=>
(
string
)
k
.
Key
==
nameof
(
Device
)).
Value
as
Device
;
if
(
device
==
null
)
{
_logger
.
LogWarning
(
$"未能找到客户端
{
clientid
}
回话附加的设备信息,现在断开此链接。 "
);
await
client
.
DisconnectAsync
();
}
}
else
{
_logger
.
LogWarning
(
$"未能找到客户端
{
clientid
}
上下文信息"
);
}
return
device
;
}
private
async
Task
RequestAttributes
(
string
[]
tpary
,
Dictionary
<
string
,
object
>
keyValues
,
Device
device
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录