Пример #1
0
        private void HandleException(Exception exception)
        {
            if (exception != null)
            {
                string msg = exception.Message + Environment.NewLine + exception.StackTrace;
                Logging.ToLog(msg);
                ClientMail.SendMail("Необработанное исключение", msg, mailTo);
            }

            Logging.ToLog("!!!App - Аварийное завершение работы");
            ExcelInterop.Instance.CloseExcel();
            Process.GetCurrentProcess().Kill();
        }
Пример #2
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            mailTo = InfomatSelfChecking.Properties.Settings.Default.MailTo;
            string msg = "App - Запуск приложения";

            Logging.ToLog(msg);
            ClientMail.SendMail(msg, msg, mailTo);

            MainWindow window = new MainWindow();

            window.Show();
        }
        public ExcelInterop()
        {
            Logging.ToLog("ExcelInterop - Запуск Excel");
            foreach (Process p in Process.GetProcessesByName("EXCEL"))
            {
                p.Kill();
            }

            xlApp = new Excel.Application();
            if (xlApp == null)
            {
                string msg = "ExcelInterop - Не удалось открыть приложение Excel";
                Logging.ToLog(msg);
                ClientMail.SendMail(subject, msg, stpAddress);
                return;
            }

            xlApp.Visible = false;
            string currentDir = Directory.GetCurrentDirectory();

            templateFullPath = Path.Combine(currentDir, "ExcelTemplate", templateFileName);
            if (!File.Exists(templateFullPath))
            {
                string msg = "ExcelInterop - Не удалось получить доступ к файлу шаблона: " + templateFullPath;
                Logging.ToLog(msg);
                ClientMail.SendMail(subject, msg, stpAddress);
                return;
            }

            saveFolder = Path.Combine(currentDir, "Results");
            if (!Directory.Exists(saveFolder))
            {
                try {
                    Directory.CreateDirectory(saveFolder);
                } catch (Exception e) {
                    saveFolder = string.Empty;
                    Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
                }
            }

            clinic_address      = Properties.Settings.Default.ClinicAddress;
            clinic_phone_number = Properties.Settings.Default.ClinicPhoneNumber;

            DeleteOldFiles();
        }
        private Excel.Worksheet OpenTemplate(out Excel.Workbook wb, string filePostfix)
        {
            try {
                Logging.ToLog("ExcelInterop - Открытие книги: " + templateFullPath);

                string currentTemplate = Path.Combine(saveFolder, "PrintResult_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + filePostfix + ".xlsx");
                File.Copy(templateFullPath, currentTemplate);

                wb = xlApp.Workbooks.Open(currentTemplate);

                if (wb == null)
                {
                    string msg = "ExcelInterop - Не удалось открыть книгу: " + templateFullPath;
                    Logging.ToLog(msg);
                    ClientMail.SendMail(subject, msg, stpAddress);
                    return(null);
                }

                Logging.ToLog("ExcelInterop - Открытие листа: Template");
                Excel.Worksheet ws = wb.Sheets["Template"];
                if (ws == null)
                {
                    string msg = "ExcelInterop - Не удалось открыть лист: Template";
                    Logging.ToLog(msg);
                    ClientMail.SendMail(subject, msg, stpAddress);
                    return(null);
                }

                return(ws);
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            wb = null;
            return(null);
        }
        public State GetPrinterState()
        {
            Logging.ToLog("PrinterInfo - Получение статуса принтера: " + printerName);

            if (string.IsNullOrEmpty(printerName))
            {
                return(State.NotSelect);
            }

            if (printerName.ToLower().Equals("donotcheck"))
            {
                return(State.DoNotCheck);
            }

            try {
                using (ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher(NAME_SPACE, "SELECT * FROM " + CLASS_NAME)) {
                    ManagementObjectCollection colPrinters = mgmtObjSearcher.Get();

                    if (colPrinters.Count == 0)
                    {
                        Logging.ToLog("PrinterInfo - Не удалось получить информацию об установленных принтеров в разделе " +
                                      NAME_SPACE + ", " + CLASS_NAME);
                        return(State.NotFound);
                    }

                    foreach (ManagementObject printer in colPrinters)
                    {
                        string currentPrinterName = printer["Name"].ToString().ToLower();

                        if (!currentPrinterName.Equals(printerName))
                        {
                            continue;
                        }

                        long printerState       = Convert.ToInt64(printer["PrinterState"]);
                        bool printerWorkOffline = Convert.ToBoolean(printer["WorkOffline"]);

                        if ((printerState == 0 ||                         //"Printer ready"
                             printerState == 131072 ||                    //"Toner low"
                             printerState == 2048 ||                      //"Printer output bin full"
                             printerState == 131072 + 2048) &&            //"Toner low" + "Printer output bin full"
                            !printerWorkOffline)
                        {
                            isTicketSendToSTP = false;
                            return(State.Ready);
                        }

                        string printerStatus = string.Empty;

                        if (printerWorkOffline)
                        {
                            printerStatus += "Printer is working offline" + Environment.NewLine;
                        }

                        for (int i = statusCodes.Count - 1; i >= 0; i--)
                        {
                            if (printerState == 0)
                            {
                                break;
                            }

                            int code = statusCodes.ElementAt(i).Key;

                            if ((printerState - code) < 0)
                            {
                                continue;
                            }

                            printerState -= code;

                            if (code == 131073 || code == 2048)                             //"Toner low" || "Printer output bin full"
                            {
                                continue;
                            }

                            printerStatus += statusCodes[code] + Environment.NewLine;
                        }

                        Logging.ToLog("PrinterInfo - статус принтера: " + printerStatus);

                        if (!string.IsNullOrEmpty(printerStatus) && !isTicketSendToSTP)
                        {
                            string subject = "Уведомление от инфомата";
                            string body    = "Инфомату не удалось распечатать список назначений пациента, коды ошибок принтера: " +
                                             Environment.NewLine + Environment.NewLine + printerStatus;
                            ClientMail.SendMail(subject, body, mailReceiver);
                            isTicketSendToSTP = true;
                        }

                        return(State.Error);
                    }
                }
            } catch (Exception e) {
                Logging.ToLog(e.Message + Environment.NewLine + e.StackTrace);
            }

            return(State.Error);
        }