|
1 | 1 | """Example to fetch pullpoint events."""
|
2 | 2 |
|
| 3 | +from aiohttp import web |
| 4 | +import argparse |
3 | 5 | import asyncio
|
4 | 6 | import datetime as dt
|
5 | 7 | import logging
|
| 8 | +import onvif |
| 9 | +import os.path |
| 10 | +import pprint |
| 11 | +import sys |
6 | 12 |
|
| 13 | +SUBSCRIPTION_TIME = dt.timedelta(minutes=1) |
| 14 | +WAIT_TIME = dt.timedelta(seconds=30) |
7 | 15 |
|
8 |
| -from onvif import ONVIFCamera |
9 | 16 |
|
10 |
| -logging.getLogger("zeep").setLevel(logging.DEBUG) |
| 17 | +def subscription_lost(): |
| 18 | + print("subscription lost") |
11 | 19 |
|
12 | 20 |
|
13 |
| -async def run(): |
14 |
| - mycam = ONVIFCamera( |
15 |
| - "192.168.3.10", |
16 |
| - 80, |
17 |
| - "hass", |
18 |
| - "peek4boo", |
19 |
| - wsdl_dir="/home/jason/python-onvif-zeep-async/onvif/wsdl", |
| 21 | +async def post_handler(request): |
| 22 | + print(request) |
| 23 | + print(request.url) |
| 24 | + for k, v in request.headers.items(): |
| 25 | + print(f"{k}: {v}") |
| 26 | + body = await request.content.read() |
| 27 | + print(body) |
| 28 | + return web.Response() |
| 29 | + |
| 30 | + |
| 31 | +async def run(args): |
| 32 | + mycam = onvif.ONVIFCamera( |
| 33 | + args.host, |
| 34 | + args.port, |
| 35 | + args.username, |
| 36 | + args.password, |
| 37 | + wsdl_dir=f"{os.path.dirname(onvif.__file__)}/wsdl/", |
20 | 38 | )
|
21 | 39 | await mycam.update_xaddrs()
|
22 | 40 |
|
23 |
| - if not await mycam.create_pullpoint_subscription(): |
24 |
| - print("PullPoint not supported") |
25 |
| - return |
26 |
| - |
27 |
| - event_service = mycam.create_events_service() |
28 |
| - properties = await event_service.GetEventProperties() |
29 |
| - print(properties) |
30 |
| - capabilities = await event_service.GetServiceCapabilities() |
31 |
| - print(capabilities) |
32 |
| - |
33 |
| - pullpoint = mycam.create_pullpoint_service() |
34 |
| - await pullpoint.SetSynchronizationPoint() |
35 |
| - req = pullpoint.create_type("PullMessages") |
36 |
| - req.MessageLimit = 100 |
37 |
| - req.Timeout = dt.timedelta(seconds=30) |
38 |
| - messages = await pullpoint.PullMessages(req) |
39 |
| - print(messages) |
40 |
| - |
41 |
| - subscription = mycam.create_subscription_service("PullPointSubscription") |
42 |
| - termination_time = ( |
43 |
| - (dt.datetime.utcnow() + dt.timedelta(days=1)) |
44 |
| - .isoformat(timespec="seconds") |
45 |
| - .replace("+00:00", "Z") |
46 |
| - ) |
47 |
| - await subscription.Renew(termination_time) |
48 |
| - await subscription.Unsubscribe() |
| 41 | + capabilities = await mycam.get_capabilities() |
| 42 | + pprint.pprint(capabilities) |
| 43 | + |
| 44 | + if args.notification: |
| 45 | + app = web.Application() |
| 46 | + app.add_routes([web.post("/", post_handler)]) |
| 47 | + runner = web.AppRunner(app) |
| 48 | + await runner.setup() |
| 49 | + site = web.TCPSite(runner, args.notification_address, args.notification_port) |
| 50 | + await site.start() |
| 51 | + |
| 52 | + receive_url = f"http://{args.notification_address}:{args.notification_port}/" |
| 53 | + manager = await mycam.create_notification_manager( |
| 54 | + receive_url, |
| 55 | + SUBSCRIPTION_TIME, |
| 56 | + subscription_lost, |
| 57 | + ) |
| 58 | + await manager.set_synchronization_point() |
| 59 | + |
| 60 | + print(f"waiting for messages at {receive_url}...") |
| 61 | + await asyncio.sleep(WAIT_TIME.total_seconds()) |
| 62 | + |
| 63 | + await manager.shutdown() |
| 64 | + await runner.cleanup() |
| 65 | + else: |
| 66 | + manager = await mycam.create_pullpoint_manager( |
| 67 | + SUBSCRIPTION_TIME, subscription_lost |
| 68 | + ) |
| 69 | + await manager.set_synchronization_point() |
| 70 | + |
| 71 | + pullpoint = manager.get_service() |
| 72 | + print("waiting for messages...") |
| 73 | + messages = await pullpoint.PullMessages( |
| 74 | + { |
| 75 | + "MessageLimit": 100, |
| 76 | + "Timeout": WAIT_TIME, |
| 77 | + } |
| 78 | + ) |
| 79 | + print(messages) |
| 80 | + |
| 81 | + await manager.shutdown() |
| 82 | + |
49 | 83 | await mycam.close()
|
50 | 84 |
|
51 | 85 |
|
52 |
| -if __name__ == "__main__": |
| 86 | +def main(): |
| 87 | + logging.getLogger("zeep").setLevel(logging.DEBUG) |
| 88 | + |
| 89 | + parser = argparse.ArgumentParser(prog="EventTester") |
| 90 | + parser.add_argument("--host", default="192.168.3.10") |
| 91 | + parser.add_argument("--port", type=int, default=80) |
| 92 | + parser.add_argument("--username", default="hass") |
| 93 | + parser.add_argument("--password", default="peek4boo") |
| 94 | + parser.add_argument("--notification", action=argparse.BooleanOptionalAction) |
| 95 | + parser.add_argument("--notification_address") |
| 96 | + parser.add_argument("--notification_port", type=int, default=8976) |
| 97 | + |
| 98 | + args = parser.parse_args(sys.argv[1:]) |
| 99 | + if args.notification and args.notification_address is None: |
| 100 | + parser.error("--notification requires --notification_address") |
| 101 | + |
53 | 102 | loop = asyncio.get_event_loop()
|
54 |
| - loop.run_until_complete(run()) |
| 103 | + loop.run_until_complete(run(args)) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments