Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IoTSharp
IoTSharp
提交
175e52a2
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 搜索 >>
提交
175e52a2
编写于
5月 19, 2019
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
gateway can create device
上级
3e336e27
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
78 addition
and
16 deletion
+78
-16
IoTSharp/Controllers/DevicesController.cs
IoTSharp/Controllers/DevicesController.cs
+4
-11
IoTSharp/Dtos/DevicePutDto.cs
IoTSharp/Dtos/DevicePutDto.cs
+14
-0
IoTSharp/Extensions/DataExtension.cs
IoTSharp/Extensions/DataExtension.cs
+21
-3
IoTSharp/Extensions/DeviceExtension.cs
IoTSharp/Extensions/DeviceExtension.cs
+36
-0
IoTSharp/Handlers/MqttEventsHandler.cs
IoTSharp/Handlers/MqttEventsHandler.cs
+3
-2
未找到文件。
IoTSharp/Controllers/DevicesController.cs
浏览文件 @
175e52a2
...
...
@@ -136,7 +136,7 @@ namespace IoTSharp.Controllers
// PUT: api/Devices/5
[
Authorize
(
Roles
=
nameof
(
UserRole
.
CustomerAdmin
))]
[
HttpPut
(
"{id}"
)]
public
async
Task
<
IActionResult
>
PutDevice
(
Guid
id
,
Device
device
)
public
async
Task
<
IActionResult
>
PutDevice
(
Guid
id
,
Device
PutDto
device
)
{
if
(
id
!=
device
.
Id
)
{
...
...
@@ -145,7 +145,7 @@ namespace IoTSharp.Controllers
var
cid
=
User
.
Claims
.
First
(
c
=>
c
.
Type
==
IoTSharpClaimTypes
.
Customer
);
var
tid
=
User
.
Claims
.
First
(
c
=>
c
.
Type
==
IoTSharpClaimTypes
.
Tenant
);
var
dev
=
_context
.
Device
.
First
(
d
=>
d
.
Id
==
device
.
Id
);
var
dev
=
_context
.
Device
.
Include
(
d
=>
d
.
Tenant
).
Include
(
d
=>
d
.
Customer
).
First
(
d
=>
d
.
Id
==
device
.
Id
);
var
tenid
=
dev
.
Tenant
.
Id
;
var
cusid
=
dev
.
Customer
.
Id
;
...
...
@@ -157,9 +157,7 @@ namespace IoTSharp.Controllers
{
return
BadRequest
(
new
ApiResult
(
ApiCode
.
DoNotAllow
,
$"Do not allow access to devices from other customers or tenants"
));
}
_context
.
Entry
(
device
).
State
=
EntityState
.
Modified
;
dev
.
Name
=
device
.
Name
;
try
{
await
_context
.
SaveChangesAsync
();
...
...
@@ -199,12 +197,7 @@ namespace IoTSharp.Controllers
return
NotFound
(
new
ApiResult
<
Device
>(
ApiCode
.
NotFoundTenantOrCustomer
,
$"Not found Tenant or Customer "
,
device
));
}
_context
.
Device
.
Add
(
device
);
_context
.
DeviceIdentities
.
Add
(
new
DeviceIdentity
()
{
Device
=
device
,
IdentityType
=
IdentityType
.
AccessToken
,
IdentityId
=
Guid
.
NewGuid
().
ToString
().
Replace
(
"-"
,
""
)
});
_context
.
AfterCreateDevice
(
device
);
await
_context
.
SaveChangesAsync
();
return
await
GetDevice
(
device
.
Id
);
}
...
...
IoTSharp/Dtos/DevicePutDto.cs
0 → 100644
浏览文件 @
175e52a2
using
IoTSharp.Data
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
IoTSharp.Dtos
{
public
class
DevicePutDto
{
public
Guid
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
}
}
IoTSharp/Extensions/DataExtension.cs
浏览文件 @
175e52a2
...
...
@@ -20,7 +20,7 @@ namespace IoTSharp.Extensions
return
(
ok
,
devices
.
FirstOrDefault
());
}
/// <summary>
///
///
Save Data to Device's <typeparamref name="D"/> and <typeparamref name="L"/>
/// </summary>
/// <typeparam name="L">Latest</typeparam>
/// <typeparam name="D">Data</typeparam>
...
...
@@ -28,6 +28,24 @@ namespace IoTSharp.Extensions
/// <param name="device"></param>
/// <returns></returns>
internal
static
async
Task
<(
int
ret
,
Dic
exceptions
)>
SaveAsync
<
L
,
D
>(
this
ApplicationDbContext
_context
,
Dictionary
<
string
,
object
>
data
,
Device
device
,
DataSide
dataSide
)
where
L
:
DataStorage
,
new
()
where
D
:
DataStorage
,
new
()
{
Dic
exceptions
=
_context
.
PreparingData
<
L
,
D
>(
data
,
device
,
dataSide
);
int
ret
=
await
_context
.
SaveChangesAsync
();
return
(
ret
,
exceptions
);
}
/// <summary>
/// Preparing Data to Device's <typeparamref name="D"/> and <typeparamref name="L"/>
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="D"></typeparam>
/// <param name="_context"></param>
/// <param name="data"></param>
/// <param name="device"></param>
/// <param name="dataSide"></param>
/// <returns></returns>
internal
static
Dic
PreparingData
<
L
,
D
>(
this
ApplicationDbContext
_context
,
Dictionary
<
string
,
object
>
data
,
Device
device
,
DataSide
dataSide
)
where
L
:
DataStorage
,
new
()
where
D
:
DataStorage
,
new
()
{
Dic
exceptions
=
new
Dic
();
data
.
ToList
().
ForEach
(
kp
=>
...
...
@@ -59,9 +77,9 @@ namespace IoTSharp.Extensions
exceptions
.
Add
(
kp
.
Key
,
ex
.
Message
);
}
});
int
ret
=
await
_context
.
SaveChangesAsync
();
return
(
ret
,
exceptions
);
return
exceptions
;
}
public
static
object
JPropertyToObject
(
this
JProperty
property
)
{
object
obj
=
null
;
...
...
IoTSharp/Extensions/DeviceExtension.cs
0 → 100644
浏览文件 @
175e52a2
using
IoTSharp.Data
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
namespace
IoTSharp.Extensions
{
public
static
class
DeviceExtension
{
/// <summary>
/// When creating a device, all the things that need to be done here are done
/// </summary>
/// <param name="_context"></param>
/// <param name="device"></param>
public
static
void
AfterCreateDevice
(
this
ApplicationDbContext
_context
,
Device
device
)
{
if
(
device
.
Customer
==
null
||
device
.
Tenant
==
null
||
string
.
IsNullOrEmpty
(
device
.
Name
))
{
throw
new
Exception
(
"Customer or Tenant or Name is null or empty!"
);
}
else
{
_context
.
DeviceIdentities
.
Add
(
new
DeviceIdentity
()
{
Device
=
device
,
IdentityType
=
IdentityType
.
AccessToken
,
IdentityId
=
Guid
.
NewGuid
().
ToString
().
Replace
(
"-"
,
""
)
});
Dictionary
<
string
,
object
>
pairs
=
new
Dictionary
<
string
,
object
>();
pairs
.
Add
(
"CreateDateTime"
,
DateTime
.
Now
);
_context
.
PreparingData
<
AttributeLatest
,
AttributeData
>(
pairs
,
device
,
DataSide
.
ServerSide
);
}
}
}
}
IoTSharp/Handlers/MqttEventsHandler.cs
浏览文件 @
175e52a2
...
...
@@ -129,13 +129,14 @@ namespace IoTSharp.Handlers
Device
devicedatato
=
device
;
if
(
tpary
[
1
]
!=
"me"
&&
device
.
DeviceType
==
DeviceType
.
Gateway
)
{
var
ch
=
from
g
in
_dbContext
.
Gateway
.
Include
(
c
=>
c
.
Children
)
where
g
.
Id
==
device
.
Id
select
g
;
var
ch
=
from
g
in
_dbContext
.
Gateway
.
Include
(
g
=>
g
.
Tenant
).
Include
(
g
=>
g
.
Customer
).
Include
(
c
=>
c
.
Children
)
where
g
.
Id
==
device
.
Id
select
g
;
var
gw
=
ch
.
FirstOrDefault
();
var
subdev
=
from
cd
in
gw
.
Children
where
cd
.
Name
==
tpary
[
1
]
select
cd
;
if
(!
subdev
.
Any
())
{
devicedatato
=
new
Device
()
{
Id
=
Guid
.
NewGuid
(),
Name
=
tpary
[
1
],
DeviceType
=
DeviceType
.
Device
,
Tenant
=
device
.
Tenant
,
Customer
=
device
.
Customer
};
devicedatato
=
new
Device
()
{
Id
=
Guid
.
NewGuid
(),
Name
=
tpary
[
1
],
DeviceType
=
DeviceType
.
Device
,
Tenant
=
gw
.
Tenant
,
Customer
=
gw
.
Customer
,
Owner
=
gw
};
gw
.
Children
.
Add
(
devicedatato
);
_dbContext
.
AfterCreateDevice
(
devicedatato
);
_dbContext
.
SaveChangesAsync
();
}
else
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录