00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef ZMSERVER_H
00017 #define ZMSERVER_H
00018
00019
00020 #include <unistd.h>
00021 #include <string>
00022 #include <sstream>
00023 #include <vector>
00024 #include <map>
00025 #include <mysql/mysql.h>
00026
00027 using namespace std;
00028
00029 extern void loadZMConfig(const string &configfile);
00030 extern void connectToDatabase(void);
00031 extern void kickDatabase(bool debug);
00032
00033
00034 extern MYSQL g_dbConn;
00035 extern string g_zmversion;
00036 extern string g_password;
00037 extern string g_server;
00038 extern string g_database;
00039 extern string g_webPath;
00040 extern string g_user;
00041 extern string g_webUser;
00042 extern string g_binPath;
00043
00044 #define DB_CHECK_TIME 60
00045 extern time_t g_lastDBKick;
00046
00047 const string FUNCTION_MONITOR = "Monitor";
00048 const string FUNCTION_MODECT = "Modect";
00049 const string FUNCTION_NODECT = "Nodect";
00050 const string FUNCTION_RECORD = "Record";
00051 const string FUNCTION_MOCORD = "Mocord";
00052 const string FUNCTION_NONE = "None";
00053
00054 const string RESTART = "restart";
00055 const string RELOAD = "reload";
00056 const string RUNNING = "running";
00057
00058 typedef enum
00059 {
00060 IDLE,
00061 PREALARM,
00062 ALARM,
00063 ALERT,
00064 TAPE
00065 } State;
00066
00067 typedef struct
00068 {
00069 int size;
00070 bool valid;
00071 bool active;
00072 bool signal;
00073 State state;
00074 int last_write_index;
00075 int last_read_index;
00076 time_t last_image_time;
00077 int last_event;
00078 int action;
00079 int brightness;
00080 int hue;
00081 int colour;
00082 int contrast;
00083 int alarm_x;
00084 int alarm_y;
00085 char control_state[256];
00086 } SharedData;
00087
00088 typedef enum { TRIGGER_CANCEL, TRIGGER_ON, TRIGGER_OFF } TriggerState;
00089
00090
00091 typedef struct
00092 {
00093 int size;
00094 TriggerState trigger_state;
00095 int trigger_score;
00096 char trigger_cause[32];
00097 char trigger_text[256];
00098 char trigger_showtext[32];
00099 } TriggerData_old;
00100
00101
00102 typedef struct
00103 {
00104 int size;
00105 TriggerState trigger_state;
00106 int trigger_score;
00107 char trigger_cause[32];
00108 char trigger_text[256];
00109 char trigger_showtext[256];
00110 } TriggerData;
00111
00112 typedef struct
00113 {
00114 string name;
00115 string type;
00116 string function;
00117 int enabled;
00118 string device;
00119 int image_buffer_count;
00120 int width;
00121 int height;
00122 int mon_id;
00123 SharedData *shared_data;
00124 unsigned char *shared_images;
00125 int last_read;
00126 string status;
00127 int frame_size;
00128 int palette;
00129 int controllable;
00130 int trackMotion;
00131
00132 string getIdStr()
00133 {
00134 if (id == "")
00135 {
00136 std::stringstream out;
00137 out << mon_id;
00138 id = out.str();
00139 }
00140 return id;
00141 }
00142
00143 private:
00144 string id;
00145
00146 } MONITOR;
00147
00148 class ZMServer
00149 {
00150 public:
00151 ZMServer(int sock, bool debug);
00152 ~ZMServer();
00153
00154 void processRequest(char* buf, int nbytes);
00155
00156 private:
00157 string getZMSetting(const string &setting);
00158 bool send(const string s) const;
00159 bool send(const string s, const unsigned char *buffer, int dataLen) const;
00160 void sendError(string error);
00161 void getMonitorList(void);
00162 void initMonitor(MONITOR *monitor);
00163 int getFrame(unsigned char *buffer, int bufferSize, MONITOR *monitor);
00164 long long getDiskSpace(const string &filename, long long &total, long long &used);
00165 void tokenize(const string &command, vector<string> &tokens);
00166 void handleHello(void);
00167 string runCommand(string command);
00168 void getMonitorStatus(string id, string type, string device, string channel,
00169 string function, string &zmcStatus, string &zmaStatus,
00170 string enabled);
00171 void handleGetServerStatus(void);
00172 void handleGetMonitorStatus(void);
00173 void handleGetMonitorList(void);
00174 void handleGetCameraList(void);
00175 void handleGetEventList(vector<string> tokens);
00176 void handleGetEventFrame(vector<string> tokens);
00177 void handleGetAnalyseFrame(vector<string> tokens);
00178 void handleGetLiveFrame(vector<string> tokens);
00179 void handleGetFrameList(vector<string> tokens);
00180 void handleDeleteEvent(vector<string> tokens);
00181 void handleDeleteEventList(vector<string> tokens);
00182 void handleGetEventDates(vector<string> tokens);
00183 void handleRunZMAudit(void);
00184 void handleSetMonitorFunction(vector<string> tokens);
00185 void zmcControl(MONITOR *monitor, const string &mode);
00186 void zmaControl(MONITOR *monitor, const string &mode);
00187
00188 bool m_debug;
00189 int m_sock;
00190 map<int, MONITOR *> m_monitors;
00191 string m_eventFileFormat;
00192 string m_analyseFileFormat;
00193 key_t m_shmKey;
00194 };
00195
00196
00197 #endif