分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-01-16 5884c9023393061afbe6d3d6e709e53e672ddde8
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
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Common;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Entity.ToAGV;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
 
namespace WIDESEA_WMS
{
    public partial class ToAGVServer
    {
        /// <summary>
        /// 修改任务状态
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static AGVRespone UpdateTaskState(string json)
        {
            VOLContext context = new VOLContext();
            Idt_agvtaskRepository repository = new dt_agvtaskRepository(context);
            AGVRespone respone = new AGVRespone();
            UpdateTasteInfo tasteInfo = new();
            try
            {
                if (string.IsNullOrEmpty(json) || json == "null")
                    throw new Exception("未获取到请求参数数据");
                tasteInfo = JsonConvert.DeserializeObject<UpdateTasteInfo>(json);
                if (string.IsNullOrEmpty(tasteInfo.TASK_NO))
                    throw new Exception("未获取到任务号!");
                var task = repository.FindFirst(x => x.agv_tasknum == tasteInfo.TASK_NO);
                if (task == null)
                    throw new Exception("未找到任务,任务ID:" + tasteInfo.TASK_NO);
                if (tasteInfo.TASK_State < 1 || tasteInfo.TASK_State > 4)
                    throw new Exception("未定义的任务状态:" + tasteInfo.TASK_State);
                var StateName = Enum.GetName(typeof(AGVTaskStateEnum), tasteInfo.TASK_State);
                if (task.agv_taskstate == StateName)
                {
                    respone.success = 2;
                    respone.Message = tasteInfo.TASK_NO + ":重复调用,任务状态为:" + tasteInfo.TASK_State;
                    return respone;
                }
                if (tasteInfo.TASK_State == 1)
                    task.agv_executingBeginTime = DateTime.Now;
                if (tasteInfo.TASK_State == 2)
                    task.agv_executingEndTime = DateTime.Now;
                if (tasteInfo.TASK_State == 3)
                    task.agv_completeBeginTime = DateTime.Now;
                task.agv_taskstate = StateName;
                var count = repository.Update(task, true);
                if (count < 1)
                    throw new Exception("任务状态更新失败!任务ID:" + tasteInfo.TASK_NO + ";任务状态:" + tasteInfo.TASK_State);
                if (tasteInfo.TASK_State == 4)
                    Task.AddHtyTask(task);
 
 
            }
            catch (Exception ex)
            {
                respone.success = 3;
                respone.Message = ex.Message;
            }
            WriteWMSLog.LogAdd(tasteInfo.TASK_NO, respone.success == 1 ? "成功 " : "失败", "WMS", "AGV", json, JsonConvert.SerializeObject(respone), "AGV反馈任务状态", "UpdateTaskState", respone.Message);
            return respone;
        }
 
        /// <summary>
        /// 下发AGV任务
        /// </summary>
        /// <param name="agvtask"></param>
        public static void SendAGVTask(dt_agvtask agvtask)
        {
            if (agvtask == null) return;
            VOLContext context = new VOLContext();
            Idt_agvtaskRepository repository = new dt_agvtaskRepository(context);
            AGVRespone respone = new AGVRespone();
            RequestTask requestTask = new RequestTask();
            string postJson = string.Empty;
            string report = string.Empty;
            try
            {
                requestTask.TASK_NO = agvtask.agv_tasknum;
                requestTask.FROM_POSITION = agvtask.agv_fromaddress;
                requestTask.TO_POSITION = agvtask.agv_toaddress;
                requestTask.Material = agvtask.agv_materielid;
                requestTask.Priority = agvtask.agv_grade;
                postJson = JsonConvert.SerializeObject(requestTask);
                report = RequestData(postJson);
                respone = JsonConvert.DeserializeObject<AGVRespone>(report);
                if (respone.success != 1)
                    throw new Exception(respone.Message);
                agvtask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
                agvtask.agv_realesstime = DateTime.Now;
                repository.Update(agvtask, true);
            }
            catch (Exception ex)
            {
                respone.Message = ex.Message;
            }
            WriteWMSLog.LogAdd(requestTask.TASK_NO, respone.success == 1 ? "成功 " : "失败", "AGV", "WMS", postJson, report, "下发AGV任务", "SendAGVTask", respone.Message);
        }
 
        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="postData">参数</param>
        /// <param name="address">路径</param>
        /// <returns></returns>
        public static string RequestData(string postData, string address = "")
        {
            string reponse = string.Empty;
            try
            {
                //string tmp = "";
                //if (null != postData)
                //    tmp = JsonConvert.SerializeObject(postData);
                byte[] param = Encoding.UTF8.GetBytes(postData);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.GetRequestStream().Write(param, 0, param.Length);
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Stream stream = response.GetResponseStream();
                    StreamReader streamReader = new StreamReader(stream);
                    string webResponse = streamReader.ReadToEnd();
                    reponse = webResponse;
                }
            }
            catch (Exception ex)
            {
                reponse = ex.Message;
            }
            return reponse;
        }
    }
}