首页 > 编程语言 >大趋智能打印机java api

大趋智能打印机java api

时间:2022-11-28 09:44:14浏览次数:62  
标签:java String com openapi shanheyongmu api import 大趋 public

对接飞鹅和易联云后 ,网上几乎没资料对大趋智能打印机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,String,com,openapi,shanheyongmu,api,import,大趋,public
From: https://www.cnblogs.com/shanheyongmu/p/16931382.html

相关文章

  • 简单记录JAVA浮点类型判断BUG
    //0.3*1==0.3结果:trueSystem.out.println("0.3*1==0.3结果:"+(0.3*1==0.3));//0.1*3==0.3结果:falseSystem.out.println("0.......
  • java技术架构图
    架构图有哪几种  业务架构:需求初期业务的结果和过程描述一般比较模糊,可能来自于某个老板、运营或用户的反馈。客户说海尔洗衣机洗土豆会堵,海尔立马设计专门的土豆洗......
  • Java基础语法
          ......
  • java中String.intern()方法具有什么功能呢?
    下文笔者讲述java中String.intern()方法的功能简介说明,如下所示:String.intern原理String.intern():此方法是一个Native方法底层调用C++的StringTable::in......
  • Difference Between Method Overloading and Method Overriding in Java
    ThedifferencesbetweenMethodOverloadingandMethodOverridinginJavaareasfollows:MethodOverloading MethodOverridingMethodoverloadingis......
  • fastapi系列之-在多进程使用websocket一些问题细节的小结
    由于之前一直未深入去了解过关于fastapi中websocket多进程问题,由于之前的测试有可能都是但进程的方式进行启动测试,即便有时候是多进程的方式启动,但是巧合的是估计刚好用户......
  • Fastapi微服务系列(1)-之GRPC入门篇
    一些微服务说明前言在转回python之前,其实就对微服务有所尝试,不过当时使用的是go-micro-v2来进行了解,当时也只是浅尝辄止,没深入继续深究~其实微服务这东西没必要为了微服......
  • Java基础
    java基础java的基本程序设计结构第一个程序Main.javapublicclassMain{publicstaticvoidmain(String[]args){System.out.println("hello,world")......
  • 01.java设计模式之单例模式
    参考:https://blog.csdn.net/liu_da_da/article/details/125425561一、什么是单例模式保证一个类仅有一个实例,并提供一个访问它的全局访问点。二、单例的几种实现方式1......
  • Java: How To Count Words
    publicclassMain{publicstaticvoidmain(String[]args){Stringwords="OneTwoThreeFour";intcountWords=words.split("\\s").length;Sy......