KTK_Store.cs 17.3 KB
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
using log4net; 
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading;


namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 康泰克单台自动料仓
    /// </summary>
    public abstract class KTK_Store
    {
        /// <summary>
        ///1=设备联机(正常就绪)(入库后,BOX恢复原始状态)(出库后,移载装置恢复原始状态),
        ///2=急停,3=故障,4=警告,5=调试
        ///  6=入库执行中,7=入仓完成,8=入仓失败
        /// 9=出库执行,10=出仓完成,11=出库失败
        /// </summary>
        public StoreStatus storeStatus = StoreStatus.ResetMove;
        /// <summary>
        /// 提示消息,一般发给服务器后清空(LineBean表示报警提示消息,BoxBean表示出入库失败的原因记录)
        /// </summary>
        public string WarnMsg = "";
        public string Name { get; set; }
        public int DeviceID { get; set; }

        public BaseConfig baseConfig;
        /// <summary>
        /// 料仓状态
        /// </summary>
        private StoreRunStatus storerunstatus = StoreRunStatus.Wait;
        /// <summary>
        /// 定时器
        /// </summary>
        protected System.Timers.Timer mainTimer;

        private bool isInit = false;

        public bool IsDebug = false;

        public StoreMoveInfo MoveInfo = null;
        public KTK_Store()
        {
        }
        public DateTime StartTime { get; set; }

        public DateTime lastAirCloseTime = DateTime.Now;
        public bool isInSuddenDown = false;
        public bool isNoAirCheck = false;
        public StoreAlarmType alarmType = StoreAlarmType.None;
        public AlarmInfo alarmInfo = new AlarmInfo();

        ///// <summary>
        ///// 记录上一次的部分IO状态,主要是急停,气压检测信号,复位信号,用来判断是否io发生改变
        ///// </summary>
        //public Dictionary<string, IO_VALUE> DILastValueMap = new Dictionary<string, IO_VALUE>();

        //public object lastDiListLock = "";
        //public void addLastDI(string type, IO_VALUE value)
        //{

        //    try
        //    {
        //        lock (lastDiListLock)
        //        {
        //            if (DILastValueMap.ContainsKey(type))
        //            {
        //                DILastValueMap.Remove(type);
        //            }
        //            DILastValueMap.Add(type, value);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LogUtil.error(ex.ToString());
        //    }
        //}


        public TimeSpan GetStoreRunTime()
        {
            if (StartTime != null)
            {
                return DateTime.Now - StartTime;
            }
            else
            {
                return new TimeSpan(0);
            }
        }
        protected bool isInPro = false;
        protected virtual void BusyMoveProcess()
        {
            //if (isInPro)
            //{
            //    return;
            //}
            isInPro = true;
            try
            {
                switch (MoveInfo.MoveType)
                {
                    case StoreMoveType.InStore:
                        InStoreProcess();
                        isInPro = false;
                        break;
                    case StoreMoveType.OutStore:
                        OutStoreProcess();
                        isInPro = false;
                        break;
                    case StoreMoveType.ReturnHome:
                        ResetProcess();
                        isInPro = false;
                        break;
                    case StoreMoveType.StoreReset:
                        ResetProcess();
                        isInPro = false;
                        break;
                    case StoreMoveType.CheckPosition:
                        InventoryProcess();
                        isInPro = false;
                        break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("BusyMoveProcess出错:" + ex.ToString());
            }
            isInPro = false;
        }
        protected void SaveAlarmInfo(StoreAlarmType alarmType, string alarmDetial, string alarmMsg, StoreMoveType storeMoveType)
        {
            alarmMsg = alarmMsg.Replace(Name, "");
            int inoutStatus = 0;
            if (storeMoveType.Equals(StoreMoveType.InStore))
            {
                inoutStatus = 1;
            }
            else if (storeMoveType.Equals(StoreMoveType.InStore))
            {
                inoutStatus = 2;
            }
            int aType = 0;
            switch (alarmType)
            {
                case StoreAlarmType.AxisMoveError:
                    aType = 2;
                    break;
                case StoreAlarmType.AxisAlarm:
                    aType = 2;
                    break;
                case StoreAlarmType.SuddenStop:
                    aType = 1;
                    alarmDetial = "1";
                    break;
                case StoreAlarmType.NoAirCheck:
                    aType = 1;
                    alarmDetial = "2";
                    break;
                case StoreAlarmType.IoSingleTimeOut:
                    aType = 3;
                    break;
                case StoreAlarmType.StellAlarm:
                    aType = 2;
                    alarmDetial = "5";
                    break;
                default: break;
            }
            alarmInfo = new AlarmInfo(DeviceID, aType, alarmDetial, alarmMsg, inoutStatus);
        }

        /// <summary>
        /// 开始运行
        /// </summary>
        public abstract bool StartRun( );
        /// <summary>
        /// 停止运行
        /// </summary>
        public abstract void StopRun();
        /// <summary>
        /// 报警
        /// </summary>
        /// <param name="alarmType"></param>
        public abstract void Alarm(StoreAlarmType alarmType, string alarmDetial, string alarmMsg, StoreMoveType storeMoveType);
        /// <summary>
        /// 重置(夹料装置状态不变)
        /// </summary>
        public abstract void Reset(bool isNeedClearAuto = true);

        /// <summary>
        /// 停止所有运动
        /// </summary>
        public abstract void StopMove();

        /// <summary>
        /// 重置处理
        /// </summary>
        protected abstract void ResetProcess();
        /// <summary>
        /// 盘点处理
        /// </summary>
        protected virtual void InventoryProcess()
        {
        }

        protected void ACAxisHomeMove(ConfigMoveAxis moveAxis)
        {
            moveAxis.TargetPosition = 0;
            LogUtil.debug(moveAxis.DisplayStr + "speed[" + moveAxis.HomeHighSpeed + "]开始原点返回");
            int org = ACServerManager.instance.GetHomeSingle(moveAxis.DeviceName, moveAxis.GetAxisValue());
            MoveInfo.WaitList.Add(WaitResultInfo.WaitAxisHome(moveAxis, org));
            AxisManager.instance.HomeMove(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed, moveAxis.HomeLowSpeed, moveAxis.HomeAddSpeed);
        }

        protected void ACAxisMove(ConfigMoveAxis moveAxis, int targetPosition, int targetSpeed)
        {
            MoveInfo.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, targetPosition, targetSpeed));
            moveAxis.TargetPosition = targetPosition;
            AxisManager.instance.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition, targetSpeed,moveAxis.AddSpeed,moveAxis.DelSpeed);
        }

        protected bool ACAxisMoveIsEnd(ConfigMoveAxis moveAxis, int targetPosition, int targetSpeed, out string msg)
        {
            msg = "";
            string deviceName = moveAxis.DeviceName;
            short axisNo = moveAxis.GetAxisValue();
            bool countError = false;
            bool IsOk = AxisManager.instance.AbsMoveIsEnd(deviceName, axisNo, targetPosition, moveAxis.CanErrorCountMax, out countError);
            if (IsOk)
            {
                return true;
            }
            if (countError)
            {
                int outCount = AxisManager.instance.GetActualtPosition(deviceName, axisNo);
                //判断是否需要重新运动
                if (MoveInfo.CanWhileCount > 0)
                {
                    LogUtil.error(Name + moveAxis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount +
                     "],误差过大,重新开始运动,剩余[" + MoveInfo.CanWhileCount + "]次");
                    AxisManager.instance.AbsMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetPosition, targetSpeed,moveAxis.AddSpeed,moveAxis.DelSpeed);
                    MoveInfo.CanWhileCount--;
                }
                else
                {
                    msg = Name + " storeMoveStep=" + MoveInfo.MoveStep + moveAxis.DisplayStr + "目标位置[" + targetPosition + "]当前位置[" + outCount
                        + "],误差过大,需要报警";
                    LogUtil.error(msg);
                }
            } 
            return false;
        }
        protected bool ACHomeMoveIsEnd(ConfigMoveAxis moveAxis, out string msg)
        {
            msg = "";
            if (AxisManager.instance.IsHomeMoveEnd(moveAxis.DeviceName, moveAxis.GetAxisValue()))
            {
                //原点完成并且位置=0
                int outCount = AxisManager.instance.GetActualtPosition(moveAxis.DeviceName, moveAxis.GetAxisValue());
                int errorCount = Math.Abs(outCount);
                if (errorCount <= moveAxis.CanErrorCountMax)
                {
                    return true;
                }
                //判断是否需要重新运动
                if (MoveInfo.CanWhileCount > 0)
                {
                    LogUtil.error(Name+  moveAxis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + MoveInfo.CanWhileCount + "]次");
                    //LogUtil.error(  StoreName +  moveAxis.DisplayStr +  "重新回原点");
                    AxisManager.instance.HomeMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed, moveAxis.HomeLowSpeed, moveAxis.HomeAddSpeed);
                    MoveInfo.CanWhileCount--;
                }
                else
                {
                    msg = Name + " storeMoveStep=" + MoveInfo.MoveStep + moveAxis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
                    LogUtil.error(  msg);
                }
            } 
            return false;
        }
 
        protected virtual void Init()
        {
            if (!isInit)
            {
                MoveInfo = new StoreMoveInfo(DeviceID);

                mainTimer = new System.Timers.Timer();
                mainTimer.Enabled = false;
                mainTimer.Interval = 300;
                mainTimer.Elapsed += timersTimer_Elapsed;
                mainTimer.AutoReset = true;
                isInit = true;
            }
        }
        public StoreRunStatus storeRunStatus
        {
            get
            {
                return storerunstatus;
            }
            set
            {
                StoreRunStatus oldStatus = storerunstatus;
                storerunstatus = value;
            }
        }
         
         
        protected abstract void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e);
        #region 出库 
        public abstract bool StartOutStoreMove(InOutParam param);
        protected abstract void OutStoreProcess();
        #endregion

        #region 入库 
        public abstract void StartInStoreMove(InOutParam param);
        protected abstract void InStoreProcess();
        #endregion



        public string GetRunStr()
        {
            string sta = "运行中";
            string aa = "";
            switch (storeRunStatus)
            {
                case StoreRunStatus.Busy:
                    sta = "忙碌";
                    break;
                case StoreRunStatus.HomeMoving:
                    sta = "原点返回";
                    break;
                case StoreRunStatus.Reset:
                    sta = "重置";
                    break;
                case StoreRunStatus.Runing:
                    sta = "运行中";
                    break;
                case StoreRunStatus.Wait:
                    sta = "等待启动";
                    break;
            }
            if (storeRunStatus > StoreRunStatus.Wait)
            {
                //"0":"急停中",   "1":"设备联机",   "2":"故障中", "3":"入库执行中",  "4":"出库执行中",   5":"料盘入仓位完成",   "6":"料盘出仓位完成",  7":"设备调试中",
                switch (storeStatus)
                {
                    case StoreStatus.Debugging:
                        aa = "设备调试中";
                        break;
                    case StoreStatus.InStoreEnd:
                        aa = "料盘入仓位完成";
                        break;
                    case StoreStatus.InStoreExecute:
                        aa = "入库执行中";
                        break;
                    case StoreStatus.InTrouble:
                        aa = "故障中";
                        break;
                    case StoreStatus.OutStoreBoxEnd:
                        aa = "料盘出仓位完成";
                        break;
                    case StoreStatus.OutStoreExecute:
                        aa = "出库执行中";
                        break;
                    case StoreStatus.StoreOnline:
                        aa = "设备联机";
                        break;
                    case StoreStatus.SuddenStop:
                        aa = "急停中";
                        break;
                    case StoreStatus.OutMoveExecute:
                        aa = "出库完成";
                        break;
                    case StoreStatus.InStoreFaild:
                        aa = "入库失败(" + WarnMsg + ")";
                        break;
                    case StoreStatus.OutStoreFaild:
                        aa = "出库失败(" + WarnMsg + ")";
                        break;
                    case StoreStatus.CheckPosMove:
                        aa = "盘点中";
                        break;
                }
                if (!aa.Equals(""))
                {

                    string inout = "";
                    if (MoveInfo.MoveType.Equals(StoreMoveType.InStore)&& MoveInfo.MoveParam!=null&& MoveInfo.MoveParam.PosInfo!=null)
                    {
                        inout = "_入库:"+MoveInfo.MoveParam.PosInfo.ToStr();
                    }
                    else if (MoveInfo.MoveType.Equals(StoreMoveType.OutStore) && MoveInfo.MoveParam != null && MoveInfo.MoveParam.PosInfo != null)
                    {
                        inout = "_出库:" + MoveInfo.MoveParam.PosInfo.ToStr();
                    }
                    else
                    {
                        inout = "   " + MoveInfo.MoveType + "_" + MoveInfo.MoveStep;
                    }
                    return sta + "_" + aa+inout;
                }
                else
                {
                    return sta + "   " + MoveInfo.MoveType + "_" + MoveInfo.MoveStep;
                }
            }
            else
            {
                return sta + "   " + MoveInfo.MoveType + "_" + MoveInfo.MoveStep;
            }
        }


        public string GetMoveStr()
        {
            string msg = ""; 
            msg += "状态: " + storeRunStatus + "\t "+  "  " + storeStatus + "\n";
            msg += "alarm:  " + alarmType + "\n";
            msg += "Move:" + MoveInfo.MoveType + "  " + MoveInfo.MoveStep + "\n"; 
            return msg;
        }

        public void IOMove(string IoType, IO_VALUE value)
        {
            IOManager.IOMove(IoType, value, baseConfig.DeviceID);
        }
        public IO_VALUE IOValue(string IoType)
        {
             return IOManager.IOValue(IoType, baseConfig.DeviceID); 
        }
        protected void SetWarnMsg(string msg)
        {
            if (String.IsNullOrEmpty(WarnMsg).Equals(false))
            {
                if (WarnMsg.Equals(msg))
                {
                    if (msg.StartsWith(Name))
                    {
                        LogUtil.error(msg, 105);
                    }
                    else
                    {
                        LogUtil.error(Name + msg, 105);
                    }
                }
                else
                {
                    if (msg.StartsWith(Name))
                    {
                        LogUtil.error(msg);
                    }
                    else
                    {
                        msg = Name + msg;
                        LogUtil.error(  msg);
                    }
                }
            }

            WarnMsg = msg;
        }
        public  bool IsDoValue(string ioType, IO_VALUE ioValue)
        {
            return IOValue(ioType).Equals(ioValue);
        } 
        public void LogInfo(string logInfo)
        {
            LogUtil.info(Name + logInfo);
        }
    }
}