DeviceControl.cs 14.2 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
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
 
using System.Timers;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    ///设备管理(读写Io的操作类)
    /// </summary>
    public class DeviceControl
    {    /// <summary>
        /// 出库时,可以不下降直接放料盘的宽度
        /// </summary>
        public const int CanPushBagWidth = 7;

        private static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
          
        /// <summary>
        /// 最后一次接受到的温度信息,key=温湿度传感器IP地址
        /// </summary>
        public static Dictionary<string, ASTemperateParam> TemperateParamMap = new Dictionary<string, ASTemperateParam>();
        /// <summary>
        /// 料仓出入库状态 ,=high表示入库,low表示出库
        /// </summary>
        public static IO_VALUE InStoreType;
        /// <summary>
        /// 放在公共地方,方便存取,夹料编码盘列表,key=编码(1-6),value=是否有物品,true=有物品
        /// </summary>
        private static Dictionary<int, FixtureInfo> fixtureCodeFullMap = new Dictionary<int, FixtureInfo>();
        private static object fixtureMapLock = "";
        /// <summary>
        /// 流水线需要的空盘数量
        /// </summary>
        public static int LineNeedEmptyTrayNum=0;
        /// <summary>
        /// 最大托盘号,必须按照顺序从1到6走过
        /// </summary>
        public static int MaxTrayNum = 0;

        public static string TrayErrorMsg = "";
        /// <summary>
        /// 盘错乱的StoreID
        /// </summary>
        public static int ErrorStoreId = -1;
       
        /// <summary>
        /// 是否需要连接服务器
        /// </summary>
        public static bool IsConnectServer = true;
        public static Boolean isNoDeviceDebug = false;
        internal static bool isNeedEmptyTray()
        {
            return LineNeedEmptyTrayNum > 0;
        }
        public static void AddNeedEmptyTrayNum()
        {
            Interlocked.Increment(ref LineNeedEmptyTrayNum);
        }

        public static void DelNeedEmptyTrayNum()
        {
            Interlocked.Decrement(ref LineNeedEmptyTrayNum);
        } 

        public static ASTemperateParam GetTemperateParam(string ipAddr)
        {
            ASTemperateParam temperate = null;
            TemperateParamMap.TryGetValue(ipAddr, out temperate);
            return temperate;
        }
        /// <summary>
        /// 对应的盘号(1-6)是否有料盘
        /// </summary>
        /// <param name="codeNum">盘号1-6</param>
        /// <returns>盘上是否有料盘</returns>
        public static bool IsFixTureFull(int codeNum)
        {
            lock (fixtureMapLock)
            {
                if (fixtureCodeFullMap.ContainsKey(codeNum))
                {
                    return fixtureCodeFullMap[codeNum].IsFull;
                }
            }
            return false;
        }
        /// <summary>
        /// 托盘是否可以累积扔盘,判断是出库的托盘,并且所放的托盘为七寸
        /// </summary> 
        public static bool IsCanOutPush(int codeNum)
        {
            lock (fixtureMapLock)
            {
                if (fixtureCodeFullMap.ContainsKey(codeNum))
                {
                    FixtureInfo info=fixtureCodeFullMap[codeNum];
                    if (info.IsFull == false)
                    {
                        return true;
                    }
                    if (info.InOrOutStore.Equals(2) && info.BagWidth.Equals(CanPushBagWidth))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public static void UpdateFixtureValue(int codeNum, bool isFull, int inOrOut)
        {
            UpdateFixtureValue(codeNum, isFull, inOrOut, 0);
        }
        /// <summary>
        /// 更新对应的盘号(1-6)是否有料盘
        /// </summary>
        /// <param name="codeNum"></param>
        /// <param name="isFull"></param>
        /// <param name="inOrOut">0=无操作,1=入库,2=出库</param>
        public static void UpdateFixtureValue(int codeNum, bool isFull,int inOrOut,int bagWidth)
        {
            lock (fixtureMapLock)
            {
                if (fixtureCodeFullMap.ContainsKey(codeNum))
                {
                    fixtureCodeFullMap[codeNum].IsFull = isFull;
                    fixtureCodeFullMap[codeNum].InOrOutStore = inOrOut;
                    fixtureCodeFullMap[codeNum].BagWidth = bagWidth;
                }
                else
                {
                    FixtureInfo fixture = new FixtureInfo(codeNum, isFull, inOrOut,bagWidth);
                    fixtureCodeFullMap.Add(codeNum, fixture);
                }
            }
        }
        /// <summary>
        /// 是否还有有料仓的盘
        /// </summary> 
        public static bool IsHasFull()
        {
            foreach (int key   in fixtureCodeFullMap.Keys)
            {
                if (fixtureCodeFullMap[key].IsFull)
                {
                    return true; 
                }
            }
            return false;
        }  
        /// <summary>
        /// 是否还有有料仓正在出库的盘
        /// </summary> 
        public static bool IsHasFullOutFixture()
        {
            foreach (int key in fixtureCodeFullMap.Keys)
            {
                if (fixtureCodeFullMap[key].IsFull && fixtureCodeFullMap[key].InOrOutStore.Equals(2))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 初始化设备列表
        /// </summary> 
        public static void InitDevice(List<string> SMCDeviceNameList, List<string> DIODeviceNameList)
        {
            foreach (string deviceName in DIODeviceNameList)
            {
                KTKDIOManager.DIOInit(deviceName);
            }

            foreach (string deviceName in SMCDeviceNameList)
            {
                KTKSMCManager.WInit(deviceName);
            } 
        }
        /// <summary>
        /// 注销设备列表
        /// </summary> 
        public static void ExitDevice(List<string> SMCDeviceNameList, List<string> DIODeviceNameList)
        {
            foreach (string deviceName in SMCDeviceNameList)
            {
                KTKSMCManager.WExit(deviceName);
            } foreach (string deviceName in DIODeviceNameList)
            {
                KTKDIOManager.DIOExit(deviceName);
            }
        }
    
        /// <summary>
        /// 写入单个扩展io值
        /// </summary> 
        public static int WriteSingleIO(ConfigIO io, IO_VALUE value)
        {
            if (io.GetIOValue() < 0)
            {
                LOGGER.Debug("WriteSingleIO中io=" + io.ToString() + ",直接返回不写入!");
                return 0;
            }
            if (io.ProType.Equals(ConfigItemType.DO))
            {
              //  if (GetSingleIO(io) != value)
                {
                    DateTime time = DateTime.Now;
                    int res= KTKDIOManager.WriteSingDO(io.DeviceName, (short)io.GetIOValue(), value);
                    TimeSpan span = DateTime.Now - time;
                    if (span.TotalMilliseconds > 10)
                    {
                        LOGGER.Info("写入IO【" + io.DisplayStr + "】的值,共耗费了【" + span.TotalMilliseconds + "】毫秒");
                    }
                    return res;
                }
            }
            else
            {
                LogUtil.error(LOGGER, "试图写IO=" + io + "的值,此IO只读不能写入!");
            }
            return 0;
        }
        /// <summary>
        /// 写入单个扩展io值
        /// </summary> 
        public static int WriteSingleIO(string deviceName, int index, IO_VALUE value)
        {
            if (index < 0)
            {
                LOGGER.Debug("WriteSingleIO中index=" + index + ",直接返回不写入!");
                return 0;
            }
            return KTKDIOManager.WriteSingDO(deviceName, (short)index, value);
        }

        public static int WriteSingleIO(string deviceName, int index, IO_VALUE value, int mSecond)
        {
            if (index < 0)
            {
                LOGGER.Debug("WriteSingleIO中index=" + index + ",直接返回不写入!");
                return 0;
            }
            LogUtil.debug(LOGGER, "**********定时写入io=" + deviceName + index + ",value=" + value);
            WriteSingleIO(deviceName, index, value);

            IO_VALUE newValue = IO_VALUE.LOW;
            if (value == IO_VALUE.LOW)
            {
                newValue = IO_VALUE.HIGH;
            }

            System.Timers.Timer mytimer = new System.Timers.Timer(mSecond);
            mytimer.Elapsed += (o1, e1) =>
            {
                try
                {
                    WriteSingleIO(deviceName, index, newValue);
                }
                catch (Exception ex)
                {
                    LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
                }
            };
            mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
            mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
            return 0;
        }

        /// <summary>
        /// 写入单个扩展io值
        /// </summary> 
        public static int WriteSingleIO(ConfigIO io, IO_VALUE value, int mSecond)
        {
            if (io.GetIOValue() < 0)
            {
                LOGGER.Debug("WriteSingleIO中io=" + io.ToString() + ",直接返回不写入!");
                return 0;
            }
            LogUtil.debug(LOGGER, "**********定时写入io=" + io + ",value=" + value);
            WriteSingleIO(io, value);

            IO_VALUE newValue = IO_VALUE.LOW;
            if (value == IO_VALUE.LOW)
            {
                newValue = IO_VALUE.HIGH;
            }

            System.Timers.Timer mytimer = new System.Timers.Timer(mSecond);
            mytimer.Elapsed += (o1, e1) =>
            {
                try
                {
                    WriteSingleIO(io, newValue);
                    LogUtil.debug(LOGGER, "**********定时回写入io=" + io + ",newValue=" + newValue);
                }
                catch (Exception ex)
                {
                    LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
                }
            };
            mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
            mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
            return 0;
        }

        /// <summary>
        /// 获取单个io的值
        /// </summary> 
        public static IO_VALUE GetSingleIO(ConfigIO io)
        {
            int ioStatus = 0;
            if (io.GetIOValue() < 0)
            {
                LOGGER.Debug("GetSingleIO中io=" + io.ToString() + ", 直接返回不读取!");
                return (IO_VALUE)ioStatus;
            }
            if (io.ProType.Equals(ConfigItemType.DO))
            {
                DateTime time = DateTime.Now;
                ioStatus = KTKDIOManager.ReadSingDOValue(io.DeviceName, (short)io.GetIOValue());
                TimeSpan span = DateTime.Now - time;
                if (span.TotalMilliseconds > 10)
                {
                    LOGGER.Info("获取DO【" + io.DisplayStr + "】的值,共耗费了【" + span.TotalMilliseconds + "】毫秒");
                }
            }
            else
            {
                DateTime time = DateTime.Now;
                ioStatus = KTKDIOManager.ReadSingDIValue(io.DeviceName, (short)io.GetIOValue()); 
                TimeSpan span = DateTime.Now - time;
                if (span.TotalMilliseconds > 10)
                {
                    LOGGER.Info("获取DI【" + io.DisplayStr + "】的值,共耗费了【" + span.TotalMilliseconds + "】毫秒");
                }
            }
            return (IO_VALUE)ioStatus;
        }
        /// <summary>
        /// 获取阻挡气缸检测类型
        /// </summary>
        /// <returns></returns>
        public static string GetStopIoType(int stopCylinderIOType)
        {
            string type = Store_IO_Type.Fixture_Check_1;
            switch (stopCylinderIOType)
            {
                case 1:
                    type = Store_IO_Type.Fixture_Check_1;
                    break;
                case 2:
                    type = Store_IO_Type.Fixture_Check_2;
                    break;
                case 3:
                    type = Store_IO_Type.Fixture_Check_3;
                    break;
                case 4:
                    type = Store_IO_Type.Fixture_Check_4;
                    break;
            }
            return type;
        }
        /// <summary>
        /// 验证托盘号是否正确
        /// </summary> 
        public static bool RightTrayCode(int trayNum, int preTrayNum,bool isCanUpdateMax)
        {
            if (preTrayNum == 0)
            {
                return true;
            }
            else if (trayNum == preTrayNum + 1)
            {
                return true;
            }
            else if (DeviceControl.MaxTrayNum > 0 && trayNum == 1 && preTrayNum == DeviceControl.MaxTrayNum)
            {
                return true;
            }
            if (trayNum == 1 && DeviceControl.MaxTrayNum == 0 && isCanUpdateMax)
            {
                DeviceControl.MaxTrayNum = preTrayNum;
                LogUtil.info(LOGGER, "更新MaxTrayNum=" + preTrayNum);
                return true;
            }
            else if (isCanUpdateMax == false && DeviceControl.MaxTrayNum == 0)
            {
                return true;
            }
            return false;
        }

        public static void UpdateTrayNumError(int errorStoreID, string errorMsg)
        {
            TrayErrorMsg = errorMsg;
            ErrorStoreId = errorStoreID;
        }
    }
}