def discover_sensor(self, object_name): request_message = app_pb2.Request_APP() request_message.request_type = app_pb2.Request_APP( ).RequestType.VerifySensor self.send_message(request_message.SerializeToString()) message = self.read_message() if object_name in message.list_object: return True else: return False
def discover_comands(self, object_name): request = app_pb2.Request_APP() request.request_type = 4 request.name = object_name self.client_socket.send(request.SerializeToString()) msg = self.client_socket.recv(1024) mensage = app_pb2.Response_APP() mensage.ParseFromString(msg) return mensage.object_comands
def listen_app_users(connection,address): while True: messagem=connection.recv(1024) user_request = app_pb2.Request_APP() user_request.ParseFromString(messagem) if user_request.request_type<5 and user_request.request_type!=1: client_ident = gateway.client_vector.index(connection) gateway.send_to_object(user_request,client_ident) if user_request.request_type==7: gateway.remove_user(connection) connection.close() break if user_request.request_type==1: server_response = app_pb2.Response_APP() if gateway.sensor_value!='NULL': server_response.object_name = user_request.name server_response.object_result = gateway.sensor_value connection.send(server_response.SerializeToString()) else: client_ident = gateway.client_vector.index(connection) gateway.send_to_object(user_request,client_ident) if user_request.request_type==5: server_response = app_pb2.Response_APP() server_response.object_name = 'LIST OBJECTS' server_response.list_object = str(list(gateway.object_dict.keys())) connection.send(server_response.SerializeToString()) if user_request.request_type==6: vector = list(gateway.object_dict.keys()) if user_request.value not in vector: server_response = app_pb2.Response_APP() server_response.object_name = 'EXISTS' server_response.exists = 0 else: server_response = app_pb2.Response_APP() server_response.object_name = 'EXISTS' server_response.exists = 1 connection.send(server_response.SerializeToString())
def send_message(client): while True: request_message = app_pb2.Request_APP() print("APP MENU:") print("Digite /LIST para ver objetos conectados ao gateaway.") print("Digite /SAIR para sair do app.") print("Digite o nome do objeto: ") command = input('') if command == '/LIST': request_message.request_type = app_pb2.Request_APP( ).RequestType.ListObjects client.send_message(request_message.SerializeToString()) listen_message(client) if command == '/SAIR': request_message.request_type = app_pb2.Request_APP( ).RequestType.LogOut client.send_message(request_message.SerializeToString()) client.client_socket.close() print("Desligando APP") break if command != '/SAIR' and command != '/LIST': request_message = app_pb2.Request_APP() request_message.request_type = app_pb2.Request_APP( ).RequestType.VerifyObject request_message.value = command client.send_message(request_message.SerializeToString()) message = listen_message(client) if message.exists == 1: request_message.request_type = app_pb2.Request_APP( ).RequestType.DiscoverComands request_message.name = command client.send_message(request_message.SerializeToString()) message = listen_message(client) if message.object_status: desire = input('Digite Comando: ') request = app_pb2.Request_APP() if desire in message.object_comands: if desire == 'vol': request.request_type = app_pb2.Request_APP.RequestType.ModStatus request.value = input('Qual o Volume Desejado: ') request.aux = 'vol' request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'channel': request.request_type = app_pb2.Request_APP.RequestType.ModStatus request.value = input('Qual o Canal Desejado: ') request.aux = 'channel' request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'svol': request.request_type = app_pb2.Request_APP.RequestType.ReadStatus request.aux = 'svol' request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'schannel': request.request_type = app_pb2.Request_APP.RequestType.ReadStatus request.aux = 'schannel' request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'off': request.request_type = app_pb2.Request_APP.RequestType.ModOnOf request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'sensor': request.request_type = app_pb2.Request_APP.RequestType.ReadSensor request.aux = desire request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'temp': request.request_type = app_pb2.Request_APP.RequestType.ModStatus request.value = input( 'Qual a temperatura desejada: ') request.aux = desire request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'sstemp': request.request_type = app_pb2.Request_APP.RequestType.ReadStatus request.aux = desire request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'msg': request.request_type = app_pb2.Request_APP.RequestType.ModStatus request.aux = desire request.value = input('Digite a messagem: ') request.name = command client.send_message(request.SerializeToString()) listen_message(client) if desire == 'smsg': request.request_type = app_pb2.Request_APP.RequestType.ReadStatus request.aux = desire request.name = command client.send_message(request.SerializeToString()) listen_message(client) else: desire = input('Digite Comando: ') request = app_pb2.Request_APP() if desire in message.object_comands: if desire == 'on': request.request_type = app_pb2.Request_APP.RequestType.ModOnOf request.name = command request.aux = desire client.send_message(request.SerializeToString()) listen_message(client)
def listen_app_users(connection, address): while True: messagem = connection.recv(1024) user_request = app_pb2.Request_APP() user_request.ParseFromString(messagem) if user_request.request_type < 5: if user_request.name in list(HA.object_dict.keys()): if user_request.request_type == 0: response = app_pb2.Response_APP() stub = HAgrpc_pb2_grpc.ActuatorGRPCStub( HA.object_dict[user_request.name]) request = HAgrpc_pb2.Request() msg = stub.SeeStatus(request) response.object_name = msg.name response.object_result = msg.result connection.send(response.SerializeToString()) if user_request.request_type == 1: response = app_pb2.Response_APP() response.object_name = user_request.name response.object_result = HA.sensor_dict[user_request.name] connection.send(response.SerializeToString()) if user_request.request_type == 2: response = app_pb2.Response_APP() stub = HAgrpc_pb2_grpc.ActuatorGRPCStub( HA.object_dict[user_request.name]) request = HAgrpc_pb2.Request(value=user_request.value) msg = stub.ModStatus(request) response.object_name = msg.name response.object_result = msg.result response.response_type = 0 connection.send(response.SerializeToString()) if user_request.request_type == 3: response = app_pb2.Response_APP() stub = HAgrpc_pb2_grpc.ActuatorGRPCStub( HA.object_dict[user_request.name]) request = HAgrpc_pb2.Request() if user_request.aux == 'OFF': msg = stub.TunrnOff(request) response.on_off_status = 'Dispositivo Esta OFF' if user_request.aux == 'ON': msg = stub.TunrnOn(request) response.on_off_status = 'Dispositivo Esta ON' response.object_name = msg.name response.response_type = 1 connection.send(response.SerializeToString()) if user_request.request_type == 4: response = app_pb2.Response_APP() stub = HAgrpc_pb2_grpc.ActuatorGRPCStub( HA.object_dict[user_request.name]) request = HAgrpc_pb2.Request() msg = stub.SeeComands(request) response.object_name = user_request.name response.object_comands[:] = msg.object_comands connection.send(response.SerializeToString()) else: server_response = app_pb2.Response_APP() server_response.object_name = 'NOT EXISTS' server_response.response_type = 2 connection.send(server_response.SerializeToString()) if user_request.request_type == 5: server_response = app_pb2.Response_APP() server_response.object_name = 'LIST OBJECTS' server_response.list_object = str(list(HA.object_dict.keys())) server_response.response_type = 2 connection.send(server_response.SerializeToString()) if user_request.request_type == 6: server_response = app_pb2.Response_APP() server_response.list_object = str(HA.atuadores) connection.send(server_response.SerializeToString()) if user_request.request_type == 7: server_response = app_pb2.Response_APP() server_response.list_object = str(HA.sensores) connection.send(server_response.SerializeToString()) if user_request.request_type == 8: connection.close() break
def send_message(client): while True: request_message = app_pb2.Request_APP() print("APP MENU:") print("Digite /LIST para ver objetos conectados ao Home Assistant.") print("Digite /SAIR para sair do app.") print("Digite o nome do dispositivo para conectar a um dispositivo.\n") command = input('') if command=='/LIST': request_message.request_type = app_pb2.Request_APP().RequestType.ListObjects client.send_message(request_message.SerializeToString()) listen_message(client) elif command=='/SAIR': request_message.request_type = app_pb2.Request_APP().RequestType.LogOut client.send_message(request_message.SerializeToString()) client.client_socket.close() print("Desligando APP") break elif client.discover_atuator(command)==True: lista_comandos = client.discover_comands(command) print('Comandos: ',lista_comandos) comando = input('Digite o comando: ') request_message = app_pb2.Request_APP() if comando in lista_comandos: if comando=='umi': request_message.value = float(input('Digite a umidade: ')) request_message.request_type = 2 request_message.name = command request_message.aux = comando if comando=='temp': request_message.value = float(input('Digite a temperatura: ')) request_message.request_type = 2 request_message.name = command request_message.aux = comando if comando=='light': request_message.value = float(input('Digite a potencia iluminacao: ')) request_message.request_type = 2 request_message.name = command request_message.aux = comando if comando in ['sumi','stemp','slight']: request_message.aux = comando request_message.request_type = 0 request_message.name = command if comando=='off': request_message.aux = 'OFF' request_message.request_type = 3 request_message.name = command if comando=='on': request_message.aux = 'ON' request_message.request_type = 3 request_message.name = command client.send_message(request_message.SerializeToString()) message = listen_message(client) elif client.discover_sensor(command)==True: global follow follow =1 t_2 = threading.Thread(target=listen_sensor, args=(client,command,request_message,)) t_2.start() follow=int(input())