-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.js
175 lines (154 loc) · 3.43 KB
/
control.js
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
var power = 0;
var auto = true;
var crying = $("#crying");
var sleeping = $("#sleeping");
var cradleControl = $(".cradle-control");
var mainControl = $(".main-overview");
var infoPanel = $(".info");
var cPopup = $("#cradle-control-popup");
var sPopup = $("#main-overview");
var mildBtn = $("#mild-button");
var medBtn = $("#medium-button");
var stopBtn = $("#stop-button");
var autoOn = $(".auto-on");
var autoOff = $(".auto-off");
var dB2 = 0;
var pants = 'D';
var health = true;
var cry = false;
// Default setup
crying.hide();
cradleControl.hide();
autoOff.hide();
// Functions
function requestCradleMovement(){
$.post("http://exceed.cupco.de/iot/Have-a-seed/web", {data: power});
}
function getCradleStatus(){
$.get("http://exceed.cupco.de/iot/Have-a-seed/web", showCradleStatus);
}
function showCradleStatus(status){
var cradle = $("#cradle-status");
if (status == 0) {
cradle.text("Idle");
}
else if (status == 1) {
cradle.text("Mild");
}
else if (status == 2) {
cradle.text("Fun");
}
}
function changeInfantState(){
if (health) {
$("#infant-status").text("Happy");
$(".condition-status").css('color', 'greenyellow');
sleeping.show();
crying.hide();
}
else {
$("#infant-status").text("Unhappy");
$(".condition-status").css('color', 'red');
crying.show();
sleeping.hide();
}
}
function autoCradleControl(){
if (auto) {
if (cry) {
power = 1;
requestCradleMovement();
}
}
}
function toggleAuto(){
auto = !auto;
autoOn.toggle();
autoOff.toggle();
}
function showCradleControl(){
infoPanel.slideUp();
cradleControl.slideDown();
}
function showInfo(){
infoPanel.slideDown();
cradleControl.slideUp();
}
function refreshData(){
$.get("http://exceed.cupco.de/iot/Have-a-seed/board",onGetweb);
if (pants == 'W') {
health = false;
}
else if (dB2 > 40 && cry != true) {
cry = true;
health = false;
}
else if (cry == true) {
health = false;
}
else {
health = true;
}
changeInfantState();
getCradleStatus();
autoCradleControl();
}
function onGetweb(result){
var dB = result.substring(1);
dB2 = dB/1024*100;
dB2 = Math.round(dB2);
if (dB2 === parseInt(dB2,10))
dB2 = dB2;
else
dB2 = 0;
pants = result.charAt(0);
setPantStatus();
setSoundStatus();
}
function setPantStatus(){
var pantStatus = $("#pantstatus");
if (pants == 'W')
pantStatus.text("Wet");
else
pantStatus.text("Dry");
}
function setSoundStatus(){
var soundstatus = $("#soundstatus");
soundstatus.text(Math.round(dB2)+"%");
}
function refreshCry(){
memory += Math.round(dB2)+" ";
memory.split
memoryCount++;
}
function resetDelay()
{
if(dB2<40){
cry = false;
if(auto){
power = 0;
requestCradleMovement();
}
}
}
// Anonymous Function
mildBtn.click(function(){
power = 1;
requestCradleMovement();
});
medBtn.click(function(){
power = 2;
requestCradleMovement();
});
stopBtn.click(function(){
power = 0;
requestCradleMovement();
});
// Set Interval
setInterval(refreshData,1000);
setInterval(resetDelay,7000)
// Button implementation
infoPanel.hover(showCradleControl);
mainControl.hover(showInfo);
autoOn.click(toggleAuto);
autoOff.click(toggleAuto);