Skip to content

Commit a69c2ca

Browse files
committed
Add commands to enable/disable via management API
1 parent f9902c5 commit a69c2ca

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"humao.rest-client"
4+
]
5+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# This file can be used with the VSCode Rest Client extension (https://marketplace.visualstudio.com/items?itemName=humao.rest-client) to play with the browser block API.
2+
@baseUrl=http://your-device-hostname:5011
3+
4+
### Ping the browser block
5+
GET {{baseUrl}}/ping
6+
7+
### Refresh the currently displayed page
8+
POST {{baseUrl}}/refresh
9+
10+
### Automatically refresh the browser window every 30 seconds
11+
POST {{baseUrl}}/autorefresh/30
12+
13+
### Disable automatic refresh
14+
POST {{baseUrl}}/autorefresh/0
15+
16+
### Re-scan for local HTTP or HTTPS services
17+
POST {{baseUrl}}/scan
18+
19+
### Get the URL currently being displayed
20+
GET {{baseUrl}}/url
21+
22+
### Set the URL to be displayed
23+
PUT {{baseUrl}}/url
24+
Content-Type: application/x-www-form-urlencoded
25+
26+
url=http://www.balena.io
27+
28+
### Set the URL with GPU and Kiosk settings
29+
PUT {{baseUrl}}/url
30+
Content-Type: application/x-www-form-urlencoded
31+
32+
url=http://www.balena.io&gpu=1&kiosk=1
33+
34+
### Get the status of the GPU
35+
GET {{baseUrl}}/gpu
36+
37+
### Enable the GPU
38+
PUT {{baseUrl}}/gpu/1
39+
40+
### Disable the GPU
41+
PUT {{baseUrl}}/gpu/0
42+
43+
### Get the status of Kiosk mode
44+
GET {{baseUrl}}/kiosk
45+
46+
### Enable Kiosk mode
47+
POST {{baseUrl}}/kiosk/1
48+
49+
### Disable Kiosk mode
50+
POST {{baseUrl}}/kiosk/0
51+
52+
### Get the flags Chromium was started with
53+
GET {{baseUrl}}/flags
54+
55+
### Get the version of Chromium
56+
GET {{baseUrl}}/version
57+
58+
### Take a screenshot of the Chromium window
59+
GET {{baseUrl}}/screenshot
60+
61+
### Get the display state
62+
GET {{baseUrl}}/display
63+
64+
### Set the display state to on
65+
POST {{baseUrl}}/display
66+
Content-Type: application/json
67+
68+
{
69+
"state": "on"
70+
}
71+
72+
### Set the display state to off
73+
POST {{baseUrl}}/display
74+
Content-Type: application/json
75+
76+
{
77+
"state": "off"
78+
}
79+
80+
### Set the display state to on using PUT
81+
PUT {{baseUrl}}/display
82+
Content-Type: application/json
83+
84+
{
85+
"state": "on"
86+
}
87+
88+
### Set the display state to off using PUT
89+
PUT {{baseUrl}}/display
90+
Content-Type: application/json
91+
92+
{
93+
"state": "off"
94+
}

readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The block provides an API for dynamic configuration, and also exposes the Chromi
1414
- Automatically displays local HTTP (port 80 or 8080) or HTTPS (443) service endpoints.
1515
- API for remote configuration and management
1616
- Chromium remote debugging port
17+
- Remotely enable/disable display
1718
---
1819

1920
## Usage
@@ -221,6 +222,24 @@ Returns the version of Chromium that `browser` is running
221222
Uses [scrot](https://opensource.com/article/17/11/taking-screen-captures-linux-command-line-scrot) to take a screenshot of the chromium window.
222223
The screenshot will be saved as a temporary file in the container.
223224

225+
226+
#### **GET** /display
227+
Returns `on` or `off` depending on the display status
228+
229+
#### **PUT** or **POST** /display
230+
Expects json payload with `state` key.
231+
232+
Turn display on:
233+
```bash
234+
curl -X PUT -H "Content-Type: application/json" -d '{"state": "on"}' http://localhost:5011/display
235+
```
236+
237+
Turn display off:
238+
```bash
239+
curl -X PUT -H "Content-Type: application/json" -d '{"state": "off"}' http://localhost:5011/display
240+
```
241+
242+
224243
---
225244

226245
## Supported devices

src/server.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
} = require('set-interval-async/dynamic')
1111
const { spawn } = require('child_process');
1212
const { readFile, unlink } = require('fs').promises;
13+
const { exec } = require('child_process');
1314
const path = require('path');
1415
const os = require('os');
1516

@@ -35,6 +36,8 @@ let flags = [];
3536
// Refresh timer object
3637
let timer = {};
3738

39+
// store the display state
40+
let displayState = 'on';
3841
// Returns the URL to display, adhering to the hieracrchy:
3942
// 1) the configured LAUNCH_URL
4043
// 2) a discovered HTTP service on the device
@@ -388,6 +391,72 @@ app.post('/scan', (req, res) => {
388391
return res.status(200).send('ok');
389392
});
390393

394+
// Function to enable the display
395+
function enableDisplay() {
396+
exec('xset dpms force on', (error, stdout, stderr) => {
397+
if (error) {
398+
console.error(`Error enabling display: ${error}`);
399+
return;
400+
}
401+
console.log('Display enabled');
402+
});
403+
}
404+
405+
// Function to disable the display
406+
function disableDisplay() {
407+
exec('xset dpms force off', (error, stdout, stderr) => {
408+
if (error) {
409+
console.error(`Error disabling display: ${error}`);
410+
return;
411+
}
412+
console.log('Display disabled');
413+
});
414+
}
415+
416+
app.post('/display', (req, res) => {
417+
if (!req.body.state) {
418+
return res.status(400).send('Bad request: missing state in the body element');
419+
}
420+
421+
const state = req.body.state.toLowerCase();
422+
if (state !== 'on' && state !== 'off') {
423+
return res.status(400).send('Bad request: state must be "on" or "off"');
424+
}
425+
426+
displayState = state;
427+
if (displayState === 'on') {
428+
enableDisplay();
429+
} else {
430+
disableDisplay();
431+
}
432+
433+
return res.status(204).send(`Display state set to ${displayState}`);
434+
});
435+
436+
app.put('/display', (req, res) => {
437+
if (!req.body.state) {
438+
return res.status(400).send('Bad request: missing state in the body element');
439+
}
440+
441+
const state = req.body.state.toLowerCase();
442+
if (state !== 'on' && state !== 'off') {
443+
return res.status(400).send('Bad request: state must be "on" or "off"');
444+
}
445+
446+
displayState = state;
447+
if (displayState === 'on') {
448+
enableDisplay();
449+
} else {
450+
disableDisplay();
451+
}
452+
453+
return res.status(204).send(`Display state set to ${displayState}`);
454+
});
455+
456+
app.get('/display', (req, res) => {
457+
return res.status(200).send(displayState);
458+
});
459+
391460
app.listen(API_PORT, () => {
392461
console.log('Browser API running on port: ' + API_PORT);
393462
});

0 commit comments

Comments
 (0)