dpv007 commited on
Commit
3e0c793
Β·
1 Parent(s): 28f4cfe
scripts/simulateSensors.js CHANGED
@@ -23,7 +23,7 @@ function generateReading(sensorId) {
23
  sensorId,
24
  timestamp: new Date().toISOString(),
25
  pH: (6.5 + Math.random() * 2).toFixed(2),
26
- turbidity: (Math.random() * 10).toFixed(2),
27
  temperature: (15 + Math.random() * 15).toFixed(2),
28
  hardness: (50 + Math.random() * 200).toFixed(2),
29
  };
 
23
  sensorId,
24
  timestamp: new Date().toISOString(),
25
  pH: (6.5 + Math.random() * 2).toFixed(2),
26
+ turbidity: (100+Math.random() * 10).toFixed(2),
27
  temperature: (15 + Math.random() * 15).toFixed(2),
28
  hardness: (50 + Math.random() * 200).toFixed(2),
29
  };
services/mqttIngestion.js CHANGED
@@ -53,7 +53,7 @@ async function getPotability(pH, hardness, turbidity, temperature) {
53
  try {
54
  // Step 1: Make initial API call to get EVENT_ID
55
  const postData = JSON.stringify({
56
- data: [pH, turbidity, temperature, hardness]
57
  });
58
 
59
  const options = {
@@ -98,29 +98,34 @@ async function getPotability(pH, hardness, turbidity, temperature) {
98
 
99
  for (const line of lines) {
100
  if (!line.startsWith('data:')) continue;
101
-
102
  const jsonStr = line.replace('data:', '').trim();
103
- const json = JSON.parse(jsonStr);
104
- if (Array.isArray(json) && json.length > 0) {
105
- lastValue = json[json.length - 1];
 
 
 
 
 
 
 
 
 
 
106
  }
107
  }
108
 
109
- if (typeof lastValue !== 'string') {
110
- console.error('βœ— Unexpected prediction payload:', lastValue);
111
- resolve(null);
112
- return;
113
- }
114
-
115
- if (!lastValue.includes('Probabilities')) {
116
- console.error('βœ— Potability API returned an error or invalid response:', lastValue);
117
  resolve(null);
118
  return;
119
  }
120
 
121
  const match = lastValue.match(/ Potable:\s*([0-9.]+)/);
122
 
123
- const potability = match ? 1-parseFloat(match[1]) : null;
 
124
 
125
  if (potability === null || Number.isNaN(potability)) {
126
  console.error('βœ— Could not extract Potable confidence from prediction:', lastValue);
@@ -184,7 +189,7 @@ async function processMessage(topic, message) {
184
 
185
  // Get potability prediction from external API
186
  console.log('πŸ”„ Fetching potability prediction...');
187
- const potability = await getPotability(data.pH, data.turbidity, data.temperature, data.hardness);
188
 
189
  if (potability !== null && typeof potability === 'number' && !Number.isNaN(potability)) {
190
  data.potability = potability;
 
53
  try {
54
  // Step 1: Make initial API call to get EVENT_ID
55
  const postData = JSON.stringify({
56
+ data: [pH, hardness, turbidity, temperature]
57
  });
58
 
59
  const options = {
 
98
 
99
  for (const line of lines) {
100
  if (!line.startsWith('data:')) continue;
101
+
102
  const jsonStr = line.replace('data:', '').trim();
103
+ if (!jsonStr) continue;
104
+
105
+ try {
106
+ const json = JSON.parse(jsonStr);
107
+ if (Array.isArray(json) && json.length > 0) {
108
+ // Find the string output that contains 'Probabilities'
109
+ const validStr = json.find(s => typeof s === 'string' && s.includes('Probabilities'));
110
+ if (validStr) {
111
+ lastValue = validStr;
112
+ }
113
+ }
114
+ } catch (e) {
115
+ // ignore parse errors for partial chunks
116
  }
117
  }
118
 
119
+ if (!lastValue) {
120
+ console.error('βœ— Potability API returned an error or invalid response:', predData);
 
 
 
 
 
 
121
  resolve(null);
122
  return;
123
  }
124
 
125
  const match = lastValue.match(/ Potable:\s*([0-9.]+)/);
126
 
127
+ // Extract the Potable probability directly
128
+ const potability = match ? parseFloat(match[1]) : null;
129
 
130
  if (potability === null || Number.isNaN(potability)) {
131
  console.error('βœ— Could not extract Potable confidence from prediction:', lastValue);
 
189
 
190
  // Get potability prediction from external API
191
  console.log('πŸ”„ Fetching potability prediction...');
192
+ const potability = await getPotability(data.pH, data.hardness, data.turbidity, data.temperature);
193
 
194
  if (potability !== null && typeof potability === 'number' && !Number.isNaN(potability)) {
195
  data.potability = potability;
test_api.js ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const https = require('https');
2
+
3
+ const POTABILITY_API = 'https://dpv007-newclear.hf.space/gradio_api/call/predict_potability';
4
+
5
+ function test(pH, hardness, turbidity, temperature) {
6
+ const postData = JSON.stringify({
7
+ data: [pH, hardness, turbidity, temperature]
8
+ });
9
+
10
+ const options = {
11
+ method: 'POST',
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ 'Content-Length': Buffer.byteLength(postData)
15
+ }
16
+ };
17
+
18
+ const req = https.request(POTABILITY_API, options, (res) => {
19
+ let data = '';
20
+ res.on('data', chunk => data += chunk);
21
+ res.on('end', () => {
22
+ console.log("POST Response:", data);
23
+ const eventId = JSON.parse(data).event_id;
24
+
25
+ const predReq = https.request(`${POTABILITY_API}/${eventId}`, {method: 'GET'}, (predRes) => {
26
+ let predData = '';
27
+ predRes.on('data', chunk => predData += chunk);
28
+ predRes.on('end', () => {
29
+ console.log("GET Response:", predData);
30
+ });
31
+ });
32
+ predReq.end();
33
+ });
34
+ });
35
+ req.write(postData);
36
+ req.end();
37
+ }
38
+
39
+ test(6.96, 108.95, 15.57, 90.33);