Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
d15f4074
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 搜索 >>
提交
d15f4074
编写于
10月 12, 2020
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化存储
上级
a6251443
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
32 addition
and
24 deletion
+32
-24
IoTSharp/Handlers/EventBusHandler.cs
IoTSharp/Handlers/EventBusHandler.cs
+13
-10
IoTSharp/Storage/EFStorage.cs
IoTSharp/Storage/EFStorage.cs
+19
-14
未找到文件。
IoTSharp/Handlers/EventBusHandler.cs
浏览文件 @
d15f4074
...
@@ -33,7 +33,7 @@ namespace IoTSharp.Handlers
...
@@ -33,7 +33,7 @@ namespace IoTSharp.Handlers
{
{
private
readonly
AppSettings
_appSettings
;
private
readonly
AppSettings
_appSettings
;
readonly
ILogger
_logger
;
readonly
ILogger
_logger
;
readonly
IServiceScope
scope
;
private
readonly
IServiceScopeFactory
_scopeFactor
;
private
readonly
IStorage
_storage
;
private
readonly
IStorage
_storage
;
public
EventBusHandler
(
ILogger
<
EventBusHandler
>
logger
,
IServiceScopeFactory
scopeFactor
public
EventBusHandler
(
ILogger
<
EventBusHandler
>
logger
,
IServiceScopeFactory
scopeFactor
...
@@ -42,7 +42,7 @@ namespace IoTSharp.Handlers
...
@@ -42,7 +42,7 @@ namespace IoTSharp.Handlers
{
{
_appSettings
=
options
.
Value
;
_appSettings
=
options
.
Value
;
_logger
=
logger
;
_logger
=
logger
;
scope
=
scopeFactor
.
CreateScope
()
;
_scopeFactor
=
scopeFactor
;
_storage
=
storage
;
_storage
=
storage
;
}
}
[
CapSubscribe
(
"iotsharp.services.datastream.attributedata"
)]
[
CapSubscribe
(
"iotsharp.services.datastream.attributedata"
)]
...
@@ -50,17 +50,20 @@ namespace IoTSharp.Handlers
...
@@ -50,17 +50,20 @@ namespace IoTSharp.Handlers
{
{
Task
.
Run
(
async
()
=>
Task
.
Run
(
async
()
=>
{
{
using
(
var
_
dbContext
=
scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>
())
using
(
var
_
scope
=
_scopeFactor
.
CreateScope
())
{
{
var
device
=
_dbContext
.
Device
.
FirstOrDefault
(
d
=>
d
.
Id
==
msg
.
DeviceId
);
using
(
var
_dbContext
=
_scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>())
if
(
device
!=
null
)
{
{
var
result2
=
await
_dbContext
.
SaveAsync
<
AttributeLatest
>(
msg
.
MsgBody
,
device
.
Id
,
msg
.
DataSide
);
var
device
=
_dbContext
.
Device
.
FirstOrDefault
(
d
=>
d
.
Id
==
msg
.
DeviceId
);
result2
.
exceptions
?.
ToList
().
ForEach
(
ex
=>
if
(
device
!=
null
)
{
{
_logger
.
LogError
(
$"
{
ex
.
Key
}
{
ex
.
Value
}
{
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
msg
.
MsgBody
[
ex
.
Key
])}
"
);
var
result2
=
await
_dbContext
.
SaveAsync
<
AttributeLatest
>(
msg
.
MsgBody
,
device
.
Id
,
msg
.
DataSide
);
});
result2
.
exceptions
?.
ToList
().
ForEach
(
ex
=>
_logger
.
LogInformation
(
$"更新
{
device
.
Name
}
(
{
device
.
Id
}
)属性数据结果
{
result2
.
ret
}
"
);
{
_logger
.
LogError
(
$"
{
ex
.
Key
}
{
ex
.
Value
}
{
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
msg
.
MsgBody
[
ex
.
Key
])}
"
);
});
_logger
.
LogInformation
(
$"更新
{
device
.
Name
}
(
{
device
.
Id
}
)属性数据结果
{
result2
.
ret
}
"
);
}
}
}
}
}
});
});
...
...
IoTSharp/Storage/EFStorage.cs
浏览文件 @
d15f4074
...
@@ -18,6 +18,7 @@ namespace IoTSharp.Storage
...
@@ -18,6 +18,7 @@ namespace IoTSharp.Storage
{
{
private
readonly
AppSettings
_appSettings
;
private
readonly
AppSettings
_appSettings
;
readonly
ILogger
_logger
;
readonly
ILogger
_logger
;
private
readonly
IServiceScopeFactory
_scopeFactor
;
readonly
IServiceScope
scope
;
readonly
IServiceScope
scope
;
private
readonly
ApplicationDbContext
_context
;
private
readonly
ApplicationDbContext
_context
;
...
@@ -27,6 +28,7 @@ namespace IoTSharp.Storage
...
@@ -27,6 +28,7 @@ namespace IoTSharp.Storage
{
{
_appSettings
=
options
.
Value
;
_appSettings
=
options
.
Value
;
_logger
=
logger
;
_logger
=
logger
;
_scopeFactor
=
scopeFactor
;
scope
=
scopeFactor
.
CreateScope
();
scope
=
scopeFactor
.
CreateScope
();
_context
=
scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>();
_context
=
scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>();
}
}
...
@@ -86,23 +88,26 @@ namespace IoTSharp.Storage
...
@@ -86,23 +88,26 @@ namespace IoTSharp.Storage
bool
result
=
false
;
bool
result
=
false
;
try
try
{
{
using
(
var
_
dbContext
=
scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>
())
using
(
var
_
scope
=
_scopeFactor
.
CreateScope
())
{
{
msg
.
MsgBody
.
ToList
().
ForEach
(
kp
=>
using
(
var
_dbContext
=
scope
.
ServiceProvider
.
GetRequiredService
<
ApplicationDbContext
>())
{
{
if
(
kp
.
Value
!=
null
)
msg
.
MsgBody
.
ToList
().
ForEach
(
kp
=>
{
{
var
tdata
=
new
TelemetryData
()
{
DateTime
=
DateTime
.
Now
,
DeviceId
=
msg
.
DeviceId
,
KeyName
=
kp
.
Key
,
Value_DateTime
=
new
DateTime
(
1970
,
1
,
1
)
};
if
(
kp
.
Value
!=
null
)
tdata
.
FillKVToMe
(
kp
);
{
_dbContext
.
Set
<
TelemetryData
>().
Add
(
tdata
);
var
tdata
=
new
TelemetryData
()
{
DateTime
=
DateTime
.
Now
,
DeviceId
=
msg
.
DeviceId
,
KeyName
=
kp
.
Key
,
Value_DateTime
=
new
DateTime
(
1970
,
1
,
1
)
};
}
tdata
.
FillKVToMe
(
kp
);
});
_dbContext
.
Set
<
TelemetryData
>().
Add
(
tdata
);
var
result1
=
await
_dbContext
.
SaveAsync
<
TelemetryLatest
>(
msg
.
MsgBody
,
msg
.
DeviceId
,
msg
.
DataSide
);
}
result1
.
exceptions
?.
ToList
().
ForEach
(
ex
=>
});
{
var
result1
=
await
_dbContext
.
SaveAsync
<
TelemetryLatest
>(
msg
.
MsgBody
,
msg
.
DeviceId
,
msg
.
DataSide
);
_logger
.
LogError
(
$"
{
ex
.
Key
}
{
ex
.
Value
}
{
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
msg
.
MsgBody
[
ex
.
Key
])}
"
);
result1
.
exceptions
?.
ToList
().
ForEach
(
ex
=>
});
{
_logger
.
LogInformation
(
$"新增(
{
msg
.
DeviceId
}
)遥测数据更新最新信息
{
result1
.
ret
}
"
);
_logger
.
LogError
(
$"
{
ex
.
Key
}
{
ex
.
Value
}
{
Newtonsoft
.
Json
.
JsonConvert
.
SerializeObject
(
msg
.
MsgBody
[
ex
.
Key
])}
"
);
});
_logger
.
LogInformation
(
$"新增(
{
msg
.
DeviceId
}
)遥测数据更新最新信息
{
result1
.
ret
}
"
);
}
}
}
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录