From f77584307db34e9cfe5a8825ef5afacee155b328 Mon Sep 17 00:00:00 2001 From: Ircama Date: Tue, 8 Aug 2023 23:18:54 +0200 Subject: [PATCH] Refinements --- README.md | 74 ++++++++++++++++++++++++++++----------------- epson_print_conf.py | 32 ++++++++++++++------ 2 files changed, 69 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index dc4be45..3de2e64 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The software also provides a configurable printer dictionary, which can be easil ``` git clone https://github.com/Ircama/epson_print_conf +pip3 install pyyaml pip3 install pyasn1==0.4.8 pip3 install git+https://github.com/etingof/pysnmp.git cd epson_print_conf @@ -35,11 +36,12 @@ It is tested with Ubuntu / Windows Subsystem for Linux, Windows. ## Usage ``` -usage: epson_print_conf.py [-h] -m MODEL -a HOSTNAME [-i] [-q QUERY] [--reset_waste_ink] - [--detect-key] [-d] [-e DUMP_EEPROM DUMP_EEPROM] [--dry-run] - [--write-first-ti-received-time YEAR MONTH DAY] [-R READ_EEPROM] - [-W WRITE_EEPROM] [-S WS_TO_STRING] [-t TIMEOUT] [-r RETRIES] - [-c CONFIG_FILE] +usage: epson_print_conf.py [-h] -m MODEL -a HOSTNAME [-p PORT] [-i] [-q QUERY_NAME] + [--reset_waste_ink] [-d] + [--write-first-ti-received-time YEAR MONTH DAY] [--dry-run] + [-R ADDRESS_SET] [-W ADDRESS_VALUE_SET] [-e FIRST_ADDRESS LAST_ADDRESS] + [--detect-key] [-S SEQUENCE_STRING] [-t TIMEOUT] [-r RETRIES] + [-c CONFIG_FILE] [--simdata SIMDATA_FILE] optional arguments: -h, --help show this help message and exit @@ -47,32 +49,35 @@ optional arguments: Printer model. Example: -m XP-205 (use ? to print all supported models) -a HOSTNAME, --address HOSTNAME Printer host name or IP address. (Example: -m 192.168.1.87) + -p PORT, --port PORT Printer port (default is 161) -i, --info Print all available information and statistics (default option) - -q QUERY, --query QUERY + -q QUERY_NAME, --query QUERY_NAME Print specific information. (Use ? to list all available queries) --reset_waste_ink Reset all waste ink levels to 0 - --detect-key Detect the read_key via brute force -d, --debug Print debug information - -e DUMP_EEPROM DUMP_EEPROM, --eeprom-dump DUMP_EEPROM DUMP_EEPROM - Dump EEPROM (arguments: start, stop) - --dry-run Dry-run change operations --write-first-ti-received-time YEAR MONTH DAY Change the first TI received time - -R READ_EEPROM, --read-eeprom READ_EEPROM - Read the values of a list of printer EEPROM addreses. Format is: address [, - ...] - -W WRITE_EEPROM, --write-eeprom WRITE_EEPROM + --dry-run Dry-run change operations + -R ADDRESS_SET, --read-eeprom ADDRESS_SET + Read the values of a list of printer EEPROM addreses. Format is: address + [, ...] + -W ADDRESS_VALUE_SET, --write-eeprom ADDRESS_VALUE_SET Write related values to a list of printer EEPROM addresses. Format is: address: value [, ...] - -S WS_TO_STRING, --write-sequence-to-string WS_TO_STRING + -e FIRST_ADDRESS LAST_ADDRESS, --eeprom-dump FIRST_ADDRESS LAST_ADDRESS + Dump EEPROM + --detect-key Detect the read_key via brute force + -S SEQUENCE_STRING, --write-sequence-to-string SEQUENCE_STRING Convert write sequence of numbers to string. -t TIMEOUT, --timeout TIMEOUT SNMP GET timeout (floating point argument) -r RETRIES, --retries RETRIES SNMP GET retries (floating point argument) -c CONFIG_FILE, --config CONFIG_FILE - read a configuration file including the full log dump of a previous operation - with '-d' flag (instead of accessing the printer via SNMP) + read a configuration file including the full log dump of a previous + operation with '-d' flag (instead of accessing the printer via SNMP) + --simdata SIMDATA_FILE + write SNMP dictionary map to simdata file Epson Printer Configuration via SNMP (TCP/IP) ``` @@ -110,6 +115,30 @@ python3 epson_print_conf.py -m XP-205 -a 192.168.1.87 -R 173,172 ## API Interface +### Specification + +```python +EpsonPrinter(model, hostname, port, timeout, retries, dry_run) +``` + +- `model`: printer model +- `hostname`: IP address or network name of the printer +- `port`: SNMP port number (default is 161) +- `timeout`: printer connection timeout in seconds (float) +- `retries`: connection retries if error or timeout occurred +- `dry_run`: boolean (True if write dry-run mode is enabled) + +### Exceptions + +``` +TimeoutError +ValueError +``` + +(And *pysnmp* exceptions.) + +### Sample + ```python import epson_print_conf import logging @@ -117,7 +146,7 @@ import logging logging.basicConfig(level=logging.DEBUG, format="%(message)s") # if logging is needed printer = epson_print_conf.EpsonPrinter( - printer_model="XP-205", hostname="192.168.1.87") + model="XP-205", hostname="192.168.1.87") if not printer.parm: print("Unknown printer") @@ -152,15 +181,6 @@ printer.brute_force_read_key() printer.write_first_ti_received_time(2000, 1, 2) ``` -### Exceptions - -``` -TimeoutError -ValueError -``` - -(And *pysnmp* exceptions.) - ## Output example Example of advanced printer status with an XP-205 printer: diff --git a/epson_print_conf.py b/epson_print_conf.py index ea966e2..c52f7c5 100644 --- a/epson_print_conf.py +++ b/epson_print_conf.py @@ -301,7 +301,7 @@ class EpsonPrinter: eeprom_link: str = f'{SNMP_OID_ENTERPRISE}.{SNMP_EPSON}.{OID_PRV_CTRL}.1' session: object - printer_model: str + model: str hostname: str parm: dict mib_dict: dict = {} @@ -317,11 +317,13 @@ class EpsonPrinter: def __init__( self, - printer_model: str = None, + model: str = None, hostname: str = None, + port: int = 161, timeout: (None, float) = None, retries: (None, float) = None, - dry_run: bool = False) -> None: + dry_run: bool = False + ) -> None: """Initialise printer model.""" # process "alias" definintion for printer_name, printer_data in self.PRINTER_CONFIG.copy().items(): @@ -360,13 +362,14 @@ class EpsonPrinter: "in '%s' configuration.", sameas, printer_name ) - self.printer_model = printer_model + self.model = model self.hostname = hostname + self.port = port self.timeout = timeout self.retries = retries self.dry_run = dry_run - if self.printer_model in self.valid_printers: - self.parm = self.PRINTER_CONFIG[self.printer_model] + if self.model in self.valid_printers: + self.parm = self.PRINTER_CONFIG[self.model] else: self.parm = None @@ -470,7 +473,7 @@ class EpsonPrinter: else: return write_op - def snmp_mib(self, mib, label="unknown"): + def snmp_mib(self, mib: str, label: str = "unknown") -> (str, Any): """Generic SNMP query, returning value of a MIB.""" if self.mib_dict: if mib not in self.mib_dict: @@ -485,7 +488,7 @@ class EpsonPrinter: if not self.hostname: return None, False utt = UdpTransportTarget( - (self.hostname, 161), + (self.hostname, self.port), ) if self.timeout is not None: utt.timeout = self.timeout @@ -1557,6 +1560,14 @@ if __name__ == "__main__": action="store", help='Printer host name or IP address. (Example: -m 192.168.1.87)', required=True) + parser.add_argument( + '-p', + '--port', + dest='port', + type=int, + default=161, + action="store", + help='Printer port (default is 161)') parser.add_argument( '-i', '--info', @@ -1701,8 +1712,9 @@ if __name__ == "__main__": logging.getLogger().setLevel(logging.DEBUG) printer = EpsonPrinter( - args.model, - args.hostname, + model=args.model, + hostname=args.hostname, + port=args.port, timeout=args.timeout, retries=args.retries, dry_run=args.dry_run)