-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add adapters * enable test on push
- Loading branch information
Showing
12 changed files
with
606 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,19 @@ | |
<!-- PROJECT HEADER --> | ||
<br /> | ||
<p align="center"> | ||
<h3 align="center">ThermalMqttastic</h3> | ||
<h3 align="center">Thermaltastic</h3> | ||
|
||
<p align="center"> | ||
<img align="center" src="./images/print.jpg" alt="drawing" width="170"/> | ||
<br /> | ||
<br /> | ||
Control a Adafruit thermal printer over mqtt. This library is very WIP. | ||
Control a Adafruit thermal printer over different adapters. This library is very WIP. | ||
<br /> | ||
<br /> | ||
· | ||
<a href="https://github.com/beuluis/ThermalMqttastic/issues">Report Bug</a> | ||
<a href="https://github.com/beuluis/Thermaltastic/issues">Report Bug</a> | ||
· | ||
<a href="https://github.com/beuluis/ThermalMqttastic/issues">Request Feature</a> | ||
<a href="https://github.com/beuluis/Thermaltastic/issues">Request Feature</a> | ||
· | ||
</p> | ||
</p> | ||
|
@@ -31,45 +31,77 @@ I wanted to talk to a thermal printer over an api. I experimented with a esp32 a | |
|
||
After investigating the [Adafruit library](https://github.com/adafruit/Adafruit-Thermal-Printer-Library/tree/master) and many many failed other attempts, I concluded that I can extract the heavy lifting to TypeScript and only run a light mqtt to serial implementation on the esp32. | ||
|
||
To allow different 'streams' like mqtt I came up with the adapter concept. | ||
|
||
So now you can utilize the versatile package landscape of NPM to generate bitmaps, wrap it in REST APIs and and and. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i @beuluis/thermal-mqttastic | ||
npm i @beuluis/thermaltastic | ||
``` | ||
|
||
### Unstable installation | ||
|
||
The `next` dist-tag is kept in sync with the latest commit on main. So this contains always the latest changes but is highly unstable. | ||
|
||
```bash | ||
npm i @beuluis/thermal-mqttastic@next | ||
npm i @beuluis/thermaltastic@next | ||
``` | ||
|
||
## Usage | ||
|
||
```typescript | ||
const printer = new Thermaltastic(adapter); | ||
|
||
await printer.begin(); | ||
|
||
await printer.println('Hello World!'); | ||
``` | ||
|
||
## Adapters | ||
|
||
The original library used a serial stream to send the bytes to the printer. In this implementation we use adapters to achieve this. | ||
|
||
A adapter defines how the printer receives the bytes. | ||
|
||
### MqttasticAdapter | ||
|
||
Send the to print bytes over mqtt. | ||
|
||
> :warning: **You need the corresponding arduino MQTT client also listening**: See [ThermalMqttasticPrinter](https://registry.platformio.org/libraries/beuluis/ThermalMqttasticPrinter) for more details. | ||
You also need a MQTT broker. An example would be [eclipse-mosquitto](https://hub.docker.com/_/eclipse-mosquitto). | ||
|
||
```typescript | ||
const printer = new ThermalMqttastic({ | ||
const adapter = new MqttasticAdapter({ | ||
mqttUrl: 'mqtt://localhost:1883', | ||
mqttOptions: { | ||
password: '12345678', | ||
}, | ||
}); | ||
|
||
await printer.begin(); | ||
|
||
await printer.println('Hello World!'); | ||
new Thermaltastic(adapter); | ||
``` | ||
|
||
### MQTT connection | ||
#### MQTT connection | ||
|
||
`mqttOptions` is the option interface of the [MQTT](https://www.npmjs.com/package/mqtt) package. Please refer to this documentation on how to establish the connection. | ||
|
||
### Implement your own adapter | ||
|
||
For your own adapter you just need to implement the `Adapter` interface. | ||
|
||
```typescript | ||
export class MyAdapter implements Adapter { | ||
public async begin() {} | ||
|
||
public async write(...bytes: [number, number?, number?, number?]) {} | ||
|
||
public async writeBytes(...bytes: [number, number?, number?, number?]) {} | ||
} | ||
``` | ||
|
||
## Functions | ||
|
||
Parameters get validated using [zod](https://www.npmjs.com/package/zod). Please refer to this documentation on how the parameters get validated. | ||
|
@@ -566,11 +598,7 @@ await printer.setCharSpacing(10); | |
You can enable the debugging logger when you provide a logger to the constructor. | ||
|
||
```typescript | ||
const printer = new ThermalMqttastic({ | ||
mqttUrl: 'mqtt://localhost:1883', | ||
mqttOptions: { | ||
password: '12345678', | ||
}, | ||
const printer = new Thermaltastic(adapter, { | ||
logger: console, | ||
}); | ||
``` | ||
|
@@ -596,12 +624,12 @@ Luis Beu - [email protected] | |
<!-- MARKDOWN LINKS & IMAGES --> | ||
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --> | ||
|
||
[contributors-shield]: https://img.shields.io/github/contributors/beuluis/ThermalMqttastic.svg?style=flat-square | ||
[contributors-url]: https://github.com/beuluis/ThermalMqttastic/graphs/contributors | ||
[forks-shield]: https://img.shields.io/github/forks/beuluis/ThermalMqttastic.svg?style=flat-square | ||
[forks-url]: https://github.com/beuluis/ThermalMqttastic/network/members | ||
[stars-shield]: https://img.shields.io/github/stars/beuluis/ThermalMqttastic.svg?style=flat-square | ||
[stars-url]: https://github.com/beuluis/ThermalMqttastic/stargazers | ||
[issues-shield]: https://img.shields.io/github/issues/beuluis/ThermalMqttastic.svg?style=flat-square | ||
[issues-url]: https://github.com/beuluis/ThermalMqttastic/issues | ||
[license-shield]: https://img.shields.io/github/license/beuluis/ThermalMqttastic.svg?style=flat-square | ||
[contributors-shield]: https://img.shields.io/github/contributors/beuluis/Thermaltastic.svg?style=flat-square | ||
[contributors-url]: https://github.com/beuluis/Thermaltastic/graphs/contributors | ||
[forks-shield]: https://img.shields.io/github/forks/beuluis/Thermaltastic.svg?style=flat-square | ||
[forks-url]: https://github.com/beuluis/Thermaltastic/network/members | ||
[stars-shield]: https://img.shields.io/github/stars/beuluis/Thermaltastic.svg?style=flat-square | ||
[stars-url]: https://github.com/beuluis/Thermaltastic/stargazers | ||
[issues-shield]: https://img.shields.io/github/issues/beuluis/Thermaltastic.svg?style=flat-square | ||
[issues-url]: https://github.com/beuluis/Thermaltastic/issues | ||
[license-shield]: https://img.shields.io/github/license/beuluis/Thermaltastic.svg?style=flat-square |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@beuluis/thermal-mqttastic", | ||
"version": "0.0.3", | ||
"description": "Wrapper for my adafruit thermal printer api", | ||
"name": "@beuluis/thermaltastic", | ||
"version": "0.1.0", | ||
"description": "Control a Adafruit thermal printer over different adapters", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
|
@@ -21,7 +21,7 @@ | |
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/beuluis/ThermalMqttastic.git" | ||
"url": "git+https://github.com/beuluis/Thermaltastic.git" | ||
}, | ||
"keywords": [ | ||
"thermal", | ||
|
@@ -32,9 +32,9 @@ | |
"author": "Luis Beu <[email protected]> (https://luisbeu.de/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/beuluis/ThermalMqttastic/issues" | ||
"url": "https://github.com/beuluis/Thermaltastic/issues" | ||
}, | ||
"homepage": "https://github.com/beuluis/ThermalMqttastic#readme", | ||
"homepage": "https://github.com/beuluis/Thermaltastic#readme", | ||
"prettier": "@beuluis/prettier-config", | ||
"lint-staged": { | ||
"*.md": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable canonical/filename-match-regex */ | ||
import type { AsyncMqttClient, IClientOptions } from 'async-mqtt'; | ||
import { connectAsync } from 'async-mqtt'; | ||
import type { Adapter } from '../Thermaltastic'; | ||
|
||
export interface MqttasticAdapterInitOptions { | ||
mqttOptions?: IClientOptions; | ||
mqttUrl: string; | ||
} | ||
|
||
export class MqttasticAdapter implements Adapter { | ||
public async begin() { | ||
this.mqttClient = await connectAsync(this.mqttUrl, this.mqttOptions); | ||
} | ||
|
||
public constructor(initOptions: MqttasticAdapterInitOptions) { | ||
this.mqttUrl = initOptions.mqttUrl; | ||
this.mqttOptions = initOptions.mqttOptions; | ||
} | ||
|
||
protected mqttClient?: AsyncMqttClient; | ||
|
||
protected mqttOptions?: IClientOptions; | ||
|
||
protected mqttUrl: string; | ||
|
||
protected async publish(topic: string, payload: string) { | ||
if (!this.mqttClient) { | ||
throw new Error('MQTT client is not initialized. Did you call begin()?'); | ||
} | ||
|
||
await this.mqttClient.publish(topic, payload, { qos: 2 }); | ||
} | ||
|
||
public async write(...bytes: [number, number?, number?, number?]) { | ||
const payloadString = bytes.join(','); | ||
|
||
await this.publish('write', payloadString); | ||
} | ||
|
||
public async writeBytes(...bytes: [number, number?, number?, number?]) { | ||
const payloadString = bytes.join(','); | ||
|
||
await this.publish('writeBytes', payloadString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './MqttasticAdapter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './ThermalMqttastic'; | ||
export * from './adapters'; | ||
export * from './Thermaltastic'; | ||
export * from './enums'; |
Oops, something went wrong.
72b4173
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
124 tests passing in 4 suites.
Report generated by 🧪jest coverage report action from 72b4173