TLKCoreService

A entry to access all TMY supported devices

class tlkcore.TLKCoreService.TLKCoreService(web_callback=None, web_client_id=None, working_root=None)

The entry to init TLKCoreService instance then start to set some init values, developers just maintain ONLY ONE instance for TLKCoreService

Parameters:
  • web_callback (function, optional) – callback function for WEB-TLK. Defaults to None.

  • web_client_id (str, optional) – client id for WEB-TLK. Defaults to None.

  • working_root (str, optional) – the working root directory path. If None, uses current working directory. Defaults to None.

Note

By default, TLKCoreService will create ‘files’ and ‘tlk_core_log’ directories in the working root directory. The system will automatically apply logging configuration using TMYLogging.

Examples

  • Use current working directory (default behavior)

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    
  • Use a custom working root directory

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService(working_root="/path/to/custom/directory")
    
  • Initialize with web callback and client ID for WEB-TLK

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService(web_callback=my_callback, web_client_id="user123")
    
  • If TLKCore libraries under lib/

    >>> import sys
    >>> from pathlib import Path
    >>> sys.path.insert(0, str(Path("lib").absolute()))
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService(working_root=".")
    
  • If TLKCore libraries under Python site-packages/

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService(working_root=".")
    

Note

We will skip TLKCoreService generation in the following examples, and examples always use service as variable name.

Changed in version v2.3.0: Add working_root parameter to customize working directory path, removed log_path from the logging configuration.

queryTLKCoreVer()

Provide the version of TLKCore

Returns:

RetData
  • str: The version of TLKCore

Return type:

RetType

Examples

  • Query the version of TLKCore

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> version = service.queryTLKCoreVer().RetData
    >>> print(f"TLKCore Version: v{version}")
    TLKCore Version: v2.2.0
    
setRootDirectory(base_dir=None, files_dir_name='files', log_dir_name='tlk_core_log')

Set or change the working directories for files and logs at runtime

Parameters:
  • base_dir (str, optional) – Base directory path. If None, uses current root directory. Defaults to None.

  • files_dir_name (str, optional) – Name of files directory. Defaults to ‘files’.

  • log_dir_name (str, optional) – Name of log directory. Defaults to ‘tlk_core_log’.

Returns:

RetData
  • dict: Contains working directory paths, format will be {root_directory, files_dir, log_dir} if successful

Return type:

RetType

Examples

  • Set directories to current working directory (default)
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> ret = service.setRootDirectory()
    >>> print(ret.RetData)
    {'root_directory': '/current/working/path', 'files_dir': '/current/working/path/files/', 'log_dir': '/current/working/path/tlk_core_log/'}
    
  • Set directories to a specific path
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> ret = service.setRootDirectory("/path/to/your/directory")
    >>> print(ret.RetData)
    {'root_directory': '/path/to/your/directory', 'files_dir': '/path/to/your/directory/files/', 'log_dir': '/path/to/your/directory/tlk_core_log/'}
    
  • Set directories with custom names
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> ret = service.setRootDirectory("/path/to/your/directory", "my_files", "my_logs")
    >>> print(ret.RetData)
    {'root_directory': '/path/to/your/directory', 'files_dir': '/path/to/your/directory/my_files/', 'log_dir': '/path/to/your/directory/my_logs/'}
    

Added in version v2.3.0.

getRootDirectory()

Get current working directories information

Returns:

RetData
  • dict: Contains current working directories, format will be {root_directory, files_dir, log_dir}

Return type:

RetType

Examples

  • Get current working directories
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> dirs = service.getRootDirectory().RetData
    >>> print(dirs)
    {'root_directory': '/current/working/path', 'files_dir': '/current/working/path/files/', 'log_dir': '/current/working/path/tlk_core_log/'}
    

Added in version v2.3.0.

scanDevices(interface=<DevInterface.LAN: 1>, callback=None, keepDevs: list = [])

