This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TheaterMode.js
109 lines (91 loc) · 2.56 KB
/
TheaterMode.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
import { Component } from "react";
import ColorsPreview from "./ColorsPreview";
import { theaterModeStyles } from "./../styles/components/TheaterMode";
class TheaterMode extends Component {
constructor(props) {
super(props);
this.state = {
color: [],
client: null,
};
this.watchScreenInterval = null;
}
componentWillReceiveProps({ client }) {
// Wait until bridge is connected and lights found
if (!this.state.client && client && !this.watchScreenInterval) {
this.startListeners(client);
}
}
componentWillUnmount() {
if (this.watchScreenInterval) {
clearInterval(this.watchScreenInterval);
this.watchScreenInterval = null;
}
}
startListeners(client) {
this.setState(
{
client,
},
() => {
this.initScreenObserver();
},
);
}
componentDidMount() {
if (this.props.client && !this.watchScreenInterval) {
this.startListeners(this.props.client);
}
}
initScreenObserver() {
const screenshot = this.props.remote.require("desktop-screenshot");
const getColors = this.props.remote.require("./getImageColors");
const app = this.props.remote.app;
const pathName = app.getPath("temp") + "screenshot.png";
const readScreenColors = async () => {
if (!this.state.client || !this.watchScreenInterval) {
return false;
}
try {
const { color, brightness } = await getColors(pathName);
this.props.switchMultipleLights(color, brightness);
} catch (err) {
console.log("Something weird happened 🤔", err);
} finally {
readScreenColors();
}
};
this.watchScreenInterval = setInterval(() => {
screenshot(pathName, {});
}, 250);
readScreenColors();
}
getEncodedScreenshot() {
let absoluteScreenshotPath;
if (this.props.remote) {
const fs = this.props.remote.require("fs");
absoluteScreenshotPath =
this.props.remote.app.getPath("temp") + "screenshot.png";
const bitmap = fs.readFileSync(absoluteScreenshotPath);
return new Buffer(bitmap).toString("base64");
}
return false;
}
render() {
const { color } = this.props;
const lastScreenshot = this.getEncodedScreenshot();
return (
<div>
{lastScreenshot && (
<img
className="screenshot"
src={"data:image/png;base64," + lastScreenshot}
/>
)}
<ColorsPreview color={color} />
<style jsx>{theaterModeStyles}</style>
</div>
);
}
}
export default TheaterMode;