const os = require('os'); const dbcloud = require('./dbcloud.js'); const dotenv = require('dotenv'); const axios = require('axios').default; dotenv.config({path: '../.env'}); module.exports = { // qrcode: async function(mobileuid, mobilename, qr ) { // payload = {} // payload['mobileuid'] = mobileuid; // payload['mobilename'] = mobilename; // payload['qrcode'] = qr; // await axios.post(process.env.URL_QRCODE, payload, { timeout: 2000, headers: { 'content-type': 'application/json' } }); // console.log(new Date().toISOString() + " >>> SEND QR CODE ---------------------------------------"); // return 200; // }, monitor: async function(mobileuid, mobilename, stat) { let _totalmem = parseInt(os.totalmem()); let _freemem = parseInt(os.freemem()); let _memory = 100 - (_freemem / _totalmem * 100); payload = {} payload['mobileuid'] = mobileuid; payload['mobilename'] = mobilename; payload['memory'] = _memory; let db = dbcloud.getDb(); let mco = await db.collection('tab_counts').find({ "_id": mobileuid }).limit(1).toArray(); if ( mco.length == 0 ) { payload['_id'] = mobileuid; payload['hour'] = 0; payload['in'] = 0; payload['out'] = 0; payload['lastmessage'] = new Date(new Date() + 'UTC'); payload['in_today'] = 0; payload['out_today'] = 0; await db.collection('tab_counts').insertOne(payload); }else{ payload['hour'] = mco[0]['hour']; payload['in'] = mco[0]['in']; payload['out'] = mco[0]['out']; payload['lastmessage'] = mco[0]['lastmessage'] payload['in_today'] = mco[0]['in_today']; payload['out_today'] = mco[0]['out_today']; } payload['dt'] = new Date(new Date() + 'UTC'); payload['status'] = stat; console.log(new Date().toISOString() + " >>> SEND MONITOR ALARM ---------------------------------------"); console.log(new Date().toISOString() + " >>> payload: ",payload); let monitor = await db.collection('tab_monitor').find({ "_id": mobileuid }).limit(1).toArray(); if ( monitor.length == 0 ) { payload['_id'] = mobileuid await db.collection('tab_monitor').insertOne(payload); } else{ await db.collection('tab_monitor').updateOne({ "_id": mobileuid }, { $set: payload }, true); } return 200; } }