大趋智能打印机java api
对接飞鹅和易联云后 ,网上几乎没资料对大趋智能打印机java api分享,故此分享一波。
SnParam.java
package com.shanheyongmu.openapi.param; import lombok.Data; import lombok.NoArgsConstructor; @NoArgsConstructor @Data public class SnParam { /** * 打印机编号 */ private String sn; public SnParam(String sn) { this.sn = sn; } }
PrinterAddParam.java
package com.shanheyongmu.openapi.param; import lombok.Data; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Range; import javax.validation.constraints.NotBlank; @Data public class PrinterAddParam { /** * 打印机编号 */ @NotBlank @Length(max = 50) private String sn; /** * 设备密钥 */ @NotBlank @Length(max = 255) private String key; /** * 设备名称或备注 */ @Length(max = 50) private String name; @Range(min = 1, max = 16) private Integer lang; }
PrintStatusQueryParam.java
package com.shanheyongmu.openapi.param; import lombok.Data; import lombok.EqualsAndHashCode; import javax.validation.constraints.NotNull; @Data @EqualsAndHashCode(callSuper = true) public class PrintStatusQueryParam extends SnParam { /** * 打印请求ID */ @NotNull private Long printId; public PrintStatusQueryParam(@NotNull String sn, @NotNull Long printId) { super(sn); this.printId = printId; } }
PrintParam.java
package com.shanheyongmu.openapi.param; import lombok.Data; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Range; /** * 打印请求 */ @Data public class PrintParam extends SnParam { /** * 打印小票模板内容 */ @Length(max = 6000) private String content; /** * 播报音源 */ @Length(max = 120) private String voice; /** * 播报语音次数,默认播报1次,不能超过3次 */ @Range(min = 1, max = 5) private Integer voicePlayTimes; /** * 多次播报语音时的间隔秒数,默认3秒 */ private String voicePlayInterval; /** * 打印小票张数,不传默认1, 取值范围: 1~5 */ @Range(min = 1, max = 5) private Integer copies; }
com.shanheyongmu.openapi.result 新建6个类
ResponseResult.java
package com.shanheyongmu.openapi.result; import lombok.Data; import java.io.Serializable; /** * 标准响应结构体 * @param <T> 响应业务数据 */ @Data public class ResponseResult<T> implements Serializable { private String code; private String message; private T data; }
PrinterAddResultData.java
package com.shanheyongmu.openapi.result; import lombok.Data; import java.util.List; @Data public class PrinterAddResultData { /** * 多台设备发生增加失败时返回原因列表,都成功时返回空列表(注意:增加单时失败的原因在message中) */ List<AddFailResult> fail; @Data public static class AddFailResult { private String sn; /** * 失败原因 */ private String reason; } }
PrinterUnbindResultData.java
package com.shanheyongmu.openapi.result; import lombok.Data; import java.util.List; /** * 打印解绑结果 */ @Data public class PrinterUnbindResultData { /** * 多台设备解绑成功时,返回成功的SN列表 */ List<String> ok; /** * 多台设备发生解绑失败时返回原因列表 */ List<UnbindFailResult> fail; @Data public static class UnbindFailResult { private String sn; /** * 失败原因 */ private String reason; } }
PrinterStatusResultData.java
package com.shanheyongmu.openapi.result; import lombok.Data; @Data public class PrinterStatusResultData { /** * 在线状态 0/1 * 0=不在线 1=在线 */ int onlineStatus; /** * 设备状态 * * * -1=初始化 0=就绪 1=打印中 2=缺纸 3=过温 4=打印故障 */ int workStatus; /** * 设备状态说明 */ String workStatusDesc; }
PrintRequestResultData.java
package com.shanheyongmu.openapi.result; import lombok.Data; /** * 请求打印结果 */ @Data public class PrintRequestResultData { /** * 打印请求ID */ private long printId; /** * 当前打印机队列长度 */ private Integer queueSize; }
com.shanheyongmu.openapi.result.callback下
PrintRequestStateCallbackData.java
package com.shanheyongmu.openapi.result.callback; import lombok.Data; /** * 回调打印结果 */ @Data public class PrintRequestStateCallbackData { /** * 打印ID */ private long printId; /** * 状态 * 0=待打印 1=打印中 2=成功 3=失败 4=已取消 */ private String status; }
CallbackResult.java
package com.shanheyongmu.openapi.result.callback; import lombok.Data; /** * 回调结果 */ @Data public class CallbackResult { /** * 回调业务类型 * * 5=打印请求状态发生改变 6=打印机打印发生改变 */ private int type; /** * 回调时间(unix timestamp 秒) */ private long rtime; /** * 业务json string */ private String data; }
DaQuApi.java
package com.shanheyongmu.openapi.util; import com.shanheyongmu.openapi.result.PrinterAddResultData; import com.shanheyongmu.openapi.param.PrintParam; import com.shanheyongmu.openapi.param.PrintStatusQueryParam; import com.shanheyongmu.openapi.param.PrinterAddParam; import com.shanheyongmu.openapi.param.SnParam; import com.shanheyongmu.openapi.result.*; import org.springframework.core.ParameterizedTypeReference; import java.util.List; public class DaQuApi { private static String API_PREFIX = "https://printer.juhesaas.com/openapi"; /** * 批量添加打印机 */ public static ResponseResult<PrinterAddResultData> addPrinterBatch(List<PrinterAddParam> printerList) { String url = API_PREFIX + "/addPrinter"; return DaQuRequestUtils.post(url, printerList, new ParameterizedTypeReference<ResponseResult<PrinterAddResultData>>() { }); } /** * 查询设备状态 */ public static ResponseResult<PrinterStatusResultData> getDeviceStatus(String sn) { return DaQuRequestUtils.post(API_PREFIX + "/getDeviceStatus", new SnParam(sn), new ParameterizedTypeReference<ResponseResult<PrinterStatusResultData>>() { }); } /** * 请求打印 */ public static ResponseResult<PrintRequestResultData> print(PrintParam printParam) { return DaQuRequestUtils.post(API_PREFIX + "/print", printParam, new ParameterizedTypeReference<ResponseResult<PrintRequestResultData>>() { }); } /** * 查询小票打印结果 */ public static ResponseResult<PrintStatusData> getPrintStatus(PrintStatusQueryParam printStatusQueryParam) { return DaQuRequestUtils.post(API_PREFIX + "/getPrintStatus", printStatusQueryParam, new ParameterizedTypeReference<ResponseResult<PrintStatusData>>() { }); } /** * 解绑打印机 */ public static ResponseResult<PrinterUnbindResultData> unbind(List<String> snList) { return DaQuRequestUtils.post(API_PREFIX + "/delPrinter", snList, new ParameterizedTypeReference<ResponseResult<PrinterUnbindResultData>>() { }); } }
DaQuRequestUtils.java 大趋智能云打印机工具类,大趋智能 TRENDIT P7
package com.shanheyongmu.openapi.util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.shanheyongmu.openapi.result.ResponseResult; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.UUID; public class DaQuRequestUtils { private static ObjectMapper objectMapper = new ObjectMapper(); /** * 发起请求 * * @param url url * @param body 请求body对象 * @param typeReference 响应类型 */ public static <R, P> ResponseResult<R> post(String url, P body, ParameterizedTypeReference<ResponseResult<R>> typeReference) { HttpHeaders hexSignHeader = getHeader(body); hexSignHeader.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<P> request = new HttpEntity<>(body, hexSignHeader); return new RestTemplate().exchange(url, HttpMethod.POST, request, typeReference).getBody(); } /** * 设备请求头 */ public static HttpHeaders getHeader(Object requestParam) { String appId = "{your appid}"; String appSecret = "{your appSecret}"; long stime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); String uid = UUID.randomUUID().toString(); String originContext = uid + appId + stime + appSecret; if (requestParam != null) { try { // 注意:如果有自定义Spring MVC HttpMessageConverter,请注意两边序列化规则保持一至 originContext += objectMapper.writeValueAsString(requestParam); } catch (JsonProcessingException e) { e.printStackTrace(); } } HttpHeaders headers = new HttpHeaders(); String sign = DigestUtils.md5Hex(originContext); headers.add("appid", appId); headers.add("uid", uid); headers.add("stime", String.valueOf(stime)); headers.add("sign", sign); return headers; } }
测试用例
package com.shanheyongmu.openapi.util; import com.shanheyongmu.openapi.param.PrintParam; import com.shanheyongmu.openapi.param.PrintStatusQueryParam; import com.shanheyongmu.openapi.param.PrinterAddParam; import com.shanheyongmu.openapi.result.*; import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static org.junit.Assert.*; public class DaQuApiTest { String testSN = "67002004xxxx"; String testKey = "7w4566"; @Test public void unbindTest() { ResponseResult<PrinterUnbindResultData> responseResult = DaQuApi.unbind(Arrays.asList(testSN)); System.out.println(responseResult); Assert.assertEquals("0", responseResult.getCode()); Assert.assertEquals("OK", responseResult.getMessage()); } @Test public void addPrinterBatchTest() { List<PrinterAddParam> printerList = new ArrayList<>(); PrinterAddParam printer1 = new PrinterAddParam(); printer1.setSn(testSN); printer1.setKey(testKey); printer1.setName("openApiTestSN"); printerList.add(printer1); ResponseResult<PrinterAddResultData> responseResult = DaQuApi.addPrinterBatch(printerList); System.out.println(responseResult); Assert.assertEquals("0", responseResult.getCode()); Assert.assertEquals("OK", responseResult.getMessage()); } @Test public void getDeviceStatusTest() { ResponseResult<PrinterStatusResultData> responseResult = DaQuApi.getDeviceStatus(testSN); System.out.println(responseResult); Assert.assertEquals("0", responseResult.getCode()); Assert.assertEquals("OK", responseResult.getMessage()); } @Test public void printTest() { PrintParam printParam = new PrintParam(); printParam.setSn(testSN); printParam.setContent("test print"); printParam.setVoicePlayTimes(1); printParam.setVoice("1"); // 模拟美团接单 ResponseResult<PrintRequestResultData> responseResult = DaQuApi.print(printParam); System.out.println(responseResult); Assert.assertEquals("0", responseResult.getCode()); Assert.assertEquals("OK", responseResult.getMessage()); Assert.assertNotNull(responseResult.getData()); Assert.assertNotNull(responseResult.getData().getPrintId()); Assert.assertNotNull(responseResult.getData().getQueueSize()); } @Test public void getPrintStatusTest() { PrintStatusQueryParam printStatusQueryParam = new PrintStatusQueryParam(testSN, 1045401059247738881L); ResponseResult<PrintStatusData> responseResult = DaQuApi.getPrintStatus(printStatusQueryParam); System.out.println(responseResult); Assert.assertEquals("0", responseResult.getCode()); Assert.assertEquals("OK", responseResult.getMessage()); } }
早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。