wanshenmean
2025-04-22 d19b7aa95cd3d4898b87be260d8c8292a2c7eb12
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
using Furion;
using Furion.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.DBNames;
using WIDESEA_Core;
using WIDESEA_Core.SqlSuger;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS
{
    public class JobWorker : ISpareTimeWorker
    {
        private bool _taskScheduler = true;
 
        public static List<dt_equipmentinfo> equipmentinfos = new List<dt_equipmentinfo>();
        public static List<Cache> caches = new List<Cache>();
        public static List<Cache> ZcachesList = new List<Cache>();
        public static List<Cache> FcachesList = new List<Cache>();
 
        /// <summary>
        /// 服务重启
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="count"></param>
 
        [SpareTime(1000, "StartServer", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
        public async Task StartServer(SpareTimer timer, long count)
        {
            if (_taskScheduler)
            {
                //SpareTime.Cancel("StartServer");
                WebResponseContent responseContent = WCSService.ConnectServer();
                if (responseContent.Status)
                {
                    if (WCSService.Clients.Count > 0)
                    {
                        _taskScheduler = false;
                        await Task.CompletedTask;
                        WriteLogHelper.WriteLog("服务开启", $"开启结果:{responseContent.Message}   开启时间:{DateTime.Now.ToString("F")},服务开启");
                        SpareTime.Cancel("StartServer");
                    }
                }
                else
                {
                    WriteLogHelper.WriteLog("服务开启", $"开启结果:{responseContent.Message}   开启时间:{DateTime.Now.ToString("F")},服务开启");
                }
            }
        }
 
        /// <summary>
        /// 每五秒执行
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="count"></param>
        [SpareTime(5000, "jobName", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
        public async void DoSomething(SpareTimer timer, long count)
        {
            PLCClient client = null;
            caches = new List<Cache>();
 
            List<Cache> cachess = new List<Cache>();
            if (WCSService.Clients != null)
            {
                if (WCSService.Clients.Count > 0)
                {
                    client = WCSService.Clients[0];
                    if (client.ConnectionMessage == "拆包间")
                    {
                        int x = client.PLCDBItems.Select(x => x.EquipNum).Select(int.Parse).ToList().Max();
                        for (int i = 1; i < x + 1; i++)
                        {
                            string state = client.ReadValue(AGVDBName.R_AGV_CacheState.ToString() + i, i.ToString()).ToString();
                            Cache cache = new Cache()
                            {
                                cacheNo = i,
                                cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架",
                                cacheType = client.ReadValue(AGVDBName.R_AGV_CacheType.ToString() + i, i.ToString()).ToString(),
                            };
                            if (cache.cacheState != "无此缓存架")
                            {
                                caches.Add(cache);
                            }
                            if (cache.cacheState == "无货")
                            {
                                cachess.Add(cache);
                            }
                        }
                        //ChatHub chatHub = new ChatHub();
                        ChatHub chatHub = App.GetService<ChatHub>();
                        await chatHub.SendMessage("ReciveMessage", (caches));
                        await chatHub.SendMessage("SendError", (cachess));
                    }
                    else
                    {
                        SpareTime.Cancel("jobName");
                    }
                }
            }
        }
 
        /// <summary>
        /// 每五秒执行 库存
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="count"></param>
        [SpareTime(5000, "jobName1", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
        public async void SendContainer(SpareTimer timer, long count)
        {
            PLCClient client = null;
            List<Container> containers = new List<Container>();
 
            if (WCSService.Clients != null)
            {
                if (WCSService.Clients.Count > 0)
                {
                    client = WCSService.Clients[0];
                    if (client.ConnectionMessage == "拆包间")
                    {
                        containers = SqlSugarHelper.WCSDB.Queryable<Container>().ToList();
                        ChatHub chatHub = App.GetService<ChatHub>();
                        await chatHub.SendMessage("SendContainer", (containers));
                    }
                    else
                    {
                        SpareTime.Cancel("jobName1");
                    }
                }
            }
        }
 
        /// <summary>
        /// 每五秒执行 投料间
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="count"></param>
        [SpareTime(5000, "SendFeedingData", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
        public async void SendFeedingData(SpareTimer timer, long count)
        {
            PLCClient client = null;
 
            if (WCSService.Clients != null)
            {
                if (WCSService.Clients.Count > 0)
                {
                    client = WCSService.Clients[0];
                    List<Cache> Zcachess = new List<Cache>();
                    List<Cache> Zcaches = new List<Cache>();
                    List<Cache> Fcachess = new List<Cache>();
                    List<Cache> Fcaches = new List<Cache>();
                    if (client.ConnectionMessage == "投料间")
                    {
                        ChatHub chatHub = App.GetService<ChatHub>();
                        int xz = client.PLCDBItems.Where(x => x.ItemName.Contains(AGVDBName.R_AGV_CacheStateZ.ToString())).Select(x => x.EquipNum).Select(int.Parse).ToList().Max();
                        int xf = client.PLCDBItems.Where(x => x.ItemName.Contains(AGVDBName.R_AGV_CacheStateF.ToString())).Select(x => x.EquipNum).Select(int.Parse).ToList().Max();
                        for (int i = 1; i < xz + 1; i++)
                        {
                            string state = client.ReadValue(AGVDBName.R_AGV_CacheStateZ.ToString() + i, i.ToString()).ToString();
                            Cache cache = new Cache()
                            {
                                cacheNo = i,
                                cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架",
                                //cacheState = "无货",
                                cacheType = client.ReadValue(AGVDBName.R_AGV_CacheTypeZ.ToString() + i, i.ToString()).ToString(),
                            };
                            if (cache.cacheState != "无此缓存架")
                            {
                                Zcaches.Add(cache);
                            }
                            if (cache.cacheState == "无货")
                            {
                                Zcachess.Add(cache);
                            }
                        }
                        await chatHub.SendMessage("SendZFeedingData", Zcaches);
                        await chatHub.SendMessage("SendZError", Zcachess);
                        ZcachesList = Zcachess;
                        for (int i = 1; i < xf + 1; i++)
                        {
                            string state = client.ReadValue(AGVDBName.R_AGV_CacheStateF.ToString() + i, i.ToString()).ToString();
                            Cache cache = new Cache()
                            {
                                cacheNo = i,
                                cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架",
                                cacheType = client.ReadValue(AGVDBName.R_AGV_CacheTypeF.ToString() + i, i.ToString()).ToString(),
                            };
                            if (cache.cacheState != "无此缓存架")
                            {
                                Fcaches.Add(cache);
                            }
                            if (cache.cacheState == "无货")
                            {
                                Fcachess.Add(cache);
                            }
                        }
                        await chatHub.SendMessage("SendFFeedingData", Fcaches);
                        await chatHub.SendMessage("SendFError", Fcachess);
                        FcachesList = Fcachess;
                    }
                    else
                    {
                        SpareTime.Cancel("SendFeedingData");
                    }
                }
            }
        }
    }
}