中央热水项目
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.

93 lines
3.1 KiB

package com.mh.user.controller;
import com.alibaba.fastjson2.JSONObject;
import com.mh.common.http.HttpResult;
import com.mh.common.utils.StringUtils;
import com.mh.user.annotation.SysLogger;
import com.mh.user.constants.Constant;
import com.mh.user.entity.ControlSetEntity;
import com.mh.user.entity.DeviceCodeParamEntity;
import com.mh.user.entity.DeviceInstallEntity;
import com.mh.user.entity.PumpSetEntity;
import com.mh.user.model.DeviceModel;
import com.mh.user.model.SerialPortModel;
import com.mh.user.serialport.SerialPortSingle2;
import com.mh.user.service.*;
import com.mh.user.utils.ExchangeStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("serial")
public class DeviceOperateController {
@Autowired
DeviceInstallService deviceInstallService;
@Autowired
PumpSetService pumpSetService;
@Autowired
ControlSetService controlSetService;
@Autowired
NowDataService nowDataService;
@Autowired
private DeviceControlService deviceControlService;
//操作设备
@SysLogger(title = "控制管理", optDesc = "设置设备参数值")
@PostMapping(value = "/operate")
public HttpResult operateDevice(@RequestBody List<SerialPortModel> params) {
try {
Constant.WEB_FLAG = true; //单抄,暂时停止采集
String returnStr = deviceControlService.readOrWriteDevice(params, Constant.WRITE);
if (!StringUtils.isBlank(returnStr) && "fail".equals(returnStr)) {
return HttpResult.error(500, "fail");
}
Constant.WEB_FLAG = false; //单抄,恢复采集
return HttpResult.ok();
} catch (Exception e) {
log.error("前端发送控制异常==>", e);
Constant.WEB_FLAG = false; //单抄,恢复采集
return HttpResult.error(500, "fail");
} finally {
Constant.WEB_FLAG = false;
}
}
//读数据
@SysLogger(title = "控制管理", optDesc = "读设备数据")
@PostMapping(value = "/read")
public HttpResult readData(@RequestBody List<SerialPortModel> params) {
try {
Constant.WEB_FLAG = true; //单抄,暂时停止采集
String rtData = deviceControlService.readOrWriteDevice(params, Constant.READ);
Constant.WEB_FLAG = false; //单抄,恢复采集
if (ExchangeStringUtil.getJSONType(rtData)) {
Map map = JSONObject.parseObject(rtData);
return HttpResult.ok("success", map);
} else {
if (rtData.equals("fail")) {
return HttpResult.error(500, "fail");
} else {
return HttpResult.ok(rtData, rtData);
}
}
} catch (Exception e) {
Constant.WEB_FLAG = false; //单抄,恢复采集
return HttpResult.error(500, "fail");
} finally {
Constant.WEB_FLAG = false; //单抄,恢复采集
}
}
}