You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
package com.mh.user.controller; |
|
|
|
import com.mh.common.http.HttpResult; |
|
import com.mh.user.job.S7PlcCollectionJob; |
|
import lombok.extern.slf4j.Slf4j; |
|
import org.springframework.web.bind.annotation.*; |
|
|
|
/** |
|
* S7 PLC控制接口 |
|
* 提供手动读写PLC点位的功能 |
|
* |
|
* @author System |
|
* @date 2026-06-23 |
|
*/ |
|
@Slf4j |
|
@RestController |
|
@RequestMapping("/s7plc") |
|
public class S7PlcController { |
|
|
|
private final S7PlcCollectionJob s7PlcCollectionJob; |
|
|
|
public S7PlcController(S7PlcCollectionJob s7PlcCollectionJob) { |
|
this.s7PlcCollectionJob = s7PlcCollectionJob; |
|
} |
|
|
|
/** |
|
* 手动写入数据到PLC |
|
* |
|
* @param cpmId 采集参数ID |
|
* @param value 要写入的值 |
|
* @return 操作结果 |
|
*/ |
|
@PostMapping("/write") |
|
public HttpResult writeData(@RequestParam Long cpmId, @RequestParam Object value) { |
|
try { |
|
log.info("收到手动写入请求: cpmId={}, value={}", cpmId, value); |
|
|
|
boolean success = s7PlcCollectionJob.writeData(cpmId, value); |
|
|
|
if (success) { |
|
return HttpResult.ok("写入成功"); |
|
} else { |
|
return HttpResult.error(500, "写入失败"); |
|
} |
|
} catch (Exception e) { |
|
log.error("手动写入异常: cpmId={}", cpmId, e); |
|
return HttpResult.error(500, "写入异常: " + e.getMessage()); |
|
} |
|
} |
|
|
|
/** |
|
* 清理S7连接器缓存 |
|
* 用于重启连接或维护 |
|
* |
|
* @return 操作结果 |
|
*/ |
|
@PostMapping("/clearCache") |
|
public HttpResult clearCache() { |
|
try { |
|
log.info("收到清理S7连接器缓存请求"); |
|
s7PlcCollectionJob.clearConnectorCache(); |
|
return HttpResult.ok("缓存清理成功"); |
|
} catch (Exception e) { |
|
log.error("清理缓存异常", e); |
|
return HttpResult.error(500, "清理缓存异常: " + e.getMessage()); |
|
} |
|
} |
|
}
|
|
|