Commit d252cd12 authored by Gimhan A.H.L.D.K's avatar Gimhan A.H.L.D.K

add nodejs backend

parent 8e7792f9
PORT = '3010';
API_KEY = '09835ebba2c6a45b02dd9a232e6068c6';
\ No newline at end of file
const express = require('express');
const axios = require('axios');
const firebaseAdmin = require('firebase-admin');
const app = express();
const port = process.env.PORT || 3001;
const serviceAccount = require('./firebase/serviceAccountKey.json');
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: 'https://your-project-id.firebaseio.com'
});
const firestore = firebaseAdmin.firestore();
const weatherDataCollection = firestore.collection('weatherData');
const fetchDeviceAndStore = async (farmId, deviceId) => {
try {
const url = `http://192.168.73.130/api/data/${deviceId}`;
const response = await axios.get(url);
const weatherData = response.data;
const { humidity, temperature } = weatherData;
const timestamp = new Date().toISOString();
await weatherDataCollection.doc(farmId).set({
climateData: {
[timestamp]: { humidity, temperature }
}
}, { merge: true });
} catch (error) {
console.log('Error fetching weather data:', error);
}
};
const fetchInterval = 3600000; // 1 hour in milliseconds
setInterval(async () => {
try {
const deviceSnapshot = await firestore.collection('devices').get();
deviceSnapshot.forEach((deviceDoc) => {
const { deviceId } = deviceDoc.data();
fetchDeviceAndStore(deviceDoc.id, deviceId);
});
} catch (error) {
console.log('Error fetching devices:', error);
}
}, fetchInterval);
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
{
"type": "service_account",
"project_id": "clean-57425",
"private_key_id": "ffd01106fd495f4f9c8b34ee4fc84ebfc59c5a54",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgPvp5xN8g45gC\nrc58W8Yw6OCOtDsaPSjlQYVJ+nQrVHkpOGrtaZIU4zYdMTOQoe8oEEGtAsXW+bbI\nlwLYWuuG9N+LNUyXcnlH/F8/QMhAWb1IwKtqqM6gy079CmTZraYwLF0Ze7GazVmP\nOww6rETgzaPCj/m+yCHACdQOSZno2KT3yTK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\ngCHrFiYB7srG8yDFf3Jp7Kr3VSpNuPwCkSbwuKGawOy3Q8m2XGfHDUO/doyvdSFz\nE0QGGr0P7zzclUyKk4nmYiVKFPCZM2LZFZO3GoVi2cWMjBboFKKIjr1AqGZdj3DE\n61R1RPq+V4D7Y2HJ7C0HUQvvRKRYCUfOi+pfjWECgYEA4wVrJF1Zj79osSwpIXmj\nWOvXSqFvgeVt7dboUZTGN1+iJQRQ9AdlEpXbYyx3jQa5TL5/pPmYq+kMdG4GkrN3\ni+4x3f6f5D4yicF3mQyt6wj96/AYCAzCCE80FUVGr80UuTcJ4GDh5ujohWl2ymkC\npbEZV0RibF1PAEkTY8Vr6Rg=\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-hj20w@clean-57425.iam.gserviceaccount.com",
"client_id": "117627597479089090103",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-hj20w%40clean-57425.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.4.0",
"express": "^4.18.2",
"firebase-admin": "^11.10.1"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment