Compare commits
3 Commits
aa41351185
...
26b87ed628
Author | SHA1 | Date | |
---|---|---|---|
|
26b87ed628 | ||
|
37a7074aea | ||
|
f15d4085c6 |
7
data/printers.json
Normal file
7
data/printers.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
[
|
||||
{ "name": "p04", "ip": "10.7.0.134", "model": "m3655idn" },
|
||||
{ "name": "p05", "ip": "10.7.0.135", "model": "m2040dn" },
|
||||
{ "name": "p06", "ip": "10.7.0.136", "model": "m2040dn" },
|
||||
{ "name": "p07", "ip": "10.7.0.137", "model": "m2040dn" },
|
||||
{ "name": "p08", "ip": "10.7.0.138", "model": "p6235cdn" }
|
||||
]
|
|
@ -8,7 +8,7 @@
|
|||
"clean": "rimraf ./dist",
|
||||
"build": "npm run clean && tsc",
|
||||
"start": "node src",
|
||||
"dev": "nodemon --ext js,ts,mts,mjs ./src/index.ts --exec ts-node-esm"
|
||||
"dev": "nodemon --ext js,ts,mts,mjs,json ./src/index.ts --exec ts-node-esm"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
|
|
@ -1,76 +1,14 @@
|
|||
import {
|
||||
PrinterInfo,
|
||||
PrinterModel,
|
||||
PrinterObjectIDConfig,
|
||||
PrinterObjectIDs,
|
||||
Varbind,
|
||||
VarbindString
|
||||
} from './types.mjs'
|
||||
|
||||
import snmp from 'net-snmp'
|
||||
import { printerObjectIDConfigs } from './printerObjectIDConfigs.mjs'
|
||||
|
||||
const printerObjectIDConfigs: PrinterObjectIDConfig[] = [
|
||||
{
|
||||
model: 'm3655idn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
model: 'p6235cdn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.4',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.4',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.4'
|
||||
},
|
||||
cyan: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
},
|
||||
magenta: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.2',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.2',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.2'
|
||||
},
|
||||
yellow: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.3',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.3',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.3'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
model: 'm2040dn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
import snmp from 'net-snmp'
|
||||
|
||||
export class Printer {
|
||||
constructor(name: string, ip: string, model: PrinterModel) {
|
||||
|
@ -205,14 +143,14 @@ export class Printer {
|
|||
}
|
||||
|
||||
async getPrinterInfo(): Promise<PrinterInfo> {
|
||||
const session = snmp.createSession(this.ip, 'public')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const session = snmp.createSession(this.ip, 'public')
|
||||
session.get(this.oIDsArray(), (error, varbinds) => {
|
||||
const varbindsString = this.deBufferizeVarbinds(varbinds)
|
||||
const info = this.objectIDsToPrinterInfo(varbindsString)
|
||||
|
||||
resolve(info)
|
||||
session.close()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
46
src/PrinterRepository.mts
Normal file
46
src/PrinterRepository.mts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { Printer } from './Printer.mjs'
|
||||
import fs from 'fs'
|
||||
|
||||
export class PrinterRepository {
|
||||
constructor() {
|
||||
if (PrinterRepository.instance) return PrinterRepository.instance
|
||||
|
||||
this.loadFromJSON()
|
||||
PrinterRepository.instance = this
|
||||
}
|
||||
|
||||
private printers: Printer[] = []
|
||||
static instance: PrinterRepository
|
||||
|
||||
loadFromJSON() {
|
||||
const json = JSON.parse(fs.readFileSync('./data/printers.json', 'utf8'))
|
||||
this.printers = json.map(
|
||||
({ name, ip, model }: Printer) => new Printer(name, ip, model)
|
||||
)
|
||||
}
|
||||
|
||||
saveToJSON() {
|
||||
const json = JSON.stringify(this.printers)
|
||||
fs.writeFileSync('./data/printers.json', json, 'utf8')
|
||||
}
|
||||
|
||||
addPrinter(printer) {
|
||||
this.printers.push(printer)
|
||||
this.saveToJSON()
|
||||
}
|
||||
|
||||
removePrinter(printerName) {
|
||||
this.printers = this.printers.filter(
|
||||
printer => printer.name !== printerName
|
||||
)
|
||||
this.saveToJSON()
|
||||
}
|
||||
|
||||
getPrinter(printerName) {
|
||||
return this.printers.find(printer => printer.name === printerName)
|
||||
}
|
||||
|
||||
getAllPrinters() {
|
||||
return this.printers
|
||||
}
|
||||
}
|
|
@ -1,16 +1,19 @@
|
|||
import express from 'express'
|
||||
import { printersDB } from './printers.mjs'
|
||||
|
||||
import { Printer } from './Printer.mjs'
|
||||
import { PrinterInfo } from './types.mjs'
|
||||
import fs from 'fs'
|
||||
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
const printersDB = JSON.parse(fs.readFileSync('./data/printers.json', 'utf8'))
|
||||
|
||||
const printers: Printer[] = printersDB.map(
|
||||
printer => new Printer(printer.name, printer.ip, printer.model)
|
||||
)
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
app.get('/api/printer', async (req, res) => {
|
||||
const printersInfo: PrinterInfo[] = await Promise.all(
|
||||
printers.map(printer => printer.getPrinterInfo())
|
||||
)
|
||||
|
@ -18,7 +21,7 @@ app.get('/', async (req, res) => {
|
|||
res.send(printersInfo)
|
||||
})
|
||||
|
||||
app.get('/:printerName', async (req, res) => {
|
||||
app.get('/api/printer/:printerName', async (req, res) => {
|
||||
const printerName = req.params.printerName
|
||||
const printer = printers.find(printer => printer.name === printerName)
|
||||
|
||||
|
|
64
src/printerObjectIDConfigs.mts
Normal file
64
src/printerObjectIDConfigs.mts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { PrinterObjectIDConfig } from './types.mjs'
|
||||
|
||||
export const printerObjectIDConfigs: PrinterObjectIDConfig[] = [
|
||||
{
|
||||
model: 'm3655idn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
model: 'p6235cdn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.4',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.4',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.4'
|
||||
},
|
||||
cyan: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
},
|
||||
magenta: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.2',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.2',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.2'
|
||||
},
|
||||
yellow: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.3',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.3',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.3'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
model: 'm2040dn',
|
||||
objectIds: {
|
||||
model: '1.3.6.1.2.1.25.3.2.1.3.1',
|
||||
serial: '1.3.6.1.2.1.43.5.1.1.17.1',
|
||||
counter: '1.3.6.1.4.1.1347.43.10.1.1.12.1.1',
|
||||
toners: {
|
||||
black: {
|
||||
current: '1.3.6.1.2.1.43.11.1.1.9.1.1',
|
||||
max: '1.3.6.1.2.1.43.11.1.1.8.1.1',
|
||||
model: '1.3.6.1.2.1.43.11.1.1.6.1.1'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
|
@ -1,29 +0,0 @@
|
|||
import { Printer } from './types.mjs'
|
||||
|
||||
export const printersDB: Printer[] = [
|
||||
{
|
||||
name: 'p04',
|
||||
ip: '10.7.0.134',
|
||||
model: 'm3655idn'
|
||||
},
|
||||
{
|
||||
name: 'p05',
|
||||
ip: '10.7.0.135',
|
||||
model: 'm2040dn'
|
||||
},
|
||||
{
|
||||
name: 'p06',
|
||||
ip: '10.7.0.136',
|
||||
model: 'm2040dn'
|
||||
},
|
||||
{
|
||||
name: 'p07',
|
||||
ip: '10.7.0.137',
|
||||
model: 'm2040dn'
|
||||
},
|
||||
{
|
||||
name: 'p08',
|
||||
ip: '10.7.0.138',
|
||||
model: 'p6235cdn'
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue
Block a user