Beispiel #1
0
        public TResponseResult <bool> Send(List <LogPackage> packets)
        {
            RequestResultType requestResult = RequestResultType.ConnectionUnavailable;
            var isSended = false;
            //  пытаемся отправить серверу, если нет - то сохраняем сообщения в папке для отправки
            var req = new NetRequests();

            foreach (var packet in packets)
            {
                //SendToDisk !!! Request will not execute. It will always return error, then save on disk
                var result          = req.SendLogPackage(packet);
                var jsonFileContent = Newtonsoft.Json.JsonConvert.SerializeObject(packet);
                requestResult = result.RequestResult;
                // logger.Info("Sendind paket result: ({0}){1}{2}Paket Content: {3}", (int)result.RequestResult, result.RequestResult, Environment.NewLine, jsonFileContent);
                if (result.RequestResult != RequestResultType.Successful)
                {
                    //  save in folder
                    //logger.Error("Sendind paket exception: ({0}){1}{2}Paket Content: {3}", (int)result.RequestResult, result.RequestResult, Environment.NewLine, jsonFileContent);


                    var jsonFileName = string.Format(fileNameTemplate, DateTime.Now.ToString(fileDateTemplate), packet.WFMId, packet.IIN, packet.Type);

                    using (var writer = new System.IO.StreamWriter(Path.Combine(configuration.GetUploadPath(), jsonFileName)))
                    {
                        writer.Write(jsonFileContent);
                    }
                }
                else
                {
                    isSended = true;
                }
                System.Windows.Forms.Application.DoEvents();
            }
            return(new TResponseResult <bool>(isSended, requestResult));
        }
Beispiel #2
0
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (sendTimeStart != sendTimeEnd)
            {
                if (!isBetween(sendTimeStart, sendTimeEnd))
                {
                    logger.Info("It is not the time to start sending. Start time: {0}, end time: {1}, now: {2}. Sleep: {3}", sendTimeStart, sendTimeEnd, DateTime.Now, sleepTimeOnWaitSendOnTime);
                    Thread.Sleep(sleepTimeOnWaitSendOnTime);
                    return;
                }
            }

            if (!sendingProcess)
            {
                lock (lockObject)
                {
                    if (!sendingProcess)
                    {
                        sendingProcess = true;

                        var config    = new CommonLib.ConfigurationSettings();
                        var filesPath = config.GetUploadPath();
                        var files     = Directory.GetFiles(filesPath, "*.json");

                        if (files != null && files.Length > 0)
                        {
                            var fileToSend = getOlderFile(files);
                            var req        = new NetRequests();
                            req.updateUrls();
                            string fileContent = "";
                            if (FileHelper.ReadFile(fileToSend, out fileContent))
                            {
                                ArmDataPackage package = null;
                                try
                                {
                                    package = JsonConvert.DeserializeObject <ArmDataPackage>(fileContent);
                                }
                                catch (Exception ex)
                                {
                                    //  если файл попался хреновый, то мы его удалим, но и запишем его содержимое в лог ошибок
                                    logger.Error(ex, string.Format("Exception when read file packet json. \r\n Packet Content: {0}", fileContent));
                                    if (File.Exists(fileToSend))
                                    {
                                        File.Delete(fileToSend);
                                    }
                                }

                                if (package == null)
                                {
                                    return;
                                }
                                var sendResult = req.SendPackage(package);
                                if (sendResult.RequestResult == RequestResultType.Successful)
                                {
                                    //  del file
                                    logger.Info("Package {0} sending succesfuly", fileToSend);
                                    if (File.Exists(fileToSend))
                                    {
                                        File.Delete(fileToSend);
                                    }
                                    logger.Info("Package {0} deleted", fileToSend);
                                }
                                else
                                {
                                    logger.Error("Coudn't send package {0}, Back-End return error: ({1}){2}. Sleep: {3}", fileToSend, (int)sendResult.RequestResult, sendResult.RequestResult, sleepTimeOnError);
                                    //  ошибка при отправке файла, спим
                                    Thread.Sleep(sleepTimeOnError);
                                }
                            }
                        }
                        else
                        {
                            //  спим, если не нашли файлов для отправки
                            Thread.Sleep(sleepTimeOnNoneFiles);
                            logger.Info("There are no files to send. Sleep: {0}", sleepTimeOnNoneFiles);
                        }
                        sendingProcess = false;
                    }
                }
            }
        }