def waitSensors(conn): while True: data = conn.recv(1024) # Recebe os dados if not data: break # Check if is the admin if (data.decode("utf-8") == password): if (len(adminMessages) != 0): json_data = json.dumps(adminMessages, sort_keys=False, indent=2) conn.send(json_data.encode("utf-8")) adminMessages.clear() else: conn.send("Não há avisos".encode("utf-8")) else: try: dataAndStatistic = [] data = json.loads(data.decode("utf-8")) fakeSensorData = FakeSensor(data["sensorType"], data["metric"], data["time"], data["message"], data["status"], data["id"]) dataAndStatistic.insert(0, conn) dataAndStatistic.insert(1, fakeSensorData) connection.append(dataAndStatistic) print('Received from the client :', fakeSensorData.__dict__) if (len(connection) >= connectionNumber): engenier() except: print("Error") conn.send("Data has been error".encode("utf-8")) conn.close()
def getInfo(con): # message sent to server fakeSensorData.sensorType = "Sensor de umidade" fakeSensorData.time = 150 fakeSensorData.metric = random.randint(100, 1500) fakeSensorData.id = "CEBOLA" json_data = json.dumps(fakeSensorData.__dict__, sort_keys=False, indent=2) con.send(json_data.encode("utf-8")) # message received from server data = json.loads(con.recv(1024)) if(data != ""): feedback = FakeSensor(data["sensorType"], data["metric"], data["time"], data["message"], data["status"], data["id"]) print('Received from the server :', feedback.__dict__) print ("Fechou a conexao") time.sleep(13) Main()
# Import socket module from threading import Thread from fakeSensor import FakeSensor import random import socket import json import time # Global Variable fakeSensorData = FakeSensor() def getInfo(con): # message sent to server fakeSensorData.sensorType = "Sensor de umidade" fakeSensorData.time = 150 fakeSensorData.metric = random.randint(100, 1500) fakeSensorData.id = "CEBOLA" json_data = json.dumps(fakeSensorData.__dict__, sort_keys=False, indent=2) con.send(json_data.encode("utf-8")) # message received from server data = json.loads(con.recv(1024)) if(data != ""): feedback = FakeSensor(data["sensorType"], data["metric"], data["time"], data["message"], data["status"], data["id"]) print('Received from the server :', feedback.__dict__) print ("Fechou a conexao") time.sleep(13) Main() def Main():