samedi 12 octobre 2019

Un serveur de marée (?)

Un ingénieux développeur ( https://github.com/joelkoz ) a mis en ligne le code d'un serveur web de données json et graphique de marées basé sur le célèbre xtide, fameux et historique logiciel de calcul des marées : https://github.com/joelkoz/xtwsd

Ce serveur web renvoi des horaires de marée, des graphes et autres coefficients harmoniques

Ma seule contribution est cette méthode de mise en œuvre ainsi que l'ajout des coefficients harmoniques pour Arcachon, mais attention les résultats pour ce port sont expérimentaux et d'ailleurs incorrect, à peaufiner donc.

Si vous disposez de meilleurs coefficients harmoniques pour Arcachon et autres, je suis preneur !

Installation : 

La quincaillerie:
pi@dietpi:~/wtide$ sudo apt install git cmake gcc g++ bzip2 ragel libpng-dev libssl-dev
..
..
..

La librairie boost:
pi@dietpi:~/wtide$ wget https://dl.bintray.com/boostorg/release/1.71.0/source/boost_1_71_0.tar.bz2
pi@dietpi:~/wtide$ tar --bzip2 -xf boost_1_71_0.tar.bz2
pi@dietpi:~/wtide$ cd boost_1_71_0
pi@dietpi:~/wtide/boost_1_71_0$ ./bootstrap.sh -with-libraries=system
pi@dietpi:~/wtide/boost_1_71_0$ sudo ./b2 install
..
..
...updated 15188 targets...


La librairie TCD:
pi@dietpi:~/wtide/boost_1_71_0$ cd ..
pi@dietpi:~/wtide$ wget https://flaterco.com/files/xtide/libtcd-2.2.7-r2.tar.bz2
pi@dietpi:~/wtide$ tar --bzip2 -xf libtcd-2.2.7-r2.tar.bz2
pi@dietpi:~/wtide$ cd libtcd-2.2.7
pi@dietpi:~/wtide/libtcd-2.2.7$ ./configure
pi@dietpi:~/wtide/libtcd-2.2.7$ make
pi@dietpi:~/wtide/libtcd-2.2.7$ sudo make install
pi@dietpi:~/wtide/libtcd-2.2.7$ cd ..

Le serveur:
pi@dietpi:~/wtide$ git clone --recursive https://github.com/joelkoz/xtwsd.git
pi@dietpi:~/wtide$ mkdir xtwsd/build
pi@dietpi:~/wtide$ cd xtwsd/build
pi@dietpi:~/wtide/xtwsd/build$ cmake -Wno-dev ..
pi@dietpi:~/wtide$ cd xtwsd/
pi@dietpi:~/wtide/xtwsd$ ls
pi@dietpi:~/wtide/xtwsd$ cd build
pi@dietpi:~/wtide/xtwsd/build$ make
..
..
..
zzzzzz..rrr
..
pi@dietpi:~/wtide/xtwsd/build$ sudo make install

Le fichier d'harmoniques:
pi@dietpi:~/wtide/xtwsd/build$ cd /usr/local/share/
pi@dietpi:/usr/local/share$ sudo mkdir xtide
pi@dietpi:/usr/local/share$ cd xtide
pi@dietpi:/usr/local/share/xtide$ sudo wget https://flaterco.com/files/xtide/harmonics-dwf-20190620-free.tar.bz2
pi@dietpi:/usr/local/share/xtide$ sudo tar --bzip2 -xf harmonics-dwf-20190620-free.tar.bz2
pi@dietpi:/usr/local/share/xtide$ sudo rm harmonics-dwf-20190620-free.tar.bz2
pi@dietpi:/usr/local/share/xtide$ sudo cp harmonics-dwf-20190620/harmonics-dwf-20190620-free.tcd harmonics.tcd
pi@dietpi:/usr/local/share/xtide$ sudo rm -R harmonics-dwf-20190620/
pi@dietpi:/usr/local/share/xtide$ export HFILE_PATH=/usr/local/share/xtide/harmonics.tcd
pi@dietpi:~/wtide/xtwsd/build$ sudo nano /etc/xtide.conf

    /usr/local/share/xtide/harmonics.tcd
   

Test #1:
pi@dietpi:/usr/local/share/xtide$ cd /home/pi/xtwsd/build
pi@dietpi:~/wtide/xtwsd/build$ sudo ./xtwsd 800
xtwsd v0.2
Starting web service on port 800
^C

Test #2 dans un screen:
pi@dietpi:~/wtide/xtwsd/build$ screen -S server
pi@dietpi:~/wtide/xtwsd/build$ sudo ./xtwsd 800

^a d


Ajoutons les ports des Caraïbes:

pi@dietpi:~/wtide/xtwsd/build$ ./nos2xt -f ../src/util/caribbean.txt -p 800

Building context map...Indexing /usr/local/share/xtide/harmonics.tcd...
Done.
Building context map...Done.
..
..
..
Building context map...Done.

Ajoutons Arcachon Eyrac (au hazard)
pi@dietpi:~/wtide/xtwsd/build$ nano eyrac-full.json


{
"comments": "",
"country": "France",
"harmonics": {
"confidence": 10,
"constituents": [
{
"name": "M2",
"amp": 1.339,
"epoch": 147.0
},
{
"name": "S2",
"amp": 0.443,
"epoch": 188.0
},
{
"name": "N2",
"amp": 0.261,
"epoch": 130.0
},
{
"name": "K1",
"amp": 0.045,
"epoch": 100.0
},
{
"name": "O1",
"amp": 0.069,
"epoch": 350.0
},
{
"name": "NU2",
"amp": 0.0492,
"epoch": 130.0
},
{
"name": "MU2",
"amp": 0.0414,
"epoch": 130.0
},
{
"name": "2N2",
"amp": 0.0343,
"epoch": 130.0
},
{
"name": "OO1",
"amp": 0.003,
"epoch": 100
},
{
"name": "LAM2",
"amp": 0.0094,
"epoch": 188
},
{
"name": "M1",
"amp": 0.0049,
"epoch": 100.0
},
{
"name": "J1",
"amp": 0.0055,
"epoch": 100.0
},
{
"name": "MM",
"amp": 0.0,
"epoch": 0.0
},
{
"name": "SSA",
"amp": 0.0,
"epoch": 0.0
},
{
"name": "SA",
"amp": 0.011,
"epoch": 182.0
},
{
"name": "MF",
"amp": 0.0,
"epoch": 0.0
},
{
"name": "RHO",
"amp": 0.0026,
"epoch": 100.0
},
{
"name": "Q1",
"amp": 0.0150,
"epoch": 304.0
},
{
"name": "T2",
"amp": 0.0261,
"epoch": 188
},
{
"name": "R2",
"amp": 0.0035,
"epoch": 188.00
},
{
"name": "2Q1",
"amp": 0.0018,
"epoch": 100
},
{
"name": "P1",
"amp": 0.015,
"epoch": 100.0
},
{
"name": "L2",
"amp": 0.0383,
"epoch": 147
},
{
"name": "K2",
"amp": 0.1197,
"epoch": 188
},
{
"name": "M3",
"amp": 0.0,
"epoch": 0
},
{
"name": "M4",
"amp": 0.091,
"epoch": 93
},
{
"name": "M6",
"amp": 0.0,
"epoch": 0
},
{
"name": "M8",
"amp": 0.0,
"epoch": 0
},
{
"name": "S1",
"amp": 0.0,
"epoch": 0
},
{
"name": "S4",
"amp": 0.0,
"epoch": 0
},
{
"name": "S6",
"amp": 0.0,
"epoch": 0
},
{
"name": "MK3",
"amp": 0.0,
"epoch": 0
},
{
"name": "2MK3",
"amp": 0.0,
"epoch": 0
},
{
"name": "MN4",
"amp": 0.028,
"epoch": 72.0
},
{
"name": "2SM2",
"amp": 0.0,
"epoch": 0
},
{
"name": "MSF",
"amp": 0.0,
"epoch": 0
},
{
"name": "MS4",
"amp": 0.0500,
"epoch": 159
}
],
"datum": "Mean Lower Low Water",
"datumOffset": 0,
"zoneOffset": 0
},
"levelUnits": "meters",
"name": "Arcachon Eyrac",
"notes": "",
"position": {
"lat": 44.665076,
"long": -1.163666
},
"referenceStation": true,
"source": {
"context": "SHOM",
"name": "xtwsd",
"stationId": "1"
},
"timezone": ":Europe/Paris",
"type": "tide"
}

pi@dietpi:~/wtide/xtwsd/build$ curl -vX POST http://127.0.0.1:800/harmonics -d @eyrac-full.json --header "Content-Type: application/json"

Test #3:
On récupère la marée(?) pour Arcachon:
Dans un navigateur:
http://192.168.1.3:800/location/SHOM:1?start=2019-10-12%2000:00%20am%20EDT&local=1&detailed=1

ou 192.168.1.3 est l'adresse du Pi.. On obtient quelque chose comme ça:

{"index":214,"id":"SHOM:1","name":"Arcachon Eyrac","position":{"lat":44.665076,"long":-1.163666},"timezone":":Europe/Paris","note":"","type":"tide","levelUnits":"m","events":[{"time":"2019-10-12 12:13 AM CEST","name":"Low Tide","isMaxMin":true,"level":-1.4912528607527973},{"time":"2019-10-12 06:29 AM CEST","name":"High Tide","isMaxMin":true,"level":1.3308788824466118},{"time":"2019-10-12 06:31 AM CEST","name":"Moonset","isMaxMin":false},{"time":"2019-10-12 08:15 AM CEST","name":"Sunrise","isMaxMin":false},{"time":"2019-10-12 12:26 PM CEST","name":"Low Tide","isMaxMin":true,"level":-1.5278729066763577},{"time":"2019-10-12 06:43 PM CEST","name":"High Tide","isMaxMin":true,"level":1.5119263360270265},{"time":"2019-10-12 07:20 PM CEST","name":"Moonrise","isMaxMin":false},{"time":"2019-10-12 07:26 PM CEST","name":"Sunset","isMaxMin":false}]}


Un beau graphe:
http://192.168.1.3:800//graph/SHOM:1?local=1&detailed=1

ohhhh le beau svg (ci-dessous tout moche en png)!


Démarrage automatique?... ok:
pi@dietpi:~ sudo nano /etc/init.d/xtwsdd

#! /bin/sh
### BEGIN INIT INFO
# Provides: xtwsd
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Serveur de marees
# Description: Serveur web json de marée basé sur xtide
### END INIT INFO

# Do NOT "set -e"

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Serveur de marees"
NAME=xtwsd
USERNAME=root
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON=/usr/local/bin/$NAME
DAEMON_ARGS="$DAEMON_ARGS 800"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

pidof_xtwsd() {
# if there is actually a xtwsd process whose pid is in PIDFILE,
# print it and return 0.
if [ -e "$PIDFILE" ]; then
if pidof $NAME | tr ' ' '\n' | grep -w $(cat $PIDFILE); then
return 0
fi
fi
return 1
}

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --chuid $USERNAME --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
reload)
log_daemon_msg "Reloading $DESC" "$NAME"
PID=$(pidof_xtwsd) || true
if [ "${PID}" ]; then
kill -HUP $PID
log_end_msg 0
else
log_end_msg 1
fi
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload}" >&2
exit 3
;;
esac

:

 ^o ^x


pi@dietpi:~ sudo chmod 755 /etc/init.d/xtwsdd
pi@dietpi:~ sudo update-rc.d xtwsdd defaults
pi@dietpi:~ sudo systemctl daemon-reload

Test #4
pi@dietpi:~ sudo /etc/init.d/xtwsdd start

Navigateur : http://192.168.1.3:800//graph/SHOM:1?local=1&detailed=1
On doit obtenir un graphe

 pi@dietpi:~ sudo reboot
..
..
Navigateur : http://192.168.1.3:800//graph/SHOM:1?local=1&detailed=1
Idem

Voilà !