|
| 1 | + |
| 2 | +import socket |
| 3 | +import json |
| 4 | +import time |
| 5 | +import random |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +HOST = "127.0.0.1" |
| 9 | +PORT = 5000 |
| 10 | +INTERVALO_ENVIO = 2 |
| 11 | + |
| 12 | +robot_id = input("Digite o ID do robô: ").strip() or "robot_01" |
| 13 | + |
| 14 | + |
| 15 | +def log(level, message): |
| 16 | + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 17 | + print(f"[{timestamp}] [{level}] [{robot_id}] {message}") |
| 18 | + |
| 19 | + |
| 20 | +def gerar_dados(): |
| 21 | + return { |
| 22 | + "robot_id": robot_id, |
| 23 | + "temperatura": round(random.uniform(35.0, 90.0), 2), |
| 24 | + "vibracao": round(random.uniform(0.2, 2.0), 2), |
| 25 | + "rpm": random.randint(800, 1800) |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def conectar(): |
| 30 | + cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 31 | + cliente.settimeout(5) |
| 32 | + cliente.connect((HOST, PORT)) |
| 33 | + return cliente |
| 34 | + |
| 35 | + |
| 36 | +def executar_cliente(): |
| 37 | + cliente = None |
| 38 | + |
| 39 | + while True: |
| 40 | + try: |
| 41 | + if cliente is None: |
| 42 | + log("INFO", f"Tentando conectar ao servidor em {HOST}:{PORT}...") |
| 43 | + cliente = conectar() |
| 44 | + log("INFO", "Conexão estabelecida com sucesso.") |
| 45 | + |
| 46 | + dados = gerar_dados() |
| 47 | + mensagem = json.dumps(dados) |
| 48 | + |
| 49 | + cliente.send(mensagem.encode("utf-8")) |
| 50 | + log("SEND", f"Dados enviados: {dados}") |
| 51 | + |
| 52 | + resposta_bruta = cliente.recv(1024) |
| 53 | + if not resposta_bruta: |
| 54 | + log("WARN", "Servidor fechou a conexão.") |
| 55 | + cliente.close() |
| 56 | + cliente = None |
| 57 | + time.sleep(2) |
| 58 | + continue |
| 59 | + |
| 60 | + try: |
| 61 | + resposta = json.loads(resposta_bruta.decode("utf-8")) |
| 62 | + except json.JSONDecodeError: |
| 63 | + log("ERROR", f"Resposta inválida do servidor: {resposta_bruta!r}") |
| 64 | + time.sleep(INTERVALO_ENVIO) |
| 65 | + continue |
| 66 | + |
| 67 | + if "erro" in resposta: |
| 68 | + log("ERROR", f"Erro retornado pelo servidor: {resposta}") |
| 69 | + else: |
| 70 | + diagnostico = resposta.get("diagnostico", "desconhecido") |
| 71 | + total_alertas = resposta.get("alertas_totais", "N/A") |
| 72 | + nivel = "ALERT" if diagnostico == "FALHA" else "INFO" |
| 73 | + log(nivel, f"Diagnóstico={diagnostico} | Alertas Totais={total_alertas}") |
| 74 | + |
| 75 | + print("-" * 70) |
| 76 | + time.sleep(INTERVALO_ENVIO) |
| 77 | + |
| 78 | + except socket.timeout: |
| 79 | + log("WARN", "Timeout na comunicação com o servidor. Tentando novamente...") |
| 80 | + if cliente: |
| 81 | + cliente.close() |
| 82 | + cliente = None |
| 83 | + time.sleep(2) |
| 84 | + |
| 85 | + except ConnectionRefusedError: |
| 86 | + log("ERROR", "Conexão recusada. Verifique se o servidor está rodando.") |
| 87 | + if cliente: |
| 88 | + cliente.close() |
| 89 | + cliente = None |
| 90 | + time.sleep(3) |
| 91 | + |
| 92 | + except ConnectionResetError: |
| 93 | + log("WARN", "Conexão resetada pelo servidor. Reconectando...") |
| 94 | + if cliente: |
| 95 | + cliente.close() |
| 96 | + cliente = None |
| 97 | + time.sleep(2) |
| 98 | + |
| 99 | + except KeyboardInterrupt: |
| 100 | + log("INFO", "Cliente encerrado manualmente.") |
| 101 | + if cliente: |
| 102 | + cliente.close() |
| 103 | + break |
| 104 | + |
| 105 | + except Exception as e: |
| 106 | + log("ERROR", f"Erro inesperado no cliente: {e}") |
| 107 | + if cliente: |
| 108 | + cliente.close() |
| 109 | + cliente = None |
| 110 | + time.sleep(2) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + executar_cliente() |
0 commit comments