Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Synapse Network Application Protocol (SNAP) Gateway
- Author: Jon Wilder
- Project Manager: Jon Wilder
- Created on 1/21/2021
- (C) Copyright 2021 SohCahToa Embedded Solutions. All rights reserved.
- """
- from snapconnect import snap
- import sys
- import time
- class SnapGateway(object):
- def __init__(self):
- # Create a SNAP instance
- self.snap = snap.Snap(funcs = {})
- # Create event handler object
- self.hooks = self.EventHandler(self)
- # Event: Serial connection with SNAP node established
- self.snap.set_hook(snap.hooks.HOOK_SERIAL_OPEN,self.hooks.on_serial_open)
- # Establish a serial connection with serial connected SNAP node
- self.snap.open_serial(snap.SERIAL_TYPE_RS232,"/dev/serial0")
- # Accept incoming TCP/IP connections
- self.snap.accept_tcp()
- class EventHandler(object):
- def __init__(self,parent):
- # Create parent class object
- self.snap = parent
- def on_serial_open(self,serial_type,port,addr):
- # Get event time
- event_time = time.strftime("%b %d, %Y %H:%M:%S")
- if serial_type is snap.SERIAL_TYPE_RS232:
- # If serial connection type is an RS232 serial port connection
- serial_type = "RS232"
- elif serial_type is snap.SERIAL_TYPE_SNAPSTICK100 or serial_type is snap.SERIAL_TYPE_SNAPSTICK200:
- # If serial connection type is a USB connection
- serial_type = "USB"
- # Print statement containing serial connection information
- print("{}: {} connection established with SNAP node {} on port {}").format(event_time,serial_type,self.snap.getInfo(addr),port)
- def getInfo(self,address):
- address = snap.binascii.hexlify(address)
- snap_address = ""
- for i in xrange(0,2):
- snap_address += address[i]
- snap_address += "."
- for i in xrange(2,4):
- snap_address += address[i]
- snap_address += "."
- for i in xrange(4,6):
- snap_address += address[i]
- return snap_address
- if __name__ == "__main__":
- try:
- snapberrypi = SnapGateway()
- snapberrypi.snap.loop()
- except KeyboardInterrupt:
- sys.exit(0)
- # End of file
Add Comment
Please, Sign In to add comment