Scan the active devices to obtain its information including SN, address and device type.

Parameters:
  • interface (DevInterface, optional) – To scan target devices, we need to decide which communication interfaces included, and it can be combined. Defaults to tlkcore.TMYPublic.DevInterface.LAN.

  • callback (function, optional) – callback function for WEB-TLK. Defaults to None.

  • keepDevs (list, optional) – A whitelist of SN to ignore de-initing while scanning. Defaults to [].

Returns:

RetData
  • list: elements with str type, and its format: [’SN,address,dev_type’, …] or [’SN,address,dev_type,DFU mode’, …]

Return type:

RetType

Examples

  • Scan for devices and retrieve scanned information
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> ret = service.scanDevices()
    >>> scan_list = ret.RetData
    >>> print(scan_list)
    ['D2230E013-28,192.168.100.111,9']
    
  • Sometimes device in DFU mode now, and it can not handle any requests except for DFU process.
    >>> print(scan_list)
    ['D2230E013-28,192.168.100.111,9,DFU']
    

Note

Must call initDev() after this function

Changed in version v2.3.0: Add DFU in reponse if device in DFU mode now

getScanInfo(sn=None, filter=<ScanFilter.ALL: 3>)

Fetch integrated scan info after called scanDevices()

Parameters:
  • sn (str, optional) – Given specific SN to get info of SN. Defaults to None.

  • filter (ScanFilter, optional) – Filter for scan results. Defaults to ScanFilter.ALL.

Returns:

RetData
  • dict: all scanned results, format will be {SN: (address, dev_type, in_dfu)}

Return type:

RetType

Examples

  • Retrieve scanned information after scanning
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> service.scanDevices()
    >>> scan_info = service.getScanInfo().RetData
    >>> print(scan_info)
    {'D2230E013-28': ('192.168.100.111', 9, False)}
    
  • Retrieve scanned information after scanning with specific SN
    >>> sn = "D2230E013-28"
    
    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> service.scanDevices()
    >>> scan_info = service.getScanInfo(sn).RetData
    >>> print(scan_info)
    {'D2230E013-28': ('192.168.100.111', 9, False)}
    

Note

Must call initDev() after this function

Added in version v1.2.0.

Changed in version v2.3.0: Add filter parameter, and add in_dfu status in scan result

initDev(sn: str, address='', dev_type=-1)

Init device with SN, or directly init by specific Address & dev_type without scanning. This initiation represents internal device object instance creation by this function.

Parameters:
  • sn (str) – serial number.

  • address (str, optional) – network address for Ethernet, Com port or USB interface. Defaults to “”.

  • dev_type (int, optional) – TMYTEK’s device type, comes from scan result. Defaults to -1.

Returns:

An object containing the result of the initialization.
  • OK: If the operation is successful.

  • ERROR: If there is a general error during the operation.

Return type:

RetType

Examples

  • Init device with given SN after calling scanning

    >>> from tlkcore import TLKCoreService
    >>> service = TLKCoreService()
    >>> service.scanDevices()
    
    >>> sn = "D2230E013-28"
    >>> ret = service.initDev(sn)
    >>> print(ret)
    [RetType] OK
    
  • Init device with specific SN, address, dev_type (without scanning), and these parameters obtained from scan result.

    >>> ret = service.initDev("D2230E013-28", "192.168.100.111", 9)
    >>> print(ret)
    [RetType] OK
    
  • Example code for init device with specific SN, address, dev_type
    Example code if takes lib/ to import
    import sys
    from pathlib import Path
    sys.path.insert(0, str(Path("lib").absolute()))
    from tlkcore import TLKCoreService
    
    service = TLKCoreService()
    service.initDev("D2230E013-28", "192.168.100.111", 9)
    

Changed in version v1.2.0: support address, dev_type parameter

DeInitDev(sn)

De-init device to free related resources.

Parameters:

sn (str) – The SN of target device

Return type:

RetType