对接飞鹅和易联云后 ,网上几乎没资料对大趋智能打印机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());
}
}


大趋智能打印机java api的更多相关文章

  1. 第08章 ElasticSearch Java API

    本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...

  2. Elasticsearch的CRUD:REST与Java API

    CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...

  3. Java API 快速速查宝典

    Java API 快速速查宝典 作者:明日科技,陈丹丹,李银龙,王国辉 著 出版社:人民邮电出版社 出版时间:2012年5月 Java编程的最基本要素是方法.属性和事件,掌握这些要素,就掌握了解决实际 ...

  4. Sample: Write And Read data from HDFS with java API

    HDFS: hadoop distributed file system 它抽象了整个集群的存储资源,可以存放大文件. 文件采用分块存储复制的设计.块的默认大小是64M. 流式数据访问,一次写入(现支 ...

  5. mybatis Java API

    既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升技能了. MyBatis 的 Java API 就是你收获你所做的努力的地方.正如你即将看到的,和 JDBC 相比, MyB ...

  6. HDFS基础和java api操作

    1. 概括 适合一次写入多次查询情况,不支持并发写情况 通过hadoop shell 上传的文件存放在DataNode的block中,通过linux shell只能看见block,看不见文件(HDFS ...

  7. 详解Java API之正则表达式

    正则表达式描述的是一种规则,符合这种限定规则的字符串我们认为它某种满足条件的,是我们所需的.在正则表达式中,主要有两种字符,一种描述的是普通的字符,另一种描述的是元字符.其中元字符是整个正则表达式的核 ...

  8. Elasticsearch java api 基本搜索部分详解

    文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...

  9. Java数据持久层框架 MyBatis之API学习八(Java API详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  10. Spark Java API 之 CountVectorizer

    Spark Java API 之 CountVectorizer 由于在Spark中文本处理与分析的一些机器学习算法的输入并不是文本数据,而是数值型向量.因此,需要进行转换.而将文本数据转换成数值型的 ...

随机推荐

  1. PLM产品生命周期管理,包含哪些阶段?

    PLM:Product Lifecycle Management=产品生命周期管理.产品的整个生命周期包括:投入期.成长期.成熟期.衰退期.结束期.PLM系统使企业可以把多年积累的所有产品相关数据放到 ...

  2. WSUS连接错误需要重置服务器

    在WSUS完成部署后,总是遇到控制台错误,提示需要重置服务器节点.https://www.cnblogs.com/qishine/p/12727982.html错误:连接错误尝试连接WSUS服务器时出 ...

  3. MySQL5.7.15数据库配置主从服务器实现双机热备实例教程

    环境说明 程序在:Web服务器192.168.0.57上面 数据库在:MySQL服务器192.168.0.67上面 实现目的:增加一台MySQL备份服务器(192.168.0.68),做为MySQL服 ...

  4. ELK 性能优化实践 ---总结篇

    版本及硬件配置 JDK:JDK1.8_171-b11 (64 位) ES集群:由3台16核32G的虚拟机部署 ES 集群,每个节点分配 20 G 堆内存 ELK版本:6.3.0 垃圾回收器:ES 默认 ...

  5. NSIS Studio2.1汉化版

    这个东西早些时候是我从老外那里下载回来后放在了群共享里面,中间跟着作者的节奏更新了几次,后来和LmTec聊这个软件的时候提出了汉化的设想,可能是LmTec兄弟看这个软件确实不错,就一口答应了下来,结果 ...

  6. 用golang开发系统软件的一些细节

    用golang开发系统软件的一些细节 作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 (本文的pdf版本) ...

  7. MatrixOne从入门到实践01——初识MatrixOne

    初识MatrixOne 简介 MatrixOrigin 矩阵起源 是一家数据智能领域的创新企业,其愿景是成为数字世界的核心技术提供者. 物理世界的数字化和智能化无处不在.我们致力于建设开放的技术开源社 ...

  8. Mysql主从配置步骤与各种错误

    测试环境: 2台腾讯云服务器.CentOS 7.2 64位,1G,lnmp. PHP:5.6:Mysql:5.5 两台干净的服务器 下面开始配置主服务器(master) 1.修改配置:  log-bi ...

  9. .NET性能系列文章一:.NET7的性能改进

    这些方法在.NET7中变得更快 照片来自 CHUTTERSNAP 的 Unsplash 欢迎阅读.NET性能系列的第一章.这一系列的特点是对.NET世界中许多不同的主题进行研究.比较性能.正如标题所说 ...

  10. 知识图谱-生物信息学-医学论文(Chip-2022)-BCKG-基于临床指南的中国乳腺癌知识图谱的构建与应用

    16.(2022)Chip-BCKG-基于临床指南的中国乳腺癌知识图谱的构建与应用 论文标题: Construction and Application of Chinese Breast Cance ...