1
czw
2025-05-15 9a5dcc53616c720c97492025faf1930e221b8aeb
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.ServiceModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using NLog;
using NLog.Config;
using NLog.Targets;
using GZ.DB.Map.OIDATABASE;
using GZ.DB.App.OIDATABASE;
using GZ.DB.Repository.OIDATABASE;
using GZ.DB.IRepository.OIDATABASE;
using GZ.DB.Entity.OIDATABASE;
namespace GZ.Projects.S7
{
public class Settings
{
#region    [自定义类][20250323144531864][Settings]
            public static bool FirstG {get;set;}=false;
        public static string HostToAgvServerUrl { get; set; }
        
        public static List<deviceInfo> deviceInfos { get; set; }
        public static string HKbaseUrl { get; set; }
        public static readonly HttpHelper apiHelper = new HttpHelper();
#endregion [自定义类][20250323144531864][Settings]
}
public class LogHelper
{
#region    [自定义类][20250323145442478][LogHelper]
    public static Dictionary<string, ILogger> loggers = new Dictionary<string, ILogger>();
 
public static void Debug(string message, string name = "") {
    ILogger logger = null;
    if (loggers.Keys.Contains(name)) {
        logger = loggers[name];
    }
    else {
        logger = LogFactory.CreateLogger(name);
        if (logger != null) {
            loggers.Add(name, logger);
        }
        else {
            logger = LogFactory.CreateLogger("console");
        }
    }
    if (logger != null) {
        logger.Debug(message);
    }
}
 
 
 
public static void Info(string message, string name = "") {
    //logger.Info(message);
    ILogger logger = null;
    if (loggers.Keys.Contains(name)) {
        logger = loggers[name];
    }
    else {
        logger = LogFactory.CreateLogger(name);
        if (logger != null) {
            loggers.Add(name, logger);
        }
        else {
            logger = LogFactory.CreateLogger("infoFile");
        }
    }
    if (logger != null) {
        logger.Info(message);
    }
}
 
public static void Error(string message, Exception ex, string name = "") {
    //logger.Error(ex, message);
    ILogger logger = null;
    if (loggers.Keys.Contains(name)) {
        logger = loggers[name];
    }
    else {
        logger = LogFactory.CreateLogger(name);
        if (logger != null) {
            loggers.Add(name, logger);
        }
        else {
            logger = LogFactory.CreateLogger("errorFile");
        }
    }
    if (logger != null) {
        logger.Error($"{message}{ex.StackTrace}");
    }
}
#endregion [自定义类][20250323145442478][LogHelper]
}
public class LogFactory
{
#region    [自定义类][20250323145505759][LogFactory]
     /// <summary>
 /// 通过配置文件配置日志
 /// </summary>
 static LogFactory() {
     var loggerNames = new List<string>() { "HosttoagvTask", "HosttoagvCar", "NDC", "杭奥" };
     LogManager.Configuration = DefaultConfig(loggerNames);
 }
 public static ILogger CreateLogger(string name) {
     var logger = LogManager.GetLogger(name);
     return logger;
 }
 
 public static LoggingConfiguration DefaultConfig(List<string> loggerNames) {
     var config = new LoggingConfiguration();
     loggerNames.ForEach(a => {
         var target = new FileTarget();
         target.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M
         target.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
         target.ArchiveFileName = @"${basedir}/Logs/" + a + "/{####}.txt";
         target.FileName = @"${basedir}/Logs/" + a + "/${shortdate}.txt";//当前文件路径
         target.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}";
 
         config.AddTarget(a, target);
         config.AddRuleForOneLevel(LogLevel.Info, target, a);
     });
 
 
     // 添加target-console
     var consoleTarget = new ColoredConsoleTarget();
     consoleTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}";
 
     config.AddTarget("console", consoleTarget);
     config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget);
 
     //添加target-info
     var infoFileTarget = new FileTarget();
     infoFileTarget.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M
     infoFileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
     infoFileTarget.ArchiveFileName = @"${basedir}/Logs/Info/{####}.txt";
     infoFileTarget.FileName = @"${basedir}/Logs/Info/${shortdate}.txt";//当前文件路径
     infoFileTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}";
 
     config.AddTarget("infoFile", infoFileTarget);
     config.AddRuleForOneLevel(LogLevel.Info, infoFileTarget);//INFO写在Info文件
 
     //添加target-err
     var errorFileTarget = new FileTarget();
     errorFileTarget.ArchiveAboveSize = 1024 * 1024 * 5;//每个文件最大5M
     errorFileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
     errorFileTarget.ArchiveFileName = @"${basedir}/Logs/Error/{####}.txt";
     errorFileTarget.FileName = @"${basedir}/Logs/Error/${shortdate}.txt";
     errorFileTarget.Layout = @"${longdate} | ${level:uppercase=false:padding=-5} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}";
 
     config.AddTarget("errorFile", errorFileTarget);
     config.AddRule(LogLevel.Error, LogLevel.Fatal, errorFileTarget);
 
 
     return config;
 }
 
