-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZenohShapeDynamics.cpp
179 lines (143 loc) · 5.56 KB
/
ZenohShapeDynamics.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file
*/
#include "ZenohShapeDynamics.hpp"
#include <iostream>
#include <string>
#include <fstream>
#include <thread>
// including JsonCpp header file
#include <jsoncpp/json/json.h>
#include "config.hpp"
#include <unistd.h>
//include "ishape.hpp"
using namespace std;
using namespace zenoh;
namespace demo { namespace ishapes {
extern char* colorString_[];
// sub_(nullptr),
ZenohShapeDynamics::ZenohShapeDynamics(int x0, int y0,
std::shared_ptr<Session> session,
const std::string& keyExpression,
const std::string& color,
int colorIdx, bool dummy)
: ShapeDynamics(x0, y0, QRect(0, 0, 0, 0)),
x0_(x0),
y0_(y0),
session_(std::move(session)),
keyexpr_(keyExpression),
attached_(false),
color_(color),
colorIdx_(colorIdx),
updateBounds_(true),
dummy_(dummy),
subscriber_(nullptr)
{
colorList_[BLUE] = QColor(0x33, 0x66, 0x99);
colorList_[RED] = QColor(0xCC, 0x33, 0x33);
colorList_[GREEN] = QColor(0x99, 0xCC, 0x66);
colorList_[ORANGE] = QColor(0xFF, 0x99, 0x33);
colorList_[YELLOW] = QColor(0xFF, 0xFF, 0x66);
colorList_[MAGENTA] = QColor(0xCC, 0x99, 0xCC);
colorList_[CYAN] = QColor(0x99, 0xCC, 0xFF);
colorList_[GRAY] = QColor(0x99, 0x99, 0x99);
colorList_[BLACK] = QColor(0x33, 0x33, 0x33);
KeyExprView keyexpr1(keyExpression);
plist_.erase(plist_.begin(), plist_.end());
subscriber_ = std::get<Subscriber>(session_->declare_subscriber(keyexpr1,[&] (const Sample *sample)// Capturing object by reference
{ // Capturing object by reference
//cout<<"Subscriber Lembda triggered. "<<std::endl;
QPoint tmp;
plist_.erase(plist_.begin(), plist_.end());
// Create a dummy when the subscribe button is clicked until the first sample arrives
if(dummy_ == true)
{
SharedShape shape;
if (shape = shape_.lock())
{
plist_.push_back(QPoint((IS_WIDTH/2)-10, (IS_HEIGHT/2)-10));
QBrush brush = QBrush(QColor(0xd3, 0xd3, 0xd3), Qt::SolidPattern);
shape->setBrush(brush);
QRect bounds(0, 0, 90, 90);
}
}
if (sample) {
//std::cout << ">> [Subscriber] Received " << " ('"<< sample->get_payload().as_string_view()<<" )'"<<std::endl;
string_view str_1 = sample->get_payload().as_string_view();
string text = static_cast<string>(str_1);
// json reader
Json::Reader reader;
// this will contain complete JSON data
Json::Value completeJsonData;
// reader reads the data and stores it in completeJsonData
reader.parse(text, completeJsonData);
//std::cout<<"color: "<<completeJsonData["color"]<<" x:"<<completeJsonData["x"]<<" y:"<<completeJsonData["y"]<<" shapesize:"<<completeJsonData["shapesize"]<<std::endl;
dummy_ = false;
SharedShape shape;
if (shape = shape_.lock())
{
string strColor= completeJsonData["color"].asString();
//std::cout<<"Color String: "<<strColor<<" | Color: "<<color_<<std::endl;
int _colorIdx_ = convert(strColor);
sTs.x_ = completeJsonData["x"].asInt64();;
sTs.y_ = completeJsonData["y"].asInt64();
sTs.color_ = strColor;
sTs.ssize_ = completeJsonData["shapesize"].asInt64();
//std::cout<<"x: "<<sTs.x_<<" , y: "<<sTs.y_<<" , color: "<<sTs.color_<<" , size: "<<sTs.ssize_<<std::endl;
if (strcmp(strColor.c_str(), color_.c_str()) == 0)
{
tmp.rx() = completeJsonData["x"].asInt64();
tmp.ry() = completeJsonData["y"].asInt64();
plist_.push_back(tmp);
if (attached_ == false)
{
attached_ = true;
QBrush brush = QBrush(colorList_[_colorIdx_], Qt::SolidPattern);
shape->setBrush(brush);
}
QRect bounds(0, 0, completeJsonData["shapesize"].asInt64(), completeJsonData["shapesize"].asInt64());
shape->setBounds(bounds);
}
}
}
}
)
);
//std::cout<<"subscrited Created"<<std::endl;
}
void ZenohShapeDynamics::setKeyExpression(const std::string& keyExpression)
{
keyexpr_ = keyExpression;
}
void ZenohShapeDynamics::setSubscriber(const std::string& keyExpression)
{
//std::cout<<"Key Experssion: "<<keyExpression<<std::endl;
//subscriberList.push_back(std::move(subscriber));
//subscriber_ = std::move(subscriber);
//subscriber_ = subscriber;
//return subscriber;
}
int ZenohShapeDynamics::convert(const std::string& str)
{
if(str == "BLUE") return 0;
else if(str == "RED") return 1;
else if(str == "GREEN") return 2;
else if(str == "ORANGE") return 3;
else if(str == "YELLOW") return 4;
else if(str == "MAGENTA") return 5;
else if(str == "CYAN") return 6;
else if(str == "GRAY") return 7;
else if(str == "BLACK") return 8;
}
ZenohShapeDynamics::~ZenohShapeDynamics()
{
}
void
ZenohShapeDynamics::simulate()
{
//sub_.pull();
//std::cout<<"Simulation Triggered() "<<std::endl;
//subscriber_.pull();
//std::cout<<"x: "<<sTs.x_<<" , y: "<<sTs.y_<<" , color: "<<sTs.color_<<" , size: "<<sTs.ssize_<<std::endl;
}
}}