我在Firebase Javascript SDK's運行 Node-red 環境的 IoT 設備上使用本機。
- 我的代碼只包含
WRITE和DELETE操作上的火力地堡RealtimeDatabase資料。 - 當物聯網設備打開時,連接也被建立。
- 但
READ除此之外,我從來沒有來自 Firebase 實時資料庫的任何資料。
盡管我沒有READ任何資料,但當我今天早上查看 Firebase 控制臺時,我看到了一個圖表,顯示了我的持續12kB/minute下載和1峰值連接。我雖然有些機器人連接到我的資料庫。所以我關閉了我的 IoT 設備,我在圖表中看到連接也從1到0(所以我確定只有我的 IoT 設備連接到資料庫)。
下載也從12kB/minute到0kB/minute:


- 我將在下面添加我的代碼 - 但如果我沒有使用任何、等方法
on,我會很困惑我下載了哪些資料。onChildAddedonValue - 我對 Firebase 資料庫很陌生,所以如果我犯了一些新手錯誤,請指出正確的方向。
當我僅使用 READ 方法時,為什么我的 IoT 設備會下載任何資料?
當物聯網設備開啟時運行的代碼:
//Load data from Global contexta
const app = global.get('app');
const database = global.get('database');
const firestore = global.get('firestore');
const auth = global.get('auth');
const firebaseConfig = {
//my credentials
};
//Set up Firebase
const fb_app = app.initializeApp(firebaseConfig);
const fb_db = database.getDatabase(); //kdybychom měli vice projektu tak app dame do parametru
const fb_ft = firestore.getFirestore();
const fb_auth = auth.getAuth();
//Save the database reference to Global context
global.set('fb_app', fb_app);
global.set('fb_db', fb_db);
global.set('fb_ft', fb_ft);
global.set('fb_auth', fb_auth);
每 1 秒寫入一次資料的代碼:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp = Math.round(msg.payload / 1000);
var UID = 'uid1';
var a1 = 0 Math.floor(Math.random() * 100);
var p1 = 50 Math.floor(Math.random() * 20);
var t1 = 20 Math.floor(Math.random() * 20);
//Send data to Firebase
const ref = fb.ref(
fb_db,
'device_realtime/' UID '/' timestamp.toString()
);
fb.set(ref, {
a1: a1,
p1: p1,
t1: t1
});
洗掉 60 秒之前的資料的代碼:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp1 = Math.round(msg.payload / 1000) - 60;
var timestamp2 = Math.round(msg.payload / 1000) - 61;
var UID = 'uid1';
//Delete old data from firebase
var reference1 = fb.ref(
fb_db,
'device_realtime/' UID '/' timestamp1.toString()
);
var reference2 = fb.ref(
fb_db,
'device_realtime/' UID '/' timestamp2.toString()
);
fb.remove(reference1)
fb.remove(reference2)
每 1 分鐘寫入一次資料的代碼:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp = Math.round(msg.payload / 1000);
var UID = 'uid1';
var a1 = 0 Math.floor(Math.random() * 100);
var p1 = 50 Math.floor(Math.random() * 20);
var t1 = 20 Math.floor(Math.random() * 20);
//Send data to Firebase
const ref = fb.ref(
fb_db,
'device_trends/' UID '/' timestamp.toString()
);
fb.set(ref, {
a1: a1,
p1: p1,
t1: t1
});
整個流程截圖:

uj5u.com熱心網友回復:
要擴展評論中的討論:
該下載數字考慮了來自資料庫的所有出站流量,包括例如對所進行的寫入的任何可能的OK回應。
根據參考的數字和一些餐巾紙數學,似乎每次寫入都對應于大約 200 位元組的下載資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359712.html
標籤:javascript 节点.js 火力基地 火力实时数据库
