Jump to content

Joao

Members
  • Content Count

    2
  • Joined

  • Last visited

Community Reputation

0 Neutral
  1. Joao

    threads

    hi again, another aprouch to my scenario discribed on first topic (import requests) is use the existing python code on threads. On my python code, i had a thread structure how i call on command line and work´s fine, but i need to intercept the information from the calls in my delphi application. To explain, i´m connecting to a lot of Facial controllers to get the access events in real time. so my code on python create a instance of a thread for each facial controller and print the events. the events comming from a http get request with x-midex/multipart content-type, so i call get and iter the response content for each chunk and when i got all the access information, such as name, card, event code, error code, date and time i print the information on screen. the problem to use delphi to do this task is the problemas of the Indy to manage x-mixed/multipar contents and i had some issues with Digest auth and the requests itself, flooding call after call, making a lot of connection/disconnection to controllers log. try python was the manufacturer answer, but i know nothing of python to be honest and i discover P4D and start to learn. My first aprouch was take my python code and put into delphi to manage disconnections, exceptions, and to get the event informations of the controllers to feed my database, but i´snt a easy task, i read the thread demos and watch the webinars but still stuck with how to use. has P4D any documentation of components, functions, procedures ? it too hard to understand how it interact with python and vice-versa, strings as parameters get me a lot of errors, and my attempts of run my threads frozzen my test app. here my code in python import requests import datetime import re from dateutil.tz import gettz class EventFacial: # { def __init__(self): self.pin = '' self.date = 0 self.card_number = 0 self.door_number = 0 self.entry_exit = 0 self.verify_mode = 0 self.event_type = 0 self.ip = 0 self.port = 0 self.user = 0 self.door = 0 self.status = 0 self.door_status = 0 self.card_type = 0 self.password = 0 self.error_code = 0 def exibir(self): print('Id:%s Nome:%s Metodo:%s Terminal:%s Status:%s, Erro:%s' % (self.pin, self.user, self.verify_mode, self.ip, self.status, self.error_code)) # } def moniorar(ip): url = "http://"+ip+"/cgi-bin/snapManager.cgi?action=attachFileProc&Flags[0]=Event&Events=[AccessControl]" # noqa username = 'admin' password = 'admin123' print('Iniciando rotina') running = True concluido = False while running: # { try: print('Chamando GET') rval = requests.get(url, auth=requests.auth.HTTPDigestAuth(username, password), stream=True, timeout=60) # noqa except Exception: continue event = {} i = 0 for chunk in rval.iter_lines(): if concluido: print('Concluido') if 'Content-Length: '.encode() in chunk: event = {} event['user_id'] = 0 event['command'] = 0 event['login_id'] = 0 event['ipaddr'] = 0 event['port'] = 0 event['user'] = 0 event['door'] = 0 event['time'] = 0 event['access_type'] = 0 event['status'] = 0 event['door_status'] = 0 event['card_type'] = 0 event['door_open_method'] = 0 event['card_number'] = 0 event['door_number'] = 1 event['password'] = '' event['error_code'] = 0 event['channel_id'] = 0 event['reader_id'] = '' event['packet_len'] = 0 event['packet_num'] = 0 event['fingerprint_info'] = 0 event['collect_result'] = 0 elif '='.encode() in chunk and 'Events['.encode() in chunk: i = i+1 try: rval = re.search(r'=', chunk.decode()) except UnicodeDecodeError: rval = None if rval: key = chunk[:rval.span()[0]].decode() value = chunk[rval.span()[1]:].decode() if 'UserType' in key: i = 34 if 'UserID' in key: event['user_id'] = int(value, 16) if value not in ['', 'admin'] else 0 elif 'CardName' in key: event['card_name'] = value elif 'CardNo' in key: event['card_number'] = int(value, 16) if value != '' else 0 elif 'CardType' in key: value = int(value) if value != '' else -1 event['card_type'] = value elif 'CreateTime' in key: event['time'] = int(value) if value != '' else 0 elif 'Door' in key: event['door'] = int(value) if value != '' else 0 elif 'ReaderID' in key: event['reader_id'] = int(value) if value != '' else 0 elif 'ErrorCode' in key: value = int(value) if value != '' else -1 event['error_code'] = ErrorCodes(int(value)) elif 'Method' in key: value = int(value) if value != '' else -1 event['door_open_method'] = 'Face Detect' if value == 15 else 'Password' elif 'Status' in key: value = int(value) if value != '' else 0 event['status'] = value elif 'Type' in key: if value == "Entry": event['access_type'] = 'ENTRY' elif value == "Exit": event['access_type'] = 'EXIT' else: event['access_type'] = 'UNKNOWN' elif 'UserType' in key: event['user'] = int(value) if value != '' else 0 elif 'EventBaseInfo.Code' in key: if value == 'AccessControl': event['command'] = 'DH_ALARM_ACCESS_CTL_EVENT' else: event['command'] = 'DH_UNKNOWN_EVENT' elif 'EventBaseInfo.Index' in key: event['door_number'] = int(value) if value != '' else 0 else: pass if i == 34: i = 0 concluido = False Facial_event = EventFacial() Facial_event.pin = event['user_id'] Facial_event.date = datetime.datetime.fromtimestamp(event['time'], gettz('Asia/Hong_Kong')) Facial_event.card_number = event['card_number'] Facial_event.door_number = event['door_number'] + 1 Facial_event.entry_exit = event['access_type'] Facial_event.verify_mode = event['door_open_method'] Facial_event.event_type = event['command'] Facial_event.ip = ip Facial_event.port = 80 Facial_event.user = event['card_name'] Facial_event.door = event['door'] Facial_event.status = event['status'] Facial_event.door_status = event['door_status'] Facial_event.card_type = event['card_type'] Facial_event.password = event['password'] Facial_event.error_code = event['error_code'] # append the event to the list Facial_event.exibir() # } # } def ErrorCodes(i): swit = { 0: 'Acesso Permitido', 16: 'Acesso Negado', 32: 'Periodo invalido' } return swit.get(i, "Erro desconhecido") moniorar('192.168.1.201')
  2. Joao

    Import Requests

    Hi, I´m trying to import and use "resquests" from python on delphi. Already did a bit of the path, but can´t use get in the righ way; on Python i´m using : import requests def test(): response = requests.get('http://192.168.1.200/gci-bin/users', auth=requests.HTTPDigestAuth('admin','admin'), stream=True, timeout=60) print(response.text) on Delphi i´m trying to import requests and use it on my code: var request, response: TVariant; begin request := import('requests'); response := request.get('URL STRING'); //<<-- here i need to insert the args used on python call such as auth=request.auth.HTTPDigestAuth('user','pass'), stream=True, timeout=60 showmessage(response.text); end; how can i use the functions and methods with args on delphi ?
×