using HHECS.API.Models; using HHECS.Bll; using HHECS.Model; using HHECS.OPC; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace HHECS.API.Controllers { public class ECSOpenApiController : ApiController { [HttpPost] public BllResult InStation(RequestModel request) { var result1 = AppSession2.Bll.GetCommonModelByCondition<Equipment>("where disable = 0 and code = 'StationDetect1'"); var result2 = AppSession2.Bll.GetCommonModelByCondition<EquipmentProp>(""); var result3 = AppSession2.Bll.GetCommonModelByCondition<EquipmentType>("where code = 'StationDetect'"); var result4 = AppSession2.Bll.GetCommonModelByCondition<EquipmentTypePropTemplate>(""); if (!result1.Success || !result2.Success || !result3.Success || !result4.Success) { return BllResultFactory.Error($"查询立库信息出错:{result1.Msg + "|" + result2.Msg + "|" + result3.Msg + "|" + result4.Msg}"); } List<Equipment> Equipments = result1.Data; List<EquipmentProp> EquipmentProps = result2.Data.Where(t => Equipments.Count(a => a.Id == t.EquipmentId) > 0).ToList(); List<EquipmentType> EquipmentTypes = result3.Data.Where(t => Equipments.Count(a => a.EquipmentTypeId == t.Id) > 0).ToList(); List<EquipmentTypePropTemplate> EquipmentTypePropTemplates = result4.Data.Where(t => EquipmentTypes.Count(a => a.Id == t.EquipmentTypeId) > 0).ToList(); //组合逻辑外键 Equipments.ForEach(t => { t.EquipmentType = EquipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId); t.EquipmentProps.AddRange(EquipmentProps.Where(i => i.EquipmentId == t.Id).ToList()); }); EquipmentProps.ForEach(t => { t.Equipment = Equipments.FirstOrDefault(i => i.Id == t.EquipmentId); //组合地址 t.Address = $"S7:[{t.Equipment.ConnectName}]{t.Address}"; t.EquipmentTypePropTemplate = EquipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId); }); var stationDetect = Equipments.FirstOrDefault(t => t.Id == 13); if (stationDetect == null || stationDetect.EquipmentType == null || stationDetect.EquipmentProps.Count == 0) { return BllResultFactory.Error("传入设备有误,未找到或属性不完整"); } try { stationDetect.EquipmentProps = result2.Data.Where(t => t.EquipmentId == stationDetect.Id).ToList(); OPCHelp plc = new OPCHelp(ConfigurationManager.AppSettings["OPCServerIP"]); if (plc.OpenConn() == false) { throw new Exception("打开PLC连接失败"); } plc.CreateGroup("group2"); plc.AddOPCItems(stationDetect.EquipmentProps); var a = plc.ReadAddress(stationDetect.EquipmentProps); if (a.Success) { using (IDbConnection connection = AppSession2.Bll.GetConnection()) { IDbTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); String str = "HUIKU1"; if (request.id == 0) { str = "HUIKU1"; } else if(request.id == 1) { str = "HUIKU2"; } else if (request.id == 2) { str = "HUIKU3"; } var prop1 = stationDetect.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == str); prop1.Value = "True"; List<EquipmentProp> equipmentProps = new List<EquipmentProp> { prop1 }; var result = plc.WriteAddress(equipmentProps); if (result.Success) { transaction.Commit(); return BllResultFactory.Sucess("站台重新检测成功"); } else { transaction.Rollback(); return BllResultFactory.Error($"站台重新检测失败:{result.Msg }"); } } catch (Exception ex) { return BllResultFactory.Error(ex.Message); } } } } catch (Exception ex) { return BllResultFactory.Error(ex.Message); } return BllResultFactory.Sucess("站台重新检测成功"); } } }