picmicrodude

Untitled

Jan 25th, 2021 (edited)
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Synapse Network Application Protocol (SNAP) Gateway
  5. Author: Jon Wilder
  6. Project Manager: Jon Wilder
  7. Created on 1/21/2021
  8. (C) Copyright 2021 SohCahToa Embedded Solutions. All rights reserved.
  9. """
  10.  
  11. from snapconnect import snap
  12. import sys
  13. import time
  14.  
  15. class SnapGateway(object):
  16.     def __init__(self):
  17.         # Create a SNAP instance
  18.         self.snap = snap.Snap(funcs = {})
  19.         # Create event handler object
  20.         self.hooks = self.EventHandler(self)
  21.         # Event: Serial connection with SNAP node established
  22.         self.snap.set_hook(snap.hooks.HOOK_SERIAL_OPEN,self.hooks.on_serial_open)
  23.         # Establish a serial connection with serial connected SNAP node
  24.         self.snap.open_serial(snap.SERIAL_TYPE_RS232,"/dev/serial0")
  25.         # Accept incoming TCP/IP connections
  26.         self.snap.accept_tcp()
  27.  
  28.     class EventHandler(object):
  29.         def __init__(self,parent):
  30.             # Create parent class object
  31.             self.snap = parent
  32.  
  33.         def on_serial_open(self,serial_type,port,addr):
  34.             # Get event time
  35.             event_time = time.strftime("%b %d, %Y %H:%M:%S")
  36.             if serial_type is snap.SERIAL_TYPE_RS232:
  37.                 # If serial connection type is an RS232 serial port connection
  38.                 serial_type = "RS232"
  39.             elif serial_type is snap.SERIAL_TYPE_SNAPSTICK100 or serial_type is snap.SERIAL_TYPE_SNAPSTICK200:
  40.                 # If serial connection type is a USB connection
  41.                 serial_type = "USB"
  42.                 # Print statement containing serial connection information
  43.             print("{}: {} connection established with SNAP node {} on port {}").format(event_time,serial_type,self.snap.getInfo(addr),port)
  44.  
  45.     def getInfo(self,address):
  46.         address = snap.binascii.hexlify(address)
  47.         snap_address = ""
  48.         for i in xrange(0,2):
  49.             snap_address += address[i]
  50.         snap_address += "."
  51.         for i in xrange(2,4):
  52.             snap_address += address[i]
  53.         snap_address += "."
  54.         for i in xrange(4,6):
  55.             snap_address += address[i]
  56.         return snap_address
  57.  
  58. if __name__ == "__main__":
  59.     try:
  60.         snapberrypi = SnapGateway()
  61.         snapberrypi.snap.loop()
  62.     except KeyboardInterrupt:
  63.         sys.exit(0)
  64.  
  65. # End of file
  66.  
Add Comment
Please, Sign In to add comment