Merge pull request #3 from m-c-frank/main
Replaced Magic Numbers with an enum
This commit is contained in:
+30
-14
@@ -16,6 +16,21 @@ class InfoEnum(enum.IntEnum):
|
|||||||
DEVICESERIAL = 11
|
DEVICESERIAL = 11
|
||||||
HARDVERSION = 12
|
HARDVERSION = 12
|
||||||
|
|
||||||
|
class RequestCodeEnum(enum.IntEnum):
|
||||||
|
GET_INFO = 64
|
||||||
|
GET_RFID = 26
|
||||||
|
HEARTBEAT = 220
|
||||||
|
SET_LABEL_TYPE = 35
|
||||||
|
SET_LABEL_DENSITY = 33
|
||||||
|
START_PRINT = 1
|
||||||
|
END_PRINT = 243
|
||||||
|
START_PAGE_PRINT = 3
|
||||||
|
END_PAGE_PRINT = 227
|
||||||
|
ALLOW_PRINT_CLEAR = 32
|
||||||
|
SET_DIMENSION = 19
|
||||||
|
SET_QUANTITY = 21
|
||||||
|
GET_PRINT_STATUS = 163
|
||||||
|
|
||||||
_packet_to_int = lambda x: int.from_bytes(x.data, 'big')
|
_packet_to_int = lambda x: int.from_bytes(x.data, 'big')
|
||||||
|
|
||||||
# TODO REMOVE MAGIC NUMBER
|
# TODO REMOVE MAGIC NUMBER
|
||||||
@@ -41,7 +56,8 @@ class PrinterClient:
|
|||||||
# print('send:',packet)
|
# print('send:',packet)
|
||||||
self._sock.send(packet.to_bytes())
|
self._sock.send(packet.to_bytes())
|
||||||
|
|
||||||
def _transceive(self, reqcode, data, respcode):
|
def _transceive(self, reqcode, data, respoffset=1):
|
||||||
|
respcode = respoffset + reqcode
|
||||||
self._send(niimbotpacket.NiimbotPacket(reqcode, data))
|
self._send(niimbotpacket.NiimbotPacket(reqcode, data))
|
||||||
resp = None
|
resp = None
|
||||||
for _ in range(6):
|
for _ in range(6):
|
||||||
@@ -58,7 +74,7 @@ class PrinterClient:
|
|||||||
return resp
|
return resp
|
||||||
|
|
||||||
def get_info(self, key):
|
def get_info(self, key):
|
||||||
if packet := self._transceive(64, bytes((key,)), 64+key):
|
if packet := self._transceive(RequestCodeEnum.GET_INFO, bytes((key,)), key):
|
||||||
match key:
|
match key:
|
||||||
case InfoEnum.DEVICESERIAL:
|
case InfoEnum.DEVICESERIAL:
|
||||||
return packet.data.hex()
|
return packet.data.hex()
|
||||||
@@ -72,7 +88,7 @@ class PrinterClient:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def get_rfid(self):
|
def get_rfid(self):
|
||||||
packet = self._transceive(26, b'\x01', 27)
|
packet = self._transceive(RequestCodeEnum.GET_RFID, b'\x01')
|
||||||
data = packet.data
|
data = packet.data
|
||||||
|
|
||||||
if data[0] == 0:
|
if data[0] == 0:
|
||||||
@@ -101,7 +117,7 @@ class PrinterClient:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def heartbeat(self):
|
def heartbeat(self):
|
||||||
packet = self._transceive(220, b'\x01', 221)
|
packet = self._transceive(RequestCodeEnum.HEARTBEAT, b'\x01')
|
||||||
closingstate = None
|
closingstate = None
|
||||||
powerlevel = None
|
powerlevel = None
|
||||||
paperstate = None
|
paperstate = None
|
||||||
@@ -137,43 +153,43 @@ class PrinterClient:
|
|||||||
|
|
||||||
def set_label_type(self, n):
|
def set_label_type(self, n):
|
||||||
assert 1 <= n <= 3
|
assert 1 <= n <= 3
|
||||||
packet = self._transceive(35, bytes((n,)), 51)
|
packet = self._transceive(RequestCodeEnum.SET_LABEL_TYPE, bytes((n,)), 16)
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def set_label_density(self, n):
|
def set_label_density(self, n):
|
||||||
assert 1 <= n <= 3
|
assert 1 <= n <= 3
|
||||||
packet = self._transceive(33, bytes((n,)), 49)
|
packet = self._transceive(RequestCodeEnum.SET_LABEL_DENSITY, bytes((n,)), 16)
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def start_print(self):
|
def start_print(self):
|
||||||
packet = self._transceive(1, b'\x01', 2)
|
packet = self._transceive(RequestCodeEnum.START_PRINT, b'\x01')
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def end_print(self):
|
def end_print(self):
|
||||||
packet = self._transceive(243, b'\x01', 244)
|
packet = self._transceive(RequestCodeEnum.END_PRINT, b'\x01')
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def start_page_print(self):
|
def start_page_print(self):
|
||||||
packet = self._transceive(3, b'\x01', 4)
|
packet = self._transceive(RequestCodeEnum.START_PAGE_PRINT, b'\x01')
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def end_page_print(self):
|
def end_page_print(self):
|
||||||
packet = self._transceive(227, b'\x01', 228)
|
packet = self._transceive(RequestCodeEnum.END_PAGE_PRINT, b'\x01')
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def allow_print_clear(self):
|
def allow_print_clear(self):
|
||||||
packet = self._transceive(32, b'\x01', 48)
|
packet = self._transceive(RequestCodeEnum.ALLOW_PRINT_CLEAR, b'\x01', 16)
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def set_dimension(self, w, h):
|
def set_dimension(self, w, h):
|
||||||
packet = self._transceive(19, struct.pack('>HH', w, h), 20)
|
packet = self._transceive(RequestCodeEnum.SET_DIMENSION, struct.pack('>HH', w, h))
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def set_quantity(self, n):
|
def set_quantity(self, n):
|
||||||
packet = self._transceive(21, struct.pack('>H', n), 22)
|
packet = self._transceive(RequestCodeEnum.SET_QUANTITY, struct.pack('>H', n))
|
||||||
return bool(packet.data[0])
|
return bool(packet.data[0])
|
||||||
|
|
||||||
def get_print_status(self):
|
def get_print_status(self):
|
||||||
packet = self._transceive(163, b'\x01', 179)
|
packet = self._transceive(RequestCodeEnum.GET_PRINT_STATUS, b'\x01', 16)
|
||||||
page, progress1, progress2 = struct.unpack('>HBB', packet.data)
|
page, progress1, progress2 = struct.unpack('>HBB', packet.data)
|
||||||
return {'page': page, 'progress1': progress1, 'progress2': progress2}
|
return {'page': page, 'progress1': progress1, 'progress2': progress2}
|
||||||
|
|||||||
Reference in New Issue
Block a user