-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
197 lines (172 loc) · 6.14 KB
/
server.js
File metadata and controls
197 lines (172 loc) · 6.14 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
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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const path = require('path');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json({ limit: '10mb' })); // For base64 images
// --- MACHINE STATE ---
let machine = {
power: false,
setupComplete: false,
isRunning: false,
isPaused: false,
timeLeft: 45 * 60,
totalDuration: 45 * 60,
status: "READY",
mode: 'MANUAL',
settings: { temp: 1, rinse: 1, spin: 2, soil: 1 },
options: {
preSoak: false, delayEnd: false, steam: false,
drumLight: false, superSpeed: false, smartControl: false
},
// NEW: AI scan result
aiScan: {
level: null, // 0-5
label: null, // "Clean" ... "Extreme"
confidence: null, // 0-100
probabilities: [],
scanning: false,
scanned: false
}
};
let timerInterval = null;
const PYTHON_API = 'http://localhost:5001';
io.on('connection', (socket) => {
socket.emit('update', machine);
socket.on('setSetting', (data) => {
if (machine.isRunning || !machine.power) return;
machine.settings[data.type] = parseInt(data.value);
recalculateTime();
io.emit('update', machine);
});
socket.on('selectStartMode', (selectedMode) => {
if (!machine.power) return;
machine.mode = selectedMode;
machine.setupComplete = true;
if (selectedMode === 'AUTO') {
machine.settings = { temp: 1, rinse: 2, spin: 3, soil: 2 };
recalculateTime();
// Reset scan state when entering AUTO
machine.aiScan = { level: null, label: null, confidence: null, probabilities: [], scanning: false, scanned: false };
}
io.emit('update', machine);
});
// NEW: Receive image from browser, forward to Python, apply results
socket.on('analyzeImage', async (imageBase64) => {
if (!machine.power || machine.mode !== 'AUTO') return;
machine.aiScan.scanning = true;
machine.aiScan.scanned = false;
io.emit('update', machine);
try {
const response = await fetch(`${PYTHON_API}/analyze`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: imageBase64 })
});
if (!response.ok) throw new Error(`Python API error: ${response.status}`);
const result = await response.json();
machine.aiScan = {
level: result.level,
label: result.label,
confidence: result.confidence,
probabilities: result.probabilities,
scanning: false,
scanned: true
};
// AUTO: Map dirt level → wash settings
applyAutoSettings(result.level);
recalculateTime();
} catch (err) {
console.error('AI analysis failed:', err.message);
machine.aiScan.scanning = false;
}
io.emit('update', machine);
});
socket.on('toggleOption', (opt) => {
if (!machine.power || machine.isRunning) return;
machine.options[opt] = !machine.options[opt];
recalculateTime();
io.emit('update', machine);
});
socket.on('togglePlay', () => {
if (!machine.power) return;
if (!machine.isRunning) startCycle();
else {
machine.isPaused = !machine.isPaused;
machine.status = machine.isPaused ? "PAUSED" : "RUNNING";
io.emit('update', machine);
}
});
socket.on('togglePower', () => {
machine.power = !machine.power;
if (!machine.power) {
resetMachine();
machine.setupComplete = false;
machine.aiScan = { level: null, label: null, confidence: null, probabilities: [], scanning: false, scanned: false };
}
io.emit('update', machine);
});
socket.on('setMode', (m) => {
if (!machine.power || machine.isRunning) return;
machine.mode = m;
if (m === 'AUTO') {
machine.settings = { temp: 1, rinse: 2, spin: 3, soil: 2 };
recalculateTime();
}
io.emit('update', machine);
});
});
// Map dirt level (0-5) → wash settings
function applyAutoSettings(level) {
const presets = [
{ temp: 0, rinse: 1, spin: 1, soil: 0 }, // 0: Clean
{ temp: 0, rinse: 1, spin: 1, soil: 0 }, // 1: Very Light
{ temp: 1, rinse: 2, spin: 2, soil: 1 }, // 2: Light
{ temp: 1, rinse: 2, spin: 2, soil: 2 }, // 3: Medium
{ temp: 2, rinse: 3, spin: 3, soil: 3 }, // 4: Heavy
{ temp: 2, rinse: 4, spin: 3, soil: 3 }, // 5: Extreme
];
machine.settings = presets[level] || presets[2];
}
function recalculateTime() {
let base = 25;
base += machine.settings.temp * 5;
base += machine.settings.rinse * 12;
base += machine.settings.spin * 3;
base += machine.settings.soil * 8;
if (machine.options.steam) base += 20;
if (machine.options.preSoak) base += 15;
if (machine.options.superSpeed) base -= 10;
if (base < 15) base = 15;
machine.timeLeft = base * 60;
machine.totalDuration = machine.timeLeft;
}
function startCycle() {
machine.isRunning = true;
machine.isPaused = false;
machine.status = "WASHING";
clearInterval(timerInterval);
timerInterval = setInterval(() => {
if (machine.isRunning && !machine.isPaused && machine.power) {
if (machine.timeLeft > 0) {
machine.timeLeft--;
io.emit('update', machine);
} else {
machine.status = "END";
machine.isRunning = false;
clearInterval(timerInterval);
io.emit('update', machine);
}
}
}, 1000);
}
function resetMachine() {
machine.isRunning = false;
machine.isPaused = false;
machine.status = "OFF";
clearInterval(timerInterval);
}
server.listen(3000, () => console.log('ProWash running on http://localhost:3000'));