From 5590713619f880f00524f757bd02fc5c5a483891 Mon Sep 17 00:00:00 2001 From: Julien Lazarewicz Date: Wed, 12 Jun 2024 00:12:41 +0200 Subject: [PATCH] Initial commit --- .gitattributes | 2 + Documentation/Daemon.txt | 4 + anemometer.json | 6 ++ com.fup.anemometre_daemon.plist | 30 +++++++ fup_anemo_daemon.log | 68 ++++++++++++++++ fup_anemometre_daemon.py | 133 ++++++++++++++++++++++++++++++++ start_daemon.sh | 3 + 7 files changed, 246 insertions(+) create mode 100644 .gitattributes create mode 100644 Documentation/Daemon.txt create mode 100644 anemometer.json create mode 100644 com.fup.anemometre_daemon.plist create mode 100644 fup_anemo_daemon.log create mode 100644 fup_anemometre_daemon.py create mode 100755 start_daemon.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/Documentation/Daemon.txt b/Documentation/Daemon.txt new file mode 100644 index 0000000..3c0af7e --- /dev/null +++ b/Documentation/Daemon.txt @@ -0,0 +1,4 @@ +Files locations : +LaunchDaemon file : com.fup.anemometre_daemon.plist - location : /Library/LaunchDaemons/ +Daemon files location : /usr/local/com.fup.anemometre_daemon/ +Daemon log files : /var/log/com.fup.anemometre_daemon.log \ No newline at end of file diff --git a/anemometer.json b/anemometer.json new file mode 100644 index 0000000..3dfaf48 --- /dev/null +++ b/anemometer.json @@ -0,0 +1,6 @@ +{ + "sensor": "anemometer", + "time": 1351824120, + "speed": 27.10, + "error": false +} \ No newline at end of file diff --git a/com.fup.anemometre_daemon.plist b/com.fup.anemometre_daemon.plist new file mode 100644 index 0000000..9a37b10 --- /dev/null +++ b/com.fup.anemometre_daemon.plist @@ -0,0 +1,30 @@ + + + + + Label + com.fup.anemometre_daemon + ProgramArguments + + /usr/local/com.fup.anemometre_daemon/start_daemon.sh + + KeepAlive + + StandardOutPath + /var/log/com.fup.anemometre_daemon.log + StandardErrorPath + /var/log/com.fup.anemometre_daemon.log + Debug + + SoftResourceLimits + + Core + 9223372036854775807 + + HardResourceLimits + + Core + 9223372036854775807 + + + \ No newline at end of file diff --git a/fup_anemo_daemon.log b/fup_anemo_daemon.log new file mode 100644 index 0000000..6e6de1a --- /dev/null +++ b/fup_anemo_daemon.log @@ -0,0 +1,68 @@ +2024-06-11 23:54:51,982 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:54:56,988 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:01,994 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:07,001 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:12,008 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:17,014 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:22,021 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:27,028 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:32,031 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:37,035 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:42,042 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:47,048 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:52,053 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:55:57,058 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:56:02,063 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:56:07,068 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:56:12,076 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:56:17,083 - fup_anemo_daemon - INFO - 27.1 +2024-06-11 23:56:22,089 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:17,954 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:22,956 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:27,962 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:32,967 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:37,973 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:42,979 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:47,985 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:52,992 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:03:57,998 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:03,004 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:08,008 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:13,013 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:18,018 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:23,023 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:28,027 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:33,033 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:38,038 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:43,043 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:48,047 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:53,052 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:04:58,057 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:03,063 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:08,069 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:13,075 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:18,080 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:23,087 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:28,091 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:33,097 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:38,104 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:43,111 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:48,116 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:53,121 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:05:58,127 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:03,133 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:08,139 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:13,145 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:18,151 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:23,157 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:28,162 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:06:33,168 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:05,006 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:10,011 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:15,017 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:20,024 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:25,030 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:30,036 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:35,043 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:40,049 - fup_anemo_daemon - INFO - 27.1 +2024-06-12 00:09:45,055 - fup_anemo_daemon - INFO - 27.1 diff --git a/fup_anemometre_daemon.py b/fup_anemometre_daemon.py new file mode 100644 index 0000000..d265ac1 --- /dev/null +++ b/fup_anemometre_daemon.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +""" +OSC/Random Example: send random numbers to OSC. + +This example sends a pseudo-random number between 0 and 1024 +to the OSC receiver on UDP port 2222. +""" +from pythonosc import udp_client +from pythonosc import osc_message_builder +import time +import signal +import random +import sys +import os +import time +import argparse +import logging +import daemon +from daemon import pidfile + +# import urllib library +from urllib.request import urlopen + +# import json +import json + +debug_p = False + +debug_flag = True +debug_working_directory = os.path.dirname(os.path.realpath(__file__)) + +prod_flag = False +prod_working_directory = "/usr/local/com.fup.anemometre_daemon" + +# SIGINT, SIGKILL, SIGTERM, SIGTSTP + +def rcv_SIGNAL(signum, frame, logf): + print(signum) + +def check_anemometer(logf, inf): + ### This does the "work" of the daemon + + logger = logging.getLogger('fup_anemo_daemon') + logger.setLevel(logging.INFO) + + fh = logging.FileHandler(logf) + fh.setLevel(logging.INFO) + + formatstr = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + formatter = logging.Formatter(formatstr) + + fh.setFormatter(formatter) + + logger.addHandler(fh) + + oscSender = udp_client.UDPClient("localhost", 2222) + + # store the URL in url as + # parameter for urlopen + url = inf + + while True: + # logger.debug("this is a DEBUG message") + # logger.info("this is an INFO message") + # logger.error("this is an ERROR message") + + # store the response of URL + response = urlopen(url) + # storing the JSON response + # from url in data + data_json = json.loads(response.read()) + + # print the json response + # print(data_json) + wind_speed = float(data_json["speed"]) + logger.info(wind_speed) + + msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer") + msg.add_arg(wind_speed) + oscSender.send(msg.build()) + + time.sleep(5) + +def start_daemon(pidf, logf, inf): + ### This launches the daemon in its context + + global debug_p + + if debug_p: + print("fup_anemo_daemon: entered run()") + print("fup_anemo_daemon: pidf = {} logf = {}".format(pidf, logf, inf)) + print("fup_anemo_daemon: about to start daemonization") + + if debug_flag: + ### XXX pidfile is a context + with daemon.DaemonContext( + #working_directory='/var/lib/fup_anemo_daemon', + working_directory=debug_working_directory, + umask=0o002, + pidfile=pidfile.TimeoutPIDLockFile(pidf), + # SIGINT, SIGKILL, SIGTERM, SIGTSTP + signal_map={ + signal.SIGTERM: rcv_SIGNAL, + signal.SIGTSTP: rcv_SIGNAL + } + ) as context: + check_anemometer(logf, inf) + + if prod_flag: + with daemon.DaemonContext( + #working_directory='/var/lib/fup_anemo_daemon', + working_directory=prod_working_directory, + umask=0o002, + pidfile=pidfile.TimeoutPIDLockFile(pidf)#, + # SIGINT, SIGKILL, SIGTERM, SIGTSTP + # signal_map={ + # signal.SIGTERM: rcv_SIGNAL, + # signal.SIGTSTP: rcv_SIGNAL, + # signal.SIGINT: rcv_SIGNAL, + # signal.SIGKILL: rcv_SIGNAL + # } + ) as context: + check_anemometer(logf, inf) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Example daemon in Python") + parser.add_argument('-p', '--pid-file', default='fup_anemo_daemon.pid') + parser.add_argument('-l', '--log-file', default='fup_anemo_daemon.log') + parser.add_argument('-i', '--input-file', default='http://localhost/anemometer.json') + + args = parser.parse_args() + + start_daemon(pidf=args.pid_file, logf=args.log_file, inf=args.input_file) \ No newline at end of file diff --git a/start_daemon.sh b/start_daemon.sh new file mode 100755 index 0000000..db894d9 --- /dev/null +++ b/start_daemon.sh @@ -0,0 +1,3 @@ +#!/bin/sh +source /usr/local/com.fup.anemometre_daemon/.venv/bin/activate +python /usr/local/com.fup.anemometre_daemon/fup_anemometre_daemon.py \ No newline at end of file