1
yanjinhui
7 天以前 0746808ea9b8e197b263f911fec116cadd0f34fb
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
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)
        {
            var messages = new List<string>();
            //string   message = "";
            try
            {
                for (int deptId = 1; deptId <= 5; deptId++)
                {
                    try
                    {
                        var devices = GetDevicesByDeptId(deptId);
                        if (devices == null)
                        {
                            messages.Add($"DeptId={deptId} 未找到设备,跳过");
                            continue;
                        }
 
                        var para = BaseDal.QueryData()
                            .Where(x => x.Deptid == deptId)
                            .OrderByDescending(x => x.CreateDate)
                            .FirstOrDefault();
 
                        if (para == null)
                        {
                            messages.Add($"DeptId={deptId} 未找到参数配置,跳过");
                            continue;
                        }
 
                        int ext = (int)para.ExtendSpeed;
                        int ret = (int)para.RetractionSpeed;
                        int liftPos = (int)para.LeftPosition;
                        int rightPos = (int)para.RightPosition;
 
                        try
                        {
                            if (devices.Value.left.Communicator.Read<bool>("M117"))
                            {
 
                                return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                            }
 
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            if (devices.Value.right.Communicator.Read<bool>("M117"))
                            {
                                return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                            }
 
                        }
                        catch (Exception)
                        {
                        }
 
                        if (extendedState == "伸出")
                        {
                   
                        try
                        {
                        if (devices.Value.left.Communicator.Read<float>("D80")>= liftPos-5)//到达最大位置)
                        {
                                    messages.Add($"轨道站{deptId}左伸缩杆已到最大位置");
                           
                        }
                        else
                        {
                                    devices.Value.left.Communicator.Write<int>("D1004", liftPos);//伸出位置
                                    devices.Value.left.Communicator.Write<int>("D1002", ext);//左PLC自动动伸出缩回速度
                                    devices.Value.left.Communicator.Write<bool>("M102", true);//伸出按钮
                            
                        } 
                        }
                        catch { }
                
                        try
                        {
                        if (devices.Value.left.Communicator.Read<float>("D80") >=rightPos-5)
                        {
                                    messages.Add($"轨道站{deptId}右伸缩杆已到最大位置");
                        
                        }
                        else
                        {
                                    devices.Value.right.Communicator.Write<int>("D1004", rightPos);
                                    devices.Value.right.Communicator.Write<int>("D1002", ret);
                            devices.Value.right.Communicator.Write<bool>("M102", true);
                            
                        }                        }
                        catch { }
                    
                }
                else if (extendedState == "缩回")
                {
                    
                        try
                        {
                        if (devices.Value.left.Communicator.Read<bool>("M115")) //到达原点位
                        {
                                    messages.Add($"轨道站{deptId}左伸缩杆已到原点");
                           
                        }
                        else
                        {
                                   // devices.Value.left.Communicator.Write<int>("D1004", liftPos);//伸出位置
                                    devices.Value.left.Communicator.Write<int>("D1002", ext);//左PLC自动伸=缩回出缩回速度
                            devices.Value.left.Communicator.Write<bool>("M103", true);//左plc缩回按钮
                            
                         }                           
                    }
                        catch { }
                    
 
                   
                        try
                        {
                        if (devices.Value.right.Communicator.Read<bool>("M115"))
                        {
                                    messages.Add($"轨道站{deptId}右伸缩杆已到原点");
                          
                        }
                        else
                        {
                                   // devices.Value.right.Communicator.Write<int>("D1004", rightPos);
                                    devices.Value.right.Communicator.Write<int>("D1002", ret);
                            devices.Value.right.Communicator.Write<bool>("M103", true);
                            
                         }
                          
                          
                        }
                        catch { }
                    
                }
                        else
                        {
                            return new WebResponseContent { Status = false, Message = "未知操作命令" };
                        }
 
                        
                    }
                    catch (Exception exDept)
                    {
                        messages.Add($"DeptId={deptId} 异常:{exDept.Message}");
                    }
                }
 
                return new WebResponseContent
                {
                    Status = true,
                    Message = messages.Count == 0 ? "执行成功" : "部分执行失败:" + string.Join(";", messages),
                    Data = messages
                };
            }
            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);
 
                //俩个plc左右各一个
                //OtherDevice liftotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG001");
                //OtherDevice rightotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG002");
 
                //找到Dt_Parameters中的最新一条数据根据当前的deid
                var para = BaseDal.QueryData().Where(x => x.Deptid == DeptId).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                int ext = (int)para.ManualExtend;//左PLC手动伸出缩回速度
                int ret = (int)para.ManualRetraction;//右PLC手动伸出缩回速度
                int liftPosition = (int)para.LeftPosition;//左伸出位置
                int rigtpostition = (int)para.RightPosition;//右伸出位置
                //bool canLeft = true,
                // canRight = true;
                string message = "";
 
                try
                {
                    if (devices.Value.left.Communicator.Read<bool>("M117")) //默认是flase 
                    {
 
                        return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                    }
 
                }
                catch (Exception)
                {
                }
                try
                {
                    if (devices.Value.right.Communicator.Read<bool>("M117"))
                    {
                        return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                    }
 
                }
                catch (Exception)
                {
                }
                if (position == "左" && ExtendedState == "伸出")
                {
                    if (devices.Value.left.Communicator.Read<float>("D80") >= liftPosition - 5) //到达目标位置
                    {
                        message += "左伸缩杆到达目标位置;";
                    }
                    else
                    {
                        devices.Value.left.Communicator.Write<int>("D1004", liftPosition);//左伸出位置
                        devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.left.Communicator.Write<bool>("M100", false); // 释放按钮
 
 
                        Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                    }
 
                }
                else if (position == "左" && ExtendedState == "缩回")
                {
                    if (devices.Value.left.Communicator.Read<bool>("M115"))//缩回的时候判断) 
                    {
                        message += "左PLC已到原点;";
                    }
                    else
                    {
                       // devices.Value.left.Communicator.Write<int>("D1004", liftPosition);//伸出位置
                        devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M101", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.left.Communicator.Write<bool>("M101", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动缩回{ext}");
                    }
 
                }
 
                if (position == "右" && ExtendedState == "伸出")
                {
                    if (devices.Value.right.Communicator.Read<float>("D80") >= rigtpostition - 5)
                    {
                        message += "右伸缩杆到达目标位置;";
                    }
                    else
                    {
                        devices.Value.right.Communicator.Write<int>("D1004", rigtpostition);//右伸出位置
                        devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M100", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.right.Communicator.Write<bool>("M100", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动伸出{ret}");
                    }
 
 
                }
                else if (position == "右" && ExtendedState == "缩回")
                {
                    if (devices.Value.right.Communicator.Read<bool>("M115"))
                    {
                        message += "右伸缩杆已到原点;";
                    }
                    else
                    {
                       // devices.Value.right.Communicator.Write<int>("D1004", rigtpostition);//伸出位置
                        devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M101", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.right.Communicator.Write<bool>("M101", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                    }
 
                }
 
 
 
                return new WebResponseContent
                {
                    Status = true,
                    Message = string.IsNullOrEmpty(message) ? "执行成功" : $"{message}",
                    Data = new
                    {
                        ManualExtend = ext,
                        ManualRetraction = ret,
                        message
                    }
                };
            }
 
 
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };
 
            }
 
        }
 
 
        /// <summary>
        ///当自动伸出需要暂停时,暂停按钮
        /// </summary>
        /// <returns></returns>
        public WebResponseContent PauseButton(int deptid)
        {
            try
            {
                //根据int.TryParse(AppSettings.Get("DepartmentID")读取到当前是轨道几,所有人都只能操作当前
                var devices = GetDevicesByDeptId(deptid);
                try
                {
                    devices.Value.left.Communicator.Write<bool>("M104", true);
                }
                catch (Exception)
                {
 
                    throw;
                }
 
                try
                {
                    devices.Value.right.Communicator.Write<bool>("M104", true);
                }
                catch (Exception)
                {
 
                    throw;
                }
                return new WebResponseContent { Status = true, Message = "暂停成功", Data = devices };
            }
            catch (Exception ex)
            {
 
                return new WebResponseContent { Status = false, Message = ex.Message };
            }
        }
 
 
        /// <summary>
        /// 伸缩杆当前位置(这个没有用)
        /// <param name="deptid">轨道站号</param>
        public WebResponseContent CurrentLocation(int deptid)
        {
            try
            {
                var devices = GetDevicesByDeptId(deptid);
                if (devices == null)
                    return new WebResponseContent { Status = false, Message = "设备未找到" };
                var deviceslift = devices.Value.left.Communicator.Read<float>("D80");
                var devicesright = devices.Value.right.Communicator.Read<float>("D80");
                Console.WriteLine($"当前左右伸缩杆返回速度分别为为{deviceslift}");
                return new WebResponseContent
                {
                    Status = true,
                    Data = new { lift = deviceslift, Right = devicesright }
                };
            }
            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);
        }
 
   
    }
 
}