pyzkaccess icon indicating copy to clipboard operation
pyzkaccess copied to clipboard

ctypes.ArgumentError at set_device_data

Open DrGhonzo opened this issue 2 years ago • 2 comments

Hi there, hope you're well and safe. Working with a C3-400 I'm getting the following traceback:

Traceback (most recent call last):
  File "C:\Users\Dr.Ghonzo\AppData\Local\Programs\Python\Python37\lib\site-packages\pyzkaccess\device_data\model.py", line 262, in save
    gen.send(None)
  File "C:\Users\Dr.Ghonzo\AppData\Local\Programs\Python\Python37\lib\site-packages\pyzkaccess\sdk.py", line 361, in set_device_data
    err = self.dll.SetDeviceData(self.handle, query_table, query_records, "")
ctypes.ArgumentError: argument 4: <class 'TypeError'>: wrong type

How can we follow? Thank you for your time. KR

DrGhonzo avatar Jun 02 '22 03:06 DrGhonzo

Hello @DrGhonzo . Could you please provide a code snippet which causes this exception? Or you're using cli? And which table you're trying to save. Thanks

bdragon300 avatar Jun 09 '22 20:06 bdragon300

Hi Igor, I'm glad to have news from you. Hope everytyhing is going well there. Below is the full code I'm working at right now. Some tips I was thinking about were that I'm using x64 python 3.7 version.

from concurrent.futures import ThreadPoolExecutor
from datetime import date
from pyzkaccess import ZKAccess, ZKSDKError
from pyzkaccess.tables import User
import logging
import paho.mqtt.client as mqtt
import queue
import threading
import time


# Create a custom logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Create handlers
stream_handler = logging.StreamHandler()
file_handler = logging.FileHandler(str(date.today()) + '_file.log')
stream_handler.setLevel(logging.DEBUG)
file_handler.setLevel(logging.WARNING)

# Create formatters and add it to handlers
stream_format = logging.Formatter('%(levelname)s - %(asctime)s - %(name)s - %(message)s')
file_format = logging.Formatter('%(levelname)s - %(asctime)s - %(name)s - %(message)s')
stream_handler.setFormatter(stream_format)
file_handler.setFormatter(file_format)

#  Add handlers to the logger
logger.addHandler(stream_handler)
logger.addHandler(file_handler)


def update_user(c, u):
    '''
        :param c: controller
        :param u: user
    '''
    u = User(card=str(u))
    for controller in c:
        controller.table(User).upsert(u)
    logger.info("updated")


def create_user(c, u):
    '''
        :param c: controllers
        :param u: user
    '''
    logger.debug(u)
    for controller in c:
        u = User(card=str(u)).with_zk(controller)
        u.save()
        logger.info("user %s created at controller %d", u, c.index(controller))


def check_events(c, q, e):
    '''
        :param c controller
        :param q queue
        :param e event
    '''

    while not e.is_set():
        # print("no events")
        logger.debug("no events")
        for door1_event in c.events.poll(timeout=1):
            # print("an event ", door1_event)
            if door1_event.card and door1_event.card != '0':
                # print('Got card #', door1_event.card)
                q.put(door1_event)
                card = door1_event.card


def handle_events(c, q, e):
    while not e.is_set():
        if not q.empty():
            msg = q.get()
            c.publish("test", str(msg))
            # print("a message ", msg)


def wait4devices():

    try:
        # looking for devices on the network
        logger.debug("looking for devices on the network")
        found = ZKAccess.search_devices('192.168.1.255')
        if found:
            # log devices info
            logger.debug("found %d devices", len(found))
            l = []
            for device in found:
                zk = ZKAccess(device=device)
                l.append(zk)
                logger.debug("devices instance " + zk.parameters.ip_address)
                # print("devices instance " + zk.parameters.ip_address)
                # print(type(zk))
            '''do something with the found list'''
            return l

    except ZKSDKError as e:
        logger.exception(e)
        return False
        # # print(str(e))


def wait4mqtt():
    # Instantiate MQTT client
    c = mqtt.Client("api")
    logger.debug("mqtt client connect")
    try:
        c.on_connect = on_connect
        c.on_message = on_message
        c.connect('localhost', 1883, 60)
        c.loop_start()
        return c

    except ConnectionRefusedError as e:
        logger.exception(e)


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        logger.debug("attempting to subscribe to 'test'")
        client.subscribe("test")


def on_message(client, userdata, msg):
    logger.debug(msg.topic)
    logger.debug(msg.payload)


def main():
    events_queue = queue.Queue()

    event = threading.Event()

    zk = wait4devices()

    client = wait4mqtt()

    client.publish("test", "starting")

    command = input("input your command: ")

    if command == 'r':

        with ThreadPoolExecutor(max_workers=len(zk) + 1) as executor:
            for device in zk:
                executor.submit(check_events, device, events_queue, event)
            executor.submit(handle_events, client, events_queue, event)

            time.sleep(20)
            # print("exiting")
            client.publish("test", "exiting")
            event.set()
    elif command == 'c':
        card = input("input card #: ")
        create_user(zk, card)
        # update_user(zk, card)
        # print(card)


if __name__ == '__main__':

    main()

DrGhonzo avatar Jun 09 '22 23:06 DrGhonzo