initial commit
This commit is contained in:
64
Controllers/PrinterController.cs
Normal file
64
Controllers/PrinterController.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json;
|
||||
using ESCPOS_NET;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/printer")]
|
||||
public class PrinterController : Controller
|
||||
{
|
||||
private readonly PrinterService _printer;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public PrinterController(ILogger<PrinterController> logger, PrinterService printer)
|
||||
{
|
||||
_printer = printer;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[Route("name")]
|
||||
[HttpGet]
|
||||
public ActionResult<string> GetName()
|
||||
{
|
||||
return _printer.GetName();
|
||||
}
|
||||
|
||||
[Route("status")]
|
||||
[HttpGet]
|
||||
public ActionResult<PrinterStatusEventArgs> GetStatus()
|
||||
{
|
||||
return _printer.GetStatus();
|
||||
}
|
||||
|
||||
[Route("print")]
|
||||
[HttpPost]
|
||||
public ActionResult Print([FromBody] JsonElement[] body)
|
||||
{
|
||||
var instructions = new List<BaseInstruction>();
|
||||
foreach (var instruction in body)
|
||||
{
|
||||
BaseInstruction? parsedInstruction = null;
|
||||
|
||||
switch (instruction.GetProperty("type").GetString())
|
||||
{
|
||||
case "print":
|
||||
parsedInstruction = instruction.Deserialize<PrintInstruction>();
|
||||
break;
|
||||
case "barcode":
|
||||
parsedInstruction = instruction.Deserialize<BarcodeInstruction>();
|
||||
break;
|
||||
default:
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if (parsedInstruction == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
instructions.Add(parsedInstruction);
|
||||
}
|
||||
_printer.Print(instructions);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user