Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WebSocket = require('ws');
- var sleep = require('sleep');
- /*[{"starts": 0, "ends": 20}], //Row 1
- [{"starts": 64, "ends": 84}] // Row 2
- ];
- */
- //kiste 1 = 0-20
- //kiste 2 = 64
- var device = {};
- var getDeviceList = {"type": "list_connected_devices"};
- var sendCanvasString = {"type": "device_pixels", "pixels": [], "device": {}};
- var Cases = [[{"starts": 0, "ends": 20}], [{"starts": 64, "ends": 84}]]
- var ColumnsPerCase = 5;
- var RowsPerCase = 4;
- var connected = false;
- var maxLeds = 512;
- var Canvas = function () {
- var canvas = [];
- this.emptyCanvas = function () {
- for (var x = 0; x < this.getWidth(); x++) {
- for (var y = 0; y < this.getHeigth(); y++) {
- canvas = canvas.concat([[0, 0, 0]]);
- }
- }
- }
- this.getSingleLayerCanvas = function () {
- var newCanvas = [];
- for (var pixel = 0; pixel < canvas.length; pixel++) {
- if (pixel == 20) {
- for (var i = 0; i < 44; i++) {
- newCanvas.push(0, 0, 0)
- }
- }
- if (canvas[pixel] != undefined) {
- for (var color = 0; color < canvas[pixel].length; color++) {
- newCanvas.push(canvas[pixel][color]);
- }
- }
- }
- return newCanvas;
- }
- this.setRandomPixel = function (rgb) {
- var y = Math.floor(Math.random() * (this.getHeigth() + 1));
- var x = Math.floor(Math.random() * (this.getWidth() + 1));
- this.setPixel(x, y, rgb);
- }
- this.getCaseHeigth = function () {
- return Cases.length;
- }
- this.getCaseWidth = function (row = 0) {
- return Cases[row].length;
- }
- this.getHeigth = function () {
- return heigth;
- }
- this.getWidth = function () {
- return width;
- }
- this.getCanvas = function () {
- return canvas;
- }
- this.setPixel = function (x, y, rgb) {
- var pixel = y * ColumnsPerCase + x
- canvas[pixel] = rgb;
- }
- var heigth = this.getCaseHeigth() * RowsPerCase;
- var width = this.getCaseWidth() * ColumnsPerCase;
- return this;
- }
- var MateLightConnection = function () {
- var ws = new WebSocket('ws://matelight.local:7890');
- this.sendCanvas = function (canvas) {
- if (connected) {
- var data = sendCanvasString;
- data.pixels = canvas;
- data.device = device;
- ws.send(JSON.stringify(data));
- }
- }
- ws.on('open', function open() {
- connected = true;
- console.log("Connected!");
- ws.send(JSON.stringify(getDeviceList));
- });
- ws.on('message', function message(data, flags) {
- reply = JSON.parse(data);
- if (reply.type == "list_connected_devices") {
- device = reply.devices[0];
- }
- });
- }
- var WikimediaConnection = function (canvas) {
- var ws = new WebSocket('ws://wikimon.hatnote.com:9000');
- ws.on('open', function open() {
- console.log("Connected!");
- });
- ws.on('message', function message(data, flags) {
- reply = JSON.parse(data);
- var color = []
- if (reply.page_title == 'Special:Log/newusers' &&
- data.url != 'byemail') {
- color = [0, 0, 255]; //Blue
- } else {
- if (reply.is_anon) {
- color = [46, 204, 113] //Green
- } else if (reply.is_bot) {
- color = [55, 89, 182] //Violett
- } else {
- color = [255, 255, 255] //White
- }
- }
- canvas.setRandomPixel(color);
- });
- }
- function fillCanvasWithRandomNumbers(canvas) {
- for (var x = 0; x < canvas.getWidth(); x++) {
- for (var y = 0; y < canvas.getHeigth(); y++) {
- canvas.setPixel(x, y, [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)]);
- }
- }
- return canvas.getSingleLayerCanvas();
- }
- function blub() {
- matelightConnection.sendCanvas(canvas.getSingleLayerCanvas()); //Live Wikipedia Changes
- //matelightConnection.sendCanvas(fillCanvasWithRandomNumbers(canvas)); //Random Colors
- }
- var matelightConnection = new MateLightConnection();
- var canvas = new Canvas();
- var wikimediaConnection = new WikimediaConnection(canvas);
- canvas.getCaseWidth()
- canvas.emptyCanvas();
- setInterval(blub, 500);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement