Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let http = require('http');
- let querystring = require('querystring');
- let axios = require('axios');
- let mysql = require('mysql');
- function request(sysId, interval) {
- let data = {
- id: sysId,
- pumpCurrent: Math.sin(interval) * 10,
- compCurrent: Math.sin(interval) * 10,
- tank1Level: Math.sin(interval) * 2,
- tank2Level: Math.sin(interval) * 2,
- tank3Level: Math.sin(interval) * 2
- }
- console.log('sysId: ' + sysId);
- axios.post('http://localhost:8080/api/data', data)
- .then(res => {
- if (res.status != 200) {
- console.log('ERROR');
- }
- })
- .catch(err => {
- console.log('ERROR');
- });
- }
- let sysIDs = [1, 2, 3, 4];
- let c = 0;
- // sysIDs.forEach(id => {
- // request(id, c);
- // });
- setInterval(() => {
- c++;
- console.log('\nRequest #: ' + c);
- sysIDs.forEach(id => {
- request(id, c);
- });
- }, 1000 * 60 * 60);
- let config = {
- dbConnection: {
- remote: {
- connectionLimit: 15,
- host: '173.27.88.239',
- user: 'user',
- password: 'pass',
- port: 3306,
- database: 'biodash'
- },
- local: {
- connectionLimit: 10,
- host: '127.0.0.1',
- user: 'user',
- password: 'pass',
- port: 3306,
- database: 'biodash'
- }
- }
- }
- let pool = mysql.createPool(config.dbConnection.remote);
- let b = new Date(2017, 04, 22);
- for (let i = 0; i < 14; i++) {
- sysIDs.forEach(id => {
- let d = new Date();
- d.setDate(b.getDate() + i);
- let data = {
- systemID: 8,
- pumpCurrent: Math.sin(i) * 10,
- compCurrent: Math.sin(i) * 10,
- tank1Level: Math.sin(i) * 2,
- tank2Level: Math.sin(i) * 2,
- tank3Level: Math.sin(i) * 2,
- time: d.toISOString().slice(0, 19).replace('T', ' ')
- }
- populateTable(data);
- });
- }
- function populateTable(f) {
- pool.getConnection((err, conn) => {
- let query = 'INSERT INTO data (systemID, pumpCurrent, compCurrent, tank1Level, tank2Level, tank3Level, time) VALUES (?, ?, ?, ?, ?, ?, ?)';
- conn.query(query, [f.systemID, f.pumpCurrent, f.compCurrent, f.tank1Level, f.tank2Level, f.tank3Level, f.time], (err, rows) => {
- if (err) {
- console.log(err);
- } else {
- }
- });
- conn.release();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement