SNode.C
Loading...
Searching...
No Matches
warema-jalousien.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "express/legacy/in/WebApp.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "log/Logger.h"
25
26#include <cstdlib>
27#include <string>
28#include <utility>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32using namespace express;
33
34static std::map<std::string, std::string> jalousien = {{"kueche", "kueche"},
35 {"strasse", "strasse"},
36 {"esstisch", "esstisch"},
37 {"balkon", "balkon"},
38 {"schlafzimmer", "schlafzimmer"},
39 {"arbeitszimmer", "arbeitszimmer"},
40 {"comfort", "komfort"},
41 {"all", "alle"}};
42
43static std::map<std::string, std::string> actions = {{"open", "up"}, {"close", "down"}, {"stop", "stop"}};
44
45int main(int argc, char* argv[]) {
46 WebApp::init(argc, argv);
47
48 legacy::in::WebApp webApp("werema");
49
50 // tls::WebApp wa;
51
52 webApp.get("/jalousien/:id", [] APPLICATION(req, res) {
53 VLOG(1) << "Param: " << req->param("id");
54 VLOG(1) << "Qurey: " << req->query("action");
55
56 std::string arguments = "aircontrol -t " + jalousien[req->param("id")] + "_" + actions[req->query("action")];
57
58 int ret = system(arguments.c_str());
59 ret = (ret >> 8) & 0xFF;
60 /* ret:
61 Bits 15-8 = Exit code.
62 Bit 7 = 1 if a core dump was produced.
63 Bits 6-0 = Signal number that killed the process.
64 */
65
66 switch (ret) {
67 case 0:
68 res->status(200).send("OK: " + arguments);
69 break;
70 case 127:
71 res->status(404).send("ERROR not found: " + arguments);
72 break;
73 }
74 });
75
76 webApp.use([] APPLICATION(req, res) {
77 res->status(404).send("No Jalousie specified");
78 });
79
80 webApp.listen(8080,
81 [instanceName = webApp.getConfig().getInstanceName()](const legacy::in::WebApp::SocketAddress& socketAddress,
82 const core::socket::State& state) {
83 switch (state) {
84 case core::socket::State::OK:
85 VLOG(1) << instanceName << ": listening on '" << socketAddress.toString() << "'";
86 break;
87 case core::socket::State::DISABLED:
88 VLOG(1) << instanceName << ": disabled";
89 break;
90 case core::socket::State::ERROR:
91 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
92 break;
93 case core::socket::State::FATAL:
94 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
95 break;
96 }
97 });
98
99 return WebApp::start();
100}
#define APPLICATION(req, res)
Definition Router.h:45
static void init(int argc, char *argv[])
Definition WebApp.cpp:34
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:38
int main(int argc, char *argv[])