-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.c
220 lines (178 loc) · 5.21 KB
/
sim.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* B2-28a
* Version 1.0 - Initial Concept
* 24-11-2013
*
* This is the simulator part of the program.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define MAX_DEVICES 100
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define OS 1
#include <windows.h>
#else
#define OS 0
#include <unistd.h>
int Sleep(int sleepMs) {return sleep(sleepMs / 1000);}
#endif
typedef struct
{
int min,
id,
setTo;
} state;
typedef struct {
char name[50];
int id,
state;
} device;
/* Same load_devices, load_current_state, write_current_state as in main.c */
int load_devices (FILE *devices, device *activeDevices)
{
char ch; /* Char to check if the next char is EOF */
int n = 0; /* Count number of devices loaded */
/* While the next char isn't EOF read devices from file */
while (((ch = fgetc(devices)) != EOF) && ungetc(ch, devices))
{
fscanf(devices, "%s ", activeDevices[n].name);
fscanf(devices, "%d ", &activeDevices[n].id);
activeDevices[n].state = 0;
n++;
}
return n;
}
void load_current_state (device *activeDevices, int numberOfDevices)
{
int i;
char filename[50];
FILE * tmp;
for (i = 0; i < numberOfDevices; i++)
{
sprintf(filename, "%d", activeDevices[i].id);
tmp = fopen(filename, "r");
/* Exit on failure */
if (tmp == NULL)
{
printf("Loading file %d failed. Quitting.\n", activeDevices[i].id);
exit(1);
}
fscanf (tmp, "%d", &activeDevices[i].state);
fclose (tmp);
printf("%-30s state is \t%d\n", activeDevices[i].name, activeDevices[i].state);
}
}
void write_current_state (device *activeDevices, int numberOfDevices)
{
int i;
char filename[50];
FILE * tmp;
for (i = 0; i < numberOfDevices; i++)
{
sprintf(filename, "%d", activeDevices[i].id);
tmp = fopen(filename, "w");
fprintf (tmp, "%d\n", activeDevices[i].state);
fclose (tmp);
}
}
void enable_random (device *activeDevices, int numberOfDevices)
{
int random;
srand(time(NULL));
random = rand() % (numberOfDevices);
activeDevices[random].state = 1;
printf("%s turned on.\n", activeDevices[random].name);
}
void disable_random (device *activeDevices, int numberOfDevices)
{
int random;
srand(time(NULL));
random = rand() % (numberOfDevices);
activeDevices[random].state = 0;
printf("%s turned off.\n", activeDevices[random].name);
}
int within_interval (int currentTime, int min2, int interval)
{
if((currentTime / interval) * interval < min2)
if((1 + currentTime / interval) * interval > min2)
return 1;
return 0;
}
int time_to_min (int t1, int t2)
{
return t1 * 60 + t2;
}
void wait_for_start_file (void)
{
int ready;
FILE * start;
struct timeval tempTime;
while (!ready)
{
gettimeofday(&tempTime, 0);
if((int) tempTime.tv_sec % 2 == 1)
{
start = fopen ("start", "r");
if (start != NULL)
ready = 1;
}
Sleep(1000);
}
}
int main (void)
{
int min = 0,
runAlready = 0,
numberOfDevices,
interval = 60;
struct timeval tempTime;
FILE * ftime;
FILE * devicesIn;
device *activeDevices;
printf("Waiting for main to start simulation.\n");
/* Wait for main to be ready */
wait_for_start_file();
activeDevices = (device *) calloc (MAX_DEVICES, sizeof(device));
devicesIn = fopen ("devices_test.txt", "r");
numberOfDevices = load_devices (devicesIn, activeDevices);
fclose (devicesIn);
load_current_state (activeDevices, numberOfDevices);
while (min < 1450)
{
gettimeofday(&tempTime, 0);
if(!runAlready && (int) tempTime.tv_sec % 2 == 1)
{
/* update all states */
load_current_state (activeDevices, numberOfDevices);
ftime = fopen ("time.txt","w");
fprintf (ftime, "%d\n", min);
fclose (ftime);
printf("Time: %02d:%02d (%d) adding %d minutes!\n", min / 60, min % 60, min, interval);
runAlready = 1;
min += interval;
/* This is a demonstration */
if(within_interval(min, time_to_min(07,20), interval))
activeDevices[6].state = 1;
if(within_interval(min, time_to_min(07,30), interval))
activeDevices[6].state = 0;
if(within_interval(min, time_to_min(07,30), interval))
activeDevices[6].state = 0;
if(within_interval(min, time_to_min(07,50), interval))
activeDevices[9].state = 1;
if(within_interval(min, time_to_min(16,20), interval))
activeDevices[4].state = 1;
if(within_interval(min, time_to_min(20,50), interval))
activeDevices[4].state = 0;
write_current_state (activeDevices, numberOfDevices);
}
if((int) tempTime.tv_sec % 2 == 0)
{
runAlready = 0;
}
Sleep(1000);
}
remove("time.txt");
return 0;
}