Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
0f8b9eb1
Commit
0f8b9eb1
authored
Feb 22, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(MQTTPublisher): небольшой рефакторинг
parent
938af6e6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
63 deletions
+59
-63
MQTTPublisher.cc
extensions/MQTTPublisher/MQTTPublisher.cc
+55
-52
MQTTPublisher.h
extensions/MQTTPublisher/MQTTPublisher.h
+4
-11
No files found.
extensions/MQTTPublisher/MQTTPublisher.cc
View file @
0f8b9eb1
...
...
@@ -143,36 +143,6 @@ void MQTTPublisher::sysCommand(const SystemMessage* sm)
}
}
//--------------------------------------------------------------------------------
string
MQTTPublisher
::
replace
(
const
std
::
string
&
text
,
MQTTPublisher
::
MQTTTextInfo
*
ti
,
MQTTPublisher
::
RangeInfo
*
ri
,
long
value
)
{
std
::
string
txt
(
text
);
ostringstream
v
;
v
<<
value
;
ostringstream
id
;
id
<<
ti
->
sid
;
ostringstream
r
;
r
<<
"["
<<
ri
->
rmin
<<
":"
<<
ri
->
rmax
<<
"]"
;
ostringstream
smin
;
smin
<<
ri
->
rmin
;
ostringstream
smax
;
smax
<<
ri
->
rmax
;
txt
=
replace_all
(
txt
,
"%v"
,
v
.
str
());
txt
=
replace_all
(
txt
,
"%n"
,
ti
->
xmlnode
.
getProp
(
"name"
));
txt
=
replace_all
(
txt
,
"%t"
,
ti
->
xmlnode
.
getProp
(
"textname"
));
txt
=
replace_all
(
txt
,
"%i"
,
id
.
str
());
txt
=
replace_all
(
txt
,
"%rmin"
,
smin
.
str
());
txt
=
replace_all
(
txt
,
"%rmax"
,
smax
.
str
());
txt
=
replace_all
(
txt
,
"%r"
,
r
.
str
());
return
std
::
move
(
txt
);
}
//--------------------------------------------------------------------------------
void
MQTTPublisher
::
help_print
(
int
argc
,
const
char
*
const
*
argv
)
{
cout
<<
" Default prefix='mqtt'"
<<
endl
;
...
...
@@ -306,27 +276,7 @@ void MQTTPublisher::sensorInfo( const UniSetTypes::SensorMessage* sm )
auto
t
=
textpublist
.
find
(
sm
->
id
);
if
(
t
!=
textpublist
.
end
()
)
{
auto
rlist
=
t
->
second
.
rlist
;
for
(
auto
&&
r
:
rlist
)
{
if
(
r
.
check
(
sm
->
value
)
)
{
string
tmsg
=
replace
(
r
.
text
,
&
(
t
->
second
),
&
r
,
sm
->
value
);
//subscribe(NULL, i.second.pubname.c_str());
myinfo
<<
"(sensorInfo): publish: topic='"
<<
t
->
second
.
pubname
<<
"' msg='"
<<
tmsg
<<
"'"
<<
endl
;
int
ret
=
publish
(
NULL
,
t
->
second
.
pubname
.
c_str
(),
tmsg
.
size
(),
tmsg
.
c_str
(),
1
,
false
);
if
(
ret
!=
MOSQ_ERR_SUCCESS
)
{
mycrit
<<
myname
<<
"(sensorInfo): PUBLISH FAILED: err("
<<
ret
<<
"): "
<<
mosqpp
::
strerror
(
ret
)
<<
endl
;
}
}
}
}
t
->
second
.
check
(
this
,
sm
->
value
,
mylog
,
myname
);
}
// -----------------------------------------------------------------------------
MQTTPublisher
::
MQTTTextInfo
::
MQTTTextInfo
(
const
string
&
rootsec
,
UniXML
::
iterator
s
,
UniXML
::
iterator
i
)
:
...
...
@@ -386,4 +336,57 @@ bool MQTTPublisher::RangeInfo::check( long val ) const
{
return
(
val
>=
rmin
&&
val
<=
rmax
);
}
// -----------------------------------------------------------------------------
//--------------------------------------------------------------------------------
void
MQTTPublisher
::
MQTTTextInfo
::
check
(
mosqpp
::
mosquittopp
*
serv
,
long
value
,
std
::
shared_ptr
<
DebugStream
>&
log
,
const
string
&
myname
)
{
for
(
auto
&&
r
:
rlist
)
{
if
(
r
.
check
(
value
)
)
{
string
tmsg
=
replace
(
&
r
,
value
);
//subscribe(NULL, i.second.pubname.c_str());
if
(
log
->
is_info
()
)
log
->
info
()
<<
myname
<<
"(check): publish: topic='"
<<
pubname
<<
"' msg='"
<<
tmsg
<<
"'"
<<
endl
;
int
ret
=
serv
->
publish
(
NULL
,
pubname
.
c_str
(),
tmsg
.
size
(),
tmsg
.
c_str
(),
1
,
false
);
if
(
ret
!=
MOSQ_ERR_SUCCESS
)
{
if
(
log
->
is_crit
()
)
log
->
crit
()
<<
myname
<<
"(check): PUBLISH FAILED: err("
<<
ret
<<
"): "
<<
mosqpp
::
strerror
(
ret
)
<<
endl
;
}
}
}
}
//--------------------------------------------------------------------------------
std
::
string
MQTTPublisher
::
MQTTTextInfo
::
replace
(
RangeInfo
*
ri
,
long
value
)
{
std
::
string
txt
(
ri
->
text
);
ostringstream
v
;
v
<<
value
;
ostringstream
id
;
id
<<
sid
;
ostringstream
r
;
r
<<
"["
<<
ri
->
rmin
<<
":"
<<
ri
->
rmax
<<
"]"
;
ostringstream
smin
;
smin
<<
ri
->
rmin
;
ostringstream
smax
;
smax
<<
ri
->
rmax
;
txt
=
replace_all
(
txt
,
"%v"
,
v
.
str
());
txt
=
replace_all
(
txt
,
"%n"
,
xmlnode
.
getProp
(
"name"
));
txt
=
replace_all
(
txt
,
"%t"
,
xmlnode
.
getProp
(
"textname"
));
txt
=
replace_all
(
txt
,
"%i"
,
id
.
str
());
txt
=
replace_all
(
txt
,
"%rmin"
,
smin
.
str
());
txt
=
replace_all
(
txt
,
"%rmax"
,
smax
.
str
());
txt
=
replace_all
(
txt
,
"%r"
,
r
.
str
());
return
std
::
move
(
txt
);
}
//--------------------------------------------------------------------------------
extensions/MQTTPublisher/MQTTPublisher.h
View file @
0f8b9eb1
...
...
@@ -100,15 +100,6 @@ class MQTTPublisher:
/*! глобальная функция для вывода help-а */
static
void
help_print
(
int
argc
,
const
char
*
const
*
argv
);
inline
std
::
shared_ptr
<
LogAgregator
>
getLogAggregator
()
{
return
loga
;
}
inline
std
::
shared_ptr
<
DebugStream
>
log
()
{
return
mylog
;
}
virtual
void
on_connect
(
int
rc
)
override
;
virtual
void
on_message
(
const
struct
mosquitto_message
*
message
)
override
;
virtual
void
on_subscribe
(
int
mid
,
int
qos_count
,
const
int
*
granted_qos
)
override
;
...
...
@@ -155,12 +146,14 @@ class MQTTPublisher:
// одиночные сообщения просто имитируются min=max=val
std
::
list
<
RangeInfo
>
rlist
;
// список сообщений..
void
check
(
mosqpp
::
mosquittopp
*
serv
,
long
value
,
std
::
shared_ptr
<
DebugStream
>&
log
,
const
std
::
string
&
myname
);
std
::
string
replace
(
RangeInfo
*
ri
,
long
value
);
};
typedef
std
::
unordered_map
<
UniSetTypes
::
ObjectId
,
MQTTTextInfo
>
MQTTTextMap
;
std
::
string
replace
(
const
std
::
string
&
text
,
MQTTTextInfo
*
ti
,
RangeInfo
*
r
,
long
value
);
MQTTMap
publist
;
MQTTTextMap
textpublist
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment