-
Notifications
You must be signed in to change notification settings - Fork 0
/
sauna.c
76 lines (60 loc) · 1.44 KB
/
sauna.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
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include "server.h"
#define DEFAULT_TEMP 75 //Celsius
int updateTemp(int temp);
int main(int argc, char **argv){
int coid;
int server_ok;
//Connect to server
coid = name_open(SERVER_NAME, 0);
//Build the update struct
saunaChange_t change;
change.type = SAUNA_TYPE;
//Init temperature
srand(time(NULL));
int temp = DEFAULT_TEMP;
int prevTemp = temp;
while(1){
printf("Current temperature in sauna: %d\n", temp);
//Only update the server if the temperature changed
if(!(prevTemp == temp)){
change.temp = temp;
MsgSend(coid, &change, sizeof(change), &server_ok, sizeof(server_ok));
}
//Check if the temperature is updating
prevTemp = temp;
temp = updateTemp(temp);
}
return 0;
}
//Generator function not needed since only one variable is being tracked
//Update function
//Randomly increase or decrease the temperature by 1*, or do nothing
//This function is meant to simulate a person changing the temperature
int updateTemp(int temp){
//Sleep for 5 seconds, arbitrary delay for the temperature to change
sleep(5);
int r = (rand() % 3) + 1;
if(r == 1){
if(temp - 1 < 65){
return temp;
} else {
return temp - 1;
}
} else if(r == 2) {
if(temp + 1 > 85){
return temp;
} else {
return temp + 1;
}
} else {
return temp;
}
}