-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
130 lines (109 loc) · 4.28 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const NetcatClient = require('netcat/client');
const helpers = {};
helpers.sendToRabbit = function(data, callback) {
console.log(data, typeof data);
if(!data || typeof data !== 'string') return console.log('Error data, must be string');
// change les nombres "string" en "int"
data = JSON.stringify(JSON.parse(data, function(k, v) {
return (typeof v === "object" || isNaN(v)) ? v : parseInt(v, 10);
}));
console.log('netcat send : ', data);
const nc = new NetcatClient();
const client = nc.addr('127.0.0.1').port(10543).connect().send(data, function(err) {
if(err) {
data = err;
console.log('netcat ERROR ', err);
}
else {
if(callback) callback();
}
}).close(function() {
console.log('netcat closed');
});
client.on('data', function(d) {
console.log(JSON.stringify(d));
console.log('---');
console.log(d.toString('ascii'));
if(callback) callback(d);
});
};
helpers.sendChoreographyToRabbit = function(chor) {
console.log('chor', chor);
const base64ch = Buffer.from(String.fromCharCode.apply(null, JSON.parse(chor))).toString('base64');
console.log('base64ch', base64ch);
helpers.sendToRabbit(JSON.stringify({"type": "command", "sequence": [{"choreography": "data:application/x-nabaztag-mtl-choreography;base64," + base64ch}]}));
// helpers.ledWrapper('2', '#f00');
// helpers.sendToRabbit({"sequence": [{"choreography": "data:application/x-nabaztag-mtl-choreography;base64," + base64ch}]});
// Ears step de -17 à 17
// [0,20,ear,dir,0,17,ear,stepsPos]
// wait=0, commande=20 (setmotordir), motor=ear, dir=dir
// wait=0, commande=17 (avance), motor=ear, delta=stepsPos
// Led
// 0 | 1 | 2 | 3 | 4; // nose, left, middle, right and bottom led
// [0,1,10,0,7,led,rgba[0],rgba[1],rgba[2],0,0,15,7,led,0,0,0,0,0]
// wait=0, commande=1 (frame_duration), timescale=10
// wait=0, commande=7 (set_led_color), led=led, r=rgba[0], g=rgba[1], b=rgba[2], ignored=0, ignored=0
// wait=15, commande=7 (set_led_color), led=led, r=0, g=0, b=0, ignored=0, ignored=0
};
helpers.IsJsonString = function(str) {
try {
JSON.parse(str);
}
catch(e) {
return false;
}
return true;
};
helpers.hexToRgbA = function(hex) {
// hexToRgbA('#fbafff')
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
let c = hex.substring(1).split('');
if(c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = '0x' + c.join('');
return '[' + [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + ', 255]';
}
throw new Error('Bad Hex');
};
helpers.colorToRGBA = function(color) {
// Returns the color as an array of [r, g, b, a] -- all range from 0 - 255
// color must be a valid canvas fillStyle. This will cover most anything
// you'd want to use.
// Examples:
// colorToRGBA('red') # [255, 0, 0, 255]
// colorToRGBA('#f00') # [255, 0, 0, 255]
var cvs, ctx;
cvs = document.createElement('canvas');
cvs.height = 1;
cvs.width = 1;
ctx = cvs.getContext('2d');
ctx.fillStyle = color;
ctx.fillRect(0, 0, 1, 1);
return ctx.getImageData(0, 0, 1, 1).data;
};
helpers.ledWrapper = function(led, color, callback) {
// interpreter._nabaztagActions.push({action: "led", parameters: [led.data, color.data]});
const rgba = helpers.hexToRgbA(color);
const base64ch = Buffer.from(String.fromCharCode.apply(null, [0, 7, led, rgba[0], rgba[1], rgba[2], 0, 0])).toString('base64');
console.log('base64ch', base64ch);
helpers.sendToRabbit(JSON.stringify({"sequence": [{"choreography": "data:application/x-nabaztag-mtl-choreography;base64," + base64ch}]}), callback);
};
helpers.getFormattedTime = function() {
const today = new Date();
const y = today.getFullYear();
// JavaScript months are 0-based.
const m = ("0" + (today.getMonth() + 1)).slice(-2);
const d = ("0" + today.getDate()).slice(-2);
const h = ("0" + today.getHours()).slice(-2);
const mi = ("0" + today.getMinutes()).slice(-2);
const s = ("0" + today.getSeconds()).slice(-2);
return y + "-" + m + "-" + d + "_" + h + "-" + mi + "-" + s;
};
helpers.bytesToSize = function(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if(bytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};
module.exports = helpers;