Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Let's require the packs that we need
- const express = require('express') // The main express pack
- const app = express();
- const jsfft = require('jsfft');
- var dgram = require('dgram'); // This packet allows us to reacive UDP packets
- var mysql = require('mysql'); // This packet allow us to CRUD to mysql
- var io = require('socket.io');
- var WatchJS = require("melanke-watchjs")
- var watch = WatchJS.watch;
- var zmq = require('zeromq')
- , sock = zmq.socket('sub')
- var mainSelect = "SELECT bandwidth,frequency,signal_strength FROM ";
- app.get('/', (req, res) => {
- res.send('Hello World!')
- });
- app.listen(8000, () => {
- console.log('Example app listening on port 8000!')
- });
- sock.connect('tcp://192.168.8.255:3000');
- sock.subscribe('');
- console.log('Subscriber connected to port 3000');
- sock.on('message', function(topic, message) {
- console.log('received a message related to:', topic.toString('utf8'), 'containing message:', message.toString('utf8'));
- });
- var con = mysql.createConnection({
- host: "localhost", // ip address of server running mysql
- user: "root", // user name to your mysql database
- password: "nolag", // corresponding password LOL
- database: "Radio" // use the specified database
- });
- // make to connection to the database.
- con.connect(function(err) {
- if (err) throw err;
- });
- function inserData(item){
- con.query("INSERT INTO radio_data (bandwidth,frequency,signal_strength) values ("+item.bandwidth+","+item.frequency+","+item.signal_strength+")", function (err, result, fields) {
- // if any error while executing above query, throw error
- if (err) throw err;
- });
- }
- function selectDataAll(table){
- con.query(mainSelect+table, function (err, result){
- if (err) throw err;
- console.log(result);
- });
- }
- function selectDataByTime(table,_interval,_time){
- var sql = " WHERE time >= DATE_SUB(NOW(), INTERVAL "+_time+" "+_interval+")";
- console.log(mainSelect + table + sql);
- con.query(mainSelect + table + sql , function (err, result){
- if (err) throw err;
- console.log(result);
- });
- }
- //selectDataByTime('radio_data',"HOUR","4");
- var PORT = 5000; // UDP listening port
- var HOST = '0.0.0.0'; // UDP listening IP
- var server = dgram.createSocket('udp4'); // Let's create the UDP socket
- // W'll start listening the UDP
- server.on('listening', function () {
- var address = server.address();
- console.log('UDP Server listening on ' + address.address + ":" + address.port);
- });
- // When we have a UDP packet..
- server.on('message', function (message, remote) {
- var barr = new Float32Array(message);
- var barr2 = new ArrayBuffer(message.length);
- var view = new DataView(barr2);
- var _floatArray = [];
- for (let index = 0; index < barr2.length; index++) {
- view.setUint8(index,barr[index])
- }
- console.log(view);
- for (let index = 0; index < barr2.length/4; index++) {
- var num = view.getFloat32(index);
- console.log(num)
- _floatArray.push(num)
- }
- console.log(_floatArray);
- console.log(peak(_floatArray));
- var num = view.getFloat32(0);
- //console.log(num)
- //console.log(peak(barr));
- });
- function peak(array){
- var max = 0
- for (let index = 0; index < array.length; index++) {
- //console.log(array[index]);
- if(array[index] > max)
- max = array[index];
- }
- console.log("---------------------------------------------------")
- return max;
- }
- console.log(jsfft);
- server.bind(PORT, HOST);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement