No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

device.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var express = require('express')
  2. var app = express()
  3. var request = require('request');
  4. var argv = require('yargs').argv;
  5. // --local_ip
  6. // --local_port
  7. // --local_name
  8. // --remote_ip
  9. // --remote_port
  10. // --remote_name
  11. // --send_period
  12. var LOCAL_ENDPOINT = {IP : argv.local_ip, PORT : argv.local_port, NAME : argv.local_name};
  13. var REMOTE_ENDPOINT = {IP : argv.remote_ip, PORT : argv.remote_port, NAME : argv.remote_name};
  14. var DATA_PERIOD = argv.send_period;
  15. function doPOST(uri, body, onResponse) {
  16. request({method: 'POST', uri: uri, json : body}, onResponse);
  17. }
  18. function register() {
  19. doPOST(
  20. 'http://' + REMOTE_ENDPOINT.IP + ':' + REMOTE_ENDPOINT.PORT + '/devices/register',
  21. {
  22. Name : LOCAL_ENDPOINT.NAME,
  23. PoC : 'http://' + LOCAL_ENDPOINT.IP + ':' + LOCAL_ENDPOINT.PORT,
  24. },
  25. function(error, response, respBody) {
  26. console.log(respBody);
  27. }
  28. );
  29. }
  30. var dataItem = 0;
  31. function sendData() {
  32. doPOST(
  33. 'http://' + REMOTE_ENDPOINT.IP + ':' + REMOTE_ENDPOINT.PORT + '/device/'+ LOCAL_ENDPOINT.NAME + '/data',
  34. {
  35. Name : LOCAL_ENDPOINT.NAME,
  36. Data : dataItem++,
  37. Time : Date.now(),
  38. },
  39. function(error, response, respBody) {
  40. console.log(respBody);
  41. }
  42. );
  43. }
  44. register();
  45. setInterval(sendData, DATA_PERIOD);