summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-06-19 11:08:57 +0200
committerxengineering <mail2xengineering@protonmail.com>2021-06-20 13:57:58 +0200
commit963b86e49fad07a3722646b9d7f4b1c09d199eb1 (patch)
tree5ccb6b66bf9ce1c53e3e97fa7057e44b66512295 /python
parent198023be0f62ef6bb35cdd5ae78f90ab0a6f187e (diff)
downloadbirdscan-963b86e49fad07a3722646b9d7f4b1c09d199eb1.tar
birdscan-963b86e49fad07a3722646b9d7f4b1c09d199eb1.tar.zst
birdscan-963b86e49fad07a3722646b9d7f4b1c09d199eb1.zip
Implement continuous Python Daemon
Diffstat (limited to 'python')
-rw-r--r--python/birdscan/__main__.py53
1 files changed, 43 insertions, 10 deletions
diff --git a/python/birdscan/__main__.py b/python/birdscan/__main__.py
index 71ba294..862ec1b 100644
--- a/python/birdscan/__main__.py
+++ b/python/birdscan/__main__.py
@@ -10,25 +10,58 @@ This is the main executable of the birdscan package.
import sys
import time
+import socket
+import argparse
def main():
"""Main function of this module"""
- if len(sys.argv) == 2: # check if argument is given (--debug)
- time.sleep(1)
- sys.stdout.write("ok\n")
- else:
+ # parse command line args
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-d", "--debug", action="store_true", help="For use in development environment")
+ parser.add_argument("-s", "--socket", default="/dev/null", type=str, help="Unix domain socket path for IPC")
+ args = parser.parse_args()
+
+ # prepare camera if on production system
+ if not args.debug:
from picamera import PiCamera
camera = PiCamera()
camera.resolution = "3280x2464"
camera.start_preview()
- # Camera warm-up time
- time.sleep(2)
- camera.capture("/var/lib/birdscan/{}.jpg".format(
- time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()))
- )
- sys.stdout.write("ok\n")
+ time.sleep(2) # camera warm-up time
+
+ # connect to IPC socket
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ sock.connect(args.socket)
+ sock.send("picamera\n".encode()) # register this service
+
+ # main loop for Python background service
+ while True:
+ cmd = read_line(sock)
+ if cmd == "single_picture":
+ if args.debug:
+ time.sleep(1)
+ sock.send("single_picture_taken\n".encode())
+ else:
+ camera.capture("/var/lib/birdscan/{}.jpg".format(
+ time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()))
+ )
+ sock.send("single_picture_taken\n".encode())
+ elif cmd == "":
+ pass
+ else:
+ sock.send("unknown_command\n".encode())
+
+def read_line(socket):
+ data = b""
+ while True:
+ byte = socket.recv(1)
+ if byte == b"\n":
+ break
+ else:
+ data += byte
+ return data.decode()
if __name__ == "__main__":