Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
f233da7d
提交
f233da7d
authored
9月 19, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
big refacting of communitation architect
上级
88905dc6
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
205 行增加
和
0 行删除
+205
-0
utils.cpp
opencv-motion-detect/inc/utils.cpp
+205
-0
没有找到文件。
opencv-motion-detect/inc/utils.cpp
0 → 100644
浏览文件 @
f233da7d
#include "utils.hpp"
// cloudutils
namespace
cloudutils
{
/// [deprecated] ref: ../config.json
json
registry
(
json
&
conf
,
string
sn
,
string
module
)
{
json
ret
;
string
api
;
try
{
api
=
conf
.
at
(
"data"
).
at
(
sn
).
at
(
"api-cloud"
).
get
<
string
>
()
+
"/register"
;
Uri
uri
=
Uri
::
Parse
(
api
);
if
(
uri
.
Host
.
empty
()
||
uri
.
Port
.
empty
()
||
uri
.
Protocol
.
find
(
"http"
)
==
string
::
npos
||
uri
.
Path
.
empty
())
{
string
msg
=
"registry error. invalid api-cloud in config: "
+
api
;
ret
[
"code"
]
=
1
;
ret
[
"msg"
]
=
msg
;
spdlog
::
error
(
msg
);
return
ret
;
}
Params
params
;
params
.
emplace
(
"sn"
,
sn
);
params
.
emplace
(
"module"
,
module
);
Client
cli
(
uri
.
Host
.
c_str
(),
stoi
(
uri
.
Port
));
auto
res
=
cli
.
Post
(
"/register"
,
Headers
(),
params
,
conf
.
dump
(),
"text/json"
);
spdlog
::
debug
(
"{} {} registry res from cloud : {}"
,
__FILE__
,
__LINE__
,
res
->
body
);
ret
=
json
::
parse
(
res
->
body
);
}
catch
(
exception
&
e
)
{
ret
[
"code"
]
=
-
1
;
string
msg
=
string
(
__FILE__
)
+
":"
+
to_string
(
__LINE__
)
+
string
(
": registry exception - "
)
+
e
.
what
();
ret
[
"msg"
]
=
msg
;
spdlog
::
error
(
msg
);
}
// /Client cli;
return
ret
;
}
/// req config
json
reqConfig
(
json
&
info
){
json
ret
;
string
api
;
try
{
api
=
info
.
at
(
"api-cloud"
).
get
<
string
>
();
Uri
uri
=
Uri
::
Parse
(
api
);
string
sn
=
info
.
at
(
"sn"
).
get
<
string
>
();
if
(
uri
.
Host
.
empty
()
||
uri
.
Port
.
empty
()
||
uri
.
Protocol
.
find
(
"http"
)
==
string
::
npos
)
{
string
msg
=
string
(
__FILE__
)
+
":"
+
to_string
(
__LINE__
)
+
": request cloud configuration error. invalid api-cloud in info: "
+
api
;
ret
[
"code"
]
=
EVCLOUD_REQ_E_PARAM
;
ret
[
"msg"
]
=
msg
;
spdlog
::
error
(
msg
);
return
ret
;
}
Params
params
;
params
.
emplace
(
"sn"
,
sn
);
Client
cli
(
uri
.
Host
.
c_str
(),
stoi
(
uri
.
Port
));
auto
res
=
cli
.
Get
(
"/config"
,
Headers
(),
params
);
if
(
res
==
nullptr
||
res
->
status
!=
200
)
{
const
char
*
msg
=
NULL
;
if
(
res
==
nullptr
)
{
msg
=
(
string
(
"error to connect to server: "
)
+
api
+
"/config"
).
c_str
();
ret
[
"code"
]
=
EVCLOUD_REQ_E_CONN
;
}
else
{
msg
=
httplib
::
detail
::
status_message
(
res
->
status
);
ret
[
"code"
]
=
res
->
status
;
}
spdlog
::
debug
(
"failed to reqConfig. {}"
,
msg
);
ret
[
"msg"
]
=
msg
;
}
else
{
spdlog
::
debug
(
"{} {} registry res from cloud : {}"
,
__FILE__
,
__LINE__
,
res
->
body
);
ret
=
json
::
parse
(
res
->
body
);
}
}
catch
(
exception
&
e
)
{
ret
[
"code"
]
=
EVCLOUD_REQ_E_DATA
;
string
msg
=
string
(
__FILE__
)
+
":"
+
to_string
(
__LINE__
)
+
string
(
": registry exception - "
)
+
e
.
what
();
ret
[
"msg"
]
=
msg
;
spdlog
::
error
(
msg
);
}
// /Client cli;
return
ret
;
}
}
// namespace cloudutils
///
namespace
strutils
{
vector
<
string
>
split
(
const
std
::
string
&
s
,
char
delimiter
)
{
std
::
vector
<
std
::
string
>
tokens
;
std
::
string
token
;
std
::
istringstream
tokenStream
(
s
);
while
(
getline
(
tokenStream
,
token
,
delimiter
))
{
tokens
.
push_back
(
token
);
}
return
tokens
;
}
}
//namespace strutils
namespace
cfgutils
{
int
getPeerId
(
string
modName
,
json
&
modElem
,
string
&
peerId
,
string
&
peerName
)
{
try
{
if
(
modName
==
"evmgr"
)
{
peerId
=
modElem
[
"sn"
].
get
<
string
>
()
+
":evmgr:0"
;
peerName
=
modName
;
}
else
if
(
modName
==
"evml"
)
{
peerId
=
modElem
[
"sn"
].
get
<
string
>
()
+
":evml"
+
modElem
[
"type"
].
get
<
string
>
()
+
":"
+
to_string
(
modElem
[
"iid"
]);
peerName
=
modName
+
modElem
[
"type"
].
get
<
string
>
();
}
else
{
peerId
=
modElem
[
"sn"
].
get
<
string
>
()
+
":"
+
modName
+
":"
+
to_string
(
modElem
[
"iid"
]);
peerName
=
modName
;
}
}
catch
(
exception
&
e
)
{
spdlog
::
error
(
"failed to get gid for {} in {}: {}"
,
modName
,
modElem
.
dump
(),
e
.
what
());
return
-
1
;
}
return
0
;
}
json
*
findModuleConfig
(
string
peerId
,
json
&
data
)
{
json
*
ret
=
NULL
;
auto
pp
=
strutils
::
split
(
peerId
,
':'
);
if
(
pp
.
size
()
!=
3
)
{
spdlog
::
error
(
"invalid peerId: {}"
,
peerId
);
return
ret
;
}
string
sn
=
pp
[
0
];
string
modName
=
pp
[
1
];
string
iid
=
pp
[
2
];
//
string
subMn
=
modName
.
substr
(
0
,
4
);
if
(
subMn
==
"evml"
)
{
subMn
=
modName
.
substr
(
4
,
modName
.
size
());
}
else
{
subMn
=
""
;
}
try
{
for
(
auto
&
[
k
,
v
]
:
data
.
items
())
{
// it's evmgr
if
(
modName
==
"evmgr"
)
{
if
(
k
==
sn
)
{
ret
=
&
v
;
break
;
}
}
else
{
json
&
ipcs
=
v
[
"ipcs"
];
for
(
auto
&
ipc
:
ipcs
)
{
json
&
modules
=
ipc
[
"modules"
];
// not evml
if
(
subMn
.
empty
())
{
if
(
modules
.
count
(
modName
)
!=
0
)
{
json
&
ml
=
modules
[
modName
];
for
(
auto
&
m
:
ml
)
{
if
(
m
[
"sn"
]
==
sn
&&
to_string
(
m
[
"iid"
])
==
iid
&&
m
[
"enabled"
]
!=
0
)
{
ret
=
&
v
;
break
;
}
}
}
}
else
{
if
(
modules
.
count
(
"evml"
)
!=
0
)
{
json
&
ml
=
modules
[
"evml"
];
for
(
auto
&
m
:
ml
)
{
if
(
subMn
==
m
[
"type"
]
&&
to_string
(
m
[
iid
])
==
iid
&&
m
[
"sn"
]
==
sn
&&
m
[
"enabled"
]
!=
0
)
{
ret
=
&
v
;
break
;
}
}
}
}
if
(
ret
!=
NULL
)
break
;
}
}
}
}
catch
(
exception
&
e
)
{
spdlog
::
error
(
"find module {} in {} exception: {}"
,
peerId
,
data
.
dump
(),
e
.
what
());
return
NULL
;
}
return
ret
;
}
}
// cfgutils
struct
StrException
:
public
std
::
exception
{
std
::
string
s
;
StrException
(
std
::
string
ss
)
:
s
(
ss
)
{}
~
StrException
()
throw
()
{}
// Updated
const
char
*
what
()
const
throw
()
{
return
s
.
c_str
();
}
};
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论