|
| 1 | +# |
| 2 | +# Copyright (c) 2022 ZettaScale Technology |
| 3 | +# |
| 4 | +# This program and the accompanying materials are made available under the |
| 5 | +# terms of the Eclipse Public License 2.0 which is available at |
| 6 | +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | +# which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | +# |
| 11 | +# Contributors: |
| 12 | +# ZettaScale Zenoh Team, <zenoh@zettascale.tech> |
| 13 | +# |
| 14 | +import time |
| 15 | +from typing import Optional |
| 16 | + |
| 17 | +import zenoh |
| 18 | +from zenoh.ext import CacheConfig, MissDetectionConfig, declare_advanced_publisher |
| 19 | + |
| 20 | + |
| 21 | +def main(conf: zenoh.Config, key: str, payload: str, history: int): |
| 22 | + # initiate logging |
| 23 | + zenoh.init_log_from_env_or("error") |
| 24 | + |
| 25 | + print("Opening session...") |
| 26 | + with zenoh.open(conf) as session: |
| 27 | + print(f"Declaring AdvancedPublisher on '{key}'...") |
| 28 | + pub = declare_advanced_publisher( |
| 29 | + session, |
| 30 | + key, |
| 31 | + cache=CacheConfig(max_samples=history), |
| 32 | + sample_miss_detection=MissDetectionConfig(heartbeat=5), |
| 33 | + publisher_detection=True, |
| 34 | + ) |
| 35 | + |
| 36 | + print("Press CTRL-C to quit...") |
| 37 | + for idx in itertools.count(): |
| 38 | + time.sleep(1) |
| 39 | + buf = f"[{idx:4d}] {payload}" |
| 40 | + print(f"Putting Data ('{key}': '{buf}')...") |
| 41 | + pub.put(buf) |
| 42 | + |
| 43 | + |
| 44 | +# --- Command line argument parsing --- --- --- --- --- --- |
| 45 | +if __name__ == "__main__": |
| 46 | + import argparse |
| 47 | + import itertools |
| 48 | + |
| 49 | + import common |
| 50 | + |
| 51 | + parser = argparse.ArgumentParser( |
| 52 | + prog="z_advanced_pub", description="zenoh advanced pub example" |
| 53 | + ) |
| 54 | + common.add_config_arguments(parser) |
| 55 | + parser.add_argument( |
| 56 | + "--key", |
| 57 | + "-k", |
| 58 | + dest="key", |
| 59 | + default="demo/example/zenoh-python-pub", |
| 60 | + type=str, |
| 61 | + help="The key expression to publish onto.", |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--payload", |
| 65 | + "-p", |
| 66 | + dest="payload", |
| 67 | + default="Pub from Python!", |
| 68 | + type=str, |
| 69 | + help="The payload to publish.", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--history", |
| 73 | + dest="history", |
| 74 | + type=int, |
| 75 | + default=1, |
| 76 | + help="The number of publications to keep in cache", |
| 77 | + ) |
| 78 | + |
| 79 | + args = parser.parse_args() |
| 80 | + conf = common.get_config_from_args(args) |
| 81 | + |
| 82 | + main(conf, args.key, args.payload, args.history) |
0 commit comments