Skip to content

Commit

Permalink
examples/python: PD: Add singal handler to exit the script
Browse files Browse the repository at this point in the history
Also, make the script send events only in response to a command. This
way, the PD app can keep running.

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Aug 7, 2024
1 parent 56d9a1d commit 0810d77
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions examples/python/pd_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
# SPDX-License-Identifier: Apache-2.0
#

import signal
import argparse
import serial
from osdp import *

exit_event = 0
def signal_handler(sig, frame):
global exit_event
print('Received SIGINT, quitting...')
exit_event = 1

signal.signal(signal.SIGINT, signal_handler)

class SerialChannel(Channel):
def __init__(self, device: str, speed: int):
self.dev = serial.Serial(device, speed, timeout=0)
Expand Down Expand Up @@ -57,19 +66,13 @@ def __del__(self):
'data': bytes([9,1,9,2,6,3,1,7,7,0]),
}

count = 0 # loop counter

while True:
while not exit_event:
## Check if we have any commands from the CP
cmd = pd.get_command(timeout=5)
if cmd:
print(f"PD: Received command: {cmd}")

## Send a card read event to CP
pd.notify_event(card_event)

# if count >= 5:
# break
count += 1
## Send a card read event to CP
pd.notify_event(card_event)

pd.teardown()

0 comments on commit 0810d77

Please sign in to comment.