-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.cpp
More file actions
167 lines (137 loc) · 3.35 KB
/
Copy pathsettings.cpp
File metadata and controls
167 lines (137 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <jsoncpp/json.h>
#include <fstream>
#include <iostream>
#include <string>
#include "settings.h"
using namespace std;
// Default values if no config file is found
#define CAM_ID 0
#define FPS 30 // capture speed in fps
#define MAX_FRAMES 30 // circular buffer depth
#define THRESHOLD 30 // detector threshold
#define BROADCASTIP "255.255.255.255" // default broadcast address
#define BROADCASTPORT 1976 // default broadcast port
#define DEVICENAME "Thomas_Eye" // Default device name
#define LISTENERPORT 2008 // Default port for the TCP socket server
#define SERVERMAXCONNECTIONS 128 // Default maximum number of connections to the TCP server
#define PATH "/home/tgrimault/Downloads/"
// Private methods
int Settings::refreshConfig()
{
ifstream configFile;
configFile.open("settings.json");
if (configFile.is_open()) {
// config file exists, read it
string configContent;
Json::Reader reader;
bool res;
configFile.seekg(0, ios::end);
int length = configFile.tellg();
configContent.resize(length);
configFile.seekg(0, ios::beg);
configFile.read(&configContent[0], length);
configFile.close();
res = reader.parse(configContent, config);
if (!res) {
cout << "Error parsing the JSON configuration file\n";
return -1;
}
} else {
// reset config with default values and write the config file
ofstream newConfigFile;
config["camID"] = CAM_ID;
config["FPS"] = FPS;
config["MAX_FRAMES"] = MAX_FRAMES;
config["THERSHOLD"] = THRESHOLD;
config["BROADCASTIP"] = BROADCASTIP;
config["BROADCASTPORT"] = BROADCASTPORT;
config["DEVICENAME"] = DEVICENAME;
config["LISTENERPORT"] = LISTENERPORT;
config["SERVERMAXCONNECTIONS"] = SERVERMAXCONNECTIONS;
config["PATH"] = PATH;
newConfigFile.open("settings.json");
if (!newConfigFile.is_open()) {
cout << "Error creating a new settings.json file.\n";
return -1;
}
newConfigFile << config.toStyledString();
newConfigFile.close();
}
return 0;
}
// Public API
Settings& Settings::instance()
{
static Settings _settings;
return _settings;
}
int Settings::getCamID()
{
if (!config.isMember("camID")) {
refreshConfig();
}
return config["camID"].asInt();
}
int Settings::getFPS()
{
if (!config.isMember("FPS")) {
refreshConfig();
}
return config["FPS"].asInt();
}
int Settings::getMaxFrames()
{
if (!config.isMember("MAX_FRAMES")) {
refreshConfig();
}
return config["MAX_FRAMES"].asInt();
}
int Settings::getThreshold()
{
if (!config.isMember("THRESHOLD")) {
refreshConfig();
}
return config["THRESHOLD"].asInt();
}
string Settings::getBroadcastIP()
{
if (!config.isMember("BROADCASTIP")) {
refreshConfig();
}
return config["BROADCASTIP"].asString();
}
int Settings::getBroadcastPort()
{
if (!config.isMember("BROADCASTPORT")) {
refreshConfig();
}
return config["BROADCASTPORT"].asInt();
}
string Settings::getDeviceName()
{
if (!config.isMember("DEVICENAME")) {
refreshConfig();
}
return config["DEVICENAME"].asString();
}
int Settings::getListenerPort()
{
if (!config.isMember("LISTENERPORT")) {
refreshConfig();
}
return config["LISTENERPORT"].asInt();
}
int Settings::getMaxAllowedConnections()
{
if (!config.isMember("SERVERMAXCONNECTIONS")) {
refreshConfig();
}
return config["SERVERMAXCONNECTIONS"].asInt();
}
string Settings::getPath()
{
if (!config.isMember("PATH")) {
refreshConfig();
}
return config["PATH"].asString();
}