87 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import json
 | |
| import printer
 | |
| 
 | |
| config_filename = "server_config.json"
 | |
| 
 | |
| 
 | |
| class PrinterConfig:
 | |
|     editables = {"address", "port"}
 | |
|     required_params = {"address"}
 | |
|     _address = None
 | |
|     _port = 9100
 | |
|     _profile = "RP-F10-80mm"
 | |
|     _printer = None
 | |
|     _dirty = True
 | |
| 
 | |
|     def load(self):
 | |
|         """Load printer configuration from config.json file."""
 | |
|         with open(config_filename, "r", encoding="utf-8") as ifd:
 | |
|             loaded_config = json.load(ifd)
 | |
|             for param_name in self.editables:
 | |
|                 if param_name in loaded_config:
 | |
|                     setattr(self, param_name, loaded_config[param_name])
 | |
| 
 | |
|     def save(self):
 | |
|         """Save current printer configuration to config.json file."""
 | |
|         with open(config_filename, "w", encoding="utf-8") as ofd:
 | |
|             json.dump(
 | |
|                 {"address": self.address, "port": self.port, "profile": self.profile},
 | |
|                 ofd,
 | |
|                 ensure_ascii=False,
 | |
|             )
 | |
| 
 | |
|     @property
 | |
|     def complete(self):
 | |
|         """Check if all required parameters are set.
 | |
| 
 | |
|         Returns:
 | |
|             bool: True if all required parameters are set, False otherwise.
 | |
|         """
 | |
|         for param in self.required_params:
 | |
|             if getattr(self, param) is None:
 | |
|                 return False
 | |
|         return True
 | |
| 
 | |
|     @property
 | |
|     def port(self):
 | |
|         return self._port
 | |
| 
 | |
|     @port.setter
 | |
|     def port(self, new_value: int):
 | |
|         if not isinstance(new_value, int) or 0 > new_value < 65535:
 | |
|             new_value = 9100
 | |
|         self._port = new_value
 | |
| 
 | |
|     @property
 | |
|     def address(self):
 | |
|         return self._address
 | |
| 
 | |
|     @address.setter
 | |
|     def address(self, new_value: str):
 | |
|         if not isinstance(new_value, str):
 | |
|             new_value = ""
 | |
|         self._address = new_value
 | |
| 
 | |
|     @property
 | |
|     def profile(self):
 | |
|         return self._profile
 | |
| 
 | |
|     @profile.setter
 | |
|     def profile(self, new_value: str):
 | |
|         raise AttributeError("Printer profile is a fixed value. Check config.py.")
 | |
| 
 | |
|     @property
 | |
|     def printer(self):
 | |
|         if self._printer is not None:
 | |
|             self._printer.printer.close()
 | |
| 
 | |
|         if self._dirty or self.printer is None:
 | |
|             self._printer = printer.Printer(
 | |
|                 address=self.address,
 | |
|                 port=self.port,
 | |
|                 profile=self.profile,
 | |
|             )
 | |
|             self._dirty = False
 | |
| 
 | |
|         return self._printer
 |