]> git.vomp.tv Git - jsonserver.git/blob - jsonserver.c
Add result to timers list call
[jsonserver.git] / jsonserver.c
1 /*
2  * jsonserver.c: A plugin for the Video Disk Recorder
3  *
4  * See the README file for copyright information and how to reach the author.
5  *
6  */
7
8 #include <vdr/plugin.h>
9 #include <stdio.h>
10
11 #include "mongoose.h"
12 #include "handler.h"
13 #include "log.h"
14 #include "config.h"
15
16 static const char *VERSION        = "0.0.1";
17 static const char *DESCRIPTION    = "JSON data server plugin for VDR";
18 static const char *MAINMENUENTRY  = "Jsonserver";
19
20 class cPluginJsonserver : public cPlugin {
21 private:
22   // Add any member variables or functions you may need here.
23   struct mg_context *mg;
24   Log* log;
25   Config* config;
26   char* cfgDocRoot;
27   char* cfgPort;
28 public:
29   cPluginJsonserver(void);
30   virtual ~cPluginJsonserver();
31   virtual const char *Version(void) { return VERSION; }
32   virtual const char *Description(void) { return DESCRIPTION; }
33   virtual const char *CommandLineHelp(void);
34   virtual bool ProcessArgs(int argc, char *argv[]);
35   virtual bool Initialize(void);
36   virtual bool Start(void);
37   virtual void Stop(void);
38   virtual void Housekeeping(void);
39   virtual void MainThreadHook(void);
40   virtual cString Active(void);
41   virtual time_t WakeupTime(void);
42   virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
43   virtual cOsdObject *MainMenuAction(void);
44   virtual cMenuSetupPage *SetupMenu(void);
45   virtual bool SetupParse(const char *Name, const char *Value);
46   virtual bool Service(const char *Id, void *Data = NULL);
47   virtual const char **SVDRPHelpPages(void);
48   virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
49   };
50
51 cPluginJsonserver::cPluginJsonserver(void)
52 {
53   // Initialize any member variables here.
54   // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
55   // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
56
57   config = NULL;
58   log = NULL;
59   cfgDocRoot = NULL;
60   cfgPort = NULL;
61 }
62
63 cPluginJsonserver::~cPluginJsonserver()
64 {
65   // Clean up after yourself!
66   if (log) delete log;
67   log = NULL;
68
69   if (config) delete config;
70   config = NULL;
71 }
72
73 const char *cPluginJsonserver::CommandLineHelp(void)
74 {
75   // Return a string that describes all known command line options.
76   return NULL;
77 }
78
79 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
80 {
81   // Implement command line argument processing here if applicable.
82   return true;
83 }
84
85 bool cPluginJsonserver::Initialize(void)
86 {
87   // Initialize any background activities the plugin shall perform.
88   return true;
89 }
90
91 bool cPluginJsonserver::Start(void)
92 {
93   // Start any background activities the plugin shall perform.
94   const char* configDir = cPlugin::ConfigDirectory("jsonserver");
95   if (!configDir)
96   {
97     dsyslog("jsonserver: Error: Could not get config dir from VDR");
98     return false;
99   }
100   char* configFile;
101   asprintf(&configFile, "%s/jsonserver.conf", configDir);
102
103   dsyslog("%s", configFile);
104   
105   log = new Log();
106   
107   config = new Config();
108   if (config->init(configFile))
109   {
110     dsyslog("jsonserver: Config file found");
111     free(configFile);
112   }
113   else
114   {
115     dsyslog("jsonserver: Error: Config file not found");
116     free(configFile);
117     delete config;
118     config = NULL;
119     return false;
120   }
121
122   char* cfgLogFilename = config->getValueString("General", "Log file");
123   if (cfgLogFilename)
124   {
125     log->init(Log::DEBUG, cfgLogFilename);
126     delete[] cfgLogFilename;
127     log->log("Main", Log::INFO, "Logging started");
128   }
129   else
130   {
131     dsyslog("jsonserver: Logging disabled");
132   }
133   
134   cfgDocRoot = config->getValueString("General", "JS App Dir");
135   if (!cfgDocRoot)
136   {
137     log->log("Main", Log::CRIT, "Config General/JS App Dir not found");
138     dsyslog("jsonserver: Error: Could not load JS App Dir from plugin config file");
139     delete log;
140     delete config;
141     log = NULL;
142     config = NULL;
143     return false;
144   }
145
146   cfgPort = config->getValueString("General", "Port number");
147   if (!cfgPort)
148   {
149     cfgPort = new char[5];
150     strcpy(cfgPort, "8005");
151   }
152   
153   // Make Mongoose options
154   const char *options[] =
155   {
156     "document_root", cfgDocRoot,
157     "listening_ports", cfgPort,
158     "num_threads", "5",
159     NULL
160   };
161
162   // Prepare callbacks structure. We have only one callback, the rest are NULL.
163   struct mg_callbacks callbacks;
164   memset(&callbacks, 0, sizeof(callbacks));
165   callbacks.begin_request = jsonserver_request_handler;
166   
167   mg = mg_start(&callbacks, NULL, options);
168   log->log("JSONServer", Log::INFO, "Mongoose started");
169   return true;
170 }
171
172 void cPluginJsonserver::Stop(void)
173 {
174   // Stop any background activities the plugin is performing.
175   mg_stop(mg);
176   if (cfgDocRoot) delete[] cfgDocRoot; cfgDocRoot = NULL;
177   if (cfgPort) delete[] cfgPort; cfgPort = NULL;
178 }
179
180 void cPluginJsonserver::Housekeeping(void)
181 {
182   // Perform any cleanup or other regular tasks.
183 }
184
185 void cPluginJsonserver::MainThreadHook(void)
186 {
187   // Perform actions in the context of the main program thread.
188   // WARNING: Use with great care - see PLUGINS.html!
189 }
190
191 cString cPluginJsonserver::Active(void)
192 {
193   // Return a message string if shutdown should be postponed
194   return NULL;
195 }
196
197 time_t cPluginJsonserver::WakeupTime(void)
198 {
199   // Return custom wakeup time for shutdown script
200   return 0;
201 }
202
203 cOsdObject *cPluginJsonserver::MainMenuAction(void)
204 {
205   // Perform the action when selected from the main VDR menu.
206   return NULL;
207 }
208
209 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
210 {
211   // Return a setup menu in case the plugin supports one.
212   return NULL;
213 }
214
215 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
216 {
217   // Parse your own setup parameters and store their values.
218   return false;
219 }
220
221 bool cPluginJsonserver::Service(const char *Id, void *Data)
222 {
223   // Handle custom service requests from other plugins
224   return false;
225 }
226
227 const char **cPluginJsonserver::SVDRPHelpPages(void)
228 {
229   // Return help text for SVDRP commands this plugin implements
230   return NULL;
231 }
232
233 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
234 {
235   // Process SVDRP commands this plugin implements
236   return NULL;
237 }
238
239 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!