-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampler.js
More file actions
113 lines (100 loc) · 3.41 KB
/
sampler.js
File metadata and controls
113 lines (100 loc) · 3.41 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
const http = require('http');
/**
* Task 3/5: Validation Logic
* Simple check to ensure voltage is within a safe physical range (0-5V).
*/
function processData(data) {
if (data.voltage === undefined || data.voltage < 0 || data.voltage > 5) {
return null;
}
return {
sensor_id: data.sensor_id || "UNKNOWN_SENSOR",
voltage: data.voltage,
timestamp: new Date().toISOString()
};
}
/**
* Task 5: Integration with Transformer
* This function sends the validated voltage to the Python Flask service.
*/
function sendToTransformer(validatedData, callback) {
const postData = JSON.stringify({
sensor_id: validatedData.sensor_id,
raw_voltage: validatedData.voltage,
timestamp: validatedData.timestamp
});
const options = {
hostname: '127.0.0.1',
port: 5001, // Port where your Python Transformer is running
path: '/transform',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
}
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
try {
callback(null, JSON.parse(body));
} catch (e) {
callback("Error parsing Transformer response");
}
});
});
req.on('error', (e) => {
callback(`Transformer connection failed: ${e.message}`);
});
req.write(postData);
req.end();
}
/**
* Main Server Logic
*/
const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/sample') {
let body = '';
req.on('data', chunk => { body += chunk.toString(); });
req.on('end', () => {
try {
const input = JSON.parse(body);
const validated = processData(input);
if (!validated) {
res.writeHead(400, {'Content-Type': 'application/json'});
return res.end(JSON.stringify({ error: "Invalid Voltage (Must be 0-5V)" }));
}
// Forward to Transformer
sendToTransformer(validated, (err, transformerResult) => {
if (err) {
res.writeHead(500, {'Content-Type': 'application/json'});
return res.end(JSON.stringify({ error: err }));
}
// Return the final temperature to the original requester
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({
status: "Success",
received_voltage: validated.voltage,
transformer_output: transformerResult
}));
});
} catch (e) {
res.writeHead(400);
res.end(JSON.stringify({ error: "Invalid JSON format" }));
}
});
} else {
res.writeHead(404);
res.end();
}
});
// Start the Sampler
const PORT = 3000;
if (require.main === module) {
server.listen(PORT, () => {
console.log(`Sampler (Node.js) running at http://127.0.0.1:${PORT}`);
console.log(`Forwarding requests to Transformer at http://127.0.0.1:5001/transform`);
});
}
module.exports = { processData };