#!/bin/sh set -e [ -n "$DEBUG" ] && { set -x; curl_args='-v'; } # check requirements for tool in awk curl jq flock; do if ! command -v "$tool" >/dev/null; then printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2 exit 1 fi done # incidence = (sumCasesLast7Days / EWZ) * 100000 lat=${HOME_LAT:?} lon=${HOME_LON:?} fields='GEN,RS,EWZ,EWZ_BL,BL_ID,cases,cases_per_100k,cases7_per_100k,cases7_bl_per_100k,last_update,BL,IBZ' # see https://experience.arcgis.com/experience/478220a4c454480e823b17327b2bf1d4 uri="https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=${fields}&geometry=${lon}%2C${lat}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json" _data=$(curl -sSL -H 'Accept: application/json' ${curl_args} ${uri} | jq -r '.features[0].attributes') [ -z "${_data}" ] && printf "{}\n" HISTFILE=${HOME}/covid-history.csv loc=$(printf "${_data}" | jq -r '.GEN') last_update=$(printf "${_data}" | jq -r '.last_update') # printf "${_data}" | jq -r '.' incidence_f=$(printf "%.1f" $(printf "${_data}" | jq -r '.cases7_bl_per_100k')) incidence_i=$(printf "%.0f" "${incidence_f}") # store tmp_HISTFILE=$(mktemp -u --tmpdir waybar-module-covid.XXXXX) ( cat ${HISTFILE} ts=$(printf "${last_update}" | cut -d',' -f1 | tr '.' '-') printf "%s,%s\n" "${ts}" "${incidence_f}" ) | uniq | tee ${tmp_HISTFILE} >/dev/null flock -x ${HISTFILE} mv ${tmp_HISTFILE} ${HISTFILE} mxy=$(awk -F',' '{y=y+$2} END{print ((NR*(NR+1))/2)/NR, y/NR}' "${HISTFILE}") trend=$(awk -F',' --assign="mx=$(printf "$mxy" | cut -d' ' -f1)" --assign="my=$(printf "$mxy" | cut -d' ' -f2)" '{up= up+(NR-mx)*($2-my); down= down+(NR-mx)**2} END{if(down==0){print 0}else{print up/down};}' "${HISTFILE}") trend_arrow="󰔴" [ $(printf "%.0f" ${trend}) -gt 0 ] && trend_arrow="󰔵" [ $(printf "%.0f" ${trend}) -lt 0 ] && trend_arrow="󰔳" color='green' if [ "${incidence_i}" -eq "0" ]; then color='gray' fi if [ "${incidence_i}" -gt "1" ]; then color='green' fi if [ "${incidence_i}" -gt "25" ]; then color='yellow' fi if [ "${incidence_i}" -gt "35" ]; then color='orange' fi if [ "${incidence_i}" -gt "50" ]; then color='red' fi if [ "${incidence_i}" -gt "100" ]; then color='darkred' fi if [ "${incidence_i}" -gt "250" ]; then color='darkdarkred' fi tooltip=" 7 Tage Inzidenz für ${loc} last update: ${last_update} history (${trend_arrow}): $(awk -F, '{print $1 " " $2}' ${HISTFILE} | tail -n10) " jq -nr \ "{ text: \"${trend_arrow}${incidence_f}\", percentage: \"${incidence_i}\", class: \"${color}\", alt: \"${color}\", tooltip: \"${tooltip}\" } | @text"