Compare commits

..

3 Commits

Author SHA1 Message Date
Douglas Barone
26b87ed628 Organize files 2023-05-15 12:40:44 -04:00
Douglas Barone
37a7074aea Added Repository 2023-05-15 12:23:33 -04:00
Douglas Barone
f15d4085c6 Use /data 2023-05-15 11:45:46 -04:00
7 changed files with 128 additions and 99 deletions

7
data/printers.json Normal file
View 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" }
]

View File

@ -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": "",

View File

@ -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
View 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
}
}

View File

@ -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)

View 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'
}
}
}
}
]

View File

@ -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'
}
]