-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainAlgorithm.js
More file actions
405 lines (348 loc) · 16.2 KB
/
Copy pathtrainAlgorithm.js
File metadata and controls
405 lines (348 loc) · 16.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* 🎓 TRAINING MODE: F8-Controlled Learning System
* Purpose: Pause automation to manually teach correct irrigation points
*
* Press F8 to resume automation after clicking correct points
*/
import fs from 'fs';
import path from 'path';
const TRAINING_FILE = './training/training-data.json';
/**
* Train the algorithm by allowing manual point selection
*
* @param {Page} page - Playwright page object
* @param {string} farmName - Name of current farm
* @param {string} date - Current date being processed
* @param {Object} predictedFirst - Algorithm's first point prediction {x, y, svgX, svgY}
* @param {Object} predictedLast - Algorithm's last point prediction {x, y, svgX, svgY}
* @returns {Promise<Object>} - User corrections and offsets
*/
export async function trainAlgorithm(page, farmName, date, predictedFirst, predictedLast) {
console.log('\n🎓 ════════════════════════════════════════');
console.log('🎓 TRAINING MODE: F8 TO RESUME');
console.log('🎓 ════════════════════════════════════════\n');
console.log(` Farm: ${farmName}`);
console.log(` Date: ${date}`);
console.log(` Predicted First (START): Screen(${predictedFirst.x}, ${predictedFirst.y})`);
console.log(` Predicted Last (END): Screen(${predictedLast.x}, ${predictedLast.y})\n`);
// ═══════════════════════════════════════════════════════════════════════
// STEP 1: Inject Visual Interface (Banner + Click Listener + F8 Handler)
// ═══════════════════════════════════════════════════════════════════════
await page.evaluate((first, last) => {
// Reset state
window._userClicks = [];
window._resumeAutomation = false;
// ─────────────────────────────────────────────────────────────────────
// 1A. CREATE BANNER
// ─────────────────────────────────────────────────────────────────────
const banner = document.createElement('div');
banner.id = 'training-banner';
banner.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
background: linear-gradient(135deg, #FF6B6B 0%, #FFD93D 50%, #6BCF7F 100%);
color: #000;
padding: 25px;
text-align: center;
font-size: 24px;
font-weight: bold;
z-index: 2147483647;
box-shadow: 0 8px 16px rgba(0,0,0,0.4);
font-family: 'Arial', sans-serif;
border-bottom: 5px solid #333;
animation: bannerPulse 2s ease-in-out infinite;
`;
banner.innerHTML = `
🎓 LEARNING MODE 🎓<br>
<span style="font-size: 18px; font-weight: normal; color: #333;">
Click correct points: <span style="color: #00ff00; font-weight: bold;">Start=Green</span>,
<span style="color: #ff0000; font-weight: bold;">End=Red</span>
</span><br>
<span style="font-size: 22px; color: #fff; background: #000; padding: 5px 15px; border-radius: 5px; margin-top: 10px; display: inline-block;">
Press [F8] to Resume ⏩
</span>
`;
// Add pulse animation
const style = document.createElement('style');
style.textContent = `
@keyframes bannerPulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.02); }
}
@keyframes dotPulse {
0%, 100% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.3); opacity: 0.7; }
}
`;
document.head.appendChild(style);
document.body.appendChild(banner);
// ─────────────────────────────────────────────────────────────────────
// 1B. SHOW PREDICTED POINTS (for reference)
// ─────────────────────────────────────────────────────────────────────
const overlay = document.createElement('div');
overlay.id = 'training-overlay';
overlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
pointer-events: none;
z-index: 2147483646;
`;
// First point (GREEN)
const firstDot = document.createElement('div');
firstDot.style.cssText = `
position: absolute;
left: ${first.x - 30}px;
top: ${first.y - 30}px;
width: 60px;
height: 60px;
border: 6px dashed #00ff00;
border-radius: 50%;
background: rgba(0, 255, 0, 0.2);
pointer-events: none;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.6);
`;
overlay.appendChild(firstDot);
const firstLabel = document.createElement('div');
firstLabel.innerHTML = '🟢 FIRST (Predicted)';
firstLabel.style.cssText = `
position: absolute;
left: ${first.x - 70}px;
top: ${first.y - 60}px;
background: #00ff00;
color: #000;
padding: 8px 12px;
border-radius: 5px;
font-size: 14px;
font-weight: bold;
pointer-events: none;
`;
overlay.appendChild(firstLabel);
// Last point (RED)
const lastDot = document.createElement('div');
lastDot.style.cssText = `
position: absolute;
left: ${last.x - 30}px;
top: ${last.y - 30}px;
width: 60px;
height: 60px;
border: 6px dashed #ff0000;
border-radius: 50%;
background: rgba(255, 0, 0, 0.2);
pointer-events: none;
box-shadow: 0 0 20px rgba(255, 0, 0, 0.6);
`;
overlay.appendChild(lastDot);
const lastLabel = document.createElement('div');
lastLabel.innerHTML = '🔴 LAST (Predicted)';
lastLabel.style.cssText = `
position: absolute;
left: ${last.x - 70}px;
top: ${last.y - 60}px;
background: #ff0000;
color: #fff;
padding: 8px 12px;
border-radius: 5px;
font-size: 14px;
font-weight: bold;
pointer-events: none;
`;
overlay.appendChild(lastLabel);
document.body.appendChild(overlay);
// ─────────────────────────────────────────────────────────────────────
// 1C. CLICK LISTENER (on entire document)
// ─────────────────────────────────────────────────────────────────────
window._clickHandler = function(event) {
// Ignore clicks on banner
if (event.target.closest('#training-banner')) return;
const clickNum = window._userClicks.length + 1;
const color = clickNum === 1 ? '#FFD700' : clickNum === 2 ? '#FF6347' : '#00BFFF';
const label = clickNum === 1 ? 'START' : clickNum === 2 ? 'END' : `#${clickNum}`;
// Save click coordinates
window._userClicks.push({
x: event.clientX,
y: event.clientY,
timestamp: Date.now(),
label: label
});
console.log(`[TRAINING] Click #${clickNum}: (${event.clientX}, ${event.clientY}) - ${label}`);
// Draw visual feedback dot
const dot = document.createElement('div');
dot.style.cssText = `
position: fixed;
left: ${event.clientX - 15}px;
top: ${event.clientY - 15}px;
width: 30px;
height: 30px;
border: 4px solid ${color};
border-radius: 50%;
background: ${color}AA;
z-index: 2147483645;
pointer-events: none;
animation: dotPulse 1s ease-in-out infinite;
box-shadow: 0 0 15px ${color};
`;
const labelDiv = document.createElement('div');
labelDiv.innerHTML = label;
labelDiv.style.cssText = `
position: fixed;
left: ${event.clientX - 25}px;
top: ${event.clientY + 20}px;
background: ${color};
color: #000;
padding: 4px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: bold;
z-index: 2147483645;
pointer-events: none;
`;
document.body.appendChild(dot);
document.body.appendChild(labelDiv);
// Update banner with click count
const banner = document.getElementById('training-banner');
if (banner && clickNum <= 2) {
const clickInfo = document.createElement('div');
clickInfo.style.cssText = `
font-size: 16px;
color: ${color};
margin-top: 5px;
font-weight: bold;
`;
clickInfo.textContent = `✓ ${label} clicked (${clickNum}/2)`;
banner.appendChild(clickInfo);
}
};
document.addEventListener('click', window._clickHandler, true);
// ─────────────────────────────────────────────────────────────────────
// 1D. F8 KEY HANDLER (Resume Automation)
// ─────────────────────────────────────────────────────────────────────
window._keyHandler = function(event) {
if (event.key === 'F8' || event.keyCode === 119) {
event.preventDefault();
event.stopPropagation();
console.log('[TRAINING] F8 pressed! Resuming automation...');
window._resumeAutomation = true;
// Update banner
const banner = document.getElementById('training-banner');
if (banner) {
banner.style.background = 'linear-gradient(135deg, #00ff00 0%, #00aa00 100%)';
banner.innerHTML = `
✅ RESUMED - Processing clicks...<br>
<span style="font-size: 18px;">Automation continuing...</span>
`;
}
// Remove handlers
document.removeEventListener('click', window._clickHandler, true);
document.removeEventListener('keydown', window._keyHandler, true);
}
};
document.addEventListener('keydown', window._keyHandler, true);
console.log('[TRAINING] ✅ Training UI injected. Waiting for F8...');
}, predictedFirst, predictedLast);
console.log(' ⏸️ Automation PAUSED - Waiting for F8 key press...');
console.log(' → Click the correct irrigation points on the chart');
console.log(' → Press F8 when ready to continue\n');
// ═══════════════════════════════════════════════════════════════════════
// STEP 2: WAIT FOR F8 (The Pause Mechanism)
// ═══════════════════════════════════════════════════════════════════════
try {
await page.waitForFunction(
() => window._resumeAutomation === true,
{ timeout: 0 } // No timeout - wait indefinitely
);
console.log(' ✅ F8 detected! Resuming automation...\n');
} catch (error) {
console.log(' ⚠️ Wait interrupted:', error.message);
}
// ═══════════════════════════════════════════════════════════════════════
// STEP 3: RETRIEVE USER CLICKS & CALCULATE OFFSETS
// ═══════════════════════════════════════════════════════════════════════
const userClicks = await page.evaluate(() => {
const clicks = window._userClicks || [];
// Cleanup UI
const banner = document.getElementById('training-banner');
const overlay = document.getElementById('training-overlay');
if (banner) banner.remove();
if (overlay) overlay.remove();
// Remove all training dots
document.querySelectorAll('[style*="2147483645"]').forEach(el => el.remove());
return clicks;
});
console.log(` 📝 Retrieved ${userClicks.length} user clicks`);
let trainingData = {
userClicks: userClicks,
hasCorrections: false,
offsets: null
};
if (userClicks.length >= 2) {
const userFirst = userClicks[0];
const userLast = userClicks[1];
// Calculate offsets
const offsetFirst = {
x: userFirst.x - predictedFirst.x,
y: userFirst.y - predictedFirst.y
};
const offsetLast = {
x: userLast.x - predictedLast.x,
y: userLast.y - predictedLast.y
};
console.log(` 📊 Offsets calculated:`);
console.log(` FIRST: X=${offsetFirst.x.toFixed(1)}px, Y=${offsetFirst.y.toFixed(1)}px`);
console.log(` LAST: X=${offsetLast.x.toFixed(1)}px, Y=${offsetLast.y.toFixed(1)}px`);
trainingData.hasCorrections = true;
trainingData.offsets = { first: offsetFirst, last: offsetLast };
// ═══════════════════════════════════════════════════════════════════════
// STEP 4: SAVE TO TRAINING FILE
// ═══════════════════════════════════════════════════════════════════════
const trainingEntry = {
timestamp: new Date().toISOString(),
farm: farmName,
date: date,
algorithmPrediction: {
first: predictedFirst,
last: predictedLast
},
userCorrections: {
first: userFirst,
last: userLast
},
offsets: trainingData.offsets,
feedback: `User provided ${userClicks.length} clicks`
};
// Load existing training data
let allTrainingData = [];
if (fs.existsSync(TRAINING_FILE)) {
try {
const fileContent = fs.readFileSync(TRAINING_FILE, 'utf-8');
allTrainingData = JSON.parse(fileContent);
} catch (err) {
console.log(' ⚠️ Could not parse existing training file, creating new');
allTrainingData = [];
}
}
// Append new entry
allTrainingData.push(trainingEntry);
// Ensure directory exists
const trainingDir = path.dirname(TRAINING_FILE);
if (!fs.existsSync(trainingDir)) {
fs.mkdirSync(trainingDir, { recursive: true });
}
// Save to file
fs.writeFileSync(TRAINING_FILE, JSON.stringify(allTrainingData, null, 2));
console.log(` 💾 Training data saved to: ${TRAINING_FILE}`);
console.log(` 📈 Total training entries: ${allTrainingData.length}\n`);
} else if (userClicks.length === 1) {
console.log(` ⚠️ Only 1 click detected - need at least 2 (START and END)`);
console.log(` → Skipping training save\n`);
} else {
console.log(` ℹ️ No clicks detected - accepting algorithm prediction`);
console.log(` → No training data saved\n`);
}
console.log('🎓 ✅ Training complete. Resuming automation...\n');
return trainingData;
}