using Gather.Common.Util; using GatherAPI.api; using HslCommunication.Core; using HslCommunication.ModBus; using log4net; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Pomelo.Data.MySql.Memcached; using System.Text; namespace GatherAPI.Controllers { [Route("api/[controller]")] [ApiController] public class ControlDeviceController : Controller { public ControlDeviceController() { } /// /// 控制设备接口 /// /// [HttpPost] public JsonResult Index([FromBody] ControlDeviceRequest req) { try { if (string.IsNullOrEmpty(req.ip)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "IP不能为空")); } if (string.IsNullOrEmpty(req.port)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "端口号不能为空")); } if (string.IsNullOrEmpty(req.station)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "站号不能为空")); } if (string.IsNullOrEmpty(req.function_code)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "功能码不能为空")); } if (string.IsNullOrEmpty(req.data_type)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "数据类型不能为空")); } if (string.IsNullOrEmpty(req.byte_order)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "字节顺序不能为空")); } if (string.IsNullOrEmpty(req.is_bit_read)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "是否按二进制位读取不能为空")); } if (req.is_bit_read == "1") { if (string.IsNullOrEmpty(req.bit_index)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "位索引(0-7)不能为空")); } } if (string.IsNullOrEmpty(req.attr_value)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "点位不能为空")); } if (string.IsNullOrEmpty(req.value)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "值不能为空")); } if (string.IsNullOrEmpty(req.start_address_from_zero)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "是否从0开始不能为空")); } var modbusTcp = new ModbusTcpNet(req.ip, int.Parse(req.port), byte.Parse(req.station)); if (req.start_address_from_zero == "1") { modbusTcp.AddressStartWithZero = true; } else { modbusTcp.AddressStartWithZero = false; } if (req.function_code == "01") { modbusTcp.ByteTransform.DataFormat = GetDataFormat(req.byte_order); var value = bool.Parse(req.value); var result1 = modbusTcp.Write(req.attr_value, value); if (result1.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.function_code == "03") { modbusTcp.ByteTransform.DataFormat = GetDataFormat(req.byte_order); if (req.is_bit_read == "1") { req.attr_value = req.attr_value + "." + req.bit_index; } if (req.data_type == "short") { var value = short.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "ushort") { var value = ushort.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "int") { if (req.is_bit_read == "1") { bool value = req.value == "1"; var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else { var value = int.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } } else if (req.data_type == "uint") { var value = uint.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "long") { var value = long.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "ulong") { var value = ulong.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "float") { var value = float.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else if (req.data_type == "double") { var value = double.Parse(req.value); var result = modbusTcp.Write(req.attr_value, value); if (result.IsSuccess) { return Json(Tool.GetJsonWithCode(APICode.Success, "控制成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "控制失败")); } } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "数据类型不支持")); } } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "功能码不支持")); } } catch (Exception ex) { Util.LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } private DataFormat GetDataFormat(string byteOrder) { if (string.IsNullOrEmpty(byteOrder)) { return DataFormat.ABCD; } switch (byteOrder?.ToUpper()) { case "ABCD": return DataFormat.ABCD; case "BADC": return DataFormat.BADC; case "CDAB": return DataFormat.CDAB; case "DCBA": return DataFormat.DCBA; default: return DataFormat.ABCD; } } } }