client3.py
124 lines
| 3.3 KiB
| text/x-python
|
PythonLexer
/ scripts / client3.py
|
r11 | #!/usr/bin/python | ||
# -*- coding: UTF-8 -*- | ||||
import time | ||||
from datetime import datetime, timedelta | ||||
import zmq | ||||
import json | ||||
import random | ||||
REQUEST_TIMEOUT = 15000 | ||||
REQUEST_RETRIES = 10 | ||||
SERVER_ENDPOINT = "tcp://localhost:4444" | ||||
context = zmq.Context() | ||||
#print("Connecting to server…") | ||||
socket = context.socket(zmq.REQ) | ||||
socket.connect (SERVER_ENDPOINT) | ||||
print('Starting ZMQ client...') | ||||
poll = zmq.Poller() | ||||
poll.register(socket, zmq.POLLIN) | ||||
sequence = 0 | ||||
retries_left = REQUEST_RETRIES | ||||
#dt=datetime(2018,5,15) #en vez de poner now() | ||||
dt=datetime.now() | ||||
while retries_left: | ||||
sequence += 1 | ||||
request = str(sequence).encode() | ||||
print("HF_JRO: Sending (%s)" % request) | ||||
#==== datos noise ========= | ||||
n1 = random.uniform(20.0,20.2) | ||||
n2 = random.uniform(21.0,21.3) | ||||
n3 = random.uniform(22.0,22.2) | ||||
n4 = random.uniform(23.0,23.2) | ||||
n5 = random.uniform(24.0,24.2) | ||||
n6 = random.uniform(25.0,25.3) | ||||
n7 = random.uniform(26.0,26.2) | ||||
n8 = random.uniform(27.0,27.2) | ||||
#=== fin de datos noise === | ||||
#==== datos rti =========== | ||||
yrange = list(range(0,200,2)) | ||||
elementos=[] | ||||
while len(elementos)<=100: | ||||
elementos.append(random.randint(15, 35)) | ||||
#=== fin de datos rti ===== | ||||
#==== datos spc =========== | ||||
x_range = list(range(80)) | ||||
matrix=[] | ||||
while len(matrix)<=80: | ||||
matrix.append(elementos) | ||||
#=== fin de datos spc ===== | ||||
dt1=dt+timedelta(seconds=30) #ahora voy a cambiar cada 30min ya no cada 30 segundos | ||||
#dt1 = datetime.now() | ||||
dato1 = {"time":time.mktime(dt1.timetuple()), | ||||
"yrange": yrange, | ||||
"xrange": x_range, | ||||
"localtime": True, | ||||
"interval": 30, | ||||
"exp_code": 100, | ||||
"data": {"noise":[n1, n2, n3, n4, n5, n6, n7, n8], | ||||
"rti":[elementos,elementos,elementos,elementos,elementos,elementos,elementos,elementos], | ||||
"spc":[matrix,matrix,matrix,matrix,matrix,matrix,matrix,matrix] | ||||
} | ||||
} | ||||
#print("Sending..") | ||||
dt = dt1 | ||||
#=== para simular huecos === | ||||
#if dt.hour in (3 ,4): | ||||
# continue | ||||
socket.send_json(dato1) | ||||
expect_reply = True | ||||
while expect_reply: | ||||
socks = dict(poll.poll(REQUEST_TIMEOUT)) | ||||
if socks.get(socket) == zmq.POLLIN: | ||||
reply = socket.recv_string() | ||||
if not reply: | ||||
break | ||||
if reply == "recibido": | ||||
print("I: Server replied OK (%s)" % reply) | ||||
retries_left = REQUEST_RETRIES | ||||
expect_reply = False | ||||
else: | ||||
print("E: Malformed reply from server: %s" % reply) | ||||
else: | ||||
print("W: No response from server, retrying…") | ||||
# Socket is confused. Close and remove it. | ||||
socket.setsockopt(zmq.LINGER, 0) | ||||
socket.close() | ||||
poll.unregister(socket) | ||||
retries_left -= 1 | ||||
if retries_left == 0: | ||||
print("E: Server seems to be offline, abandoning") | ||||
break | ||||
print("I: Reconnecting and resending (%s)" % request) | ||||
# Create new connection | ||||
socket = context.socket(zmq.REQ) | ||||
socket.connect(SERVER_ENDPOINT) | ||||
poll.register(socket, zmq.POLLIN) | ||||
#socket.send(request) | ||||
#time.sleep(5) | ||||
time.sleep(30) | ||||
context.term() | ||||
#=================== | ||||