Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- '''Connect to a WirelessHART Manager and print data notifications
- The connection parameters are taken from the environment variables SMSDK_HOST and SMSDK_PORT.
- '''
- import os
- import sys
- sys.path += [
- os.path.join('.'), # path to directory containing SmartMeshSDK
- ]
- import datetime
- import threading
- import pprint
- import SmartMeshSDK.ApiException
- from SmartMeshSDK.ApiDefinition import HartMgrDefinition
- from SmartMeshSDK.HartMgrConnector import HartMgrConnector
- # Configuration parameters
- DEFAULT_HOST = '192.168.99.100'
- DEFAULT_PORT = 4445
- pp = pprint.PrettyPrinter()
- class NotifHandler(threading.Thread):
- '''Get notifications from the WirelessHART Manager
- This class is based on Thread, so it can be run asynchronously
- '''
- def __init__(self, mgr):
- threading.Thread.__init__(self)
- self.mgr = mgr
- def run(self):
- while True:
- notif = self.mgr.getNotification(1)
- if not notif:
- continue
- # TODO: detect exit conditions
- # Raw notifications
- #pp.pprint(notif)
- # Structured data
- # notif[0] is the type, notif[1] is the notification
- # the notification time is a timestamp milliseconds
- notif_time = datetime.datetime.fromtimestamp(notif[1].time/1000)
- payload_data = ["{0:02X}".format(b) for b in notif[1].payload]
- fmt = "DATA: mac:{0} time:{1} payload:[{2}]\n {3}"
- print fmt.format(notif[1].macAddr,
- notif_time.isoformat(),
- len(notif[1].payload),
- " ".join(payload_data))
- def main(host, port):
- # Initialize the WirelessHART Manager Connector
- mgr = HartMgrConnector.HartMgrConnector()
- # Connect to the Manager over the XML API
- mgr.connect({'host': host, 'port': int(port), 'use_ssl': False})
- # Subscribe to data notifications
- mgr.dn_subscribe('data')
- # Create an object to receive notifications
- notifier = NotifHandler(mgr)
- notifier.run() # use notifier.start() to run in a separate thread
- # Disconnect from the Manager
- mgr.disconnect()
- if __name__ == '__main__':
- try:
- host = os.environ['SMSDK_HOST']
- except KeyError:
- host = DEFAULT_HOST
- try:
- port = os.environ['SMSDK_PORT']
- except KeyError:
- port = DEFAULT_PORT
- main(host, port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement