Skip to content

Commit 2a553e7

Browse files
committed
1 parent 7899600 commit 2a553e7

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# **Changelog for PG3 Python Interface**
22

3+
### 3.3.16
4+
- Enhanced webhook support
5+
36
### 3.3.15
47
- Added polyglot.DELNODE events
58

udi_interface/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '3.3.15'
1+
__version__ = '3.3.16'
22
__description__ = 'UDI Python Interface for Polyglot version 3'
33
__url__ = 'https://github.com/UniversalDevicesInc/udi_python_interface'
44
__author__ = 'Universal Devices Inc.'

udi_interface/interface.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import re
1717
import sys
1818
from threading import Thread, current_thread, Lock
19+
from datetime import datetime
1920
import time
2021
import netifaces
2122
import logging
@@ -229,7 +230,7 @@ class Interface(object):
229230

230231
__exists = False
231232

232-
def __init__(self, classes, envVar=None):
233+
def __init__(self, classes, options=None):
233234
if self.__exists:
234235
warnings.warn('Only one Interface is allowed.')
235236
return
@@ -239,6 +240,8 @@ def __init__(self, classes, envVar=None):
239240
except:
240241
LOGGER.critical('Failed to parse init. Exiting...')
241242
sys.exit(1)
243+
self._options = options if options is not None else {}
244+
self._enableWebhook = self._options.get('enableWebhook', False)
242245
self.config = None
243246
self.isInitialConfig = False
244247
self.currentLogLevel = 10
@@ -332,7 +335,7 @@ def _connect(self, mqttc, userdata, flags, reason_code, properties):
332335
if self.using_mosquitto:
333336
connected = {"connected": [{}]}
334337
self._mqttc.publish('udi/pg3/connections/ns/{}'.format(self.id), json.dumps(connected), retain=True)
335-
failed = {"disconnected": [{}]}
338+
failed = self._disconnectedMessage()
336339
self._mqttc.will_set('udi/pg3/connections/ns/{}'.format(self.id), json.dumps(failed), qos=0, retain=True)
337340

338341
results.append((self.topicInput, tuple(
@@ -542,7 +545,7 @@ def _startMqtt(self):
542545

543546
if self.using_mosquitto:
544547
# Set up the will, do we need this here?
545-
failed = {"disconnected": [{}]}
548+
failed = self._disconnectedMessage()
546549
self._mqttc.will_set('udi/pg3/connections/ns/{}'.format(self.id), json.dumps(failed), qos=0, retain=True)
547550

548551
done = False
@@ -618,7 +621,7 @@ def stop(self):
618621
LOGGER.info('Disconnecting from MQTT... {}:{}'.format(
619622
self._server, self._port))
620623
self._mqttc.loop_stop()
621-
disconnect = {"disconnected": [{}]}
624+
disconnect = self._disconnectedMessage()
622625
self._mqttc.publish('udi/pg3/connections/ns/{}'.format(self.id), json.dumps(disconnect), retain=True)
623626
self._mqttc.disconnect()
624627

@@ -1627,6 +1630,26 @@ def webhookResponse(self, body='Success', status=200):
16271630
LOGGER.debug('Returning webhook response')
16281631
self.send({'webhook': { 'body': body, 'status': status } }, 'portal')
16291632

1633+
""" Node server method to enable webhooks. Can optionally pass a config object to portal """
1634+
def webhookStart(self, config=None):
1635+
if not self._enableWebhook:
1636+
raise Exception("Webhooks are not enabled. Pass { \"enableWebhook\": True } in polyglot Interface second argument.")
1637+
1638+
LOGGER.info('Webhook start')
1639+
1640+
# Create the base webhook data
1641+
data = {
1642+
'uuid': self.uuid,
1643+
'profileNum': self.profileNum,
1644+
'timestamp': datetime.utcnow().isoformat() + 'Z'
1645+
}
1646+
1647+
# Add config if provided
1648+
if config is not None:
1649+
data['config'] = config
1650+
1651+
self.send({'webhookStart': data }, 'portal')
1652+
16301653
""" Node server method to initiate a bonjour query on the network. Response is returned as a polyglot.BONJOUR event """
16311654
def bonjour(self, type, subtypes, protocol):
16321655

@@ -1647,3 +1670,12 @@ def bonjour(self, type, subtypes, protocol):
16471670
}]
16481671
}
16491672
self.send(message, 'command')
1673+
1674+
def _disconnectedMessage(self):
1675+
message = {"disconnected": [{}]}
1676+
1677+
# If we are using webhooks, we need to tell portal. We can't send a second will, so we tell PG3 to do it for us
1678+
if self._enableWebhook:
1679+
message["webhookStop"] = [{}]
1680+
1681+
return message

0 commit comments

Comments
 (0)