#endregion [自定义类][20250323145505759][LogFactory]
}
public class HaiKangOrderInfo
{
#region    [自定义类][20250324165635320][HaiKangOrderInfo]
    
            /// <summary>
            /// 请求编号(编号唯一)
            /// </summary>
            public string reqCode { get; set; }
 
            /// <summary>
            /// 请求时间
            /// </summary>
            public string reqTime { get; set; }
 
            /// <summary>
            /// 客户端编号(如PDA、HCWMS等)
            /// </summary>
            public string clientCode { get; set; }
            
            /// <summary>
            /// 令牌号
            /// </summary>
            public string tokenCode { get; set; }
 
            /// <summary>
            /// 任务类型
            /// </summary>
            public string taskTyp { get; set; }
            
            /// <summary>
            /// 容器类型(叉车项目必传)
            /// (叉车/CTU 专用)
            /// </summary>
            public string ctnrTyp { get; set; }
 
            /// <summary>
            /// 容器编号(叉车/CTU 专用)
            /// </summary>
            public string ctnrCode { get; set; }
 
            /// <summary>
            /// 任务模式(0-普通、1-出库、2-入库、3-移库 )
            /// </summary>
            public string taskMode { get; set; }
 
            /// <summary>
            /// 工作位
            /// </summary>
            public string wbCode { get; set; }
            
            /// <summary>
            /// 货架编号
            /// </summary>
            public string podCode { get; set; }
 
            /// <summary>
            /// 方向(180:左、0:右、90:上、-90:下 )
            /// </summary>
            public string podDir { get; set; }
            
            /// <summary>
            /// 货架类型
            /// </summary>
            public string podTyp { get; set; }
            
            /// <summary>
            /// 物料批次或货架上的物料唯一编码
            /// </summary>
            public string materialLot { get; set; }
            
            /// <summary>
            /// 优先级
            /// </summary>
            public string priority { get; set; }
         
            /// <summary>
            /// 组编号
            /// </summary>
            public string groupId { get; set; }
            
            /// <summary>
            /// 自定义字段.JSON 格式
            /// </summary>
            public string data { get; set; }
       
            /// <summary>
            /// 取消类型
            /// </summary>
            public string forceCancel { get; set; }
 
            /// <summary>
            /// 回库区域编码
            /// </summary>
            public string matterArea { get; set; }
            
            /// <summary>
            /// 取消该AGV正在执行的任务单
            /// </summary>
            public string agvCode { get; set; }
            
            /// <summary>
            /// 任务单编号
            /// </summary>
            public string taskCode { get; set; }
 
            /// <summary>
            /// 地码 X 坐标
            /// </summary>
            public string cooX { get; set; }
            
            /// <summary>
            /// 地码 Y 坐标
            /// </summary>
            public string cooY { get; set; }
            
            /// <summary>
            /// 当前位置编号
            /// </summary>
            public string currentPositionCode { get; set; }
            
            /// <summary>
            /// 地图编号
            /// </summary>
            public string mapCode { get; set; }
            
            /// <summary>
            /// 地码编号
            /// </summary>
            public string mapDataCode { get; set; }
            
            /// <summary>
            /// 仓位编号
            /// </summary>
            public string stgBinCode { get; set; }
            
            /// <summary>
            /// 方法名
            /// </summary>
            public string method { get; set; }
            
            /// <summary>
            /// AGV 编号
            /// </summary>
            public string robotCode { get; set; }
            
            
            /// <summary>
            /// 容器类型
            /// </summary>
            public string ctnrType { get; set; }
            
            
            /// <summary>
            /// 巷道编号
            /// </summary>
            public string roadWayCode { get; set; }
            
            /// <summary>
            /// 巷道内顺序号
            /// </summary>
            public string seq { get; set; }
            
            /// <summary>
            /// 设备编号
            /// </summary>
            public string eqpCode { get; set; }
 
            /// <summary>
            /// 任务路径集合
            /// </summary>
            public List<positionCodePath> positionCodePath { get; set; }
        
#endregion [自定义类][20250324165635320][HaiKangOrderInfo]
}
public class positionCodePath
{
#region    [自定义类][20250324165724429][positionCodePath]
    
            /// <summary>
            /// 任务编号
            /// </summary>
            public string positionCode { get; set; }
 
            /// <summary>
            /// 类型
            /// </summary>
            public string Type { get; set; }
#endregion [自定义类][20250324165724429][positionCodePath]
}
public class HkReturnResult
{
#region    [自定义类][20250324171912560][HkReturnResult]
    
            /// <summary>
            /// 返回状态码
            /// </summary>
            public int code { get; set; }
            /// <summary>
            /// 返回说明
            /// </summary>
            public string message { get; set; }
            /// <summary>
            /// 任务号
            /// </summary>
            public string reqCode { get; set; }
#endregion [自定义类][20250324171912560][HkReturnResult]
}
public class deviceInfo
{
#region    [自定义类][20250325083629462][deviceInfo]
    
 
            public string address { get; set; }
            public string deviceName { get; set; }
            public string FuLeLineNo { get; set; }
            public string[] deviceNo { get; set; }
 
