private static StationUpdateItem DeepCopy(StationUpdateItem _obj)
        {
            BinaryFormatter BF2 = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                BF2.Serialize(stream, _obj);
                stream.Position = 0;
                return((StationUpdateItem)BF2.Deserialize(stream));
            }
        }
        public static List <StationUpdateItem> GetAllStationItems()
        {
            List <StationUpdateItem> items = new List <StationUpdateItem>();

            for (int i = 0; i < stationItems.Length; i++)
            {
                if (stationItems[i] != null)
                {
                    StationUpdateItem item = DeepCopy(stationItems[i]);
                    items.Add(item);
                }
            }
            return(items);
        }
        /// <summary>
        /// 中心站巡检分站的文件接收情况
        /// </summary>
        /// <returns></returns>
        public static byte[] GetBuffer_4(int fzh)
        {
            byte[]            sendBuffer = new byte[1];
            StationUpdateItem item       = stationItems[fzh - 1];

            if (item.updateState == StationUpdateState.reiciveCheck)
            {
                #region ----巡檢----

                sendBuffer = new byte[13];

                IniSendBufferHeader(ref sendBuffer, fzh, 4);
                //缺失文件编号	2 Byte 若该字段=0,则表示查询分站的文件接收情况,后续无文件数据体字段。
                sendBuffer[9]  = 0;
                sendBuffer[10] = 0;
                //文件数据体	256Byte
                //累加和	2 Byte
                int ljh = GetLJL(sendBuffer, 0);
                sendBuffer[sendBuffer.Length - 2] = (byte)((ljh >> 8) & 0xFF);
                sendBuffer[sendBuffer.Length - 1] = (byte)((ljh >> 0) & 0xFF);

                #endregion
            }
            else if (item.updateState == StationUpdateState.replacement)
            {
                #region ----补发----

                sendBuffer = new byte[269];

                IniSendBufferHeader(ref sendBuffer, fzh, 4);
                //缺失文件编号	2 Byte 若该字段=0,则表示查询分站的文件接收情况,后续无文件数据体字段。
                sendBuffer[9]  = 0;
                sendBuffer[10] = 0;
                //文件数据体	256Byte
                byte[] fileBuffer = GetBuffer_0(item.nowNeedSendBuffIndex - 1);
                for (int i = 0; i < fileBuffer.Length; i++)
                {
                    sendBuffer[11 + i] = fileBuffer[i];
                }
                //累加和	2 Byte
                int ljh = GetLJL(sendBuffer, 0);
                sendBuffer[sendBuffer.Length - 2] = (byte)((ljh >> 8) & 0xFF);
                sendBuffer[sendBuffer.Length - 1] = (byte)((ljh >> 0) & 0xFF);

                #endregion
            }
            return(sendBuffer);
        }
 private static void IniStationItems()
 {
     for (int i = 0; i < stationItems.Length; i++)
     {
         stationItems[i]                              = new StationUpdateItem();
         stationItems[i].fzh                          = i + 1;
         stationItems[i].updateState                  = StationUpdateState.unKnowm;
         stationItems[i].nowNeedSendBuffIndex         = 1;
         stationItems[i].stationWorkState             = new StationWorkState();
         stationItems[i].stationWorkState.softVersion = 0;
         stationItems[i].stationWorkState.hardVersion = 0;
         stationItems[i].stationWorkState.updateState = -1;
         stationItems[i].stationWorkState.devTypeID   = 0;
         stationItems[i].isUpdate                     = false;
     }
 }
        /// <summary>
        /// 只更析 updateState、nowNeedSendBuffIndex、lastSendTime、stationWorkState
        /// </summary>
        /// <param name="item"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public static bool UpdateStationItemForSys(StationUpdateItem item, ref string errorMsg)
        {
            bool success = true;

            try
            {
                stationItems[item.fzh - 1].updateState          = item.updateState;
                stationItems[item.fzh - 1].nowNeedSendBuffIndex = item.nowNeedSendBuffIndex;
                stationItems[item.fzh - 1].lastSendTime         = item.lastSendTime;
                stationItems[item.fzh - 1].stationWorkState     = item.stationWorkState;
                stationItems[item.fzh - 1].Info = item.Info;
            }
            catch (Exception ex)
            {
                LogHelper.Error("UpdateStationItemForSys Error:" + ex.Message);
                success  = false;
                errorMsg = ex.Message;
            }

            return(success);
        }
        public static StationUpdateItem GetStationItem(int fzh)
        {
            StationUpdateItem item = null;

            if (fzh >= 0 && fzh < stationItems.Length - 1)
            {
                if (stationItems[fzh - 1] == null)
                {
                    return(item);
                }
                item = DeepCopy(stationItems[fzh - 1]);
                item.isSendBuffer = false;
                switch (item.updateState)
                {
                case StationUpdateState.requesting:    //下发请求升级    下发请求升级
                    StationUpdateRequest stationUpdateRequest = new StationUpdateRequest();
                    stationUpdateRequest.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    stationUpdateRequest.DeviceId    = (byte)updatetTypeid;
                    stationUpdateRequest.HardVersion = (byte)updateHardVersion;
                    stationUpdateRequest.FileVersion = (byte)updateFileVersion;
                    stationUpdateRequest.maxVersion  = (byte)updateMaxVersion;
                    stationUpdateRequest.minVersion  = (byte)updateMinVersion;
                    stationUpdateRequest.FileCount   = updateCount;
                    stationUpdateRequest.Crc         = crcValue;
                    item.isSendBuffer = true;
                    item.protocol     = stationUpdateRequest;
                    item.protocolType = ProtocolType.StationUpdateRequest;
                    break;

                case StationUpdateState.recivingFile:    //文件接收中    下发文件
                    if (item.nowNeedSendBuffIndex >= updateCount)
                    {
                        item.updateState = StationUpdateState.reiciveCheck;
                        return(null);
                    }
                    SendUpdateBufferRequest sendUpdateBufferRequest = new SendUpdateBufferRequest();
                    sendUpdateBufferRequest.DeviceCode     = fzh.ToString().PadLeft(3, '0') + "0000";
                    sendUpdateBufferRequest.DeviceId       = (byte)updatetTypeid;
                    sendUpdateBufferRequest.HardVersion    = (byte)updateHardVersion;
                    sendUpdateBufferRequest.FileVersion    = (byte)updateFileVersion;
                    sendUpdateBufferRequest.NowBufferIndex = item.nowNeedSendBuffIndex;
                    sendUpdateBufferRequest.Buffer         = new byte[256];
                    for (int i = 0; i < sendUpdateBufferRequest.Buffer.Length; i++)
                    {
                        sendUpdateBufferRequest.Buffer[i] = updateBuffer[(item.nowNeedSendBuffIndex - 1) * 256 + i];
                    }
                    item.isSendBuffer = true;
                    item.protocol     = sendUpdateBufferRequest;
                    item.protocolType = ProtocolType.SendUpdateBufferRequest;
                    stationItems[fzh - 1].nowNeedSendBuffIndex += 1;
                    if (stationItems[fzh - 1].nowNeedSendBuffIndex >= updateCount)
                    {
                        stationItems[fzh - 1].updateState = StationUpdateState.reiciveCheck;    //下发完成后自动切换到巡检
                    }
                    break;

                case StationUpdateState.reiciveCheck:    //巡检文件接收情况     下发巡检
                    InspectionRequest inspectionRequest = new InspectionRequest();
                    inspectionRequest.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    inspectionRequest.DeviceId    = (byte)updatetTypeid;
                    inspectionRequest.HardVersion = (byte)updateHardVersion;
                    inspectionRequest.FileVersion = (byte)updateFileVersion;
                    inspectionRequest.LostFileNum = 0;
                    item.isSendBuffer             = true;
                    item.protocol     = inspectionRequest;
                    item.protocolType = ProtocolType.InspectionRequest;
                    break;

                case StationUpdateState.replacement:    //补发文件中       补发文件
                    InspectionRequest replacement = new InspectionRequest();
                    replacement.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    replacement.DeviceId    = (byte)updatetTypeid;
                    replacement.HardVersion = (byte)updateHardVersion;
                    replacement.FileVersion = (byte)updateFileVersion;
                    replacement.LostFileNum = item.nowNeedSendBuffIndex;
                    if (replacement.LostFileNum > 0)
                    {
                        replacement.FileBuffer = new byte[256];
                        for (int i = 0; i < replacement.FileBuffer.Length; i++)
                        {
                            replacement.FileBuffer[i] = updateBuffer[(item.nowNeedSendBuffIndex - 1) * 256 + i];
                        }
                    }
                    item.isSendBuffer = true;
                    item.protocol     = replacement;
                    item.protocolType = ProtocolType.InspectionRequest;
                    break;

                case StationUpdateState.restart:    //重启升级      下发重启命令
                    RestartRequest restartRequest = new RestartRequest();
                    restartRequest.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    restartRequest.DeviceId    = (byte)updatetTypeid;
                    restartRequest.HardVersion = (byte)updateHardVersion;
                    restartRequest.FileVersion = (byte)updateFileVersion;
                    item.isSendBuffer          = true;
                    item.protocol     = restartRequest;
                    item.protocolType = ProtocolType.RestartRequest;
                    break;

                case StationUpdateState.restore:    //恢复备份      下发恢复备份命令
                    ReductionRequest reductionRequest = new ReductionRequest();
                    reductionRequest.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    reductionRequest.DeviceId    = (byte)updatetTypeid;
                    reductionRequest.HardVersion = (byte)updateHardVersion;
                    reductionRequest.SoftVersion = (byte)updateFileVersion;
                    item.isSendBuffer            = true;
                    item.protocol     = reductionRequest;
                    item.protocolType = ProtocolType.ReductionRequest;
                    break;

                case StationUpdateState.updateCancle:     //取消升级    //下取消升级命令
                    UpdateCancleRequest updateCancleRequest = new UpdateCancleRequest();
                    updateCancleRequest.DeviceCode  = fzh.ToString().PadLeft(3, '0') + "0000";
                    updateCancleRequest.DeviceId    = (byte)updatetTypeid;
                    updateCancleRequest.HardVersion = (byte)updateHardVersion;
                    updateCancleRequest.FileVersion = (byte)updateFileVersion;
                    item.isSendBuffer = true;
                    item.protocol     = updateCancleRequest;
                    item.protocolType = ProtocolType.UpdateCancleRequest;
                    break;

                case StationUpdateState.getVersion:    //获取分站信息
                    GetStationUpdateStateRequest getStationUpdateStateRequest = new GetStationUpdateStateRequest();
                    getStationUpdateStateRequest.DeviceCode     = fzh.ToString().PadLeft(3, '0') + "0000";
                    getStationUpdateStateRequest.GetSoftVersion = 1;
                    getStationUpdateStateRequest.GetUpdateState = 1;
                    getStationUpdateStateRequest.GetDevType     = 1;
                    getStationUpdateStateRequest.GetHardVersion = 1;
                    item.isSendBuffer = true;
                    item.protocol     = getStationUpdateStateRequest;
                    item.protocolType = ProtocolType.GetStationUpdateStateRequest;
                    break;
                }
            }
            return(item);
        }