This repository has been archived by the owner on Jul 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.py
72 lines (60 loc) · 2.24 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
import sys
import argparse
import middleware
import random
import time
text = ["Ó mar salgado, quanto do teu sal",
"São lágrimas de Portugal!",
"Por te cruzarmos, quantas mães choraram,",
"Quantos filhos em vão rezaram!",
"Quantas noivas ficaram por casar",
"Para que fosses nosso, ó mar!",
"Valeu a pena? Tudo vale a pena",
"Se a alma não é pequena.",
"Quem quer passar além do Bojador",
"Tem que passar além da dor.",
"Deus ao mar o perigo e o abismo deu,",
"Mas nele é que espelhou o céu.",]
class Producer:
def __init__(self, datatype):
self.type = datatype
self.queue = [middleware.JSONQueue(f"/{self.type}", middleware.MiddlewareType.PRODUCER)]
if datatype == "temp":
self.gen = self._temp
elif datatype == "msg":
self.gen = self._msg
elif datatype == "weather":
self.queue = [middleware.JSONQueue(f"/{self.type}/temperature", middleware.MiddlewareType.PRODUCER),
middleware.JSONQueue(f"/{self.type}/humidity", middleware.MiddlewareType.PRODUCER),
middleware.JSONQueue(f"/{self.type}/pressure", middleware.MiddlewareType.PRODUCER)]
self.gen = self._weather
@classmethod
def datatypes(self):
return ["temp", "msg", "weather"]
def _temp(self):
time.sleep(0.1)
yield random.randint(0,40)
def _msg(self):
time.sleep(0.2)
yield random.choice(text)
def _weather(self):
time.sleep(0.1)
yield random.randint(0,40)
time.sleep(0.1)
yield random.randint(0,100)
time.sleep(0.1)
yield random.randint(10000,11000)
def run(self, length=10):
for _ in range(length):
for queue, value in zip(self.queue, self.gen()):
queue.push(value)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--type", help="type of producer: [temp, msg, weather]", default="temp")
parser.add_argument("--length", help="number of messages to be sent", default=10)
args = parser.parse_args()
if args.type not in Producer.datatypes():
print("Error: not a valid producer type")
sys.exit(1)
p = Producer(args.type)
p.run(int(args.length))