mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-06 13:00:55 +00:00
46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Lextm.SharpSnmpLib;
|
|
using Lextm.SharpSnmpLib.Messaging;
|
|
using System.Net;
|
|
using CartriX.Api.Models;
|
|
|
|
namespace CartriX.Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/Printer")]
|
|
public class PrinterController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public PrinterController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet("getPages/{id}")]
|
|
public ActionResult GetPages(int id)
|
|
{
|
|
Printer printer = _context.Printers.Find(id)!;
|
|
if (printer == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var endpoint = new IPEndPoint(IPAddress.Parse(printer!.IpAddress!), 161);
|
|
|
|
// community string (как "пароль" в SNMP v2c)
|
|
var community = new OctetString("public");
|
|
|
|
// OID sysName.0
|
|
var oid = new ObjectIdentifier(printer.PrinterUsedPagesOID!);
|
|
|
|
var result = Messenger.Get(
|
|
VersionCode.V2,
|
|
endpoint,
|
|
community,
|
|
new List<Variable> { new Variable(oid) },
|
|
timeout: 2000
|
|
);
|
|
|
|
return Ok($"{result[0].Data}");
|
|
}
|
|
}
|
|
} |