mirror of
https://github.com/m5stack/StackFlow.git
synced 2026-05-20 11:32:11 -07:00
[update] add bson support
This commit is contained in:
@@ -139,6 +139,8 @@ public:
|
||||
}else if(devname_.find("axera_") != std::string::npos){
|
||||
hal_camera_open = axera_camera_open;
|
||||
hal_camera_close = axera_camera_close;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -42,6 +42,12 @@ void zmq_com_send(int com_id, const std::string &out_str);
|
||||
|
||||
class zmq_bus_com {
|
||||
private:
|
||||
enum RAW_MSG_TYPE {
|
||||
RAW_NONE = 0,
|
||||
RAW_JSON = 1,
|
||||
RAW_BSON = 2,
|
||||
};
|
||||
|
||||
int reace_event_;
|
||||
int raw_msg_len_;
|
||||
std::string raw_msg_buff_;
|
||||
@@ -63,6 +69,7 @@ public:
|
||||
void select_json_str(const std::string &json_src, std::function<void(const std::string &)> out_fun);
|
||||
virtual void on_data(const std::string &data);
|
||||
virtual void on_raw_data(const std::string &data);
|
||||
virtual void on_bson_data(const std::string &data);
|
||||
virtual void send_data(const std::string &data);
|
||||
virtual void reace_data_event();
|
||||
virtual void send_data_event();
|
||||
|
||||
@@ -82,6 +82,11 @@ void zmq_bus_com::on_raw_data(const std::string &data)
|
||||
on_data(new_data);
|
||||
}
|
||||
|
||||
void zmq_bus_com::on_bson_data(const std::string &data)
|
||||
{
|
||||
// todo:..
|
||||
}
|
||||
|
||||
void zmq_bus_com::send_data(const std::string &data)
|
||||
{
|
||||
// printf("zmq_bus_com::send_data : send:%s\n", data.c_str());
|
||||
@@ -173,7 +178,7 @@ void zmq_bus_com::select_json_str(const std::string &json_src, std::function<voi
|
||||
do {
|
||||
enloop = false;
|
||||
switch (reace_event_) {
|
||||
case 0: {
|
||||
case RAW_NONE: {
|
||||
json_str_.reserve(json_str_.length() + src_str->length());
|
||||
const char *data = src_str->c_str();
|
||||
for (int i = 0; i < src_str->length(); i++) {
|
||||
@@ -203,7 +208,7 @@ void zmq_bus_com::select_json_str(const std::string &json_src, std::function<voi
|
||||
if ((json_str_.length() > 7) && (json_str_[1] == '\"') && (json_str_[2] == 'R') &&
|
||||
(json_str_[3] == 'A') && (json_str_[4] == 'W') && (json_str_[5] == '\"') &&
|
||||
(json_str_[6] == ':')) {
|
||||
reace_event_ = 10;
|
||||
reace_event_ = RAW_JSON;
|
||||
raw_msg_len_ = std::stoi(StackFlows::sample_json_str_get(json_str_, "RAW"));
|
||||
raw_msg_buff_.reserve(raw_msg_len_);
|
||||
if (json_src.length() > i) {
|
||||
@@ -211,6 +216,17 @@ void zmq_bus_com::select_json_str(const std::string &json_src, std::function<voi
|
||||
enloop = true;
|
||||
}
|
||||
break;
|
||||
} else if ((json_str_.length() > 7) && (json_str_[1] == '\"') && (json_str_[2] == 'B') &&
|
||||
(json_str_[3] == 'O') && (json_str_[4] == 'N') && (json_str_[5] == '\"') &&
|
||||
(json_str_[6] == ':')) {
|
||||
reace_event_ = RAW_BSON;
|
||||
raw_msg_len_ = std::stoi(StackFlows::sample_json_str_get(json_str_, "BON"));
|
||||
raw_msg_buff_.reserve(raw_msg_len_);
|
||||
if (json_src.length() > i) {
|
||||
src_str = std::make_shared<std::string>(src_str->substr(i + 1));
|
||||
enloop = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
out_fun(json_str_);
|
||||
}
|
||||
@@ -223,12 +239,12 @@ void zmq_bus_com::select_json_str(const std::string &json_src, std::function<voi
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case 10: {
|
||||
case RAW_JSON: {
|
||||
if (raw_msg_len_ > src_str->length()) {
|
||||
raw_msg_buff_.append(src_str->c_str(), src_str->length());
|
||||
raw_msg_len_ -= src_str->length();
|
||||
} else {
|
||||
reace_event_ = 0;
|
||||
reace_event_ = RAW_NONE;
|
||||
raw_msg_buff_.append(src_str->c_str(), raw_msg_len_);
|
||||
on_raw_data(raw_msg_buff_);
|
||||
raw_msg_buff_.clear();
|
||||
@@ -239,6 +255,22 @@ void zmq_bus_com::select_json_str(const std::string &json_src, std::function<voi
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case RAW_BSON: {
|
||||
if (raw_msg_len_ > src_str->length()) {
|
||||
raw_msg_buff_.append(src_str->c_str(), src_str->length());
|
||||
raw_msg_len_ -= src_str->length();
|
||||
} else {
|
||||
reace_event_ = RAW_NONE;
|
||||
raw_msg_buff_.append(src_str->c_str(), raw_msg_len_);
|
||||
on_bson_data(raw_msg_buff_);
|
||||
raw_msg_buff_.clear();
|
||||
json_str_.clear();
|
||||
if (src_str->length() > raw_msg_len_) {
|
||||
src_str = std::make_shared<std::string>(src_str->substr(raw_msg_len_ + 1));
|
||||
enloop = true;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user