SatDump 2.0.0-alpha-520736c72
Loading...
Searching...
No Matches
remote_handler_backend.h
1#pragma once
2
3#include "nlohmann/json.hpp"
4#include <cstddef>
5#include <cstdint>
6#include <exception>
7#include <functional>
8#include <mutex>
9#include <string>
10
11namespace satdump
12{
13 namespace handlers
14 {
15 class RemoteHandlerBackend
16 {
17 protected:
18 std::function<void(std::string, uint8_t *, size_t)> stream_rx;
19 std::mutex stream_rx_mtx;
20
21 void push_stream_data(std::string id, uint8_t *data, size_t size)
22 {
23 std::scoped_lock l(stream_rx_mtx);
24 stream_rx(id, data, size);
25 }
26
27 public:
28 void set_stream_rx_handler(std::function<void(std::string, uint8_t *, size_t)> f)
29 {
30 std::scoped_lock l(stream_rx_mtx);
31 stream_rx = f;
32 }
33
34 public:
35 enum cfg_res_t
36 {
37 RES_OK = 0,
38 RES_LISTUPD = 1,
39 RES_ERR = 2,
40 };
41
42 private:
43 std::mutex cfg_mtx;
44
45 virtual nlohmann::ordered_json _get_cfg_list() = 0;
46 virtual nlohmann::ordered_json _get_cfg(std::string key) = 0;
47 virtual cfg_res_t _set_cfg(std::string key, nlohmann::ordered_json v) = 0;
48
49 public:
50 nlohmann::ordered_json get_cfg_list()
51 {
52 std::scoped_lock l(cfg_mtx);
53 return _get_cfg_list();
54 }
55
56 nlohmann::ordered_json get_cfg(std::string key)
57 {
58 std::scoped_lock l(cfg_mtx);
59 return _get_cfg(key);
60 }
61
62 cfg_res_t set_cfg(std::string key, nlohmann::ordered_json v)
63 {
64 std::scoped_lock l(cfg_mtx);
65 cfg_res_t r = RES_ERR;
66 try
67 {
68 r = _set_cfg(key, v);
69 }
70 catch (std::exception &e)
71 {
72 }
73
74 push_stream_data("upd", NULL, 0);
75 return r;
76 }
77
78 nlohmann::ordered_json get_cfg()
79 {
80 nlohmann::ordered_json p;
81 auto v = get_cfg_list();
82 for (auto &v2 : v.items())
83 p[v2.key()] = get_cfg(v2.key());
84 return p;
85 }
86
87 cfg_res_t set_cfg(nlohmann::ordered_json v)
88 {
89 cfg_res_t r = RES_OK;
90 for (auto &i : v.items())
91 {
92 cfg_res_t r2 = set_cfg(i.key(), i.value());
93 if (r2 > r)
94 r = r2;
95 }
96 return r;
97 }
98
99 public:
100 RemoteHandlerBackend() {}
101 ~RemoteHandlerBackend() {}
102
103 protected:
104 //
105
106 public:
107 //
108 };
109 } // namespace handlers
110} // namespace satdump