void FtpServer_NewDataConnection(object sender, FtpDataConnectionEventArgs e)
 {
     using (var db = new ICMDBContext())
     {
         lock (m_FtpTransferStatus)
         {
             var status = (from s in m_FtpTransferStatus
                           where s.Device.ip == e.Remote.Address.ToString()
                           select s).FirstOrDefault();
             if (status != null && status.Status == FtpTransferStatus.STATUS.LOAD_ING)
             {
                 AddLog(string.Format("FTP ({0}): Data Transfer Begin", e.Remote.Address));
             }
         }
     }
 }
        void FtpServer_ClosedDataConnection(object sender, FtpDataConnectionEventArgs e)
        {
            using (var db = new ICMDBContext())
            {
                lock (m_FtpTransferStatus)
                {
                    var status = (from s in m_FtpTransferStatus
                                  where s.Device.ip == e.Remote.Address.ToString()
                                  select s).FirstOrDefault();
                    if (status != null && status.Status == FtpTransferStatus.STATUS.LOAD_ING)
                    {
                        status.Status = (status.FileSize != 0 && status.FileSize == status.SentBytes)
                                      ? FtpTransferStatus.STATUS.LOAD_SUCESS
                                      : FtpTransferStatus.STATUS.LOAD_ERROR;

                        AddLog(string.Format("FTP ({0}): Data Transfer End", e.Remote.Address));
                    }
                }
            }
        }
        private async void FtpServer_NewDataConnection(object sender, FtpDataConnectionEventArgs e)
        {
            IUpgradeTasksModel upgradeTaskDataModel = ServiceLocator.Current.GetInstance <IUpgradeTasksModel>();

            await _upgradeTaskQueue.Enqueue(() => Task.Run(() =>
            {
                try
                {
                    // TODO: check 存取 upgradeTaskDataModel.Data 是否需要 mutex 保護
                    var task = upgradeTaskDataModel.Data.Where(
                        t => t.Status == UpgradeStatus.InProgress &&
                        t.Device.ip == e.Remote.Address.ToString()
                        //t => t.Device
                        ).FirstOrDefault();
                    if (task != null)
                    {
                        UpdateUpgradeTaskLastUpdateTime(upgradeTaskDataModel, task);
                    }
                }
                catch (Exception) { }
            }
                                                           ));
        }
        private async void FtpServer_ClosedDataConnection(object sender, FtpDataConnectionEventArgs e)
        {
            IUpgradeTasksModel upgradeTaskDataModel = ServiceLocator.Current.GetInstance <IUpgradeTasksModel>();

            await _upgradeTaskQueue.Enqueue(() => Task.Run(() =>
            {
                try
                {
                    //var task = upgradeTaskDataModel.Select(
                    var task = upgradeTaskDataModel.Data.Where(
                        t => t.Status == UpgradeStatus.InProgress &&
                        t.Device.ip == e.Remote.Address.ToString()
                        //t => t.Device,
                        //t => t.UpgradeFile
                        ).FirstOrDefault();
                    if (task != null)
                    {
                        if (task.UpgradeFile == null || task.UpgradeFile.FileSize == 0)
                        {
                            MarkUpgradeTaskRemoved(upgradeTaskDataModel, task);
                            return;
                        }

                        if (task.UpgradeFile.FileSize == task.SentDataBytes)
                        {
                            MarkUpgradeTaskComplete(upgradeTaskDataModel, task);
                        }
                        else
                        {
                            MarkUpgradeTaskTBD(upgradeTaskDataModel, task);
                        }
                    }
                }
                catch (Exception) { }
            }
                                                           ));
        }