예제 #1
0
    async def subscripbe_for_realtime_updates(self) -> None:
        """Subscribes to PubNub for realtime updates."""
        # make a call to vivint's userauth endpoint
        authuser_data = await self.vivintskyapi.get_authuser_data()

        pubnub.set_stream_logger("pubnub", logging.INFO)

        pnconfig = PNConfiguration()
        pnconfig.subscribe_key = PN_SUBSCRIBE_KEY
        pnconfig.ssl = True
        pnconfig.reconnect_policy = PNReconnectionPolicy.LINEAR
        pnconfig.heartbeat_notification_options = PNHeartbeatNotificationOptions.ALL

        self.__pubnub = PubNubAsyncio(pnconfig)
        self.__pubnub.add_listener(
            VivintPubNubSubscribeListener(self.handle_pubnub_message)
        )

        pn_channel = f'{PN_CHANNEL}#{authuser_data[AuthUserAttributes.Users][AuthUserAttributes.UsersAttributes.MessageBroadcastChannel]}'  # noqa
        self.__pubnub.subscribe().channels(pn_channel).with_presence().execute()
예제 #2
0
파일: vivint.py 프로젝트: ovirs/pyvivint
    async def subscribe_for_realtime_updates(self, authuser_data: dict = None) -> None:
        """Subscribes to PubNub for realtime updates."""
        # make a call to vivint's authuser endpoint to get message broadcast channel if not supplied
        if not authuser_data:
            authuser_data = await self.vivintskyapi.get_authuser_data()

        pubnub.set_stream_logger("pubnub", logging.INFO)

        pnconfig = PNConfiguration()
        pnconfig.subscribe_key = PN_SUBSCRIBE_KEY
        pnconfig.ssl = True
        pnconfig.reconnect_policy = PNReconnectionPolicy.LINEAR
        pnconfig.heartbeat_notification_options = PNHeartbeatNotificationOptions.ALL

        self.__pubnub = PubNubAsyncio(pnconfig)
        self.__pubnub.add_listener(
            VivintPubNubSubscribeListener(self.handle_pubnub_message)
        )

        pn_channel = f"{PN_CHANNEL}#{authuser_data[AuthUserAttribute.USERS][AuthUserAttribute.UserAttribute.MESSAGE_BROADCAST_CHANNEL]}"
        self.__pubnub.subscribe().channels(pn_channel).with_presence().execute()
예제 #3
0
from flask import Flask, jsonify, request
from pubnub.pubnub import PubNub
from pubnub.enums import PNStatusCategory, PNHeartbeatNotificationOptions
from pubnub.pnconfiguration import PNConfiguration
from pubnub.callbacks import SubscribeCallback
from pprint import pprint

from backend.blockchain.block import Block
from backend.wallet.transaction import Transaction

pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-b6a63c8a-f851-11ea-a11c-fa009153ffbc'
pnconfig.publish_key = 'pub-c-6f41af51-60aa-4832-a186-25aa2f4e1997'
pnconfig.uuid = "SableServerUUID-PUB"
pnconfig.heartbeat_notification_options = PNHeartbeatNotificationOptions.ALL

CHANNELS = {'TEST': 'TEST', 'BLOCK': 'BLOCK', 'TRANSACTION': 'TRANSACTION'}


class Listener(SubscribeCallback):
    def __init__(self, blockchain, transaction_pool):
        self.blockchain = blockchain
        self.transaction_pool = transaction_pool

    def message(self, pubnub, message_object):
        print(
            f'\n-- Channel: {message_object.channel} | Message: {message_object.message}'
        )

        if message_object.channel == CHANNELS['BLOCK']: