-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubnub_apis.py
63 lines (47 loc) · 1.56 KB
/
pubnub_apis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# developed by Gabi Zapodeanu, TSA, GPO, Cisco Systems
import json
import requests
import pubnub
import datetime
import time
from config import PUB_KEY, SUB_KEY, CHANNEL
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
def pubnub_init():
# initialize the channel
pnconfig = PNConfiguration()
pnconfig.subscribe_key = SUB_KEY
pnconfig.publish_key = PUB_KEY
pnconfig.ssl = False
pubnub = PubNub(pnconfig)
return pubnub
def publish_callback(result, status):
print("\nPublish result: ", result)
# Handle PNPublishResult and PNStatus
def here_now_callback(result, status):
if status.is_error():
# handle error
return
for channel_data in result.channels:
print("\nChannel status now:")
print("channel: %s" % channel_data.channel_name)
print("occupancy: %s" % channel_data.occupancy)
for occupant in channel_data.occupants:
print("uuid: %s, state: %s" % (occupant.uuid, occupant.state))
def pub_message(command):
pubnub = pubnub_init()
# print("\nPubNub Channel Info: ", pubnub)
pubnub.publish().channel(CHANNEL).message(command).async(publish_callback)
pubnub.here_now() \
.channels(CHANNEL) \
.include_uuids(True) \
.async(here_now_callback)
"""
pubnub = pubnub_init()
print("\nPubNub Channel Info: ", pubnub)
pubnub.publish().channel(CHANNEL).message('NYC-9300#oper#show ip int bri').async(publish_callback)
pubnub.here_now() \
.channels(CHANNEL) \
.include_uuids(True) \
.async(here_now_callback)
"""