diff --git a/.vscode/launch.json b/.vscode/launch.json index f2df95f..95ab75c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,10 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/bin/www.js" + "program": "${workspaceFolder}/bin/www.js", + "env": { + "APIHOST": "https://sensorapi.fuerst-stuttgart.de" + } } ] } \ No newline at end of file diff --git a/Dockerfile_noise b/Dockerfile_noise new file mode 100644 index 0000000..cf727ee --- /dev/null +++ b/Dockerfile_noise @@ -0,0 +1,21 @@ +FROM node:18-alpine + +ADD package.json /tmp/package.json +RUN cd /tmp && npm install +RUN mkdir -p /opt/app && cp -a /tmp/node_modules /tmp/package.json /opt/app/ +WORKDIR /opt/app +ADD . /opt/app/ + +RUN apk add --no-cache tzdata +ENV TZ Europe/Berlin +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +RUN deluser --remove-home node + +RUN touch cmds.sh \ + && echo 'npm start' >>cmds.sh + +EXPOSE 3000 + +CMD sh ./cmds.sh + diff --git a/app.js b/app.js index 076456e..0b97fe3 100644 --- a/app.js +++ b/app.js @@ -38,6 +38,9 @@ app.set('view engine', 'pug'); app.get('*', (req, res, next) => { const subdom = req.headers.host.split('.') + if (subdom[0] === 'laerm') { + subdom[0] = 'noise' + } app.set('category',subdom[0]) next() }) @@ -49,6 +52,7 @@ app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/api', apiRouter) +app.use('/srv', apiRouter) app.use('/', indexRouter.router); // catch 404 and forward to error handler diff --git a/build_and_copy.sh b/build_and_copy.sh new file mode 100755 index 0000000..b937267 --- /dev/null +++ b/build_and_copy.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Build Docker-Container +# +# Call: buildit.sh name [target] +# +# The Dockerfile must be named like Dockerfile_name +# +# 2018-09-20 rxf +# - before sending docker image to remote, tag actual remote image +# +# 2018-09-14 rxf +# - first Version +# + +set -x +port="" +orgName=noise +name=noise + +usage() +{ + echo "Usage build_and_copy.sh [-p port] [-n name] target" + echo " Build docker container $name and copy to target" + echo "Params:" + echo " target: Where to copy the container to " + echo " -p port: ssh port (default 22)" + echo " -n name: new name for container (default: $orgName)" +} + +while getopts n:p:h? o +do + case "$o" in + n) name="$OPTARG";; + p) port="-p $OPTARG";; + h) usage; exit 0;; + *) usage; exit 1;; + esac +done +shift $((OPTIND-1)) + +while [ $# -gt 0 ]; do + if [[ -z "$target" ]]; then + target=$1 + shift + else + echo "bad option $1" + # exit 1 + shift + fi +done + +docker build -f Dockerfile_$orgName -t $name . + +dat=`date +%Y%m%d%H%M` + +if [ "$target" == "localhost" ] +then + docker tag $name $name:V_$dat + exit +fi + +ssh $port $target "docker tag $name $name:V_$dat" +docker save $name | bzip2 | pv | ssh $port $target 'bunzip2 | docker load' diff --git a/docs/Readme.md b/docs/Readme.md new file mode 100644 index 0000000..29b8609 --- /dev/null +++ b/docs/Readme.md @@ -0,0 +1,5 @@ +####Neues Sensor-API + +* Programm **sensor_web_api**. Läuft auf Port 3000 +* Aufruf über: **laerm.test.me:3000** (oder feinstaub.test... oder multigeiger.test...) +* Zugriff auf die DB mit dem Programm **SensorApi**. Dieses reagiert auf Port 3005 und muss im Hintergrund laufen! diff --git a/package.json b/package.json index f03112e..466e8f8 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "laerm-web-sensorapi", - "version": "3.0.4", - "date": "2023-04-24 18:00 UTC", + "version": "3.0.5", + "date": "2023-11-12 10:00 UTC", "private": true, "scripts": { - "start": "node bin/www.js >>log/laerm_web.log 2>&1" + "start": "node bin/www.js >>/var/log/noise.log 2>&1" }, "type": "module", "bin": { diff --git a/public/javascripts/chart_utilities.js b/public/javascripts/chart_utilities.js index 9970f72..2014e8d 100644 --- a/public/javascripts/chart_utilities.js +++ b/public/javascripts/chart_utilities.js @@ -17,7 +17,7 @@ export const noise_ymin = 30; // lowest value on y-axis for n export const noise_ymax = 120; // highest value on y-axis for noise export let peaklim = 70; // threshold for peak count -export const url = '/api' +export const url = '/srv' export function createGlobObtions() { // Options, die für alle Plots identisch sind @@ -217,7 +217,7 @@ export const fetchfromserver = async (url) => { } export const addAddress = async (sid) => { - let url = `/api/getaddress?sensorid=${sid}` + let url = `/srv/getaddress?sensorid=${sid}` let erg = await fetchfromserver(url) if (!erg.err) { return erg.address diff --git a/public/javascripts/global.js b/public/javascripts/global.js index 7f28ce8..b29f0b4 100644 --- a/public/javascripts/global.js +++ b/public/javascripts/global.js @@ -116,7 +116,7 @@ import {setCurrentTab, getCurrentTab, showError, fetchfromserver} from "./chart_ }) const getSensorType = async (sid) => { - let url = `/api/getoneproperty?sensorid=${sid}` + let url = `/srv/getoneproperty?sensorid=${sid}` const data = await fetchfromserver(url) return data.props.type } diff --git a/public/javascripts/map.js b/public/javascripts/map.js index aa4550b..2b786bf 100644 --- a/public/javascripts/map.js +++ b/public/javascripts/map.js @@ -12,12 +12,12 @@ const mapparams = { bounds: null, popuptext: '', clickedSensor: 0, - APIURL: '/api/getmapdata?', + APIURL: '/srv/getmapdata?', MAXZOOM: 19 } export async function showMap(params) { - if(sysparams.category === 'laerm') { + if(sysparams.category === 'noise') { utils.removeTabs() if(mapparams.map && mapparams.map.remove) { mapparams.map.off(); @@ -36,7 +36,7 @@ export async function showMap(params) { await buildMarkers(params, mapparams) }); - if(sysparams.category === 'laerm') { + if(sysparams.category === 'noise'){ mapn.buildLegend(mapparams) } @@ -45,7 +45,7 @@ export async function showMap(params) { } const buildMarkers = async (params, mapparams) => { - if(sysparams.category === 'laerm') { + if(sysparams.category === 'noise') { return mapn.buildMarkers(params, mapparams) } else if(sysparams.category === 'multigeiger') { return mapg.buildMarkers(params, mapparams) diff --git a/public/javascripts/map_noise.js b/public/javascripts/map_noise.js index 064ecee..a380293 100644 --- a/public/javascripts/map_noise.js +++ b/public/javascripts/map_noise.js @@ -57,9 +57,9 @@ export async function buildMarkers(params, mpp) { if ((params.weeks > 0) && (x.weeks > params.weeks)) { continue } - // if (x.value <= -4) { - // continue - // } + if (x.value <= -5) { + continue + } let marker = L.marker([x.location[1], x.location[0]], { icon: new L.Icon({ iconUrl: buildIcon(getColor(parseInt(x.value)), 0, x.indoor), diff --git a/public/javascripts/map_utilities.js b/public/javascripts/map_utilities.js index 3f09bfa..7ec5cb6 100644 --- a/public/javascripts/map_utilities.js +++ b/public/javascripts/map_utilities.js @@ -4,7 +4,7 @@ import * as utils from "./chart_utilities.js" import * as dt from './datetime.js' function getColor(d, mapparams) { - if(sysparams.category === 'laerm') { + if(sysparams.category === 'noise') { return mapn.getColor(d, mapparams) } else if(sysparams.category === 'multigeiger') { return mapg.getColor(d, mapparams) diff --git a/public/javascripts/showcharts.js b/public/javascripts/showcharts.js index ed5550b..c889c03 100644 --- a/public/javascripts/showcharts.js +++ b/public/javascripts/showcharts.js @@ -55,7 +55,7 @@ export async function showChart(params, typ, container) { return this.axis.defaultLabelFormatter.call(this); } } - let url = `/api/getsensordata?sensorid=${params.sid}&data=${typ}&datetime=${params.datetime}` + let url = `/srv/getsensordata?sensorid=${params.sid}&data=${typ}&datetime=${params.datetime}` if (typ === 'live') { url += `&span=${params.span}` } else if ((typ === 'havg') || (typ === 'davg')) { @@ -63,7 +63,7 @@ export async function showChart(params, typ, container) { } let erg = await fetchfromserver(url) if (!erg.err) { - if(sysparams.category === 'laerm') { + if(sysparams.category === 'noise') { utils.showTabs() } document.querySelector('#odth').style.display = 'none' diff --git a/routes/api.js b/routes/api.js index 413dbab..48447f3 100644 --- a/routes/api.js +++ b/routes/api.js @@ -21,12 +21,13 @@ let APIHOST = process.env.APIHOST || 'http://localhost:3005' /* GET home page. */ router.get('/:cmd', async function(req, res, next) { // req.i18n.changeLanguage(req.query.lng) + const calledAs = req.baseUrl const cmd = req.params.cmd + const pretty = (req.query.pretty !== undefined) const lng = getLanguage() - let url = APIHOST - url += (req.originalUrl + `&lng=${lng}`) + let url = APIHOST + '/api' + req.originalUrl.slice(4) + `&lng=${lng}` try { - const response = await axios(encodeURI(url)); + const response = await axios.get(encodeURI(url)); if (response.status !== 200) { res.json({err: `${trans('ESYSCALL')} status=${response.status}`}) } @@ -34,9 +35,14 @@ router.get('/:cmd', async function(req, res, next) { res.json(response.data) return } - if ((cmd === 'getoneproperty') || (cmd === 'getaddress')) { + // if called as '/api' then directly return the data + if ((calledAs === '/api') || (cmd === 'getoneproperty') || (cmd === 'getaddress')) { + if(pretty) { + res.send(JSON.stringify(response.data,null,2)) + } else { res.json(response.data) - return + } + return } const options = response.data.options for(let x of disttable) { diff --git a/routes/index.js b/routes/index.js index baa7a6c..4cff543 100644 --- a/routes/index.js +++ b/routes/index.js @@ -9,8 +9,8 @@ const renderOpts = (sid, cat) => { let tit = 'Feinstaub' if(cat === 'multigeiger') { tit = 'Geiger' - } else if (cat == 'laerm') { - tit = 'Lärm' + } else if (cat == 'noise') { + tit = 'Noise' } return { title: tit, @@ -28,7 +28,7 @@ const translate = (x) => { } const getIndex = (cat) => { - if (cat === 'laerm') { + if (cat == 'noise'){ return 'index_noise' } else if (cat === 'multigeiger') { return 'index_geiger'