1.添加依赖
-
org.springframework.cloud -
spring-cloud-starter-openfeign -
3.1.3
2.创建调用接口
- package com.wpitedatagathering.carctrl.gathering.service;
-
- import com.wpitedatagathering.carctrl.gathering.entity.VolmeasureScantask;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestParam;
-
- import java.net.URI;
-
- @FeignClient(name="VolmeasureScanTaskFeignClient",url="EMPTY")
- public interface VolmeasureScanTaskFeignClient {
-
- //新增任务
- @PostMapping("dataSync/scanTask/add")
- String add(URI uri,@RequestBody VolmeasureScantask volmeasureScantask);
- //完成任务
- @GetMapping("/dataSync/scanTask/complete")
- String complete(URI uri,@RequestParam("taskID") String taskID);
- }
3.创建实体类
- package com.wpitedatagathering.carctrl.gathering.entity;
-
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
-
- import java.io.Serializable;
- import java.util.Date;
-
- /**
- * 扫描任务
- * @TableName VolMeasure_ScanTask
- */
- @TableName(value = "dbo.VolMeasure_ScanTask")
- public class VolmeasureScantask implements Serializable {
- /**
- * 主键ID
- */
- @TableId(value="kid")
- private String kid;
-
- /**
- * 任务名称
- */
- private String taskname;
-
- /**
- * 添加时间
- */
- private Date adddate;
-
- /**
- *
- */
- private Integer isdel;
-
- /**
- *
- */
- private Date deldate;
-
- /**
- * 是否初始化完成
- */
- private Integer isinit;
-
- /**
- * 0 子表记录未插入完 1 子表记录插入完
- */
- private Byte iscomplete;
-
- /**
- *
- */
- private Integer isdetailinit;
-
- private static final long serialVersionUID = 1L;
-
- /**
- * 主键ID
- */
- public String getKid() {
- return kid;
- }
-
- /**
- * 主键ID
- */
- public void setKid(String kid) {
- this.kid = kid;
- }
-
- /**
- * 任务名称
- */
- public String getTaskname() {
- return taskname;
- }
-
- /**
- * 任务名称
- */
- public void setTaskname(String taskname) {
- this.taskname = taskname;
- }
-
- /**
- * 添加时间
- */
- public Date getAdddate() {
- return adddate;
- }
-
- /**
- * 添加时间
- */
- public void setAdddate(Date adddate) {
- this.adddate = adddate;
- }
-
- /**
- *
- */
- public Integer getIsdel() {
- return isdel;
- }
-
- /**
- *
- */
- public void setIsdel(Integer isdel) {
- this.isdel = isdel;
- }
-
- /**
- *
- */
- public Date getDeldate() {
- return deldate;
- }
-
- /**
- *
- */
- public void setDeldate(Date deldate) {
- this.deldate = deldate;
- }
-
- /**
- * 是否初始化完成
- */
- public Integer getIsinit() {
- return isinit;
- }
-
- /**
- * 是否初始化完成
- */
- public void setIsinit(Integer isinit) {
- this.isinit = isinit;
- }
-
- /**
- * 0 子表记录未插入完 1 子表记录插入完
- */
- public Byte getIscomplete() {
- return iscomplete;
- }
-
- /**
- * 0 子表记录未插入完 1 子表记录插入完
- */
- public void setIscomplete(Byte iscomplete) {
- this.iscomplete = iscomplete;
- }
-
- /**
- *
- */
- public Integer getIsdetailinit() {
- return isdetailinit;
- }
-
- /**
- *
- */
- public void setIsdetailinit(Integer isdetailinit) {
- this.isdetailinit = isdetailinit;
- }
-
- @Override
- public boolean equals(Object that) {
- if (this == that) {
- return true;
- }
- if (that == null) {
- return false;
- }
- if (getClass() != that.getClass()) {
- return false;
- }
- VolmeasureScantask other = (VolmeasureScantask) that;
- return (this.getKid() == null ? other.getKid() == null : this.getKid().equals(other.getKid()))
- && (this.getTaskname() == null ? other.getTaskname() == null : this.getTaskname().equals(other.getTaskname()))
- && (this.getAdddate() == null ? other.getAdddate() == null : this.getAdddate().equals(other.getAdddate()))
- && (this.getIsdel() == null ? other.getIsdel() == null : this.getIsdel().equals(other.getIsdel()))
- && (this.getDeldate() == null ? other.getDeldate() == null : this.getDeldate().equals(other.getDeldate()))
- && (this.getIsinit() == null ? other.getIsinit() == null : this.getIsinit().equals(other.getIsinit()))
- && (this.getIscomplete() == null ? other.getIscomplete() == null : this.getIscomplete().equals(other.getIscomplete()))
- && (this.getIsdetailinit() == null ? other.getIsdetailinit() == null : this.getIsdetailinit().equals(other.getIsdetailinit()));
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((getKid() == null) ? 0 : getKid().hashCode());
- result = prime * result + ((getTaskname() == null) ? 0 : getTaskname().hashCode());
- result = prime * result + ((getAdddate() == null) ? 0 : getAdddate().hashCode());
- result = prime * result + ((getIsdel() == null) ? 0 : getIsdel().hashCode());
- result = prime * result + ((getDeldate() == null) ? 0 : getDeldate().hashCode());
- result = prime * result + ((getIsinit() == null) ? 0 : getIsinit().hashCode());
- result = prime * result + ((getIscomplete() == null) ? 0 : getIscomplete().hashCode());
- result = prime * result + ((getIsdetailinit() == null) ? 0 : getIsdetailinit().hashCode());
- return result;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(getClass().getSimpleName());
- sb.append(" [");
- sb.append("Hash = ").append(hashCode());
- sb.append(", kid=").append(kid);
- sb.append(", taskname=").append(taskname);
- sb.append(", adddate=").append(adddate);
- sb.append(", isdel=").append(isdel);
- sb.append(", deldate=").append(deldate);
- sb.append(", isinit=").append(isinit);
- sb.append(", iscomplete=").append(iscomplete);
- sb.append(", isdetailinit=").append(isdetailinit);
- sb.append(", serialVersionUID=").append(serialVersionUID);
- sb.append("]");
- return sb.toString();
- }
- }
4.调用接口
- package com.wpitedatagathering.carctrl.gathering.service.impl;
-
- import com.wpitedatagathering.carctrl.device.config.CarConfig;
- import com.wpitedatagathering.carctrl.gathering.entity.VolmeasureScantask;
- import com.wpitedatagathering.carctrl.gathering.service.VolmeasureScanTaskFeignClient;
- import com.wpitedatagathering.carctrl.gathering.service.VolmeasureScantaskService;
- import lombok.RequiredArgsConstructor;
- import org.springframework.stereotype.Service;
-
- import java.net.URI;
-
- @Service
- @RequiredArgsConstructor
- public class VolmeasureScantaskServiceImpl implements VolmeasureScantaskService {
- private final VolmeasureScanTaskFeignClient volmeasureScanTaskFeignClient;
- private final CarConfig carConfig;
-
- //新增
- @Override
- public String add(VolmeasureScantask volmeasureScantask){
- try{
- return volmeasureScanTaskFeignClient.add(new URI(carConfig.getDataSyncUri()),volmeasureScantask);
- }catch (Exception e){
- e.printStackTrace();
- return null;
- }
- }
-
- //完成
- @Override
- public String complete(String taskID) {
- try{
- return volmeasureScanTaskFeignClient.complete(new URI(carConfig.getDataSyncUri()), taskID);
- }catch (Exception e){
- e.printStackTrace();
- return null;
- }
- }
- }
5.配置类:
- package com.wpitedatagathering.carctrl.device.config;
-
- import lombok.Data;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
-
- import java.util.List;
- import java.util.Map;
-
- //小车配置文件
- @Data
- @ConfigurationProperties(prefix = "cardevice")
- @Configuration
- public class CarConfig {
- //数据同步接口
- private String dataSyncUri;
- //小车任务起始点
- private Integer startNum;
- //分组总数
- private Integer groupNum;
- //小车预置点
- private Integer positionNum;
- //小车IP地址
- private String hostname;
- //小车端口
- private Integer port;
- //小车名称
- private String deviceName;
- //小车的指令集合列表
- private List
cmdList; - //小车的预置点
- private Map
positionValue; - }
6.application.yml 配置信息
- # 小车 ip 端口 信息
- cardevice:
- dataSyncUri: http://127.0.0.1:8859 #数据采集同步接口配置
- deviceName: 智能轨道小车
- hostname: 192.168.8.207
- port: 4196
- startNum: 1
- positionNum: 12 #预置点总数
- groupNum: 6 #分组总数
- cmdList:
- - { "cmdDesc":"查询水平位置","cmd":"FF 01 00 EB 00 00 EC"}
- - { "cmdDesc":"蜂鸣","cmd":"FF 01 00 AB 00 01 AD"}
- - { "cmdDesc":"前进开","cmd":"FF 01 00 09 00 01 0B"}
- - { "cmdDesc":"前进关","cmd":"FF 01 00 0B 00 01 0D"}
- - { "cmdDesc":"后退开","cmd":"FF 01 00 09 00 02 0C"}
- - { "cmdDesc":"后退关","cmd":"FF 01 00 0B 00 02 0E"}
- # 预置点设置和调用(调用作用是让小车行走到指定点位置,到达位置小车会自己停下来) 没有配置的用方法实现
- - { "cmdDesc":"保存1","cmd":"FF 01 00 03 00 01 05"}
- - { "cmdDesc":"调用1","cmd":"FF 01 00 07 00 01 09"}
- - { "cmdDesc":"保存2","cmd":"FF 01 00 03 00 02 06"}
- - { "cmdDesc":"调用2","cmd":"FF 01 00 07 00 02 0A"}
- - { "cmdDesc":"保存3","cmd":"FF 01 00 03 00 03 07"}
- - { "cmdDesc":"调用3","cmd":"FF 01 00 07 00 03 0B"}
- - { "cmdDesc":"保存4","cmd":"FF 01 00 03 00 04 08"}
- - { "cmdDesc":"调用4","cmd":"FF 01 00 07 00 04 0C"}
- - { "cmdDesc":"保存5","cmd":"FF 01 00 03 00 05 09"}
- - { "cmdDesc":"调用5","cmd":"FF 01 00 07 00 05 0D"}