-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
100 lines (75 loc) · 2.79 KB
/
index.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
const debug = require('debug')('puppeteer');
const debugChildP = debug.extend('cpPython');
const path = require('path');
const spawn = require('child_process').spawn;
const CronJob = require('cron').CronJob;
const puppeteer = require('puppeteer-core');
const Jimp = require('jimp');
// config variables
const config = require('./config.js');
// as a best practice
// all global variables should be referenced via global. syntax
// and their names should always begin with g
global.gConfig = config;
const fileName = 'screenshot.png';
let d = new Date();
const job = new CronJob({
cronTime: global.gConfig.refresh_interval,
timezone: global.gConfig.timezone,
onTick: function() {
let d = new Date();
debug('Display refresh START at', d);
(async () => {
// open puppeteer
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox', '--headless', '--disable-gpu'],
executablePath: '/usr/bin/chromium-browser'
})
// create a new virtual window/page in puppeteer
const page = await browser.newPage();
// set the virtual browser width and size
// should be the same as the eink display
await page.setViewport({
width: global.gConfig.display_width,
height: global.gConfig.display_height
});
// open the MagicMirror site with puppeteer
await page.goto(`http://localhost:${global.gConfig.magicmirror_port}`);
// wait x seconds to load the complete content of the MagicMirror site
// change it in config.js "wait_to_load"
await page.waitFor(global.gConfig.wait_to_load*1000);
await page.screenshot({path: fileName});
await browser.close();
// invert colors for the screenshot
if (global.gConfig.invert_color) {
// read the screenshot
const img = await Jimp.read(fileName);
// invert the image
await img.invert();
//save image
await img.quality(80).writeAsync(fileName);
}
// run the python script to display the screenshots on the eink display
const childPython = spawn('python', ['./ePaperPython/main.py']);
childPython.stdout.on('data', (data) => {
debugChildP(data.toString());
});
childPython.stderr.on('data', (err) => {
debugChildP(err.toString());
});
// if python script was finished
childPython.on('close', (code) => {
debugChildP(`child process (Python ePaperPython) exited with code ${code} (0=success).`);
const date = new Date();
if(code===0) debug('Display refresh END at', date);
});
})();
},
onComplete: function () {
const date = new Date();
debug('puppeteer STOPPED at ', d);
},
start: true,
runOnInit: true,
});
debug('Script START at', d);