printer-api/Controllers/PrinterController.cs

71 lines
1.5 KiB
C#
Raw Normal View History

2022-02-17 15:45:42 +00:00
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())
{
2022-02-17 17:29:32 +00:00
case "text":
parsedInstruction = instruction.Deserialize<TextInstruction>();
2022-02-17 15:45:42 +00:00
break;
case "barcode":
parsedInstruction = instruction.Deserialize<BarcodeInstruction>();
break;
2022-02-19 13:06:07 +00:00
case "feed":
parsedInstruction = instruction.Deserialize<FeedInstruction>();
break;
2022-02-28 12:36:29 +00:00
case "barcode2d":
parsedInstruction = instruction.Deserialize<Barcode2D>();
break;
2022-02-17 15:45:42 +00:00
default:
return BadRequest();
}
if (parsedInstruction == null)
{
return BadRequest();
}
instructions.Add(parsedInstruction);
}
_printer.Print(instructions);
return Ok();
}
}