/// <summary>
        ///  获取设备状况
        /// </summary>
        /// <param name="categoryId">设备类型id</param>
        /// <param name="deviceId">设备id</param>
        /// <returns></returns>
        public static SmartDeviceOperateState GetDeviceState(int categoryId, int deviceId)
        {
            var deviceKey = GetDeviceKey(categoryId);
            var devices   = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);

            return(devices.FirstOrDefault(x => x.Id == deviceId)?.State ?? SmartDeviceOperateState.缺失);
        }
        /// <summary>
        /// 00 201007 0000
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="time">时间</param>
        /// <param name="suffix">后缀</param>
        /// <returns></returns>
        private static IEnumerable <string> GenFlowCard(int count, DateTime time, string suffix)
        {
            var redisKey = $"{RedisPre}:{CodeType.流程卡}";
            var str      = new List <string>();
            var ws       = Stopwatch.StartNew();

            while (ws.ElapsedMilliseconds < 10 * 1000)
            {
                if (!RedisHelper.SetIfNotExist(redisKey, DateTime.Today.ToStr()))
                {
                    continue;
                }

                var startKey   = $"{RedisPre}:{DateTime.Today.ToDateStr()}-{CodeType.流程卡}";
                var startValue = RedisHelper.Get <int>(startKey);
                for (var i = 0; i < count; i++)
                {
                    str.Add($"{((int)CodeType.流程卡):D2}{DateTime.Today.ToStrShort()}{startValue++:D4}{suffix}");
                }
                RedisHelper.SetForever(startKey, startValue);
                RedisHelper.Remove(redisKey);
                break;
            }
            return(str);
        }
        public static void InitProcessor()
        {
            var users         = AccountInfoHelper.Instance.GetAll <AccountInfo>();
            var processorList = RedisHelper.Get <List <SmartProcessor> >(ProcessorKey);

            if (processorList == null)
            {
                _processors.AddRange(users.Select(x => new SmartProcessor
                {
                    Id = x.Id
                }));
            }
            else
            {
                var delete = _processors.Where(x => users.All(y => y.Id != x.Id));
                _processors.AddRange(processorList.Where(x => delete.All(y => y.Id != x.Id)).Select(z => new SmartProcessor
                {
                    Id = z.Id
                }));
            }

            var add = users.Where(x => _processors.All(y => y.Id != x.Id)).Select(z => new SmartProcessor
            {
                Id = z.Id
            });

            _processors.AddRange(add);
            UpdateProcessors(_processors);
        }
        public static IEnumerable <SmartProcessDevice> Devices()
        {
            var devices     = new List <SmartProcessDevice>();
            var categories  = SmartDeviceCategoryHelper.Instance.GetAll <SmartDeviceCategory>();
            var categoryIds = categories.GroupBy(x => x.Id).Select(y => y.Key);

            foreach (var categoryId in categoryIds)
            {
                var deviceKey      = GetDeviceKey(categoryId);
                var processDevices = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);
                devices.AddRange(processDevices);
            }
            return(devices);
        }
 private static void NeedArrange(object obj)
 {
     if (RedisHelper.SetIfNotExist(DateLock, ServerConfig.GetLockValue))
     {
         RedisHelper.SetExpireAt(DateLock, DateTime.Now.AddMinutes(5));
         var last = RedisHelper.Get <DateTime>(DateKey);
         var now  = DateTime.Now;
         if (last == default(DateTime) || last < now.Date)
         {
             var tasks    = new List <SmartTaskOrderConfirm>();
             var schedule = new List <SmartTaskOrderScheduleDetail>();
             //ArrangeSchedule(ref tasks, ref schedule, true);
             RedisHelper.SetForever(DateKey, now);
         }
     }
 }
        private static void Do()
        {
            while (true)
            {
#if !DEBUG
                if (RedisHelper.Get <int>("StateKey") != 1)
                {
                    return;
                }
#endif
                Simulate();

                var categories        = SmartDeviceCategoryHelper.Instance.GetAll <SmartDeviceCategory>();
                var releaseProcessors = new List <int>();
                foreach (var category in categories)
                {
                    while (true)
                    {
                        if (LockDevice(category.Id, "加工"))
                        {
                            var deviceKey      = GetDeviceKey(category.Id);
                            var processDevices = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);
                            foreach (var device in processDevices)
                            {
                                device.StartNextProcess();
                                if (device.CompleteThisProcess(out var processorId) && processorId != 0)
                                {
                                    releaseProcessors.Add(processorId);
                                }
                                device.ReadyDone();
                            }
                            UpdateDevices(category.Id, processDevices);
                            UnLockDevice(category.Id);
                            break;
                        }
                    }
                }

                if (releaseProcessors.Any())
                {
                    ReleaseProcess(releaseProcessors);
                }

                Thread.Sleep(5000);
            }
        }
        public static void InitDevice()
        {
            var devices     = new List <SmartProcessDevice>();
            var deviceList  = SmartDeviceHelper.Instance.GetAll <SmartDevice>();
            var categoryIds = deviceList.GroupBy(x => x.CategoryId).Select(y => y.Key);

            foreach (var categoryId in categoryIds)
            {
                devices.Clear();
                var deviceKey       = GetDeviceKey(categoryId);
                var processDevices  = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);
                var categoryDevices = deviceList.Where(x => x.CategoryId == categoryId);
                if (processDevices == null)
                {
                    devices.AddRange(categoryDevices.Select(x => new SmartProcessDevice
                    {
                        Id         = x.Id,
                        CategoryId = categoryId,
                    }));
                    UpdateDevices(categoryId, devices);
                }
                else
                {
                    if (categoryDevices.Count() != processDevices.Count ||
                        categoryDevices.Any(x => processDevices.All(y => y.Id != x.Id)) ||
                        processDevices.Any(x => categoryDevices.All(y => y.Id != x.Id)))
                    {
                        //删除
                        var delete = processDevices.Where(x => categoryDevices.All(y => y.Id != x.Id)).ToList();
                        //添加除删除外的
                        devices.AddRange(processDevices.Where(x => delete.Any(y => y.Id == x.Id)));
                        //添加
                        var add = categoryDevices.Where(x => processDevices.All(y => y.Id != x.Id)).ToList();
                        devices.AddRange(add.Select(x => new SmartProcessDevice
                        {
                            Id         = x.Id,
                            CategoryId = categoryId,
                        }));
                        UpdateDevices(categoryId, devices);
                    }
                }
            }
        }
        ///// <summary>
        ///// 安排设备和加工人
        ///// </summary>
        ///// <param name="deviceCategoryId">设备类型</param>
        ///// <param name="flowCardProcessId">流程卡流程id</param>
        ///// <param name="processNumber">单次加工数量</param>
        ///// <param name="processCount">总加工次数</param>
        ///// <param name="totalSecond">总时间</param>
        ///// <param name="deviceId"></param>
        ///// <param name="processorId"></param>
        ///// <returns></returns>
        //public ScheduleState Arrange(int deviceCategoryId, int flowCardProcessId, int processNumber, int processCount, int totalSecond, out int deviceId, ref int processorId)
        //{
        //    var state = 成功;
        //    deviceId = 0;
        //    if (deviceCategoryId == 0)
        //    {
        //        processorId = 0;
        //        return state;
        //    }

        //    while (true)
        //    {
        //        if (!LockProcessor($"安排加工人1-{flowCardProcessId}"))
        //        {
        //            continue;
        //        }

        //        var processors = RedisHelper.Get<List<SmartProcessor>>(ProcessorKey);
        //        if (!processors.Any())
        //        {
        //            state = 缺少工人;
        //            UnLockProcessor();
        //            break;
        //        }
        //        var users = SmartUserHelper.Instance.GetAll<SmartUser>();
        //        SmartProcessor processor;
        //        if (processorId == 0)
        //        {
        //            var 闲置加工人 = processors.OrderBy(a => a.TotalCount).ThenBy(b => b.Id).Where(x => x.Count < (users.FirstOrDefault(y => y.Id == x.Id)?.ProcessCount ?? 0));
        //            if (闲置加工人.Any())
        //            {
        //                processor = 闲置加工人.First();
        //                processorId = processor.Id;
        //            }
        //            else
        //            {
        //                state = 工人繁忙;
        //                UnLockProcessor();
        //                break;
        //            }

        //        }
        //        else
        //        {
        //            var pid = processorId;
        //            processor = processors.FirstOrDefault(x => x.Id == pid);
        //            if (processor == null)
        //            {
        //                state = 缺少工人;
        //                UnLockProcessor();
        //                break;
        //            }
        //        }

        //        while (true)
        //        {
        //            if (!LockDevice(deviceCategoryId, $"安排设备1-{flowCardProcessId}"))
        //            {
        //                continue;
        //            }

        //            var deviceKey = GetDeviceKey(deviceCategoryId);
        //            var devices = RedisHelper.Get<List<SmartProcessDevice>>(deviceKey);
        //            if (!devices.Any())
        //            {
        //                state = 缺少设备;
        //                UnLockDevice(deviceCategoryId);
        //                UnLockProcessor();
        //                break;
        //            }
        //            var 闲置设备 = devices.Where(x => x.CategoryId == deviceCategoryId && x.State == SmartDeviceState.未加工)
        //                                .OrderBy(a => a.FinalEndTime)
        //                                .ThenBy(b => b.NextProcesses.Count)
        //                                .ThenBy(c => c.Id);
        //            if (闲置设备.Any())
        //            {
        //                var device = 闲置设备.First();
        //                deviceId = device.Id;
        //                if (!device.NextProcesses.Any(x => x.Item1 == flowCardProcessId && x.Item2 == processor.Id))
        //                {
        //                    device.NextProcesses.Add(new Tuple<int, int, int, int, int>(flowCardProcessId, processorId, processNumber, processCount, totalSecond));
        //                    processor.Count++;
        //                    processor.TotalCount++;
        //                }
        //            }

        //            if (deviceId == 0)
        //            {
        //                var 准备中设备 = devices.Where(x => x.CategoryId == deviceCategoryId && x.State == SmartDeviceState.准备中);
        //                if (准备中设备.Any())
        //                {
        //                    var device = 准备中设备.First();
        //                    deviceId = device.Id;
        //                    if (!device.NextProcesses.Any(x => x.Item1 == flowCardProcessId && x.Item2 == processor.Id))
        //                    {
        //                        device.NextProcesses.Add(new Tuple<int, int, int, int, int>(flowCardProcessId, processorId, processNumber, processCount, totalSecond));
        //                        processor.Count++;
        //                        processor.TotalCount++;
        //                    }
        //                }
        //            }

        //            if (deviceId == 0)
        //            {
        //                state = 设备繁忙;
        //            }
        //            else
        //            {
        //                UpdateDevices(deviceCategoryId, devices);
        //                UpdateProcessors(processors);
        //            }
        //            break;
        //        }
        //        UnLockDevice(deviceCategoryId);
        //        UnLockProcessor();
        //        break;
        //    }
        //    return state;
        //}

        /// <summary>
        /// 更新设备状态
        /// </summary>
        /// <param name="deviceId">设备id</param>
        /// <param name="deviceCategoryId">设备类型id</param>
        /// <param name="state"></param>
        /// <returns></returns>
        private static void UpdateDeviceState(int deviceId, int deviceCategoryId, SmartDeviceOperateState state)
        {
            while (LockDevice(deviceCategoryId, $"更新设备状态-{state}"))
            {
                var deviceKey = GetDeviceKey(deviceCategoryId);
                var devices   = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);
                var device    = devices.FirstOrDefault(x => x.Id == deviceId && x.State != state);
                if (device != null)
                {
                    device.State = state;
                    if (state == SmartDeviceOperateState.故障中)
                    {
                        if (device.BreakThisProcess(out var processorId))
                        {
                            UpdateDevices(deviceCategoryId, devices);
                            ReleaseProcess(processorId);
                        }
                    }
                }
                UnLockDevice(deviceCategoryId);
            }
        }
        ///// <summary>
        ///// 释放加工人
        ///// </summary>
        ///// <returns></returns>
        //public void ReleaseProcess(IEnumerable<int> processorIds)
        //{
        //    while (true)
        //    {
        //        if (!LockProcessor($"释放加工人-{processorIds.Join()}"))
        //        {
        //            continue;
        //        }

        //        var processors = RedisHelper.Get<List<SmartProcessor>>(ProcessorKey);
        //        foreach (var processorId in processorIds)
        //        {
        //            var processor = processors.FirstOrDefault(x => x.Id == processorId);
        //            if (processor != null)
        //            {
        //                processor.Count--;
        //            }
        //        }
        //        UpdateProcessors(processors);
        //        UnLockProcessor();
        //        break;
        //    }
        //}

        /// <summary>
        /// 安排设备和加工人
        /// </summary>
        /// <param name="deviceCategoryId">设备类型</param>
        /// <param name="flowCardProcessId">流程卡流程id</param>
        /// <param name="processNumber">单次加工数量</param>
        /// <param name="processCount">总加工次数</param>
        /// <param name="totalSecond">总时间</param>
        /// <param name="deviceId"></param>
        /// <param name="processorId"></param>
        /// <returns></returns>
        private static ScheduleState Arrange(int deviceCategoryId, int flowCardProcessId, int processNumber, int processCount, int totalSecond, out int deviceId, ref int processorId)
        {
            var state = ScheduleState.成功;

            deviceId = 0;
            if (deviceCategoryId == 0)
            {
                var i     = RandomSeed.Next(0, _processors.Count);
                var 闲置加工人 = _processors.ElementAt(i);
                processorId = 闲置加工人.Id;
                return(state);
            }

            while (true)
            {
                if (!_processors.Any())
                {
                    state = ScheduleState.缺少工人;
                    break;
                }
                SmartProcessor processor;
                if (processorId == 0)
                {
                    var i     = RandomSeed.Next(0, _processors.Count);
                    var 闲置加工人 = _processors.ElementAt(i);
                    processor = 闲置加工人;
                }
                else
                {
                    var pid = processorId;
                    processor = _processors.FirstOrDefault(x => x.Id == pid);
                    if (processor == null)
                    {
                        state = ScheduleState.缺少工人;
                        break;
                    }
                }

                while (true)
                {
                    if (!LockDevice(deviceCategoryId, $"安排设备1-{flowCardProcessId}"))
                    {
                        continue;
                    }

                    var deviceKey = GetDeviceKey(deviceCategoryId);
                    var devices   = RedisHelper.Get <List <SmartProcessDevice> >(deviceKey);
                    if (!devices.Any())
                    {
                        state = ScheduleState.缺少设备;
                        UnLockDevice(deviceCategoryId);
                        break;
                    }
                    var 闲置设备 = devices.Where(x => x.CategoryId == deviceCategoryId && x.State == SmartDeviceOperateState.未加工)
                               .OrderBy(a => a.FinalEndTime)
                               .ThenBy(b => b.NextProcesses.Count)
                               .ThenBy(c => c.Id);
                    if (闲置设备.Any())
                    {
                        var device = 闲置设备.First();
                        deviceId = device.Id;
                        if (!device.NextProcesses.Any(x => x.Item1 == flowCardProcessId && x.Item2 == processor.Id))
                        {
                            device.NextProcesses.Add(new Tuple <int, int, int, int, int>(flowCardProcessId, processorId, processNumber, processCount, totalSecond));
                            processor.Count++;
                            processor.TotalCount++;
                        }
                    }

                    if (deviceId == 0)
                    {
                        var 准备中设备 = devices.Where(x => x.CategoryId == deviceCategoryId && x.State == SmartDeviceOperateState.准备中);
                        if (准备中设备.Any())
                        {
                            var device = 准备中设备.First();
                            deviceId = device.Id;
                            if (!device.NextProcesses.Any(x => x.Item1 == flowCardProcessId && x.Item2 == processor.Id))
                            {
                                device.NextProcesses.Add(new Tuple <int, int, int, int, int>(flowCardProcessId, processorId, processNumber, processCount, totalSecond));
                                processor.Count++;
                                processor.TotalCount++;
                            }
                        }
                    }

                    if (deviceId == 0)
                    {
                        state = ScheduleState.设备繁忙;
                    }
                    else
                    {
                        UpdateDevices(deviceCategoryId, devices);
                    }
                    break;
                }
                UnLockDevice(deviceCategoryId);
                break;
            }
            return(state);
        }
Example #10
0
 /// <summary>
 /// 获取钉钉部门用户缓存
 /// </summary>
 private static DingTalkUserList GetDingTalkUserListCache(string key)
 {
     return(RedisHelper.Get <string>(key)?.ToClass <DingTalkUserList>());
 }
Example #11
0
 /// <summary>
 /// 获取钉钉access_token缓存
 /// </summary>
 private static DingTalkAccessToken GetDingTalkAccessTokenCache(string key)
 {
     return(RedisHelper.Get <string>(key)?.ToClass <DingTalkAccessToken>());
 }
Example #12
0
 /// <summary>
 /// 获取微信部门用户缓存
 /// </summary>
 private static WeiXinUserList GetWeiXinUserListCache(string key)
 {
     return(RedisHelper.Get <string>(key)?.ToClass <WeiXinUserList>());
 }
Example #13
0
 /// <summary>
 /// 获取微信access_token缓存
 /// </summary>
 private static WeiXinAccessToken GetWeiXinAccessTokenCache(string key)
 {
     return(RedisHelper.Get <string>(key)?.ToClass <WeiXinAccessToken>());
 }