/
device.py
71 lines (52 loc) · 1.62 KB
/
device.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
64
65
66
67
68
69
70
71
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
import time
import os
from datetime import datetime, timedelta
import json
pnconfig = PNConfiguration()
pnconfig.publish_key = 'pub-c-5f42fdef-c22f-4438-9650-27d1a37c22a7'
pnconfig.subscribe_key = 'sub-c-41b2ef2c-fc62-11e9-8f6e-d28065e14af1'
pnconfig.ssl = False
pubnub = PubNub(pnconfig)
pnconfig.filter_expression = "uuid !='" + pubnub.uuid +"'"
device_channel = 'device_channel'
meta = {
'uuid': pubnub.uuid,
'type': 'device',
}
def create_message(value):
data = []
date = (datetime.now() + timedelta(seconds=value)).strftime('%Y-%m-%d %H:%M:%S')
data.append((date, value))
# data['input'] = value
# data['datetime'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
tuples = [{'date':i[0], 'input': i[1]} for i in data]
# return str(data)
return tuples
def my_publish_callback(envelope, status):
# Check whether request successfully completed or not
if not status.is_error():
pass
class MySubscribeCallback(SubscribeCallback):
def presence(self, pubnub, presence):
pass
def status(self, pubnub, status):
pass
def message(self, pubnub, message):
print("from server message: " + message.message)
## publish a message
def main():
i = 0
while True:
msg = create_message(i)
print(str(msg))
pubnub.publish().channel(device_channel).meta(meta).message(msg).sync()
i+=1
time.sleep(3)
if __name__ == "__main__":
pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels(device_channel).execute()
main()