Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (98 loc) · 3.33 KB
/
Copy pathserver.js
File metadata and controls
112 lines (98 loc) · 3.33 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
import express from 'express';
import { createServer } from 'http';
import { Server } from 'socket.io';
import os from 'os';
// Detect local network IP
function getLocalIp() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '127.0.0.1';
}
const LOCAL_IP = getLocalIp();
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: '*',
methods: ['GET', 'POST'],
},
});
// Shared demo state
let demoState = {
phase: 'IDLE', // IDLE | SATELLITE_PASS | DEPLOYING | SCANNING | ANALYZING | REPORT_READY | FIELD_SCAN
activeFieldId: null, // which field is being scanned (for single-field mode)
connectedFarmers: {}, // { fieldId: socketId }
fieldStatuses: {}, // { A: 'idle', B: 'scanning', ... }
};
io.on('connection', (socket) => {
console.log(`[Socket] Client connected: ${socket.id}`);
// Send current state AND local IP to newly connected client
socket.emit('stateUpdate', demoState);
socket.emit('localIp', LOCAL_IP);
// Farmer joins a specific field
socket.on('joinField', (fieldId) => {
demoState.connectedFarmers[fieldId] = socket.id;
console.log(`[Socket] Farmer joined field ${fieldId}`);
io.emit('farmerConnected', { fieldId, socketId: socket.id });
socket.emit('stateUpdate', demoState);
});
// Farmer requests a drone scan for their field
socket.on('requestScan', (fieldId) => {
console.log(`[Socket] Scan requested for field ${fieldId}`);
demoState.phase = 'FIELD_SCAN';
demoState.activeFieldId = fieldId;
demoState.fieldStatuses[fieldId] = 'scanning';
io.emit('stateUpdate', demoState);
io.emit('scanRequested', { fieldId });
});
// HQ reports scan complete for a field
socket.on('scanComplete', ({ fieldId, status }) => {
console.log(`[Socket] Scan complete for field ${fieldId}: ${status}`);
demoState.fieldStatuses[fieldId] = status;
demoState.phase = 'IDLE';
demoState.activeFieldId = null;
io.emit('stateUpdate', demoState);
io.emit('fieldReport', { fieldId, status });
});
// HQ triggers the full satellite + drone demo (original flow)
socket.on('startFullDemo', () => {
console.log('[Socket] Full demo started');
demoState.phase = 'SATELLITE_PASS';
io.emit('stateUpdate', demoState);
});
// HQ updates the phase
socket.on('updatePhase', (phase) => {
demoState.phase = phase;
io.emit('stateUpdate', demoState);
});
// Reset everything
socket.on('resetDemo', () => {
demoState = {
phase: 'IDLE',
activeFieldId: null,
connectedFarmers: {},
fieldStatuses: {},
};
io.emit('stateUpdate', demoState);
});
socket.on('disconnect', () => {
// Remove farmer from connected list
for (const [fieldId, sid] of Object.entries(demoState.connectedFarmers)) {
if (sid === socket.id) {
delete demoState.connectedFarmers[fieldId];
io.emit('farmerDisconnected', { fieldId });
}
}
console.log(`[Socket] Client disconnected: ${socket.id}`);
});
});
const PORT = 3001;
httpServer.listen(PORT, '0.0.0.0', () => {
console.log(`[AgriSense Sync] Socket.IO server running on http://0.0.0.0:${PORT}`);
});