diff --git a/src/main.cpp b/src/main.cpp index 2812c02..f3888bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -217,8 +217,6 @@ public: rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); })); - - s.resources.emplace_back(std::make_unique("/api/set/qos", [this](const auto& req, auto& rep) { if (req.method != "POST") { http::server::stockReply(http::server::bad_request, rep); @@ -236,7 +234,7 @@ public: api->setQosSettings(pt); - std::string result = R"({"settings":)"; + std::string result = R"({""status":"ok",settings":)"; result += api->loadSettings(); result += "}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); @@ -247,6 +245,34 @@ public: } })); + s.resources.emplace_back(std::make_unique("/api/set/bucLnb", [this](const auto& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + } + + rep.status = http::server::ok; + rep.headers.clear(); + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + + try { + std::stringstream ss; + ss.str(std::string(req.payload.begin(), req.payload.end())); + boost::property_tree::ptree pt; + read_json(ss, pt); + + api->setBucLnbSettings(pt); + + std::string result = R"({"status":"ok","settings":)"; + result += api->loadSettings(); + result += "}"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + } catch (std::exception& e) { + BOOST_LOG_TRIVIAL(error) << "WebHandle(/api/set/bucLnb): Can't set settings: " << e.what(); + const std::string result = R"({"status":"error"})"; + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + } + })); + } ~ServerResources() = default; diff --git a/src/terminal_api_driver.cpp b/src/terminal_api_driver.cpp index 3cda122..39e3c8c 100644 --- a/src/terminal_api_driver.cpp +++ b/src/terminal_api_driver.cpp @@ -11,6 +11,7 @@ #include "../dependencies/control_system/common/protocol_commands.h" +typedef boost::property_tree::ptree::path_type json_path; /** * Этот демон нужен для того, чтобы получать статистику из API, а так же корректно сохранять настройки @@ -205,6 +206,22 @@ public: json = this->qosClassesJson; } + void setStrringsRxTx(bool readback = true) {} + void setStrringsCinc(bool readback = true) {} + + void setStrringsBucLnb(const buc_lnb_settings& bucLnb, bool readback = true) { + std::lock_guard lock(this->cpApiMutex); + buc_lnb_settings tmp = bucLnb; + CP_SetBUC_LNB_settings(this->sid, tmp); + if (readback) { + CP_GetBUC_LNB_settings(this->sid, tmp); + { + std::lock_guard lock2{this->settingsMutex}; + this->bucLnbSettings = tmp; + } + } + } + void setQosSettings(bool enabled, const std::string& str, bool readback = true) { std::lock_guard lock(this->cpApiMutex); CP_SetQoSSettings(this->sid, str, enabled); @@ -473,7 +490,43 @@ std::string api_driver::ApiDriver::loadSettings() const { return result.str(); } -api_driver::ApiDriver::~ApiDriver() = default; +void api_driver::ApiDriver::setRxTxSettings(boost::property_tree::ptree &pt) { +} + +void api_driver::ApiDriver::setCincSettings(boost::property_tree::ptree &pt) { +} + +void api_driver::ApiDriver::setBucLnbSettings(boost::property_tree::ptree &pt) { + buc_lnb_settings s{}; + + auto tmp = pt.get(json_path("lnb.powering", '/')); + switch (tmp) { + case 13: s.lnb = voltage_lnb::_13V; break; + case 18: s.lnb = voltage_lnb::_18V; break; + case 24: s.lnb = voltage_lnb::_24V; break; + case 0: + default: + s.lnb = voltage_lnb::DISABLE; + } + s.is_ref_10MHz_lnb = pt.get(json_path("lnb.refClk10M", '/')); + + tmp = pt.get(json_path("buc.powering", '/')); + switch (tmp) { + case 24: s.buc = voltage_buc::_24V; break; + case 48: s.buc = voltage_buc::_48V; break; + case 0: + default: + s.lnb = voltage_lnb::DISABLE; + } + // { "lnb": {"powering": 0} } + + s.is_ref_10MHz_buc = pt.get(json_path("buc.refClk10M", '/')); + + s.is_ref_10MHz_output = pt.get(json_path("serviceSettings.refClk10M", '/')); + s.is_save_current_state = pt.get(json_path("serviceSettings.autoStart", '/')); + + this->daemon->setStrringsBucLnb(s); +} void api_driver::ApiDriver::setQosSettings(boost::property_tree::ptree &pt) { bool enabled = pt.get("en"); @@ -490,3 +543,5 @@ bool api_driver::ApiDriver::getIsCinC() const { daemon->getSettings(&s, nullptr, nullptr, nullptr, nullptr); return s.is_cinc; } + +api_driver::ApiDriver::~ApiDriver() = default; diff --git a/src/terminal_api_driver.h b/src/terminal_api_driver.h index 3544c01..8c2847b 100644 --- a/src/terminal_api_driver.h +++ b/src/terminal_api_driver.h @@ -8,7 +8,6 @@ namespace api_driver { - constexpr int CACHE_STATISTICS_UPDATE_MS = 500; constexpr int CACHE_SETTINGS_UPDATE_MS = 5000; constexpr int CACHE_QOS_UPDATE_MS = 5000; @@ -16,33 +15,51 @@ namespace api_driver { class TerminalApiDaemon; /** - * Это ApiDriver. Все ответы он будет возвращать в виде json. - */ + * Это ApiDriver. Все ответы он будет возвращать в виде json. + */ class ApiDriver { public: explicit ApiDriver(); /** - * Запуск демона - */ + * Запуск демона + */ void startDaemon(); /** - * Запросить общее состояние терминала - * @return {"txState":false,"rxState":false,"rx.sym_sync_lock":false,"rx.freq_search_lock":false,"rx.afc_lock":false,"rx.pkt_sync":false} - */ + * Запросить общее состояние терминала + * @return {"txState":false,"rxState":false,"rx.sym_sync_lock":false,"rx.freq_search_lock":false,"rx.afc_lock":false,"rx.pkt_sync":false} + */ std::string loadTerminalState() const; /** - * Сбросить статистику пакетов - */ + * Сбросить статистику пакетов + */ void resetPacketStatistics() const; std::string loadSettings() const; - ~ApiDriver(); + /** + * Установить настройки RX/TX, readback можно получить используя loadTerminalState + */ + void setRxTxSettings(boost::property_tree::ptree &pt); - void setQosSettings(boost::property_tree::ptree & pt); + /** + * Установить настройки CinC, readback можно получить используя loadTerminalState. + */ + void setCincSettings(boost::property_tree::ptree &pt); + + /** + * Установить настройки BUC и LNB, readback можно получить используя loadTerminalState. + */ + void setBucLnbSettings(boost::property_tree::ptree &pt); + + /** + * Установить настройки QoS, readback можно получить используя loadTerminalState. + */ + void setQosSettings(boost::property_tree::ptree &pt); + + ~ApiDriver(); private: TSID sid{0}; diff --git a/static/main.html b/static/main.html index 1668aa9..57691c9 100644 --- a/static/main.html +++ b/static/main.html @@ -863,18 +863,21 @@ // TODO сделать всплывающее окно с подтверждением того, что настройки действительно нужно применить let query = { "buc.refClk10M": this.param.buc.refClk10M, - "buc.powering": this.param.buc.powering, + "buc.powering": parseInt(this.param.buc.powering), "lnb.refClk10M": this.param.lnb.refClk10M, - "lnb.powering": this.param.lnb.powering, + "lnb.powering": parseInt(this.param.lnb.powering), "serviceSettings.refClk10M": this.param.serviceSettings.refClk10M, "serviceSettings.autoStart": this.param.serviceSettings.autoStart } fetch('/api/set/bucLnb', { method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, body: JSON.stringify(query) - }).then(() => { + }).then(async (resp) => { this.submitStatus.bucLnb = false - this.performUpdateSettings() + this.updateBucLnbSettings(await resp.json()) }) }, @@ -926,10 +929,13 @@ console.log(query) fetch('/api/set/qos', { method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, body: JSON.stringify(query) - }).then(async (response) => { + }).then(async (resp) => { this.submitStatus.qos = false - this.updateQosSettings(await response.json()) + this.updateQosSettings(await resp.json()) }) },