-
Notifications
You must be signed in to change notification settings - Fork 4
/
producer.py
79 lines (70 loc) · 1.93 KB
/
producer.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
import argparse
import json
import time
from confluent_kafka import Producer
code = {
'A': '*-',
'B': '-***',
'C': '-*-*',
'D': '-**',
'E': '*',
'F': '**-*',
'G': '--*',
'H': '****',
'I': '**',
'J': '*---',
'K': '-*-',
'L': '*-**',
'M': '--',
'N': '-*',
'O': '---',
'P': '*--*',
'Q': '--*-',
'R': '*-*',
'S': '***',
'T': '-',
'U': '**-',
'V': '***-',
'W': '*--',
'X': '-**-',
'Y': '-*--',
'Z': '--**',
}
def get_json_str(timestamp, new_status):
d = {
'timestamp': timestamp,
'new_status': new_status,
}
print (json.dumps(d))
return json.dumps(d)
def send_long(p, topic, key):
p.produce(topic, key=key, value=get_json_str(time.time(), "on"))
time.sleep(0.1)
p.produce(topic, key=key, value=get_json_str(time.time(), "off"))
time.sleep(0.05)
def send_short(p, topic, key):
p.produce(topic, key=key, value=get_json_str(time.time(), "on"))
time.sleep(0.05)
p.produce(topic, key=key, value=get_json_str(time.time(), "off"))
time.sleep(0.05)
def send_letter(letter, p, topic, key):
for send in code[letter]:
if send == '*':
send_short(p, topic, key)
else:
send_long(p, topic, key)
time.sleep(0.1)
parser = argparse.ArgumentParser(description='Send string via morse code light bulb.')
parser.add_argument('--key', type=str, default='1',
help='key')
parser.add_argument('--topic', type=str, default='light_bulb',
help='publish topic')
parser.add_argument('--string', type=str, default='ABC',
help='string data (A-Z)')
args = parser.parse_args()
if not args.string.isalpha():
raise RuntimeError("Input string should only contain letter A-Z.")
p = Producer({'bootstrap.servers': 'localhost:9091'})
for letter in args.string.upper():
send_letter(letter, p, args.topic, args.key)
p.flush(30)