Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
},
rules: {
// Add your custom rules here

},
};
28 changes: 28 additions & 0 deletions .github/workflows/npm-gulp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: NodeJS with Gulp

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Build
run: |
npm install
gulp
5 changes: 4 additions & 1 deletion api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"type": "git",
"url": "https://github.com/ado24/NAD-C338-REST-API.git"
},
"private": true
"private": true,
"dependencies": {
"cors": "^2.8.5"
}
}
4 changes: 3 additions & 1 deletion api-server/properties.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
API_SERVER_LISTENER_PORT=3000
BLUOS_TCP_PORT=30001
BLUOS_IP=10.0.0.251
BLUOS_IP=10.0.0.251
KEEPALIVE_TIMEOUT=61000
HEADERS_TIMEOUT=62000
133 changes: 126 additions & 7 deletions api-server/server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
import express from 'express';
import { NADC338 } from '../model/NAD-C338.js';
import dotenv from 'dotenv';
import https from 'https';
import fs from 'fs';
import cors from 'cors';
dotenv.config({'path': './api-server/properties.env'});

const app = express();

const port = process.env.API_SERVER_LISTENER_PORT;
const ip = process.env.BLUOS_IP;
const bluOsPort = parseInt(process.env.BLUOS_TCP_PORT);
const keepAliveTimeout = parseInt(process.env.KEEPALIVE_TIMEOUT);
const headersTimeout = parseInt(process.env.HEADERS_TIMEOUT);

app.use(express.json());
// Load SSL certificate and private key
//const ca = fs.readFileSync('path/to/your/ca_bundle.crt', 'utf8');

const endpointKeyPath = "./keys/server.key";
const endpointCertPath = "./certs/server.crt";
const caPath = "./certs/server.crt";

const credentials = {
key: fs.readFileSync(endpointKeyPath),
cert: fs.readFileSync(endpointCertPath),
ca: fs.readFileSync(caPath)
};
let nad = new NADC338(ip, bluOsPort);

app.use(cors());
app.use(express.json());
app.use((req, res, next) => {
// Set default security headers
res.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
next();
});
app.use((req, res, next) => {
// Add default cache control for modification endpoints
if (req.method !== 'GET') {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
}
next();
});

// GET endpoints
// Frequently changing state - minimal cache
app.get('/power', async (req, res) => {
try {
const power = await nad.getPower();
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.json({ power });
} catch (error) {
res.status(500).send(error.message);
Expand All @@ -25,6 +66,11 @@ app.get('/power', async (req, res) => {
app.get('/volume', async (req, res) => {
try {
const volume = await nad.getVolume();
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.json({ volume });
} catch (error) {
res.status(500).send(error.message);
Expand All @@ -34,6 +80,11 @@ app.get('/volume', async (req, res) => {
app.get('/source', async (req, res) => {
try {
const source = await nad.getSource();
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.json({ source });
} catch (error) {
res.status(500).send(error.message);
Expand All @@ -43,15 +94,25 @@ app.get('/source', async (req, res) => {
app.get('/mute', async (req, res) => {
try {
const mute = await nad.getMute();
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.json({ mute });
} catch (error) {
res.status(500).send(error.message);
}
});

// Less frequently changing settings - short cache
app.get('/brightness', async (req, res) => {
try {
const brightness = await nad.getBrightness();
res.set({
'Cache-Control': 'public, max-age=5',
'Vary': 'Accept-Encoding'
});
res.json({ brightness });
} catch (error) {
res.status(500).send(error.message);
Expand All @@ -61,15 +122,24 @@ app.get('/brightness', async (req, res) => {
app.get('/bass', async (req, res) => {
try {
const bass = await nad.getBass();
res.set({
'Cache-Control': 'public, max-age=5',
'Vary': 'Accept-Encoding'
});
res.json({ bass });
} catch (error) {
res.status(500).send(error.message);
}
});

// Configuration settings - longer cache
app.get('/auto-sense', async (req, res) => {
try {
const autoSense = await nad.getAutoSense();
res.set({
'Cache-Control': 'public, max-age=60',
'Vary': 'Accept-Encoding'
});
res.json({ autoSense });
} catch (error) {
res.status(500).send(error.message);
Expand All @@ -79,13 +149,17 @@ app.get('/auto-sense', async (req, res) => {
app.get('/auto-standby', async (req, res) => {
try {
const autoStandby = await nad.getAutoStandby();
res.set({
'Cache-Control': 'public, max-age=60',
'Vary': 'Accept-Encoding'
});
res.json({ autoStandby });
} catch (error) {
res.status(500).send(error.message);
}
});

// POST/PUT endpoints
// POST/PUT/PATCH/DELETE endpoints
app.post('/power', async (req, res) => {
try {
const { state } = req.body;
Expand All @@ -94,7 +168,23 @@ app.post('/power', async (req, res) => {
} else if (state === 'Off') {
await nad.powerOff();
}
res.sendStatus(200);
const power = await nad.getPower();
res.json({ power });
} catch (error) {
res.status(500).send(error.message);
}
});

app.patch('/power', async (req, res) => {
try {
const { state } = req.body;
if (state === 'On') {
await nad.powerOn();
} else if (state === 'Off') {
await nad.powerOff();
}
//const power = await nad.getPower();
res.json({ state });
} catch (error) {
res.status(500).send(error.message);
}
Expand All @@ -104,7 +194,19 @@ app.put('/volume', async (req, res) => {
try {
const { level } = req.body;
await nad.setVolume(level);
res.sendStatus(200);
const volume = await nad.getVolume();
res.json({ volume });
} catch (error) {
res.status(500).send(error.message);
}
});

app.patch('/volume', async (req, res) => {
try {
const { level } = req.body;
await nad.setVolume(level);
//const volume = await nad.getVolume();
res.json({ level });
} catch (error) {
res.status(500).send(error.message);
}
Expand Down Expand Up @@ -157,6 +259,15 @@ app.post('/bass', async (req, res) => {
}
});

app.patch('/bass', async (req, res) => {
try {
await nad.setBass();
res.json({ bass: 'On' });
} catch (error) {
res.status(500).send(error.message);
}
});

app.delete('/bass', async (req, res) => {
try {
await nad.unsetBass();
Expand Down Expand Up @@ -202,6 +313,14 @@ app.delete('/auto-standby', async (req, res) => {
}
});

app.listen(port, () => {
console.log(`Node.js server running at http://localhost:${port}`);
});
// Create HTTPS server
const httpsServer = https.createServer(credentials, app);

httpsServer.listen(port, () => {
console.log(`HTTPS server running at https://localhost:${port}`);
});

httpsServer.keepAliveTimeout = keepAliveTimeout;
httpsServer.headersTimeout = headersTimeout;

app.set('timeout', keepAliveTimeout);
Loading
Loading