对接飞鹅和易联云后 ,网上几乎没资料对大趋智能打印机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. 【loj2538】 【PKUWC 2018】Slay the Spire dp

    我们不难发现,假设抽了x张攻击牌,y张强化牌,那么肯定是打出尽可能多张的强化牌后,再开始出攻击牌(当然最少要一张攻击牌) 我们设G(i,j)表示:所有(抽到的攻击牌牌数为i,打出的攻击牌牌数为j)的方 ...

  2. 更改elasticsearch中索引的mapping

    文章转载自:https://www.cnblogs.com/uglyliu/p/12331964.html 昨天研发说在kibana中统计userid字段不出图,后来查到该字段显示冲突了,然后再查看了 ...

  3. Confluence预览中文附件出现乱码

    转载自:https://blog.51cto.com/u_13776519/5329428 背景介绍: 1.使用docker方式安装运行的Confluence 2.进行了破解,使用外置数据库 3.do ...

  4. Beats在Kibana中的集中管理

    前提条件: 1.es版本是白金版 2.es开启安全设置,kibana访问es需要密码 操作步骤汇总: 1-3步是基础环境配置 4-9步是注册beats到集中管理平台,然后启动beats,只是单纯启动b ...

  5. ELK 性能优化实践

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI5MTU1MzM3MQ==&mid=2247489814&idx=1&sn=6916f8b7 ...

  6. @input含义和用法

    @input :一般用于监听事件只要输入的值变化了就会触发input 示例: <div id="div1"> <input type="text&quo ...

  7. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  8. 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)

    维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...

  9. 实例分析Scheduled Thread Pool Executor与Timer的区别

    摘要:JDK 1.5开始提供Scheduled Thread PoolExecutor类,Scheduled Thread Pool Executor类继承Thread Pool Executor类重 ...

  10. 使用python制作动图

    利用python制作gif图 引言 当写文章时候,多张图片会影响排版,可以考虑制作gif图 准备 pip install imageio 代码 # This is a sample Python sc ...