            /// <summary>
            ///  对应线体 -优先级
            /// </summary>
            public string[] areaPriy { get; set; }
 
            public string[] location { get; set; }
            /// <summary>
            ///  1 维希尔机械臂
            ///  2 捷瞬机械臂臂
            ///  3 捷瞬输送线
            /// </summary>
            public int deviceType { get; set; }
            public int enable { get; set; }
 
            /// <summary>
            /// 任务权重。 
            /// </summary>
            public int taskPri { get; set; } = 60;
#endregion [自定义类][20250325083629462][deviceInfo]
}
public class HttpHelper
{
#region    [自定义类][20250325095622918][HttpHelper]
    public string Post(string url, string postData, string contentType = "application/json", string sessionId = "") {
    Console.WriteLine(url);
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = contentType;
    request.ContentLength = byteArray.Length;
    request.Timeout = 3000;
    if (sessionId != "") {
        request.Headers.Set("ASP.NET_SessionId", sessionId);
    }
    StreamReader reader = null;
    Stream stream = null;
    WebResponse rsp = null;
    try {
        stream = request.GetRequestStream();
        stream.Write(byteArray, 0, byteArray.Length);
        stream.Close();
        rsp = request.GetResponse();
        stream = rsp.GetResponseStream();
        reader = new StreamReader(stream);
        return reader.ReadToEnd();
    }
    catch (Exception ex) {
        Console.WriteLine($"{url} err={ex.Message}");
        return "";
    }
    finally {
        // 释放资源
        if (reader != null) reader.Close();
        if (stream != null) stream.Close();
        if (rsp != null) rsp.Close();
    }
 
}
#endregion [自定义类][20250325095622918][HttpHelper]
}
public class HKResult
{
#region    [自定义类][20250325095900399][HKResult]
    /// <summary>
/// 返回编码
/// </summary>
public string code { get; set; }
 
/// <summary>
/// 返回消息
/// </summary>
public string message { get; set; }
 
/// <summary>
/// 请求编号
/// </summary>
public string reqCode { get; set; }
 
/// <summary>
/// 自定义返回(返回任务单号)
/// </summary>
public string data { get; set; }
#endregion [自定义类][20250325095900399][HKResult]
}
public class SYSHelper
{
#region    [自定义类][20250325131633664][SYSHelper]
    private static object locker = new object();
internal static int GetSerialNumber(string snType, string prefix, string Vend = "0")
{
    if (Vend == "1")
        Vend = DateTime.Now.ToString("yyMMdd");
    int result = 0;
    lock (locker)
    {
        ISYSRepository sysservice = new SYSRepository();
        var sId = sysservice.FindEntity(a => a.CN_S_TYPE.Trim() == snType && a.CN_S_PRE.Trim() == prefix + Vend);
 
        if (sId != null)
        {
            sId.CN_N_MAX++;
            sId.CN_T_LAST = DateTime.Now;
            sysservice.Update(sId);
            result = sId.CN_N_MAX;
        }
        else
        {
            sId = new SYSEntity { CN_S_TYPE = snType, CN_T_LAST = DateTime.Now, CN_S_PRE = prefix + Vend, CN_N_MAX = 0 };
            sysservice.Insert(sId);
        }
    }
    return result;
}
#endregion [自定义类][20250325131633664][SYSHelper]
}
public class TaskProcess
{
#region    [自定义类][20250325152141671][TaskProcess]
    internal static bool CreateTask(string no, string from, string to, string taskType, int pri, List<string> cntrs, int cntrCount = 1, int startLayer = 1, int endLayer = 1)
{
    var cntrInfo = string.Join(",", cntrs);
    ILocRepository locc = new LocRepository();
    var fromLoc = locc.FindEntity(x=>x.S_LOC_CODE == from);
    var endLoc =locc.FindEntity(x=>x.S_LOC_CODE == to);
 
    TaskEntity wmsTask = new TaskEntity()
    {
        S_TASK_NO = GenerateTaskNo(),
        S_START_LAREA = fromLoc.S_AREA_CODE,
        S_END_LAREA = endLoc.S_AREA_CODE,
        S_DEPART_NAME = "",
        S_START_LOC = from,
        S_END_LOC = to,
        S_TYPE = taskType,
        S_SRC_NO = no,//
        N_PRIORITY = pri,
        S_WORK_MODE = "agv",
        S_B_STATE = "未执行",
        S_CNTRS = cntrInfo,
        N_START_LAYER = startLayer,
        N_END_LAYER = endLayer,
        N_CNTR_COUNT = cntrCount
    };
    ITaskRepository taskserice = new TaskRepository();
    return true;
}
 
 
 
        public static System.String GenerateTaskNo()
        {
            var id = SYSHelper.GetSerialNumber("任务号", "TN", "1");
            var date = DateTime.Now.ToString("yyMMdd");
            return $"TN{date}{id.ToString().PadLeft(4, '0')}";
        }
#endregion [自定义类][20250325152141671][TaskProcess]
}
}