-
Notifications
You must be signed in to change notification settings - Fork 5
/
publisher_raw.py
executable file
·87 lines (68 loc) · 1.63 KB
/
publisher_raw.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
import math
import os
#import redis
import socket
import signal
import sys
import time
#REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
HOST = "127.0.0.1"
PORT = 6379
CHANNEL = 'sinewave'
# Redis Protocol specification
# ----------------------------
# https://redis.io/topics/protocol
#
# Run this:
#
# redis-cli -p 7777 PUBLISH sinewave XXXXXXXX
#
# while:
#
# nc -l 7777 (or # nc -l -p 7777 ???)
#
# Result:
#
# *3
# $7
# PUBLISH
# $8
# sinewave
# $8
# XXXXXXXX
#
# References:
#
# - `Quick, write me a Redis client <https://medium.com/concerning-pharo/quick-write-me-a-redis-client-5fbe4ddfb13d>`_
# - `An Arduino library for Redis that works on ESP8266 <https://www.arduinolibraries.info/libraries/redis-for-esp8266>`_
#
def main():
#connection = redis.StrictRedis.from_url(REDIS_URL)
s = socket.socket()
s.connect((HOST, PORT))
n = 1
while True:
value = 30 + int(30 * math.sin(n / 4))
row = 'R' * value
print('\x1b[1;36;40m' + row + '\x1b[0m')
#connection.publish('sinewave', row)
# > PUBLISH channel message
message = row
#print('> publish %s %s' % (channel, message))
command = "*3\r\n$7\r\nPUBLISH\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n" % (
len(CHANNEL),
CHANNEL,
len(message),
message
)
#print(command)
s.send(command.encode('utf-8'))
response = s.recv(256)
#print(response)
n += 1
time.sleep(0.1)
s.close()
if __name__ == "__main__":
signal.signal(signal.SIGINT, lambda signum, frame: sys.exit(0))
main()