【YIHE-TOOL使用,如有侵权随时可删】

YIHE-TOOL

简述

目前它只是一个简单的工具,包含:
1、视频流工具,包含新增、删除、修改、查询等操作。
2、ip检测是否在线
3、读取bit位码
4、支持PLC之间通讯

JAR引入


<dependency>
    <groupId>yihe-tool</groupId>
    <artifactId>yihe-tool</artifactId>
    <version>1.0.5</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/lib/yihe-tool-1.0.5.jar</systemPath>
</dependency>

依赖引入

<!-- 一般项目中都有这两个 如果没有再去添加依赖 -->
<dependencies>
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java</artifactId>
        <version>3.14.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.6</version>
    </dependency>
</dependencies>

使用指南

//初始化
MediaMtxConfig mediaMtxConfig = new MediaMtxConfig("http://10.10.19.112:9997", "http://10.10.19.112:8889");
//获取集合
System.out.println(mediaMtxConfig.readList().getBody());
// 获取单个视频流
System.out.println(mediaMtxConfig.readPath("rtsp01"));
// 添加264视频
System.out.println(mediaMtxConfig.addPathH264("rtsp02", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102"));
// 添加265视频
System.out.println(mediaMtxConfig.addPathH265("rtsp03", "rtsp://admin:hnks7777@10.92.10.6:554/Streaming/Channels/102"));
// 修改264视频
System.out.println(mediaMtxConfig.upPathH264("rtsp02", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102").getStatus());
// 修改265视频
System.out.println(mediaMtxConfig.upPathH265("rtsp03", "rtsp://admin:hnks7777@10.12.20.26:554/Streaming/Channels/102").getStatus());
// 删除视频流
System.out.println(mediaMtxConfig.delPath("rtsp02").getStatus());
// 读取 bit 下标为0开始 第二位bit 为1==true 0==false
System.out.println(BitUtils.readBit(15, 1));
// 获取ip是否在线
System.out.println(IpPingUtils.isHostReachable("10.10.19.112"));

Springboot 使用

1、创建 application-hk.yml文件

mtx:
  #流API
  media-mtx-url: http://10.10.19.112:9997
  #流地址
  media-mtx-stream_url: http://10.10.19.112:8889

2、创建 MediaMtx.java 实体

@Configuration
@ConfigurationProperties(prefix = "mtx")
@Data
public class Mtx {
    private String mediaMtxUrl;
    private String mediaMtxStreamUrl;
}

3、配置 MediaMtxConfig

@Configuration
public class MtxConfig {
    @Autowired
    Mtx mtx;

    @Bean(name = "mtxConfig")
    public MediaMtxConfig mtxConfig() {
        return new MediaMtxConfig(mtx.getMediaMtxUrl(), mtx.getMediaMtxStreamUrl());
    }
}

4、使用方式1

@Api(value = "测试", tags = {"测试"})
@RestController
@RequestMapping("/hk/api/v1")
public class HkApiController {
    @Autowired
    private MediaMtxConfig mtxConfig;

    @ApiOperation("测试")
    @GetMapping(value = "/get")
    public AjaxResult getOrganization() {
        String s = mtxConfig.readPath("rtsp1133");
        System.out.println(s);
        return AjaxResult.success();
    }
}

5、使用方式2

@Api(value = "测试", tags = {"测试"})
@RestController
@RequestMapping("/hk/api/v1")
public class HkApiController {
    private final MediaMtxConfig mtxConfig;
    public HkApiController(@Qualifier("mtx1") MediaMtxConfig mtxConfig) {
        this.mtxConfig = mtxConfig;
    }
    
    @ApiOperation("测试")
    @GetMapping(value = "/get")
    public AjaxResult getOrganization() {
        String s = mtxConfig.readPath("rtsp1133");
        System.out.println(s);
        return AjaxResult.success();
    }
}

vue 使用

<iframe src="http://10.10.19.112:8889/rtsp01" scrolling="no" frameborder="0"></iframe>

PLC通讯相关

前言

  • 支持单数据读写,多数据读写,大数据量自动分包读写
  • 支持序列化批量多地址且地址不连续的读写
  • 支持读写DB区,I区,Q区,M区,V
  • 支持读写西门子S1500S1200S400S300S200Smart西门子机床828D
  • 支持PLC自动重连

知识点

知识点1:访问数据类型与JAVA数据类型和PLC数据类型对应关系

访问数据类型数据类型名称数据大小[位]数据大小[字节]JAVA数据类型PLC数据类型示例
boolean布尔类型11/8BooleanBOOLtrue
byte字节类型81ByteBYTE0x11
uint16无符号2字节整型162IntegerWORD/UINT65535
int16有符号2字节整型162ShortWORD/INT-32760
uint32无符号4字节整型324LongDWORD/UDINT70000
int32有符号4字节整型324IntegerDWORD/DINT-70000
float324字节浮点型324FloatREAL3.14
float648字节浮点型648DoubleLREAL3.14
string字符型81StringStringABC
time时间/耗时324LongTime100ms
date日期162LocalDateDate2023-04-03
timeOfDay一天中的时间324LocalTimeTimeOfDay10:22:11
dtl日期+时间9612LocalDateTimeDTL2023-04-03 10:22:11

Springboot S7 使用

1、创建 application-hk.yml 文件

plc:
  x: 192.168.10.180
  d: 192.168.10.200
  enabled: true

2、读取PLC基础信息

@Configuration
@ConfigurationProperties(prefix = "plc")
@Data
public class HkPlcProperties {
    private String x;
    private Boolean enabled;
    private String d;
}

3、创建配置类

@Configuration
public class S7Config {
    @Autowired
    private HkPlcProperties plcProperties;

    @Bean(name = "s7PLC1")
    @ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
    public S7PLC s7PLC1() {
        return new S7PLC(EPlcType.S1500, plcProperties.getX());
    }

    @Bean(name = "s7Serializer1")
    @ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
    public S7Serializer s7Serializer1(@Qualifier("s7PLC1") S7PLC s7PLC1) {
        return new S7Serializer(s7PLC1);
    }

    @Bean(name = "s7PLC2")
    @ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
    public S7PLC s7PLC2() {
        return new S7PLC(EPlcType.S1500, plcProperties.getD());
    }

    @Bean(name = "s7Serializer2")
    @ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
    public S7Serializer s7Serializer2(@Qualifier("s7PLC2") S7PLC s7PLC2) {
        return new S7Serializer(s7PLC2);
    }
}

4、使用

@Component
@ConditionalOnProperty(name = "plc.enabled", havingValue = "true")
public class S7ComponentSelector {

    private final S7PLC s7PLC1;
    private final S7PLC s7PLC2;
    private final S7Serializer s7Serializer1;
    private final S7Serializer s7Serializer2;

    @Autowired
    HkPlcProperties plcProperties;

    @Autowired
    public S7ComponentSelector(@Qualifier("s7PLC1") S7PLC s7PLC1,
                               @Qualifier("s7PLC2") S7PLC s7PLC2,
                               @Qualifier("s7Serializer1") S7Serializer s7Serializer1,
                               @Qualifier("s7Serializer2") S7Serializer s7Serializer2) {
        this.s7PLC1 = s7PLC1;
        this.s7PLC2 = s7PLC2;
        this.s7Serializer1 = s7Serializer1;
        this.s7Serializer2 = s7Serializer2;
    }

    // 根据地址选择PLC
    public S7PLC selectPLC(String address) {
        if (address.equals(plcProperties.getX())) {
            return s7PLC1;
        } else {
            return s7PLC2;
        }
    }

    // 根据地址选择序列化器
    public S7Serializer selectSerializer(String address) {
        if (address.equals(plcProperties.getX())) {
            return s7Serializer1;
        } else {
            return s7Serializer2;
        }
    }
}

Springboot ModbusTcp 使用

1、ModbusTcp 配置 注意 这只是其中一种写法 可以根据自己需求修改

import lombok.Getter;

@Configuration
public class ModbusTcpConfig {
    @Getter
    private final HashMap<String, Object> plcMap = new HashMap<>();
    private final String plc_one = "192.168.1.88";
    private final String plc_two = "10.10.12.253";
    
    @Bean
    public HashMap<String, Object> ModbusTcpPLC() throws RuntimeException {
        if (StringUtils.isNotEmpty(plc_one) && StringUtils.isNotEmpty(plc_two)) {
            ModbusTcp hPlc1 = new ModbusTcp(plc_one);
            ModbusTcp hPlc2 = new ModbusTcp(plc_two);
            plcMap.put("plc_one", hPlc1);
            plcMap.put("plc_two", hPlc2);
            return plcMap;
        } else {
            throw new RuntimeException("Plc is null");
        }
    }
}

2、使用案例 也可以 自己进行实体封装或者Map封装

@RestController
@RequestMapping("/api/get/plc")
public class HkPlcCom {
    private static final Logger logger = LoggerFactory.getLogger(HkPlcCom.class);
    
    @Autowired
    private ModbusTcpConfig modbusTcpConfig;
    
    @PostMapping("/info")
    public AjaxResult info() {
        try {
            ModbusTcp plc = (ModbusTcp) modbusTcpConfig.getPlcMap().get("plc_one");
            int 大车位置 = plc.readInt16(1, 0);
            return AjaxResult.success(大车位置);
        } catch (SocketRuntimeException e) {
            logger.error("Socket runtime exception occurred: {}", e.getMessage());
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("An unexpected error occurred: {}", e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值