yanjinhui
2025-05-08 dce06906b2757824ad6ecfd02c1ff04ee8e9ad20
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using SqlSugar;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITelescopicService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
 
 
namespace WIDESEAWCS_TelescopicService
{
    public class ParametersService : ServiceBase<Dt_Parameters, IRepository<Dt_Parameters>>, IParametersService
    {
        public IRepository<Dt_Parameters> Repository => BaseDal;
        private readonly IRepository<Sys_User> _user;
 
        public ParametersService(IRepository<Dt_Parameters> BaseDal, IRepository<Sys_User> user) : base(BaseDal)
        {
            _user = user;   
        }
 
 
        /// <summary>
        /// 自动控制伸缩杆的伸出和缩回速度(一次性控制所有的)
        /// </summary>
        /// <param name="ExtendedState">伸/出状态</param>
        /// <returns></returns>
        public WebResponseContent automation(string extendedState)
        {
            try
            {
                for (int deptId = 1; deptId <= 5; deptId++)
                {
                    var devices = GetDevicesByDeptId(deptId);
                    if (devices == null)
                    {
                        Console.WriteLine($"DeptId={deptId} 未找到设备,跳过");
                        continue;
                    }
 
                    var para = BaseDal.QueryData()
                        .Where(x => x.Deptid == deptId)
                        .OrderByDescending(x => x.CreateDate)
                        .FirstOrDefault();
 
                    if (para == null)
                    {
                        Console.WriteLine($"DeptId={deptId} 未找到参数配置,跳过");
                        continue;
                    }
 
                    int ext = (int)para.ExtendSpeed;
                    int ret = (int)para.RetractionSpeed;
 
                    if (extendedState == "伸出")
                    {
                        devices.Value.left.Communicator.Write<int>("D500", ext);//伸出速度
                        devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
 
                        devices.Value.right.Communicator.Write<int>("D500", ext);
                        devices.Value.right.Communicator.Write<bool>("M100", true);
 
                        Console.WriteLine($"DeptId={deptId} 伸出操作,速度={ext}");
                    }
                    else if (extendedState == "缩回")
                    {
                        devices.Value.left.Communicator.Write<int>("D501", ret);//伸出速度
                        devices.Value.left.Communicator.Write<bool>("M101", true);//伸出按钮
 
                        devices.Value.right.Communicator.Write<int>("D501", ret);
                        devices.Value.right.Communicator.Write<bool>("M101", true);
 
                        Console.WriteLine($"DeptId={deptId} 缩回操作,速度={ret}");
                    }
                    else
                    {
                        return new WebResponseContent { Status = false, Message = "未知的操作命令" };
                    }
                }
 
                return new WebResponseContent { Status = true, Message = "全部设备操作完成" };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = $"操作异常:{ex.Message}" };
            }
        }
 
 
 
 
 
 
 
        /// <summary>
        /// 手动控制,伸缩杆的缩回和伸出速度
        /// </summary>
        /// <param name="position">伸缩杆的位置(左右)</param>
        /// <param name="ExtendedState">伸/缩状态</param>
        /// <returns></returns>
        /// 
        //public WebResponseContent ManualOperation(string position, string ExtendedState)
        //{
        //    try
        //    {
        //        //俩个plc左右各一个
        //        OtherDevice liftotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG001");
        //        OtherDevice rightotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG002");
        //        //找到Dt_Parameters中的最新一条数据
        //        var para = BaseDal.QueryData().OrderBy(x => x.CreateDate).FirstOrDefault();
        //        int ext = (int)para.ManualExtend;//自动伸出速度
        //        int ret = (int)para.ManualRetraction;//自动缩回速度
        //            if (position == "左" && ExtendedState == "伸出")
        //            {
        //                var values = liftotherDevice.Communicator.Write<int>("D500", ext);//速度
        //    var button = liftotherDevice.Communicator.Write<bool>("M100", true);//伸出按钮
        //    Console.WriteLine($"{position}伸缩杆伸出{ext}");
        //            }
        //            if (position == "左" && ExtendedState == "缩回")
        //            {
        //                var values = liftotherDevice.Communicator.Write<int>("D501", ret);
        //var button = liftotherDevice.Communicator.Write<bool>("M101", true);//缩回按钮
        //Console.WriteLine($"{position}伸缩杆缩回{ret}");
        //            }
        //        if (position == "右" && ExtendedState == "伸出")
        //        {
        //            var values = rightotherDevice.Communicator.Write<int>("D500", ext);//速度
        //            var button = rightotherDevice.Communicator.Write<bool>("M100", true);//伸出按钮
        //            Console.WriteLine($"{position}伸缩杆伸出{ext}");
        //        }
        //        if (position == "右" && ExtendedState == "缩回")
        //        {
        //            var values = rightotherDevice.Communicator.Write<int>("D501", ret);
        //            var button = rightotherDevice.Communicator.Write<bool>("M101", true);//缩回按钮
        //            Console.WriteLine($"{position}伸缩杆缩回{ret}");
        //        }
        //        return new WebResponseContent { Status = true, Message = "成功" };
        //    }
        //    catch (Exception ex)
        //    {
        //        return new WebResponseContent { Status = false, Message = ex.Message };
 
        //    }
        //}
 
 
 
        /// <summary>
        /// 手动控制,伸缩杆的缩回和伸出速度
        /// </summary>
        /// <param name="position">伸缩杆的位置(左右)</param>
        /// <param name="ExtendedState">伸/缩状态</param>
        /// <param name="DeptId">股道号</param>
        /// <returns></returns>
        public WebResponseContent ManualOperation(string position, string ExtendedState,int DeptId)
                {
            try
            {
 
                var devices = GetDevicesByDeptId(DeptId);
                if (devices == null)
                    return new WebResponseContent { Status = false, Message = "设备未找到" };
 
                var para = BaseDal.QueryData(x=>x.Deptid== DeptId).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (para == null)
                    return new WebResponseContent { Status = false, Message = "参数未配置" };
 
                int ext = (int)para.ExtendSpeed;
                int ret = (int)para.RetractionSpeed;
 
                if (position == "左" && ExtendedState == "伸出")
                {
                    devices.Value.left.Communicator.Write<int>("D500", ext);//伸出速度
                    devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                    Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                }
                else if (position == "左" && ExtendedState == "缩回")
                {
                    devices.Value.left.Communicator.Write<int>("D501", ret);//缩回速度
                    devices.Value.left.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                }
                else if (position == "右" && ExtendedState == "伸出")
                {
                    devices.Value.right.Communicator.Write<int>("D500", ext);
                    devices.Value.right.Communicator.Write<bool>("M100", true);
                    Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                }
                else if (position == "右" && ExtendedState == "缩回")
                {
                    devices.Value.right.Communicator.Write<int>("D501", ret);
                    devices.Value.right.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                }
 
                else
                {
                    return new WebResponseContent { Status = false, Message = "状态参数错误" };
                }
 
                return new WebResponseContent { Status = true, Message = "成功" };
 
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };
 
            }
        }
 
 
        /// <summary>
        /// 获取设备号
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public (OtherDevice left, OtherDevice right)? GetDevicesByDeptId(int deptId)
        {
            // 左设备号:1 -> 001,2 -> 003,3 -> 005...
            int baseCode = 1 + (deptId - 1) * 2;
            string leftCode = $"SSG{baseCode.ToString("D3")}";
            //右设备:1->002 ,2->004
            string rightCode = $"SSG{(baseCode + 1).ToString("D3")}";
            var left = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == leftCode);
            var right = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == rightCode);
 
            if (left == null || right == null)
                return null;
 
            return (left, right);
        }
 
    }
 
}