-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_client.cc
123 lines (106 loc) · 3.65 KB
/
power_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "power_client.h"
PowerClient::PowerClient(EventLoop* loop,
const InetAddress& serverAddr)
: loop_(loop),
client_(loop, serverAddr, "PowerClient"),
dispatcher_(std::bind(&PowerClient::onUnknownMessage, this, _1, _2, _3)),
codec_(std::bind(&ProtobufDispatcher::onProtobufMessage, &dispatcher_, _1, _2, _3))
{
dispatcher_.registerMessageCallback<eh2tech::Setting>(
std::bind(&PowerClient::onSetting, this, _1, _2, _3));
dispatcher_.registerMessageCallback<eh2tech::Answer>(
std::bind(&PowerClient::onAnswer, this, _1, _2, _3));
dispatcher_.registerMessageCallback<eh2tech::Empty>(
std::bind(&PowerClient::onEmpty, this, _1, _2, _3));
client_.setConnectionCallback(
std::bind(&PowerClient::onConnection, this, _1));
client_.setMessageCallback(
std::bind(&ProtobufCodec::onMessage, &codec_, _1, _2, _3));
setting_.set_id(1);
eh2tech::Setting::CatFreq* catfreq;
catfreq=setting_.add_catfreq(); catfreq->set_cat(eh2tech::Catalog::TEMP); catfreq->set_freq(15);
catfreq=setting_.add_catfreq(); catfreq->set_cat(eh2tech::Catalog::POWER); catfreq->set_freq(3);
catfreq=setting_.add_catfreq(); catfreq->set_cat(eh2tech::Catalog::PRESS); catfreq->set_freq(9);
catfreq=setting_.add_catfreq(); catfreq->set_cat(eh2tech::Catalog::FLOW); catfreq->set_freq(5);
}
void PowerClient::connect()
{
client_.connect();
}
void PowerClient::onConnection(const TcpConnectionPtr& conn)
{
LOG_INFO << conn->localAddress().toIpPort() << " -> "
<< conn->peerAddress().toIpPort() << " is "
<< (conn->connected() ? "UP" : "DOWN");
conn_ = conn;
loop_->runEvery(1, std::bind(&PowerClient::submitMessage,this));
eh2tech::Login login;
login.set_sn("M100-20170630");
login.set_id(1);
codec_.send(conn_, login);
}
void PowerClient::onSetting(const TcpConnectionPtr&,
const MessagePtr& message,
Timestamp)
{
LOG_INFO << "onSetting: " << message->GetTypeName();
}
void PowerClient::onUnknownMessage(const TcpConnectionPtr&,
const MessagePtr& message,
Timestamp)
{
LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
}
void PowerClient::onAnswer(const muduo::net::TcpConnectionPtr&,
const AnswerPtr& message,
muduo::Timestamp)
{
LOG_INFO << "onAnswer:\n" << message->GetTypeName() << message->DebugString();
}
void PowerClient::onEmpty(const muduo::net::TcpConnectionPtr&,
const EmptyPtr& message,
muduo::Timestamp)
{
LOG_INFO << "onEmpty: " << message->GetTypeName();
}
void PowerClient::submitMessage()
{
// prepare for fake data
data_.Clear();
data_.set_id(1);
data_.set_sn("M100-20170630");
data_.set_cat(eh2tech::Catalog::POWER);
data_.add_data(240);
data_.add_data(100);
data_.add_data(530);
data_.add_data(40);
if(!conn_->connected()) loop_->quit();
static int count=0;
count++;
for(int i=0; i<setting_.catfreq_size(); ++i) {
data_.set_cat(eh2tech::Catalog(i));
eh2tech::Setting_CatFreq catfreq= setting_.catfreq(i);
int freq = catfreq.freq();
if(count%freq==0){
codec_.send(conn_, data_);
LOG_INFO << "QueryAnswer sent!"<< "cat="<<catfreq.cat();
}
}
}
int main(int argc, char* argv[])
{
LOG_INFO << "pid = " << getpid();
if (argc > 2)
{
EventLoop loop;
uint16_t port = static_cast<uint16_t>(atoi(argv[2]));
InetAddress serverAddr(argv[1], port);
PowerClient client(&loop, serverAddr);
client.connect();
loop.loop();
}
else
{
printf("Usage: %s host_ip port [q|e]\n", argv[0]);
}
}