summaryrefslogtreecommitdiff
path: root/src/esp32_capture.py
blob: 4a9ce10e9810bfb71408cfde96237479cae95dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import asyncio
import logging
import os.path

from argparse import ArgumentParser
from home.camera.esp32 import WebClient
from home.util import parse_addr, Addr
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
from typing import Optional

logger = logging.getLogger(__name__)
cam: Optional[WebClient] = None


class ESP32Capture:
    def __init__(self, addr: Addr, interval: float, output_directory: str):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.client = WebClient(addr)
        self.output_directory = output_directory
        self.interval = interval

        self.scheduler = AsyncIOScheduler()
        self.scheduler.add_job(self.capture, 'interval', seconds=arg.interval)
        self.scheduler.start()

    async def capture(self):
        self.logger.debug('capture: start')
        now = datetime.now()
        filename = os.path.join(
            self.output_directory,
            now.strftime('%Y-%m-%d-%H:%M:%S.%f.jpg')
        )
        if not await self.client.capture(filename):
            self.logger.error('failed to capture')
        self.logger.debug('capture: done')


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--addr', type=str, required=True)
    parser.add_argument('--output-directory', type=str, required=True)
    parser.add_argument('--interval', type=float, default=0.5)
    parser.add_argument('--verbose', action='store_true')
    arg = parser.parse_args()

    if arg.verbose:
        logging.basicConfig(level=logging.DEBUG)

    loop = asyncio.get_event_loop()

    ESP32Capture(parse_addr(arg.addr), arg.interval, arg.output_directory)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass