Skip to content

Commit

Permalink
Refactor printer packet
Browse files Browse the repository at this point in the history
  • Loading branch information
dtgreene committed Jun 18, 2024
1 parent e9b0fd5 commit 33cc3c2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
98 changes: 54 additions & 44 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ const PACKET_READ_COUNT = 10;

export const InfoCode = {
DENSITY: 1,
PRINT_SPEED: 2,
LABEL_TYPE: 3,
LANGUAGE_TYPE: 6,
AUTO_SHUTDOWN_TIME: 7,
DEVICE_TYPE: 8,
SOFTWARE_VERSION: 9,
Expand All @@ -23,19 +21,19 @@ export const InfoCode = {
};

export const RequestCode = {
GET_INFO: 64,
GET_RFID: 26,
GET_HEART_BEAT: 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,
GET_PRINT_STATUS: 163,
GET_RFID: 26,
SET_LABEL_DENSITY: 33,
SET_LABEL_TYPE: 35,
GET_INFO: 64,
IMAGE_DATA_META: 132,
IMAGE_DATA: 133,
GET_PRINT_STATUS: 163,
GET_HEART_BEAT: 220,
END_PAGE_PRINT: 227,
END_PRINT: 243,
};

export class PrinterClient {
Expand All @@ -47,13 +45,11 @@ export class PrinterClient {
close = () => {
this.transport.close();
};
_sendPacket = async (type, data, options = {}) => {
const { responseOffset = 1, skipResponse = false } = options;
_sendPacket = async (packet, options = {}) => {
const responseCode = options.responseCode ?? packet.type + 1;
const skipResponse = options.skipResponse ?? false;

debugLog('Writing packet!', type, data);

const packet = new PrinterPacket(type, data);
const responseCode = responseOffset + type;
debugLog('Writing packet!', packet.type, packet.data);

this.transport.write(packet.toBytes());
const response = await this._receivePacket(responseCode);
Expand Down Expand Up @@ -102,7 +98,7 @@ export class PrinterClient {

if (!chunk) return packets;

debugLog('Received chunk!', chunk);
debugLog('Received data!', chunk);

if (this.packetBuffer) {
// Add the new data to the buffer
Expand Down Expand Up @@ -160,9 +156,8 @@ export class PrinterClient {
};
getPrintStatus = async () => {
const { data } = await this._sendPacket(
RequestCode.GET_PRINT_STATUS,
[0x01],
{ responseOffset: 16 }
new PrinterPacket(RequestCode.GET_PRINT_STATUS, [0x01]),
{ responseCode: RequestCode.GET_PRINT_STATUS + 16 }
);
// >HBB
const page = data.readUInt16BE(0);
Expand All @@ -172,9 +167,11 @@ export class PrinterClient {
return { page, progress1, progress2 };
};
getInfo = async (key) => {
const { data } = await this._sendPacket(RequestCode.GET_INFO, [key], {
responseOffset: key,
});
const responseCode = RequestCode.GET_INFO + key;
const { data } = await this._sendPacket(
new PrinterPacket(RequestCode.GET_INFO, [key]),
{ responseCode }
);

switch (key) {
case InfoCode.DEVICE_SERIAL: {
Expand All @@ -196,13 +193,19 @@ export class PrinterClient {
}
};
getHeartbeat = async () => {
const { data } = await this._sendPacket(RequestCode.GET_HEART_BEAT, [0x01]);
const responseCode = 217;
const { data } = await this._sendPacket(
new PrinterPacket(RequestCode.GET_HEART_BEAT, [0x04]),
{ responseCode }
);

let closingState = null;
let powerLevel = null;
let paperState = null;
let rfidReadState = null;

// const doorOpen = Boolean(data[4]);

switch (data.length) {
case 20: {
paperState = data[18];
Expand Down Expand Up @@ -243,7 +246,9 @@ export class PrinterClient {
};
};
getRFID = async () => {
const { data } = await this._sendPacket(RequestCode.GET_RFID, [0x01]);
const { data } = await this._sendPacket(
new PrinterPacket(RequestCode.GET_RFID, [0x01])
);

if (data[0] == 0) return null;

Expand Down Expand Up @@ -278,9 +283,10 @@ export class PrinterClient {
};
setLabelType = async (type) => {
assert(type >= 1 && type <= 3);
const result = await this._sendPacket(RequestCode.SET_LABEL_TYPE, [type], {
responseOffset: 16,
});
const result = await this._sendPacket(
new PrinterPacket(RequestCode.SET_LABEL_TYPE, [type]),
{ responseCode: RequestCode.SET_LABEL_TYPE + 16 }
);

return Boolean(result.data[0]);
};
Expand All @@ -290,38 +296,39 @@ export class PrinterClient {
`Invalid density range; expected 1 - 5 but got ${density}`
);
const result = await this._sendPacket(
RequestCode.SET_LABEL_DENSITY,
[density],
{ responseOffset: 16 }
new PrinterPacket(RequestCode.SET_LABEL_DENSITY, [density]),
{ responseCode: RequestCode.SET_LABEL_DENSITY + 16 }
);

return Boolean(result.data[0]);
};
startPrint = async () => {
const result = await this._sendPacket(RequestCode.START_PRINT, [0x01]);
const result = await this._sendPacket(
new PrinterPacket(
RequestCode.START_PRINT,
[0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00]
)
);

return Boolean(result.data[0]);
};
endPrint = async () => {
const result = await this._sendPacket(RequestCode.END_PRINT, [0x01]);
const result = await this._sendPacket(
new PrinterPacket(RequestCode.END_PRINT, [0x01])
);

return Boolean(result.data[0]);
};
startPagePrint = async () => {
const result = await this._sendPacket(RequestCode.START_PAGE_PRINT, [0x01]);
const result = await this._sendPacket(
new PrinterPacket(RequestCode.START_PAGE_PRINT, [0x01])
);

return Boolean(result.data[0]);
};
endPagePrint = async () => {
const result = await this._sendPacket(RequestCode.END_PAGE_PRINT, [0x01]);

return Boolean(result.data[0]);
};
allowPrintClear = async () => {
const result = await this._sendPacket(
RequestCode.ALLOW_PRINT_CLEAR,
[0x01],
{ responseOffset: 16 }
new PrinterPacket(RequestCode.END_PAGE_PRINT, [0x01])
);

return Boolean(result.data[0]);
Expand All @@ -331,7 +338,10 @@ export class PrinterClient {
const data = Buffer.alloc(4);
data.writeUInt16BE(height, 0);
data.writeUInt16BE(width, 2);
const result = await this._sendPacket(RequestCode.SET_DIMENSION, data);
// Theres a third UInt16 that's [0x00, 0x02]
const result = await this._sendPacket(
new PrinterPacket(RequestCode.SET_DIMENSION, data)
);

return Boolean(result.data[0]);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "niimbot-cli",
"name": "niimbotjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down

0 comments on commit 33cc3c2

Please sign in to comment.