This is now two containers since one of them has the client program needed to poll the UPS monitor. The UPS is now polled and th data is sent to an API endpoint on the Node.JS server.
This commit is contained in:
parent
871a239cbf
commit
236f0e2ba1
9 changed files with 63 additions and 12 deletions
|
@ -2,7 +2,6 @@ FROM node:12-alpine
|
|||
|
||||
RUN apk update && \
|
||||
apk add --no-cache bash
|
||||
|
||||
RUN npm install
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
|
8
Dockerfile-upsc
Normal file
8
Dockerfile-upsc
Normal file
|
@ -0,0 +1,8 @@
|
|||
FROM debian:stable-slim
|
||||
|
||||
RUN apt update && \
|
||||
apt install -y nut-client curl
|
||||
|
||||
COPY ./send_upsc_data.sh /send_upsc_data.sh
|
||||
|
||||
RUN chmod a+x /send_upsc_data.sh
|
|
@ -4,9 +4,19 @@ services:
|
|||
powerpanel-webview:
|
||||
image: next-js:latest
|
||||
container_name: powerpanel
|
||||
build: .
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
volumes:
|
||||
- ./src:/usr/src/app/
|
||||
ports:
|
||||
- "3555:3555"
|
||||
command: bash -c "cd powerpanel && npm run $MODE"
|
||||
upsc:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile-upsc
|
||||
container_name: powerpanel-upsc
|
||||
links:
|
||||
- powerpanel-webview
|
||||
command: bash -c "/send_upsc_data.sh"
|
||||
|
|
8
send_upsc_data.sh
Normal file
8
send_upsc_data.sh
Normal file
|
@ -0,0 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
while [ 1 -gt 0 ]; do
|
||||
sleep 15
|
||||
OUTPUT=( $(echo $(upsc nutdev1@192.168.6.211)) )
|
||||
echo "{\"data\": \"${OUTPUT[*]//[$'\t\r\n ']}\"}" | \
|
||||
curl -H "Content-Type: application/json" -X POST -d @- http://powerpanel-webview:3555/api/upsc_handler
|
||||
done
|
|
@ -1,6 +1,6 @@
|
|||
import Head from "next/head";
|
||||
|
||||
export default function MainView() {
|
||||
export default function MainView({ powerpanel_info }) {
|
||||
return (
|
||||
<div className="container">
|
||||
<Head>
|
||||
|
@ -23,8 +23,10 @@ export default function MainView() {
|
|||
</div>
|
||||
</div>
|
||||
<div className="window-body">
|
||||
<h3>item</h3>
|
||||
<p>status_dict[item]</p>
|
||||
<h3>Info</h3>
|
||||
{Object.keys(powerpanel_info).map((data) => (
|
||||
<p key="data">{powerpanel_info[data]}</p>
|
||||
))}
|
||||
</div>
|
||||
<div className="status-bar">
|
||||
<p className="status-bar-field">item: item</p>
|
||||
|
|
1
src/powerpanel/data/powerpanel_info.json
Normal file
1
src/powerpanel/data/powerpanel_info.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"data":"battery.charge: 100 battery.charge.low: 10 battery.charge.warning: 20 battery.mfr.date: CPS battery.runtime: 27250 battery.runtime.low: 300 battery.type: PbAcid battery.voltage: 27.1 battery.voltage.nominal: 24 device.mfr: CPS device.model: CP1500AVRLCDa device.serial: CXEKP2011291 device.type: ups driver.name: usbhid-ups driver.parameter.bus: 001 driver.parameter.pollfreq: 30 driver.parameter.pollinterval: 15 driver.parameter.port: auto driver.parameter.productid: 0501 driver.parameter.synchronous: no driver.parameter.vendorid: 0764 driver.version: 2.7.4 driver.version.data: CyberPower HID 0.4 driver.version.internal: 0.41 input.voltage: 121.0 input.voltage.nominal: 120 output.voltage: 137.0 ups.beeper.status: enabled ups.delay.shutdown: 20 ups.delay.start: 30 ups.load: 0 ups.mfr: CPS ups.model: CP1500AVRLCDa ups.productid: 0501 ups.realpower.nominal: 900 ups.serial: CXEKP2011291 ups.status: OL ups.test.result: No test initiated ups.timer.shutdown: -60 ups.timer.start: -60 ups.vendorid: 0764"}
|
|
@ -1,5 +0,0 @@
|
|||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
|
||||
export default function handler(req, res) {
|
||||
res.status(200).json({ name: 'John Doe' })
|
||||
}
|
11
src/powerpanel/pages/api/upsc_handler.js
Normal file
11
src/powerpanel/pages/api/upsc_handler.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
export default function upsc_handler(req, res) {
|
||||
const fs = require("fs");
|
||||
if (req.method === "POST") {
|
||||
// Process a POST request
|
||||
const data = JSON.stringify(req.body);
|
||||
fs.writeFile("./data/powerpanel_info.json", data, "utf8");
|
||||
res.status(200).json({data:'Thank you sir, may I have another'});
|
||||
} else {
|
||||
// Handle any other HTTP method
|
||||
}
|
||||
}
|
|
@ -1,5 +1,22 @@
|
|||
import MainView from "../components/main_view";
|
||||
|
||||
export default function Home() {
|
||||
return <MainView />;
|
||||
export async function getStaticProps() {
|
||||
const fs = require("fs");
|
||||
let POWERPANEL_INFO = {};
|
||||
|
||||
try {
|
||||
let fileContent = fs.readFileSync("./data/powerpanel_info.json", "utf8");
|
||||
POWERPANEL_INFO = JSON.parse(fileContent);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
return {
|
||||
props: {
|
||||
POWERPANEL_INFO,
|
||||
},
|
||||
};
|
||||
}
|
||||
export default function Home({ POWERPANEL_INFO }) {
|
||||
return <MainView powerpanel_info={POWERPANEL_INFO} />;
|
||||
}
|
||||
|
|
Reference in a new issue