Skip to content

examples: Counting add argparse to get broker info #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/2_counting/broker-EXAMPLE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"brokers": [
{
"username": "intersect_username",
"password": "intersect_password",
"port": 1883,
"host": "127.0.0.1",
"protocol": "mqtt3.1.1"
}
]
}
53 changes: 43 additions & 10 deletions examples/2_counting/counting_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import logging
import time
import argparse, textwrap
import json

from intersect_sdk import (
INTERSECT_JSON_VALUE,
Expand Down Expand Up @@ -134,17 +136,48 @@ def client_callback(
return IntersectClientCallback(messages_to_send=[message])


def parse_arguments():
"""
Setup and parse command-line arguments.
"""

p = argparse.ArgumentParser(description="Counting Example",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
Examples:

# Starting Service
python3 counting_service.py -c mybroker.json

# Starting a Client
python3 counting_client.py -c mybroker.json
'''))
p.add_argument(
"-c", "--config",
type=str,
help="Path to broker configuration file (JSON format)."
)
return p.parse_args()


if __name__ == '__main__':
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

args = parse_arguments()

if args.config:
with open(args.config, 'r') as f:
from_config_file = json.load(f)
else:
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

# The counter will start after the initial message.
# If the service is already active and counting, this may do nothing.
Expand Down
54 changes: 44 additions & 10 deletions examples/2_counting/counting_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time
from dataclasses import dataclass
from typing import Optional
import argparse, textwrap
import json

from pydantic import BaseModel, Field
from typing_extensions import Annotated
Expand Down Expand Up @@ -162,17 +164,49 @@ def _run_count(self) -> None:
time.sleep(1.0)


def parse_arguments():
"""
Setup and parse command-line arguments.
"""

p = argparse.ArgumentParser(description="Counting Example",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
Examples:

# Starting Service
python3 counting_service.py -c mybroker.json

# Starting a Client
python3 counting_client.py -c mybroker.json
'''))
p.add_argument(
"-c", "--config",
type=str,
help="Path to broker configuration file (JSON format)."
)
return p.parse_args()

if __name__ == '__main__':
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

args = parse_arguments()

if args.config:
with open(args.config, 'r') as f:
from_config_file = json.load(f)
else:
# Hardcoded backup
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

config = IntersectServiceConfig(
hierarchy=HierarchyConfig(
organization='counting-organization',
Expand Down
11 changes: 11 additions & 0 deletions examples/2_counting_events/broker-EXAMPLE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"brokers": [
{
"username": "intersect_username",
"password": "intersect_password",
"port": 1883,
"host": "127.0.0.1",
"protocol": "mqtt3.1.1"
}
]
}
52 changes: 42 additions & 10 deletions examples/2_counting_events/counting_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import argparse, textwrap
import json

from intersect_sdk import (
INTERSECT_JSON_VALUE,
Expand Down Expand Up @@ -51,18 +53,48 @@ def event_callback(
if self.events_encountered == self.MAX_EVENTS_TO_PROCESS:
raise Exception

def parse_arguments():
"""
Setup and parse command-line arguments.
"""

p = argparse.ArgumentParser(description="Counting Example",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
Examples:

# Starting Service
python3 counting_service.py -c mybroker.json

# Starting a Client
python3 counting_client.py -c mybroker.json
'''))
p.add_argument(
"-c", "--config",
type=str,
help="Path to broker configuration file (JSON format)."
)
return p.parse_args()


if __name__ == '__main__':
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

args = parse_arguments()

if args.config:
with open(args.config, 'r') as f:
from_config_file = json.load(f)
else:
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

# start listening to events from the counting service
config = IntersectClientConfig(
Expand Down
53 changes: 43 additions & 10 deletions examples/2_counting_events/counting_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import threading
import time
import argparse, textwrap
import json

from intersect_sdk import (
HierarchyConfig,
Expand Down Expand Up @@ -52,18 +54,49 @@ def increment_counter_function(self) -> None:
self.counter *= 3
self.intersect_sdk_emit_event('increment_counter', self.counter)

def parse_arguments():
"""
Setup and parse command-line arguments.
"""

p = argparse.ArgumentParser(description="Counting Example",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
Examples:

# Starting Service
python3 counting_service.py -c mybroker.json

# Starting a Client
python3 counting_client.py -c mybroker.json
'''))
p.add_argument(
"-c", "--config",
type=str,
help="Path to broker configuration file (JSON format)."
)
return p.parse_args()


if __name__ == '__main__':
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

args = parse_arguments()

if args.config:
with open(args.config, 'r') as f:
from_config_file = json.load(f)
else:
from_config_file = {
'brokers': [
{
'username': 'intersect_username',
'password': 'intersect_password',
'port': 1883,
'protocol': 'mqtt3.1.1',
},
],
}

config = IntersectServiceConfig(
hierarchy=HierarchyConfig(
organization='counting-organization',
Expand Down