using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using System; using VOL.Core.Filters; using VOL.Data.IServices.modbus; using VOL.Data.Services.modbus; using Microsoft.Extensions.DependencyInjection; using VOL.WebApi.Utils; using VOL.Entity.DomainModels; using System.Collections.Generic; using Microsoft.Extensions.Primitives; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; /// // 尼可尼数据采集 /// namespace VOL.WebApi.Controllers.Data { /// /// 数据采集API类 /// [Route("api/NKNCapture")] [AllowAnonymous] public class NKNCaptureController : Controller { private ModbusTcpService _service; // 静态字段 private readonly IHttpContextAccessor _httpContextAccessor; private readonly IDataProcessing _dataService; // 业务处理 [ActivatorUtilitiesConstructor] public NKNCaptureController( IDataProcessing dataService, IHttpContextAccessor httpContextAccessor ) { _dataService = dataService; _httpContextAccessor = httpContextAccessor; } /// /// 采集西门子设备 /// /// IActionResult [ApiTask] [HttpGet, HttpPost, Route("gatherSiemens")] public IActionResult gatherSiemens() { var headers = _httpContextAccessor.HttpContext.Request.Headers; headers.TryGetValue("USER-DEVICE-ID", out StringValues deviceIdStr); if (1 == 1) { try { _service = new ModbusTcpService("192.168.1.99", 502); Console.WriteLine("siemens modbus tcp connected..."); } catch (Exception) { Console.WriteLine("siemens modbus tcp connect failed!"); return Content("siemens modbus tcp connect failed!"); } if (!_service.isConnected) { Console.WriteLine("siemens modbus tcp disconnect!"); return Content("siemens modbus tcp disconnect!"); } } // 切削倍率 17136 1 //_service.readData(1, 6435, "int16"); // 运行时长 分钟 //_service.readData(1, 6436, "int16"); // 工单1产量 件 //_service.readData(1, 6437, "int16"); // 工单2产量 件 //_service.readData(1, 6438, "int16"); // 工单3产量 件 //_service.readData(1, 6439, "int16"); // 当班产量 件 //_service.readData(1, 6440, "int16"); // 运行状态 0:待机 1:运行 2:故障 //_service.readData(1, 1315, "single"); // 工单1任务进度 % //_service.readData(1, 1317, "single"); // 工单2任务进度 % //_service.readData(1, 1319, "single"); // 工单3任务进度 % //_service.readData(1, 1321, "single"); // 工单1良品率 % 真实值 *= 100 //_service.readData(1, 1323, "single"); // 工单2良品率 % 真实值 *= 100 //_service.readData(1, 1325, "single"); // 工单3良品率 % 真实值 *= 100 //_service.readData(1, 1339, "single"); // 设备综合效率 % 真实值 *= 100 //dynamic d1 = _service.readData(1, 1201, "single"); // 电机温度 ℃ //dynamic d2 = _service.readData(1, 1203, "single"); // 母线电压 V //dynamic d3 = _service.readData(1, 1205, "single"); // 实际电流 A //dynamic d4 = _service.readData(1, 1215, "single"); // 累计加工次数 //dynamic d5 = _service.readData(1, 1225, "single"); // 切削倍率 //dynamic d6 = _service.readData(1, 1231, "single"); // 主轴倍率 //dynamic d7 = _service.readData(1, 6689, "string", 50); // 运行程序编号 length = 8 //dynamic d8 = _service.readData(1, 6435, "int16"); // 累计运行时长 分钟 string message = ""; bool success = int.TryParse(deviceIdStr, out int deviceId); try { if (success) { Dictionary> dataMap = new(); if (1 == 1) { dataMap = _dataService.readSiemensData(_service); } else { dataMap.Add("produce", new Dictionary()); dataMap.Add("machine", new Dictionary()); } message = saveSourceData(dataMap, deviceId); } else { message = "设备非法或已下线"; } } catch (Exception ex) { Console.WriteLine(ex.Message); message = "read data error: " + ex.Message; } finally { if (_service != null) { _service.disConnent(); } } return Content(message); } /// /// 采集广数设备 /// /// IActionResult [ApiTask] [HttpGet, HttpPost, Route("gatherGSK")] public IActionResult gatherGSK() { var headers = _httpContextAccessor.HttpContext.Request.Headers; headers.TryGetValue("USER-DEVICE-ID", out StringValues deviceIdStr); if (1 == 1) { try { _service = new ModbusTcpService("192.168.1.100", 502); Console.WriteLine("GSK modbus tcp connected..."); } catch (Exception) { Console.WriteLine("GSK modbus tcp connect failed!"); return Content("GSK modbus tcp connect failed!"); } if (!_service.isConnected) { Console.WriteLine("GSK modbus tcp disconnect!"); return Content("GSK modbus tcp disconnect!"); } } //_service.readData(1, 6429, "int16"); // 程序编号 //_service.readData(1, 6435, "int16"); // 通讯状态 5:通讯正常 15:通讯断线 //_service.readData(1, 6430, "int16"); //_service.readData(1, 6435, "int16"); // 运行时长 分钟 //_service.readData(1, 6436, "int16"); // 工单1产量 件 //_service.readData(1, 6437, "int16"); // 工单2产量 件 //_service.readData(1, 6438, "int16"); // 工单3产量 件 //_service.readData(1, 6439, "int16"); // 当班产量 件 //_service.readData(1, 6440, "int16"); // 运行状态 0:待机 1:运行 2:故障 //_service.readData(1, 1315, "single"); // 工单1任务进度 % //_service.readData(1, 1317, "single"); // 工单2任务进度 % //_service.readData(1, 1319, "single"); // 工单3任务进度 % //_service.readData(1, 1321, "single"); // 工单1良品率 % 真实值 *= 100 //_service.readData(1, 1323, "single"); // 工单2良品率 % 真实值 *= 100 //_service.readData(1, 1325, "single"); // 工单3良品率 % 真实值 *= 100 //_service.readData(1, 1331, "single"); // 设备综合效率 % 真实值 *= 100 //_service.readData(1, 6430, "int16"); // 通讯状态 5:通讯正常 15:通讯断线 //_service.readData(1, 6099, "int16"); // 工作方式 0:编辑 1:自动 2:MDI 3:DNC 4:手动 5:手轮 6:回参考点 //_service.readData(1, 6100, "int16"); // 运行状态 0:复位 1:停止 2:运行 3:暂停 //dynamic d1 = _service.readData(1, 4117, "int32"); // 当天加工数量 次 //dynamic d2 = _service.readData(1, 1201, "int32"); // 开机时间 秒 //dynamic d3 = _service.readData(1, 1203, "int32"); // 运行时间 秒 //dynamic d4 = _service.readData(1, 1241, "single"); // 进给倍率 //dynamic d5 = _service.readData(1, 1247, "single"); // 主轴倍率 //dynamic d6 = _service.readData(1, 6299, "string", 8); // 运行程序编号 length = 8 //dynamic d7 = _service.readData(1, 4115, "int32"); // 累计运行时长 分钟 //dynamic d8 = _service.readData(1, 1199, "int32"); // 累计加工数 次 string message = ""; bool success = int.TryParse(deviceIdStr, out int deviceId); try { if (success) { Dictionary> dataMap = new(); if (1 == 1) { dataMap = _dataService.readGSKData(_service); } else { dataMap.Add("produce", new Dictionary()); dataMap.Add("machine", new Dictionary()); } message = saveSourceData(dataMap, deviceId); } else { message = "设备非法或已下线"; } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); message = "read data error: " + ex.Message; } finally { if (_service != null) { _service.disConnent(); } } return Content(message); } [HttpGet, HttpPost, Route("testSave")] public IActionResult testSave() { Dictionary> dataMap = new(); dataMap.Add("machine", new Dictionary() { { "config_id", 666 } }); dataMap.Add("produce", new Dictionary() { { "config_id", 666 } }); string message = saveSourceData(dataMap, 1); return Content(message); } /// /// 保存采集数据 /// /// IActionResult private string saveSourceData(Dictionary> dataMap, int deviceId) { // 获取当前系统时间, 保证插入时间一致 DateTime now = DateTime.Now; // 设备关联的用户信息 Data_Config dataConfig = _dataService.GetDataConfig(deviceId); if (dataConfig == null) { return "设备不存在!"; } int userId = dataConfig.CreateID ?? 1; // 默认超管 Dictionary machineData = dataMap["machine"]; Data_Machine data_Machine = CommonUtil.ConvertToObject(machineData); data_Machine.config_id = deviceId; data_Machine.CreateDate = now; data_Machine.CreateID = userId; bool result1 = _dataService.saveMachineData(data_Machine, out string message1); Dictionary produceData = dataMap["produce"]; Data_Produce data_Produce = CommonUtil.ConvertToObject(produceData); data_Produce.config_id = deviceId; data_Produce.CreateDate = now; data_Produce.CreateID = userId; bool result2 = _dataService.saveProduceData(data_Produce, out string message2); return (result1 && result2) ? "OK" : "save machine: " + message1 + "; save produce: " + message2 + ";"; } } }