def render_GET_advanced(self, request, response):
        #enviar mensagem padrao de funcionando
        self.payload = request.payload
        logger.debug("\ndentro do get ")  #,self.payload)

        request.parame

        assert (isinstance(response, Response))
        response.mid = request.mid
        defines.acknowledged = True

        caminhos = request.uri_path.split("/")
        if caminhos[1] == "save":
            DBRegister.saveToFile("./registro.txt")
            response.payload = "dados salvos"
            self.payload = "dados salvos"
            response.code = defines.Codes.CREATED.number  #CREATED.number
            print("dentro do save")
        elif caminhos[1] == "load":
            DBRegister.loadFromFile("./registro.txt")
            response.payload = "dados carregados"
            self.payload = "dados carregados"
            response.code = defines.Codes.CREATED.number  #CREATED.number
            print("dentro do load")
        else:
            logger.debug("opcao invalida")
            response.code = defines.Codes.NOT_IMPLEMENTED.number  #CREATED.number
        return self, response
    def render_DELETE(self, request):
        logger.debug("\ndentro do DELETE")
        DBRegister.loadFromFile("./registro.txt")
        response.payload = "dados carregados"
        self.payload = "dados carregados"
        response.code = defines.Codes.CREATED.number

        return True
    def render_PUT(self, request):
        #registra dispositivo na memoria/BD
        #self.payload = request.payload
        logger.debug("\ndentro do put ")

        DBRegister.saveToFile("./registro.txt")
        response.payload = "dados salvos"
        self.payload = "dados salvos"
        response.code = defines.Codes.CREATED.number  #CREATED.number
        print("dentro do save")

        return self
    def device_register(self, hash_code, device):
        result = "-1"
        i = DBRegister.get(hash_code)
        if (i != -1):
            logger.debug("ja registrado!")
        else:
            logger.debug("registrando...")

            usuario = self.gerar_usuario(device)
            senha = self.gerar_senha(device)
            timestamp = time.strftime('%Y%m%d%H%M%S')

            DBRegister.add(hash_code, usuario, senha, timestamp, device)

            result = RegisterReturn(usuario, senha, timestamp).toJSON()

        return result
    def render_POST_advanced(self, request, response):
        logger.debug("entrou no post advanced login")
        self.payload = request.payload
        par = Request()
        device = request.payload
        adevice = device.split(",")
        '''    
        print ("device   :",device)
        print ("adevice  :",adevice)
        print ("request  :",request)
        print ("uri_path :",request.uri_path)
        print ("senha    :", gerar_senha(device))
        print ("usuario  :", gerar_usuario(device))
        print ("timestamp:", time.strftime('%Y%m%d%H%M%S'))
        '''
        caminhos = request.uri_path.split("/")
        #logger.("caminhos :",caminhos)
        #url = urlparse(request.uri_path)
        """ for d in adevice:
            logger.debug("dados:",str(d))
        """
        hash_code = self.gerar_hash(device)
        register_response = self.device_register(hash_code, device)

        from coapthon.messages.response import Response
        assert (isinstance(response, Response))
        response.payload = "" + register_response
        self.payload = "" + register_response
        response.code = defines.Codes.CHANGED.number  #CREATED.number
        response.mid = request.mid
        defines.acknowledged = True

        print("response.token:", response.token)
        print("request.token:", request.token)
        print("response.mid:", response.mid)
        print("request.mid:", request.mid)

        DBRegister.saveToFile("./registro.txt")
        #print("response:",response)

        return self, response
