Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. var WebSocket = require('ws');
  2. var sleep = require('sleep');
  3.  
  4. /*[{"starts": 0, "ends": 20}], //Row 1
  5. [{"starts": 64, "ends": 84}] // Row 2
  6. ];
  7. */
  8. //kiste 1 = 0-20
  9. //kiste 2 = 64
  10.  
  11.  
  12. var device = {};
  13. var getDeviceList = {"type": "list_connected_devices"};
  14. var sendCanvasString = {"type": "device_pixels", "pixels": [], "device": {}};
  15. var Cases = [[{"starts": 0, "ends": 20}], [{"starts": 64, "ends": 84}]]
  16. var ColumnsPerCase = 5;
  17. var RowsPerCase = 4;
  18. var connected = false;
  19. var maxLeds = 512;
  20.  
  21. var Canvas = function () {
  22. var canvas = [];
  23.  
  24. this.emptyCanvas = function () {
  25. for (var x = 0; x < this.getWidth(); x++) {
  26. for (var y = 0; y < this.getHeigth(); y++) {
  27. canvas = canvas.concat([[0, 0, 0]]);
  28. }
  29. }
  30. }
  31.  
  32. this.getSingleLayerCanvas = function () {
  33. var newCanvas = [];
  34.  
  35. for (var pixel = 0; pixel < canvas.length; pixel++) {
  36. if (pixel == 20) {
  37. for (var i = 0; i < 44; i++) {
  38. newCanvas.push(0, 0, 0)
  39. }
  40. }
  41. if (canvas[pixel] != undefined) {
  42. for (var color = 0; color < canvas[pixel].length; color++) {
  43. newCanvas.push(canvas[pixel][color]);
  44. }
  45. }
  46. }
  47.  
  48. return newCanvas;
  49. }
  50.  
  51. this.setRandomPixel = function (rgb) {
  52. var y = Math.floor(Math.random() * (this.getHeigth() + 1));
  53. var x = Math.floor(Math.random() * (this.getWidth() + 1));
  54.  
  55. this.setPixel(x, y, rgb);
  56. }
  57.  
  58. this.getCaseHeigth = function () {
  59. return Cases.length;
  60. }
  61.  
  62. this.getCaseWidth = function (row = 0) {
  63. return Cases[row].length;
  64. }
  65.  
  66. this.getHeigth = function () {
  67. return heigth;
  68. }
  69.  
  70. this.getWidth = function () {
  71. return width;
  72. }
  73.  
  74. this.getCanvas = function () {
  75. return canvas;
  76. }
  77.  
  78. this.setPixel = function (x, y, rgb) {
  79. var pixel = y * ColumnsPerCase + x
  80. canvas[pixel] = rgb;
  81. }
  82.  
  83. var heigth = this.getCaseHeigth() * RowsPerCase;
  84. var width = this.getCaseWidth() * ColumnsPerCase;
  85.  
  86. return this;
  87. }
  88.  
  89. var MateLightConnection = function () {
  90. var ws = new WebSocket('ws://matelight.local:7890');
  91.  
  92. this.sendCanvas = function (canvas) {
  93. if (connected) {
  94. var data = sendCanvasString;
  95. data.pixels = canvas;
  96. data.device = device;
  97.  
  98. ws.send(JSON.stringify(data));
  99. }
  100. }
  101.  
  102. ws.on('open', function open() {
  103. connected = true;
  104. console.log("Connected!");
  105. ws.send(JSON.stringify(getDeviceList));
  106. });
  107.  
  108. ws.on('message', function message(data, flags) {
  109. reply = JSON.parse(data);
  110. if (reply.type == "list_connected_devices") {
  111. device = reply.devices[0];
  112. }
  113. });
  114. }
  115.  
  116. var WikimediaConnection = function (canvas) {
  117. var ws = new WebSocket('ws://wikimon.hatnote.com:9000');
  118.  
  119. ws.on('open', function open() {
  120. console.log("Connected!");
  121. });
  122.  
  123. ws.on('message', function message(data, flags) {
  124. reply = JSON.parse(data);
  125.  
  126. var color = []
  127.  
  128. if (reply.page_title == 'Special:Log/newusers' &&
  129. data.url != 'byemail') {
  130. color = [0, 0, 255]; //Blue
  131. } else {
  132.  
  133. if (reply.is_anon) {
  134. color = [46, 204, 113] //Green
  135. } else if (reply.is_bot) {
  136. color = [55, 89, 182] //Violett
  137. } else {
  138. color = [255, 255, 255] //White
  139. }
  140.  
  141. }
  142. canvas.setRandomPixel(color);
  143.  
  144. });
  145. }
  146.  
  147. function fillCanvasWithRandomNumbers(canvas) {
  148. for (var x = 0; x < canvas.getWidth(); x++) {
  149. for (var y = 0; y < canvas.getHeigth(); y++) {
  150. canvas.setPixel(x, y, [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)]);
  151. }
  152. }
  153.  
  154. return canvas.getSingleLayerCanvas();
  155. }
  156.  
  157. function blub() {
  158. matelightConnection.sendCanvas(canvas.getSingleLayerCanvas()); //Live Wikipedia Changes
  159. //matelightConnection.sendCanvas(fillCanvasWithRandomNumbers(canvas)); //Random Colors
  160. }
  161.  
  162. var matelightConnection = new MateLightConnection();
  163. var canvas = new Canvas();
  164. var wikimediaConnection = new WikimediaConnection(canvas);
  165.  
  166. canvas.getCaseWidth()
  167.  
  168.  
  169. canvas.emptyCanvas();
  170. setInterval(blub, 500);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement