def send(): global client, count chars = "".join([random.choice(string.ascii_lowercase) for i in range(50)] ) sent.append(chars) client.publish(TOPIC_COM, aesEncryptString(chars))
def on_message(client, userdata, message): global BOT_COUNT # newBotDecode = BotPositionArr.FromString(message.payload) decrypted = aesDecrypt(message.payload).decode('utf-8') try: messageString = decrypted.split(';') if message.topic == TOPIC_COM: if messageString[1] == 'get_servers': print('client requests server name') client.publish( TOPIC_COM, aesEncryptString('server_name_response;' + str(SWARM_ID) + ';' + swarm_name)) if message.topic == TOPIC_SEVER_COM: if messageString[1] == 'connection_req': BOT_COUNT = len(robotData) print('client requests connection', { 'bot_count': BOT_COUNT, 'areana_dim': ARENA_DIM }) client.publish( TOPIC_SEVER_COM, aesEncryptString('server_response;success;' + json.dumps({ 'bot_count': BOT_COUNT, 'areana_dim': ARENA_DIM })), qos=2) if messageString[1] == 'set_dest': print("Destination reset") destinations = json.loads(messageString[2]) remapDes(destinations) print(destinations) arrageBot(robotData, destinations) if messageString[1] == 'ping': client.publish(TOPIC_SEVER_COM, aesEncryptString('ping;')) if messageString[1] == 'battStat': client.publish(TOPIC_SEVER_COM, aesEncryptString('battStat;' + battStat())) except Exception as e: # pass print("message format error", e)
print("Publishing message to topic", "swarm/{}/currentPos".format(SWARM_ID)) # serializing data using protobuff before sending data to the server initialize() flagFirst = True # pulishing the msg to the topic stableCount = 0 while(True): newBotPosArr = BotPositionArr() for i, robot in enumerate(robots_data): newBot = BotPosition() newBot.bot_id = i newBot.x_cod = robots_data[i].init_pos[0] newBot.y_cod = robots_data[i].init_pos[1] newBot.angle = 0 newBotPosArr.positions.append(newBot) data = newBotPosArr.SerializeToString() client.publish(TOPIC_COM, aesEncryptString("asd")) time.sleep(1) break client.loop_stop() #stop the loop client.disconnect()