大趋智能打印机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());
}
}
大趋智能打印机java api的更多相关文章
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- Elasticsearch的CRUD:REST与Java API
CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...
- Java API 快速速查宝典
Java API 快速速查宝典 作者:明日科技,陈丹丹,李银龙,王国辉 著 出版社:人民邮电出版社 出版时间:2012年5月 Java编程的最基本要素是方法.属性和事件,掌握这些要素,就掌握了解决实际 ...
- Sample: Write And Read data from HDFS with java API
HDFS: hadoop distributed file system 它抽象了整个集群的存储资源,可以存放大文件. 文件采用分块存储复制的设计.块的默认大小是64M. 流式数据访问,一次写入(现支 ...
- mybatis Java API
既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升技能了. MyBatis 的 Java API 就是你收获你所做的努力的地方.正如你即将看到的,和 JDBC 相比, MyB ...
- HDFS基础和java api操作
1. 概括 适合一次写入多次查询情况,不支持并发写情况 通过hadoop shell 上传的文件存放在DataNode的block中,通过linux shell只能看见block,看不见文件(HDFS ...
- 详解Java API之正则表达式
正则表达式描述的是一种规则,符合这种限定规则的字符串我们认为它某种满足条件的,是我们所需的.在正则表达式中,主要有两种字符,一种描述的是普通的字符,另一种描述的是元字符.其中元字符是整个正则表达式的核 ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- Java数据持久层框架 MyBatis之API学习八(Java API详解)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Spark Java API 之 CountVectorizer
Spark Java API 之 CountVectorizer 由于在Spark中文本处理与分析的一些机器学习算法的输入并不是文本数据,而是数值型向量.因此,需要进行转换.而将文本数据转换成数值型的 ...
随机推荐
- 基于Ubunru服务器搭建wordpress个人博客
一.环境 服务器:阿里云突发性能实例 t5-1核(vCPU) 512 MB + 网络按流量收费(该服务器适用于小型网站) 系统:Ubuntu 22.04 64位Ubuntu 22.04 64位 二. ...
- 装饰Hexo博客以及部署个人站点
我的博客最开始采用的是Hexo+hexo-theme-next搭建的,使用GitHub Pages托管并进行自动化部署,写文发布的流程非常简单方便,云端写作发布也轻而易举. 本来事情到这里就应该结束了 ...
- 工具推荐-使用RedisInsight工具对Redis集群CURD操作及数据可视化和性能监控
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 快速 ...
- Elasticsearch:跨集群搜索 Cross-cluster search (CCS)
转载自:https://blog.csdn.net/UbuntuTouch/article/details/104588232 跨集群搜索(cross-cluster search)使您可以针对一个或 ...
- 源码安装最新版keepalived,剥离日志出来并配置日志轮询
安装 yum install -y gcc openssl-devel popt-devel ipvsadm libnl3-devel net-snmp-devel libnl libnl-devel ...
- Prometheus与服务发现
这种按需的资源使用方式对于监控系统而言就意味着没有了一个固定的监控目标,所有的监控对象(基础设施.应用.服务)都在动态的变化.对于Prometheus这一类基于Pull模式的监控系统,显然也无法继续使 ...
- DophineSheduler上下游任务之间动态传参案例及易错点总结
作者简介 淡丹 数仓开发工程师 5年数仓开发经验,目前主要负责百得利MOBY新车业务 二手车业务及售后服务业务系统数仓建设 业务需求 在ETL任务之间调度时,我们有的时候会需要将上游的 ...
- TCP和UDP有啥区别?
TCP全称: Transmission Control Protocol中文名: 传输控制协议解释: 是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.用途:TCP ...
- Linux+Wine玩GTA5指南
如果你的系统没有Wine先装Wine和winetricks,Wine在各大发行版的源都能找到.记住32位和64位的Wine都要装 安装wget后,输入指令 sudo -s cd /opt mkdir ...
- 【Wine使用经验分享】Wine字体显示问题处理
字体不显示/字体为□ 首先尝试下载simsun字体到/usr/share/fonts (simsun.ttf simsun.ttc) 在新版本wine上,差不多就能解决问题. 如果还不行,就从网上下载 ...