Advertisement
FlyFar

tweet_repel.js

Mar 16th, 2023
2,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.88 KB | Cybersecurity | 0 0
  1. var arDrone = require('..');
  2. var http    = require('http');
  3.  
  4. var Twit = require('twit');
  5.  
  6. var T = new Twit({
  7.     consumer_key:         '....',
  8.     consumer_secret:      '....',
  9.     access_token:         '....',
  10.     access_token_secret:  '....'
  11.   });
  12.  
  13.  
  14. var client = arDrone.createClient();
  15. client.disableEmergency();
  16.  
  17. console.log('Connecting png stream ...');
  18. var pngStream = client.getPngStream();
  19.  
  20. console.log('Connecting twitter stream ...');
  21. var stream = T.stream('user');
  22.  
  23. var lastPng;
  24. pngStream
  25.   .on('error', console.log)
  26.   .on('data', function(pngBuffer) {
  27.     lastPng = pngBuffer;
  28.   });
  29.  
  30. var server = http.createServer(function(req, res) {
  31.   if (!lastPng) {
  32.     res.writeHead(503);
  33.     res.end('Did not receive any png data yet.');
  34.     return;
  35.   }
  36.  
  37.   res.writeHead(200, {'Content-Type': 'image/png'});
  38.   res.end(lastPng);
  39. });
  40.  
  41. function do_action(str) {
  42.     switch (true) {
  43.       case /^to/.test(str):
  44.         console.log("• doing takeoff");
  45.         client.takeoff();
  46.         break;
  47.       case /^la/.test(str):
  48.         console.log("• doing land");
  49.         client.land();
  50.         break;
  51.       case /^st/.test(str):
  52.         console.log("• doing stop");
  53.         client.stop();
  54.         break;
  55.       case /^cw/.test(str):
  56.         console.log("• rotate clockwise");
  57.         client.clockwise(0.5);
  58.         break;
  59.       case /^cc/.test(str):
  60.         console.log("• rotate counterclockwise");
  61.         client.clockwise(-0.5);
  62.         break;
  63.       default:
  64.         console.log("• unknown command:" + str + ":");
  65.         break;
  66.     }
  67.   }
  68.  
  69. console.log('streaming from twitter...');
  70. stream.on('tweet', function (tweet) {
  71.  
  72.   var msg = tweet.text.split(" ").slice(1).join(" ");
  73.   console.log("got msg from twitter:" + msg + ":");
  74.   do_action(msg);
  75.  
  76. });
  77.  
  78. server.listen(8080, function() {
  79.   console.log('Serving latest png on port 8080 ...');
  80.  
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement