Guest User

Untitled

a guest
Jan 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. const WebSocket = require('ws');
  2.  
  3. // Turns out you don't need to dump packets at all - you don't even need a valid API key or device ID when it's in LAN mode.
  4. // All you need to know is the IP address of the device, as it happily accepts WebSocket messages with any string as the "apikey".
  5. const IPADDRESS = '192.168.0.72';
  6.  
  7. const seq = () => String(Date.now());
  8. const ws = new WebSocket('ws://' + IPADDRESS + ':8081', [ 'chat' ]);
  9. const nonce = 'nonce';
  10.  
  11. // Log all messages received from the device
  12. ws.on('message', function incoming(json) {
  13. const messageData = JSON.parse(json);
  14. console.log('Received Message: %j', messageData);
  15. });
  16.  
  17. // Define method to send update message to change switch state
  18. function updateState(newState) {
  19. const updateMessageJSON = JSON.stringify({
  20. "action": "update",
  21. "deviceid": nonce,
  22. "apikey": nonce,
  23. "selfApikey": nonce,
  24. "params": {
  25. "switches": [
  26. {
  27. "switch": newState,
  28. "outlet": 0
  29. },
  30. {
  31. "switch": newState,
  32. "outlet": 1
  33. },
  34. {
  35. "switch": newState,
  36. "outlet": 2
  37. },
  38. {
  39. "switch": newState,
  40. "outlet": 3
  41. }
  42. ]
  43. },
  44. "sequence": seq(),
  45. "userAgent": "app"
  46. });
  47.  
  48. console.log('Sending Update Message: %s', updateMessageJSON);
  49. ws.send(updateMessageJSON);
  50. }
  51.  
  52. // Initiate session with device in LAN mode by sending "userOnline" action message
  53. ws.on('open', function initiateSession(ev) {
  54. const initiateSessionMessageJSON = JSON.stringify({
  55. "action": "userOnline",
  56. "ts": String(0 | Date.now / 1000),
  57. "version": 6,
  58. "apikey": nonce,
  59. "sequence": seq(),
  60. "userAgent": "app"
  61. });
  62.  
  63. console.log('Sending User Online Message: %s', initiateSessionMessageJSON);
  64. ws.send(initiateSessionMessageJSON);
  65. });
  66.  
  67. function switchOn() {
  68. console.log('Switching On');
  69. updateState('on');
  70. }
  71.  
  72. function switchOff() {
  73. console.log('Switching Off');
  74. updateState('off');
  75. }
  76.  
  77. function switchOnFor5Seconds() {
  78. switchOn();
  79. setTimeout(switchOff, 5000);
  80. }
  81.  
  82. setInterval(switchOnFor5Seconds, 10000);
Add Comment
Please, Sign In to add comment