class RegisterPropostaResource(Resource):
    db = DBRegister()

    def __init__(self, name="RegisterPropostaResource", coap_server=None):
        #nao deixar visivel
        super(RegisterPropostaResource, self).__init__(name,
                                                       coap_server,
                                                       visible=True,
                                                       observable=False,
                                                       allow_children=True)
        self.payload = "Register Resource"

    #def render_GET(self, request):
    def render_GET_advanced(self, request, response):
        #enviar mensagem padrao de funcionando
        self.payload = request.payload
        logger.debug("\ndentro do get ")  #,self.payload)

        request.parame

        assert (isinstance(response, Response))
        response.mid = request.mid
        defines.acknowledged = True

        caminhos = request.uri_path.split("/")
        if caminhos[1] == "save":
            DBRegister.saveToFile("./registro.txt")
            response.payload = "dados salvos"
            self.payload = "dados salvos"
            response.code = defines.Codes.CREATED.number  #CREATED.number
            print("dentro do save")
        elif caminhos[1] == "load":
            DBRegister.loadFromFile("./registro.txt")
            response.payload = "dados carregados"
            self.payload = "dados carregados"
            response.code = defines.Codes.CREATED.number  #CREATED.number
            print("dentro do load")
        else:
            logger.debug("opcao invalida")
            response.code = defines.Codes.NOT_IMPLEMENTED.number  #CREATED.number
        return self, response

    def render_PUT(self, request):
        #registra dispositivo na memoria/BD
        #self.payload = request.payload
        logger.debug("\ndentro do put ")

        DBRegister.saveToFile("./registro.txt")
        response.payload = "dados salvos"
        self.payload = "dados salvos"
        response.code = defines.Codes.CREATED.number  #CREATED.number
        print("dentro do save")

        return self

    def render_POST_advanced(self, request, response):
        logger.debug("entrou no post advanced login")
        self.payload = request.payload
        par = Request()
        device = request.payload
        adevice = device.split(",")
        '''    
        print ("device   :",device)
        print ("adevice  :",adevice)
        print ("request  :",request)
        print ("uri_path :",request.uri_path)
        print ("senha    :", gerar_senha(device))
        print ("usuario  :", gerar_usuario(device))
        print ("timestamp:", time.strftime('%Y%m%d%H%M%S'))
        '''
        caminhos = request.uri_path.split("/")
        #logger.("caminhos :",caminhos)
        #url = urlparse(request.uri_path)
        """ for d in adevice:
            logger.debug("dados:",str(d))
        """
        hash_code = self.gerar_hash(device)
        register_response = self.device_register(hash_code, device)

        from coapthon.messages.response import Response
        assert (isinstance(response, Response))
        response.payload = "" + register_response
        self.payload = "" + register_response
        response.code = defines.Codes.CHANGED.number  #CREATED.number
        response.mid = request.mid
        defines.acknowledged = True

        print("response.token:", response.token)
        print("request.token:", request.token)
        print("response.mid:", response.mid)
        print("request.mid:", request.mid)

        DBRegister.saveToFile("./registro.txt")
        #print("response:",response)

        return self, response

    def render_DELETE(self, request):
        logger.debug("\ndentro do DELETE")
        DBRegister.loadFromFile("./registro.txt")
        response.payload = "dados carregados"
        self.payload = "dados carregados"
        response.code = defines.Codes.CREATED.number

        return True

    #verifica se o dispostivo ja foi registrado
    #  caso nao, o registra
    #  caso sim, volta -1
    def device_register(self, hash_code, device):
        result = "-1"
        i = DBRegister.get(hash_code)
        if (i != -1):
            logger.debug("ja registrado!")
        else:
            logger.debug("registrando...")

            usuario = self.gerar_usuario(device)
            senha = self.gerar_senha(device)
            timestamp = time.strftime('%Y%m%d%H%M%S')

            DBRegister.add(hash_code, usuario, senha, timestamp, device)

            result = RegisterReturn(usuario, senha, timestamp).toJSON()

        return result
        #calcula hash
    def gerar_hash(self, device):
        #gerar json e depois hash
        b_device = device.encode()
        md5 = hashlib.md5(b_device)
        #print("md5:",md5.hexdigest())
        return md5.hexdigest()

    def gerar_senha(self, device):
        #todo: gerar senha
        str_hash = self.gerar_hash(device)
        senha = str_hash[0:5]
        #print("senha:",senha)
        return senha

    def gerar_usuario(self, device):
        str_hash = self.gerar_hash(device)
        usuario = str_hash[5:10]
        #print("usuario:",usuario)
        return usuario
    def process_login(self, request, response):
        logger.debug("entrou no process login")
        user = request.payload

        from coapthon.messages.response import Response
        assert (isinstance(response, Response))
        #response.payload = "Response changed through PUT"
        response.code = defines.Codes.CHANGED.number

        response.content_type = defines.Content_types["application/json"]
        response.mid = request.mid
        defines.acknowledged = True
        #inicio autenticacao

        auser = JsonAdapter.convertToDict(user)
        reg = DBRegister.getByUser(auser["login"])

        authenticated = False

        if reg != -1:
            par_user = auser["login"]
            par_password = auser["password"]

            print("indice:", reg)
            print("request user:"******"request senha:", par_password)
            print("db user:"******"user"])
            print("db senha:", reg["passwd"])

            if par_user == reg["user"] and par_password == reg["passwd"]:
                resultado = "" + time.strftime('%Y%m%d%H%M%S')
                logger.debug("autenticado")
                authenticated = True
            else:
                resultado = "-1"
                logger.debug("senha errada")
        else:
            resultado = "-1"
            logger.debug("nao autenticado")
            logger.debug("nao achou usuario")
            #response.code = "403"#defines.Codes.FORBIDDEN
        #fim autenticacao

        query = "-1"
        if authenticated:
            #[0]-informacao
            #[1]-inicio
            #[2]-fim
            #[3]-inverter
            #[4]-rotacionar
            query = "1;0;2;1;1"

            device = CoapDevice.fromCSV(reg["register_data"])

            queryResponse = str(QueryHelper.processQuery(query, device.uuid),
                                "utf-8")

            entry = FactorTwoEntry(resultado, queryResponse)
            Step2PropostaResource.step2_queries[resultado] = entry
            authEntry = AuthWorkFlowEntry(resultado, "compatibility", 0)
            AuthInspectorResource.auth_workflow[resultado] = authEntry

        retorno = FactorOneReturn(resultado, query).toJSON()

        #aqui calcula o

        logger.debug(retorno)
        response.payload = retorno

        logger.debug("codigos de autenticacao ativos:")
        print(Step2PropostaResource.step2_queries)
        logger.debug("niveis de autenticacao e reputacao:")
        print(AuthInspectorResource.auth_workflow)

        print("request:", request)
        print("response:", response)

        return self, response