Skip to content

Commit

Permalink
added commands to enable/disable via management API
Browse files Browse the repository at this point in the history
  • Loading branch information
hgross committed Nov 1, 2024
1 parent f9902c5 commit d8de000
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"humao.rest-client"
]
}
94 changes: 94 additions & 0 deletions browser-management-api-requests.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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.
@baseUrl=http://your-device-hostname:5011

### Ping the browser block
GET {{baseUrl}}/ping

### Refresh the currently displayed page
POST {{baseUrl}}/refresh

### Automatically refresh the browser window every 30 seconds
POST {{baseUrl}}/autorefresh/30

### Disable automatic refresh
POST {{baseUrl}}/autorefresh/0

### Re-scan for local HTTP or HTTPS services
POST {{baseUrl}}/scan

### Get the URL currently being displayed
GET {{baseUrl}}/url

### Set the URL to be displayed
PUT {{baseUrl}}/url
Content-Type: application/x-www-form-urlencoded

url=http://www.balena.io

### Set the URL with GPU and Kiosk settings
PUT {{baseUrl}}/url
Content-Type: application/x-www-form-urlencoded

url=http://www.balena.io&gpu=1&kiosk=1

### Get the status of the GPU
GET {{baseUrl}}/gpu

### Enable the GPU
PUT {{baseUrl}}/gpu/1

### Disable the GPU
PUT {{baseUrl}}/gpu/0

### Get the status of Kiosk mode
GET {{baseUrl}}/kiosk

### Enable Kiosk mode
POST {{baseUrl}}/kiosk/1

### Disable Kiosk mode
POST {{baseUrl}}/kiosk/0

### Get the flags Chromium was started with
GET {{baseUrl}}/flags

### Get the version of Chromium
GET {{baseUrl}}/version

### Take a screenshot of the Chromium window
GET {{baseUrl}}/screenshot

### Get the display state
GET {{baseUrl}}/display

### Set the display state to on
POST {{baseUrl}}/display
Content-Type: application/json

{
"state": "on"
}

### Set the display state to off
POST {{baseUrl}}/display
Content-Type: application/json

{
"state": "off"
}

### Set the display state to on using PUT
PUT {{baseUrl}}/display
Content-Type: application/json

{
"state": "on"
}

### Set the display state to off using PUT
PUT {{baseUrl}}/display
Content-Type: application/json

{
"state": "off"
}
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The block provides an API for dynamic configuration, and also exposes the Chromi
- Automatically displays local HTTP (port 80 or 8080) or HTTPS (443) service endpoints.
- API for remote configuration and management
- Chromium remote debugging port
- Remotely enable/disable display
---

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


#### **GET** /display
Returns `on` or `off` depending on the display status

#### **PUT** or **POST** /display
Expects json payload with `state` key.

Turn display on:
```bash
curl -X PUT -H "Content-Type: application/json" -d '{"state": "on"}' http://localhost:5011/display
```

Turn display off:
```bash
curl -X PUT -H "Content-Type: application/json" -d '{"state": "off"}' http://localhost:5011/display
```


---

## Supported devices
Expand Down
69 changes: 69 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
} = require('set-interval-async/dynamic')
const { spawn } = require('child_process');
const { readFile, unlink } = require('fs').promises;
const { exec } = require('child_process');
const path = require('path');
const os = require('os');

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

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

// Function to enable the display
function enableDisplay() {
exec('xset dpms force on', (error, stdout, stderr) => {
if (error) {
console.error(`Error enabling display: ${error}`);
return;
}
console.log('Display enabled');
});
}

// Function to disable the display
function disableDisplay() {
exec('xset dpms force off', (error, stdout, stderr) => {
if (error) {
console.error(`Error disabling display: ${error}`);
return;
}
console.log('Display disabled');
});
}

app.post('/display', (req, res) => {
if (!req.body.state) {
return res.status(400).send('Bad request: missing state in the body element');
}

const state = req.body.state.toLowerCase();
if (state !== 'on' && state !== 'off') {
return res.status(400).send('Bad request: state must be "on" or "off"');
}

displayState = state;
if (displayState === 'on') {
enableDisplay();
} else {
disableDisplay();
}

return res.status(204).send(`Display state set to ${displayState}`);
});

app.put('/display', (req, res) => {
if (!req.body.state) {
return res.status(400).send('Bad request: missing state in the body element');
}

const state = req.body.state.toLowerCase();
if (state !== 'on' && state !== 'off') {
return res.status(400).send('Bad request: state must be "on" or "off"');
}

displayState = state;
if (displayState === 'on') {
enableDisplay();
} else {
disableDisplay();
}

return res.status(204).send(`Display state set to ${displayState}`);
});

app.get('/display', (req, res) => {
return res.status(200).send(displayState);
});

app.listen(API_PORT, () => {
console.log('Browser API running on port: ' + API_PORT);
});
Expand Down

0 comments on commit d8de000

Please sign in to comment.