diff --git a/mqttdds/Publisher.cxx b/mqttdds/Publisher.cxx index 07deee4..0e3b872 100644 --- a/mqttdds/Publisher.cxx +++ b/mqttdds/Publisher.cxx @@ -309,6 +309,8 @@ extern std::string mqtt_topic_id; void PublisherApp::run(std::shared_ptr dev) { + std::map print_msg; + while (!is_stopped()) { //dds msg @@ -425,6 +427,51 @@ void PublisherApp::run(std::shared_ptr dev) } } } + else if (!DdsMsgData::WeightInfoOk_queue_.empty()) + { + WeighingSystem::WeightInfoOk info = std::move(DdsMsgData::WeightInfoOk_queue_.front()); + DdsMsgData::WeightInfoOk_queue_.pop(); + dds_lock.unlock(); + + auto formatFloat = [](float value) -> std::string + { + std::ostringstream oss; + oss << std::fixed << std::setprecision(2) << value; + return oss.str(); + }; + + // if (info.IsPrintTicker() == true) + // { + print_msg.clear(); + + print_msg["Title"] = "过衡单"; + print_msg["GrossDeviceID"] = std::to_string(info.GrossDeviceID()); + print_msg["GrossDeviceName"] = info.GrossDeviceName(); + print_msg["TareDeviceID"] = std::to_string(info.TareDeviceID()); + print_msg["TareDeviceName"] = info.TareDeviceName(); + print_msg["TitleCorp"] = info.EnterpriseName(); + print_msg["CoalNumber"] = info.BillNumber(); + print_msg["CarNumber"] = info.CarNumber(); + print_msg["Supplier"] = info.Supplier(); + print_msg["GoodsType"] = info.GoodsType(); + print_msg["GrossWeight"] = formatFloat(info.GrossWeight()); + print_msg["GrossTime"] = info.GrossTime(); + print_msg["GrossOperater"] = info.GrossOperater(); + print_msg["TareWeight"] = formatFloat(info.Tare()); + print_msg["TareTime"] = info.TareTime(); + print_msg["TareOperater"] = info.TareOperater(); + print_msg["SuttleWeight"] = formatFloat(info.Suttle()); + print_msg["duduction1"] = formatFloat(info.duduction1()); + print_msg["duduction2"] = formatFloat(info.duduction2()); + print_msg["duduction3"] = formatFloat(info.duduction3()); + print_msg["INCoalYard"] = info.INCoalYard(); + print_msg["OUTCoalYard"] = info.OutCoalYard(); + print_msg["BatchNumber"] = info.BatchNumber(); + print_msg["Warning"] = info.Warning(); + print_msg["Tips"] = info.Tips(); + print_msg["TicketType"] = info.TicketType(); + // } + } else { dds_lock.unlock(); @@ -446,7 +493,7 @@ void PublisherApp::run(std::shared_ptr dev) json cmd = json::parse(payload); - if (topic.find("barrier/frontup") != std::string::npos) + if (topic == ("pound/" + mqtt_topic_id + "/barrier/frontup")) { if (cmd["value"] == 1) { @@ -455,7 +502,7 @@ void PublisherApp::run(std::shared_ptr dev) bar_ctrl(ctrl); } } - else if (topic.find("barrier/frontdown") != std::string::npos) + else if (topic == ("pound/" + mqtt_topic_id + "/barrier/frontdown")) { if (cmd["value"] == 1) { @@ -464,18 +511,28 @@ void PublisherApp::run(std::shared_ptr dev) bar_ctrl(ctrl); } } - else if (topic.find("barrier/frontlock") != std::string::npos) + else if (topic == ("pound/" + mqtt_topic_id + "/barrier/frontlock")) { if (cmd["value"] == 1) { - std::ofstream(getExeDir() / "frontbar.lock") << getpid(); + std::ofstream(getExeDir() / "frontbar.lock"); + std::string payload = "{\"value\": 1}"; + auto mqtt_msg = mqtt::make_message("pound/" + std::string(mqtt_topic_id) + "/barrier/frontlockstatus", payload); + mqtt_msg->set_qos(0); + mqtt_msg->set_retained(true); + dev->publish(mqtt_msg); } else { std::filesystem::remove(getExeDir() / "frontbar.lock"); + std::string payload = "{\"value\": 0}"; + auto mqtt_msg = mqtt::make_message("pound/" + std::string(mqtt_topic_id) + "/barrier/frontlockstatus", payload); + mqtt_msg->set_qos(0); + mqtt_msg->set_retained(true); + dev->publish(mqtt_msg); } } - else if (topic.find("light/frontsignal") != std::string::npos) + else if (topic == ("pound/" + mqtt_topic_id + "/light/frontsignal")) { if (cmd["value"] == 1) { @@ -490,6 +547,10 @@ void PublisherApp::run(std::shared_ptr dev) light_ctrl(ctrl); } } + else if (topic == ("pound/" + mqtt_topic_id + "/businessok/reprint")) + { + print_receipt(print_msg); + } } else { diff --git a/mqttdds/Subscriber.cxx b/mqttdds/Subscriber.cxx index 1afabe2..fc46e48 100644 --- a/mqttdds/Subscriber.cxx +++ b/mqttdds/Subscriber.cxx @@ -62,6 +62,9 @@ SubscriberApp::SubscriberApp( , scaleinfo_topic_(nullptr) , scaleinfo_reader_(nullptr) , scaleinfo_type_(new WeighingSystem::ScaleInfoPubSubType()) + , weightinfook_topic_(nullptr) + , weightinfook_reader_(nullptr) + , weightinfook_type_(new WeighingSystem::WeightInfoOkPubSubType()) , samples_received_(0) , stop_(false) { @@ -84,6 +87,7 @@ SubscriberApp::SubscriberApp( infraredcommand_type_.register_type(participant_); license_type_.register_type(participant_); scaleinfo_type_.register_type(participant_); + weightinfook_type_.register_type(participant_); // Create the subscriber SubscriberQos sub_qos = SUBSCRIBER_QOS_DEFAULT; @@ -255,6 +259,33 @@ SubscriberApp::SubscriberApp( { throw std::runtime_error("WeighingSystem::ScaleInfo DataReader initialization failed"); } + + // Create the topic + topic_qos = TOPIC_QOS_DEFAULT; + participant_->get_default_topic_qos(topic_qos); + weightinfook_topic_ = participant_->create_topic("WeightInfoOk", weightinfook_type_.get_type_name(), topic_qos); + if (weightinfook_topic_ == nullptr) + { + throw std::runtime_error("WeighingSystem::WeightInfoOk Topic initialization failed"); + } + + // Create the reader + reader_qos = DATAREADER_QOS_DEFAULT; + subscriber_->get_default_datareader_qos(reader_qos); + reader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS; + reader_qos.reliability().max_blocking_time = Duration_t(1, 0); + reader_qos.durability().kind = DurabilityQosPolicyKind::VOLATILE_DURABILITY_QOS; + reader_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS; + reader_qos.history().depth = 1; + reader_qos.resource_limits().max_samples = 200; + reader_qos.resource_limits().max_instances = 1; + reader_qos.resource_limits().max_samples_per_instance = 100; + reader_qos.data_sharing().off(); + weightinfook_reader_ = subscriber_->create_datareader(weightinfook_topic_, reader_qos, this, StatusMask::all()); + if (weightinfook_reader_ == nullptr) + { + throw std::runtime_error("WeighingSystem::WeightInfoOk DataReader initialization failed"); + } } SubscriberApp::~SubscriberApp() @@ -384,6 +415,21 @@ void SubscriberApp::on_data_available( } } } + else if (topic_name == "WeightInfoOk") + { + WeighingSystem::WeightInfoOk sample_; + while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info))) + { + if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data) + { + { + std::unique_lock lock(DdsMsgData::queue_cv_mtx_); + DdsMsgData::WeightInfoOk_queue_.push(std::move(sample_)); + lock.unlock(); + } + } + } + } } void SubscriberApp::run(std::shared_ptr dev) diff --git a/mqttdds/main.cxx b/mqttdds/main.cxx index 5a8d115..99c1bc1 100644 --- a/mqttdds/main.cxx +++ b/mqttdds/main.cxx @@ -122,6 +122,7 @@ std::queue DdsMsgData::InfraredUpdate_queue_; std::queue DdsMsgData::InfraredCommandUpdate_queue_; std::queue DdsMsgData::LicenseSnapUpdate_queue_; std::queue DdsMsgData::ScaleInfo_queue_; +std::queue DdsMsgData::WeightInfoOk_queue_; std::mutex DdsMsgData::queue_cv_mtx_; std::queue MqttMsgData::Mqtt_msg_queue_; std::mutex MqttMsgData::queue_cv_mtx_; diff --git a/mqttdds/msg.hpp b/mqttdds/msg.hpp index 23106a4..d8a24ce 100644 --- a/mqttdds/msg.hpp +++ b/mqttdds/msg.hpp @@ -13,6 +13,7 @@ public: static std::queue InfraredCommandUpdate_queue_; static std::queue LicenseSnapUpdate_queue_; static std::queue ScaleInfo_queue_; + static std::queue WeightInfoOk_queue_; static std::mutex queue_cv_mtx_; };