-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.js
More file actions
94 lines (81 loc) · 3.23 KB
/
Copy pathsimulator.js
File metadata and controls
94 lines (81 loc) · 3.23 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
const axios = require('axios');
const express = require('express'); // NEW: Import the web framework
const app = express(); // NEW: Create a dummy app
// Securely read keys from Render's Environment Variables
const CONNECTION_KEY = process.env.CONNECTION_KEY;
const API_KEY = process.env.API_KEY;
const NODE_ID = process.env.NODE_ID;
const DEVICE_URL = 'https://device.ap-in-1.anedya.io/v1';
const CLOUD_URL = 'https://api.ap-in-1.anedya.io/v1';
/**
* Send random telemetry to Anedya Device Cloud
* Directly uses the Device API with Connection Key
*/
async function sendTelemetry() {
const temperature = parseFloat((Math.random() * (30 - 20) + 20).toFixed(2));
const humidity = parseFloat((Math.random() * (60 - 40) + 40).toFixed(2));
const timestamp = Date.now();
// FORCE strict JSON formatting to bypass Anedya's invalid_json filter
const payload = JSON.stringify({
data: [
{ variable: 'temperature', value: temperature, timestamp: timestamp },
{ variable: 'humidity', value: humidity, timestamp: timestamp }
]
});
try {
await axios.post(`${DEVICE_URL}/submitData`, payload, {
headers: {
'Content-Type': 'application/json',
'Auth-mode': 'key',
'Authorization': CONNECTION_KEY
}
});
console.log(`[Telemetry] Sent to Cloud: Temp=${temperature}°C, Hum=${humidity}%`);
} catch (error) {
console.error(`[Telemetry Error]: ${error.response?.status || 'Unknown'} - ${JSON.stringify(error.response?.data) || error.message}`);
}
}
/**
* Poll for pending commands from Cloud
* Directly uses the Cloud API with Project API Key
*/
async function fetchCommands() {
try {
// Force strict JSON formatting here too, just to be safe
const payload = JSON.stringify({ nodeId: NODE_ID });
const response = await axios.post(`${CLOUD_URL}/commands/fetch`, payload, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
});
if (response.data.success && response.data.commands) {
response.data.commands.forEach(cmd => {
console.log(`\n🔔 [Command Received] -> ${cmd.command}: ${cmd.data}`);
if (cmd.command === 'toggle-relay') {
console.log(`✅ Hardware Relay state updated to: ${cmd.data}\n`);
}
});
}
} catch (error) {
// Silent error for polling frequency
}
}
console.log(`🚀 Anedya IoT Hardware Simulator (STRICT CLOUD MODE)`);
console.log(`- Communication: Strict Cloud API (AP-IN-1)`);
console.log('----------------------------------------------------');
// Main loop
setInterval(sendTelemetry, 5000);
setInterval(fetchCommands, 2000);
sendTelemetry();
// ==========================================
// RENDER "WEB SERVICE" HACK
// This keeps the free tier instance alive!
// ==========================================
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Simulator is running 24/7 in the background!');
});
app.listen(PORT, () => {
console.log(`🌐 Dummy Web Server listening on port ${PORT} to satisfy Render.`);
});