Advertisement
Guest User

Untitled

a guest
Sep 29th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1.  
  2. // Let's require the packs that we need
  3. const express = require('express') // The main express pack
  4. const app = express();
  5. const jsfft = require('jsfft');
  6.  
  7. var dgram = require('dgram'); // This packet allows us to reacive UDP packets
  8. var mysql = require('mysql'); // This packet allow us to CRUD to mysql
  9. var io = require('socket.io');
  10. var WatchJS = require("melanke-watchjs")
  11. var watch = WatchJS.watch;
  12. var zmq = require('zeromq')
  13. , sock = zmq.socket('sub')
  14.  
  15.  
  16.  
  17.  
  18. var mainSelect = "SELECT bandwidth,frequency,signal_strength FROM ";
  19.  
  20.  
  21. app.get('/', (req, res) => {
  22. res.send('Hello World!')
  23. });
  24.  
  25. app.listen(8000, () => {
  26. console.log('Example app listening on port 8000!')
  27. });
  28.  
  29.  
  30. sock.connect('tcp://192.168.8.255:3000');
  31. sock.subscribe('');
  32. console.log('Subscriber connected to port 3000');
  33.  
  34. sock.on('message', function(topic, message) {
  35. console.log('received a message related to:', topic.toString('utf8'), 'containing message:', message.toString('utf8'));
  36. });
  37.  
  38. var con = mysql.createConnection({
  39. host: "localhost", // ip address of server running mysql
  40. user: "root", // user name to your mysql database
  41. password: "nolag", // corresponding password LOL
  42. database: "Radio" // use the specified database
  43. });
  44.  
  45. // make to connection to the database.
  46. con.connect(function(err) {
  47. if (err) throw err;
  48. });
  49.  
  50.  
  51. function inserData(item){
  52. con.query("INSERT INTO radio_data (bandwidth,frequency,signal_strength) values ("+item.bandwidth+","+item.frequency+","+item.signal_strength+")", function (err, result, fields) {
  53. // if any error while executing above query, throw error
  54. if (err) throw err;
  55. });
  56. }
  57.  
  58. function selectDataAll(table){
  59. con.query(mainSelect+table, function (err, result){
  60. if (err) throw err;
  61. console.log(result);
  62.  
  63. });
  64. }
  65.  
  66. function selectDataByTime(table,_interval,_time){
  67. var sql = " WHERE time >= DATE_SUB(NOW(), INTERVAL "+_time+" "+_interval+")";
  68. console.log(mainSelect + table + sql);
  69. con.query(mainSelect + table + sql , function (err, result){
  70.  
  71. if (err) throw err;
  72. console.log(result);
  73.  
  74. });
  75. }
  76.  
  77. //selectDataByTime('radio_data',"HOUR","4");
  78.  
  79. var PORT = 5000; // UDP listening port
  80. var HOST = '0.0.0.0'; // UDP listening IP
  81. var server = dgram.createSocket('udp4'); // Let's create the UDP socket
  82.  
  83. // W'll start listening the UDP
  84. server.on('listening', function () {
  85. var address = server.address();
  86. console.log('UDP Server listening on ' + address.address + ":" + address.port);
  87. });
  88.  
  89.  
  90. // When we have a UDP packet..
  91. server.on('message', function (message, remote) {
  92. var barr = new Float32Array(message);
  93. var barr2 = new ArrayBuffer(message.length);
  94. var view = new DataView(barr2);
  95. var _floatArray = [];
  96.  
  97.  
  98. for (let index = 0; index < barr2.length; index++) {
  99. view.setUint8(index,barr[index])
  100.  
  101. }
  102. console.log(view);
  103. for (let index = 0; index < barr2.length/4; index++) {
  104. var num = view.getFloat32(index);
  105. console.log(num)
  106. _floatArray.push(num)
  107. }
  108. console.log(_floatArray);
  109. console.log(peak(_floatArray));
  110. var num = view.getFloat32(0);
  111. //console.log(num)
  112. //console.log(peak(barr));
  113.  
  114. });
  115.  
  116. function peak(array){
  117. var max = 0
  118. for (let index = 0; index < array.length; index++) {
  119. //console.log(array[index]);
  120. if(array[index] > max)
  121. max = array[index];
  122. }
  123.  
  124. console.log("---------------------------------------------------")
  125. return max;
  126. }
  127.  
  128.  
  129. console.log(jsfft);
  130.  
  131. server.bind(PORT, HOST);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement