ECSOpenApiController.cs
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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("站台重新检测成功");
}
}
}