using Microsoft.AspNetCore.Http; using VOL.Data.IServices.modbus; using VOL.Entity.DomainModels; using VOL.Core.Services; using Microsoft.AspNetCore.Mvc; using Autofac.Core; using VOL.Core.Extensions.AutofacManager; using System.Reflection.PortableExecutable; namespace VOL.Data.Services.modbus { public class DataProcessing : IDataProcessing, IDependency // 继承IDependency,交给Ioc容器接管 { private readonly IHttpContextAccessor _httpContextAccessor; public DataProcessing(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } // 读西门子数据 public Dictionary> readSiemensData(IModbusService modbus) { // 生产数据 Dictionary map1 = new Dictionary { { "com_status",modbus.readData(1, 6430, "int16") }, // 通讯状态 5:通讯正常 15:通讯断线 { "run_time", (int)modbus.readData(1, 6435, "int16") }, // 运行时长 分钟 { "turnout_1",(int)modbus.readData(1, 6436, "int16") }, // 工单1产量 件 { "turnout_2", (int)modbus.readData(1, 6437, "int16") }, // 工单2产量 件 { "turnout_3", (int)modbus.readData(1, 6438, "int16") }, // 工单3产量 件 { "turnout_all", (int)modbus.readData(1, 6439, "int16") }, // 当班产量 件 { "status", modbus.readData(1, 6440, "int16") }, // 运行状态 0:待机 1:运行 2:故障 { "schedule_1", (decimal)modbus.readData(1, 1315, "single") }, // 工单1任务进度 % { "schedule_2",(decimal) modbus.readData(1, 1317, "single") }, // 工单2任务进度 % { "schedule_3", (decimal)modbus.readData(1, 1319, "single") }, // 工单3任务进度 % { "yield_1", (decimal)modbus.readData(1, 1321, "single") }, // 工单1良品率 % 真实值 *= 100 { "yield_2", (decimal)modbus.readData(1, 1323, "single") }, // 工单2良品率 % 真实值 *= 100 { "yield_3", (decimal)modbus.readData(1, 1325, "single") }, // 工单3良品率 % 真实值 *= 100 { "oee", (decimal)modbus.readData(1, 1339, "single") }, // 设备综合效率 % 真实值 *= 100 }; // 机床数据 Dictionary map2 = new Dictionary { // 操作模式 0:JOG 1:AUTO 2:AUTO + TEACHIN 3:JOG + REPOIN 4:JOG + REPOS // 5:MDA 6:MDA + REPOS 7:MDA + REPOS + TEAHIN8:MDA + TEACHIN { "com_status", modbus.readData(1, 6430, "int16") }, // 通讯状态 5:通讯正常 15:通讯断线 { "smode", modbus.readData(1, 6099, "int16") }, { "state", modbus.readData(1, 6429, "int16") }, // 运行状态 0:待机 1:故障 2:运行 3:暂停 { "temperature", (decimal)modbus.readData(1, 1201, "single")}, // 电机温度 ℃ { "potential", (decimal)modbus.readData(1, 1203, "single")}, // 母线电压 V { "current", (decimal)modbus.readData(1, 1205, "single")}, // 实际电流 A { "cut_rate", (decimal)modbus.readData(1, 1225, "single")}, // 切削倍率 { "main_rate", (decimal)modbus.readData(1, 1231, "single")}, // 主轴倍率 { "run_program_no", modbus.readData(1, 6689, "string", 26)}, // 加工程序号 length = 52 { "run_time_total", (long)modbus.readData(1, 6435, "int16")}, // 累计运行时长 分钟 { "quantity_total", (long)modbus.readData(1, 1215, "single")}, // 累计加工数 次 }; // 组装Map返回 Dictionary> map = new(); map.Add("produce", map1); map.Add("machine", map2); return map; } // 读广数数据 public Dictionary> readGSKData(IModbusService modbus) { // 生产数据 Dictionary map1 = new Dictionary { { "program_no", (int)modbus.readData(1, 6429, "int16") }, // 程序编号 { "com_status", modbus.readData(1, 6430, "int16") }, // 通讯状态 5:通讯正常 15:通讯断线 { "run_time", (int)modbus.readData(1, 6435, "int16") }, // 运行时长 分钟 { "turnout_1", (int)modbus.readData(1, 6436, "int16") }, // 工单1产量 件 { "turnout_2", (int)modbus.readData(1, 6437, "int16") }, // 工单2产量 件 { "turnout_3", (int)modbus.readData(1, 6438, "int16") }, // 工单3产量 件 { "turnout_all",(int)modbus.readData(1, 6439, "int16") }, // 当班产量 件 { "status", modbus.readData(1, 6440, "int16") }, // 运行状态 0:待机 1:运行 2:故障 { "schedule_1", (decimal)modbus.readData(1, 1315, "single") }, // 工单1任务进度 % { "schedule_2", (decimal)modbus.readData(1, 1317, "single") }, // 工单2任务进度 % { "schedule_3", (decimal)modbus.readData(1, 1319, "single") }, // 工单3任务进度 % { "yield_1", (decimal)modbus.readData(1, 1321, "single") }, // 工单1良品率 % 真实值 *= 100 { "yield_2", (decimal)modbus.readData(1, 1323, "single") }, // 工单2良品率 % 真实值 *= 100 { "yield_3", (decimal)modbus.readData(1, 1325, "single") }, // 工单3良品率 % 真实值 *= 100 { "oee", (decimal)modbus.readData(1, 1331, "single") }, // 设备综合效率 % 真实值 *= 100 }; // 机床数据 Dictionary map2 = new Dictionary { { "com_status", modbus.readData(1, 6430, "int16") }, // 通讯状态 5:通讯正常 15:通讯断线 { "gmode", modbus.readData(1, 6099, "int16") }, // 工作方式 0:编辑 1:自动 2:MDI 3:DNC 4:手动 5:手轮 6:回参考点 { "state", modbus.readData(1, 6100, "int16") }, // 运行状态 0:复位 1:停止 2:运行 3:暂停 { "quantity", (long)modbus.readData(1, 4117, "int32")}, // 当天加工数量 次 { "on_time", (long)modbus.readData(1, 1201, "int32")}, // 开机时间 秒 { "run_time", (long)modbus.readData(1, 1203, "int32")}, // 运行时间 秒 { "feed_rate", (decimal)modbus.readData(1, 1241, "single")}, // 进给倍率 { "main_rate", (decimal)modbus.readData(1, 1247, "single")}, // 主轴倍率 { "run_program_no", modbus.readData(1, 6299, "string", 4)}, // 运行程序编号 length = 8 { "run_time_total", (long)modbus.readData(1, 4115, "int32")}, // 累计运行时长 分钟 { "quantity_total", (long)modbus.readData(1, 1199, "int32")} // 累计加工数 次 }; // 组装Map返回 Dictionary> map = new(); map.Add("produce", map1); map.Add("machine", map2); return map; } // 查询设备信息 public Data_Config GetDataConfig(int deviceId) { return Data_ConfigService.Instance.GetConfigById(deviceId); } // 保存机床数据 public bool saveMachineData(Data_Machine machine, out string message) { SaveModel model = new SaveModel(); Dictionary mapData = new() { { "config_id", machine.config_id}, { "com_status", machine.com_status ?? 15 }, { "gmode", machine.gmode ?? 1 }, { "smode", machine.smode ?? 1 }, { "run_program_no", machine.run_program_no ?? "12345"}, { "state", machine.state ?? 0 }, { "temperature", machine.temperature ?? 0M }, { "potential", machine.potential ?? 0M}, { "current", machine.current ?? 0M}, { "quantity", machine.quantity ?? 0}, { "on_time", machine.on_time ?? 0}, { "run_time", machine.run_time ?? 0}, { "feed_rate",machine.feed_rate ?? 0M}, { "cut_rate", machine.cut_rate ?? 0M}, { "main_rate", machine.main_rate ?? 0M}, { "run_time_total", machine.run_time_total ?? 0}, { "quantity_total", machine.quantity_total ?? 0}, { "CreateID", machine.CreateID}, { "CreateDate", machine.CreateDate}, }; model.MainData = mapData; try { Core.Utilities.WebResponseContent result = Data_MachineService.Instance.Add(model); if (!result.Status) { message = result.Message; //Logger.Info(result.Message); return false; } } catch (Exception ex) { //Logger.Error(ex.Message); message = ex.Message; return false; } message = "ok"; return true; } // 保存生产数据 public bool saveProduceData(Data_Produce produce, out string message) { SaveModel model = new SaveModel(); Dictionary mapData = new() { { "config_id", produce.config_id}, { "program_no", produce.program_no ?? 12345 }, { "com_status", produce.com_status ?? 15 }, { "run_time", produce.run_time ?? 0 }, { "status", produce.status ?? 0 }, { "turnout_all", produce.turnout_all ?? 0 }, { "turnout_1", produce.turnout_1 ?? 0 }, { "turnout_2", produce.turnout_2 ?? 0 }, { "turnout_3", produce.turnout_3 ?? 0 }, { "schedule_1", produce.schedule_1 ?? 0M }, { "schedule_2", produce.schedule_2 ?? 0M }, { "schedule_3", produce.schedule_3 ?? 0M }, { "yield_1", produce.yield_1 ?? 1M }, { "yield_2", produce.yield_2 ?? 0M }, { "yield_3", produce.yield_3 ?? 0M }, { "oee", produce.oee ?? 0.95M }, // 默认95% { "CreateID", produce.CreateID}, { "CreateDate", produce.CreateDate}, }; model.MainData = mapData; try { Core.Utilities.WebResponseContent result = Data_ProduceService.Instance.Add(model); if (!result.Status) { message = result.Message; return false; } } catch (Exception ex) { message = ex.Message; return false; } message = "ok"; return true; } } }