2 * jsonserver.c: A plugin for the Video Disk Recorder
4 * See the README file for copyright information and how to reach the author.
7 // Log docs: https://github.com/gabime/spdlog
8 #include <spdlog/spdlog.h>
9 #if __has_include(<spdlog/sinks/basic_file_sink.h>)
10 #include <spdlog/sinks/basic_file_sink.h>
12 namespace spd = spdlog;
22 #include <vdr/plugin.h>
24 #if APIVERSNUM < 20102
25 #error jsonserver plugin requires VDR API version > 20101
30 // Config docs: http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API
31 #include <libconfig.h++>
33 #include "httpdclient.h"
35 static const char *VERSION = "0.0.1";
36 static const char *DESCRIPTION = "JSON data server plugin for VDR";
37 static const char *MAINMENUENTRY = "Jsonserver";
39 class cPluginJsonserver : public cPlugin {
41 // Add any member variables or functions you may need here.
42 std::shared_ptr<spd::logger> logger;
43 libconfig::Config config;
45 cPluginJsonserver(void);
46 virtual ~cPluginJsonserver();
47 virtual const char *Version(void) { return VERSION; }
48 virtual const char *Description(void) { return DESCRIPTION; }
49 virtual const char *CommandLineHelp(void);
50 virtual bool ProcessArgs(int argc, char *argv[]);
51 virtual bool Initialize(void);
52 virtual bool Start(void);
53 virtual void Stop(void);
54 virtual void Housekeeping(void);
55 virtual void MainThreadHook(void);
56 virtual cString Active(void);
57 virtual time_t WakeupTime(void);
58 virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
59 virtual cOsdObject *MainMenuAction(void);
60 virtual cMenuSetupPage *SetupMenu(void);
61 virtual bool SetupParse(const char *Name, const char *Value);
62 virtual bool Service(const char *Id, void *Data = NULL);
63 virtual const char **SVDRPHelpPages(void);
64 virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
67 cPluginJsonserver::cPluginJsonserver(void)
69 // Initialize any member variables here.
70 // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
71 // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
74 cPluginJsonserver::~cPluginJsonserver()
76 // Clean up after yourself!
80 const char *cPluginJsonserver::CommandLineHelp(void)
82 // Return a string that describes all known command line options.
86 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
88 // Implement command line argument processing here if applicable.
92 bool cPluginJsonserver::Initialize(void)
94 // Initialize any background activities the plugin shall perform.
98 bool cPluginJsonserver::Start(void)
100 // Start any background activities the plugin shall perform.
101 const char* configDir = cPlugin::ConfigDirectory("jsonserver");
104 dsyslog("jsonserver: Error: Could not get config dir from VDR");
108 std::string configFile(std::string(configDir) + std::string("/server.conf"));
109 dsyslog("%s", configFile.c_str());
112 config.readFile(configFile.c_str());
114 catch (const libconfig::FileIOException &fioex)
116 dsyslog("jsonserver: Failed to read config file");
119 catch(const libconfig::ParseException &pex)
121 dsyslog("jsonserver: Config parse error at %s: %i - %s", pex.getFile(), pex.getLine(), pex.getError());
125 std::string cfgLogFilename;
126 if (config.lookupValue("log-file", cfgLogFilename))
130 logger = spd::basic_logger_mt("jsonserver_spdlog", cfgLogFilename);
133 dsyslog("jsonserver: Failed to initialise log object - null");
137 catch (const spd::spdlog_ex& ex)
139 dsyslog("jsonserver: Failed to initialise log object: %s", ex.what());
142 logger->set_pattern("[%Y-%m-%d %T.%f] [%t] [%l] %v");
143 logger->set_level(spd::level::trace); //Set global log level to info
144 logger->flush_on(spd::level::debug); // FIXME Change this!!
146 logger->info("Main: Logging started");
150 dsyslog("jsonserver: Logging disabled");
153 std::string cfgDocRoot;
154 if (!config.lookupValue("doc-root", cfgDocRoot))
156 logger->critical("Main: Failed to load doc-root from config");
157 dsyslog("jsonserver: Could not load JS App Dir from plugin config file");
163 if (!config.lookupValue("http-port", cfgPort))
165 logger->critical("Main: Failed to load http-port from config");
166 dsyslog("jsonserver: Could not load http-port from plugin config file");
171 if (!HTTPDClient::StartServer(cfgDocRoot, cfgPort, configDir))
173 logger->critical("Main: Failed to start MHD");
174 dsyslog("jsonserver: Failed to start MHD");
182 void cPluginJsonserver::Stop(void)
184 // Stop any background activities the plugin is performing.
185 HTTPDClient::StopServer();
188 void cPluginJsonserver::Housekeeping(void)
190 // Perform any cleanup or other regular tasks.
193 void cPluginJsonserver::MainThreadHook(void)
195 // Perform actions in the context of the main program thread.
196 // WARNING: Use with great care - see PLUGINS.html!
199 cString cPluginJsonserver::Active(void)
201 // Return a message string if shutdown should be postponed
205 time_t cPluginJsonserver::WakeupTime(void)
207 // Return custom wakeup time for shutdown script
211 cOsdObject *cPluginJsonserver::MainMenuAction(void)
213 // Perform the action when selected from the main VDR menu.
217 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
219 // Return a setup menu in case the plugin supports one.
223 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
225 // Parse your own setup parameters and store their values.
229 bool cPluginJsonserver::Service(const char *Id, void *Data)
231 // Handle custom service requests from other plugins
235 const char **cPluginJsonserver::SVDRPHelpPages(void)
237 // Return help text for SVDRP commands this plugin implements
241 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
243 // Process SVDRP commands this plugin implements
247 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!