-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlux_sim.cpp
117 lines (93 loc) · 3.5 KB
/
lux_sim.cpp
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
#include <iostream>
#include <fstream> // For ofstream
#include <string>
#include <ctime> // For time()
#include <iomanip> // For setprecision()
#include <vector>
#include <sstream> // For stringstream
#include <cstdlib> // For rand()
using namespace std;
// Function to generate a random value within a given range
double generateRandomValue(double min, double max) {
//rand() function to get a value in range min, max
double random = static_cast<double>(rand()) / RAND_MAX; //casting it to double and dividing it
//by RAND_MAX, you convert the integer
//random value to a floating-point value between 0 and 1.
return min + random * (max - min);
}
// Function to write log messages to a file
void writelog(const string& message) {
ofstream logfile("task1.log", ios::app);
if (logfile.is_open()) {
logfile << message << endl;
} else {
cerr << "Error: Unable to open log file 'task1.log'" << endl;
}
logfile.close();
}
//Read how to pass parameter in this link
//https://stackoverflow.com/questions/50021775/command-line-arguments-to-execute-a-c-program
int main(int argc, char* argv[]) {
// Default values for command-line arguments
int num_sensors = 1;
int sampling_time = 60; //unit in seconds
int measurement_duration = 24; //unit in hours
// Check if the correct number of command-line arguments is provided
if (argc != 7) {
writelog("Error 01: Invalid command");
return 1;
}
// Parse command-line arguments
for (int i = 1; i < argc; i++) {
string arg = argv[i];
// Check for invalid argument values
if (num_sensors < 1 || sampling_time < 1 || measurement_duration < 1) {
writelog("Error 02: Invalid argument");
return 1;
}
// Parse the value of each command-line argument
if (arg == "-n" && i + 1 < argc) {
num_sensors = stoi(argv[i + 1]);
}
else if (arg == "-st" && i + 1 < argc) {
sampling_time = stoi(argv[i + 1]);
}
else if (arg == "-si" && i + 1 < argc) {
measurement_duration = stoi(argv[i + 1]);
}
}
// Open the output file for writing
ofstream datafile("lux_sensor.csv");
if (!datafile.is_open()) {
writelog("Error 03: File access denied");
return 1;
}
// Write header to the output file
datafile << "id,time,value\n";
// Generate simulated data
time_t current_time = time(nullptr);
time_t start_time = current_time - measurement_duration * 3600;
srand(static_cast<unsigned int>(time(nullptr)));
time_t timestamp = start_time;
// Generate data for each timestamp
while (timestamp <= current_time) {
// Get local time information
struct tm* tm_info = localtime(×tamp);
// Format timestamp using stringstream
stringstream timestamp_ss;
timestamp_ss << put_time(tm_info, "%Y:%m:%d %H:%M:%S");
string timestamp_str = timestamp_ss.str();
// Generate data for each sensor
for (int i = 1; i <= num_sensors; i++) {
double measurement = generateRandomValue(0.1, 40000.0);
// Write data to the output file
datafile << i << "," << timestamp_str << "," << fixed << setprecision(2) << measurement << "\n";
}
// Move to the next timestamp
timestamp += sampling_time;
}
// Close the output file
datafile.close();
cout << "Data generated and stored successfully!" << endl;
return 0;
}