Exemplo n.º 1
0
        public PaperStatusResult CheckPaperStatus(string brand, string model, string vid, string pid, bool enableTextFileStatus)
        {
            try
            {
                if (string.IsNullOrEmpty(brand))
                {
                    return(new PaperStatusResult {
                        IsSuccess = false, ErrorMessage = "brand cannot be null or empty."
                    });
                }

                if (string.IsNullOrEmpty(vid))
                {
                    return(new PaperStatusResult {
                        IsSuccess = false, ErrorMessage = "vid cannot be null or empty."
                    });
                }

                if (string.IsNullOrEmpty(pid))
                {
                    return(new PaperStatusResult {
                        IsSuccess = false, ErrorMessage = "pid cannot be null or empty."
                    });
                }

                string[] listPaperStatus;

                if (brand.ToLower().Trim() == "brother")
                {
                    Brother service = new Brother();
                    listPaperStatus = service.CheckPaperStatus(model, vid, pid);
                }
                else if (brand.ToLower().Trim() == "custom")
                {
                    Custom service = new Custom();
                    listPaperStatus = service.CheckPaperStatus(model, vid, pid);
                }
                else
                {
                    return(new PaperStatusResult {
                        IsSuccess = false, ErrorMessage = "brand not match."
                    });
                }

                if (enableTextFileStatus)
                {
                    string MessageStorageFolder = ConfigurationManager.AppSettings["RMS.MessageStorageFolder"];
                    if (!string.IsNullOrEmpty(MessageStorageFolder))
                    {
                        Directory.CreateDirectory(MessageStorageFolder);

                        // ทำการลบไฟล์ status ออกจาก folder
                        // โดยการดึงรายชื่อไฟล์ทั้งหมดของอุปกรณ์นั้น ไปเทียบกับใน result list
                        // หากชื่อไฟล์ ไม่อยู่ใน list ให้ลบทิ้ง (ทั้งนี้ list ต้องมีข้อมูลอยู่ หากไม่มีเลย แสดงว่า ดึง status ไม่ได้ ให้ข้ามการลบไป)

                        // ถ้ามีข้อมูล status ใน list ให้ทำการลบไฟล์ ที่ไม่เกียวข้อง
                        if (listPaperStatus.Length > 0)
                        {
                            string[] listFileStatus = Directory.GetFiles(MessageStorageFolder, "*_" + brand + ".txt");
                            if (listPaperStatus[0] != null && listPaperStatus[0].ToLower() == "ok")
                            {
                                //แสดงว่า status ปกติ ให้ลบไฟล์ทั้งหมด
                                foreach (var filestatus in listFileStatus)
                                {
                                    try
                                    {
                                        File.Delete(filestatus);
                                    }
                                    catch (Exception ex)
                                    {
                                        new RMSAppException(this, "0500", "CheckPaperStatus failed. " + filestatus + " cannot be deleted. " + ex.Message, ex, true);
                                    }
                                }
                            }
                            else
                            {
                                //ให้เลือกลบที่ไม่เกี่ยวข้อง
                                foreach (var filestatus in listFileStatus)
                                {
                                    // คัดกรองรูปแบบ text file
                                    // [STATUS]_[BRAND].txt
                                    string statusFromFile = Path.GetFileNameWithoutExtension(filestatus).Replace("_" + brand, "");
                                    if (listPaperStatus.All(a => a.ToLower() != statusFromFile.ToLower()))
                                    {
                                        try
                                        {
                                            File.Delete(filestatus);
                                        }
                                        catch (Exception ex)
                                        {
                                            new RMSAppException(this, "0500", "CheckPaperStatus failed. " + filestatus + " cannot be deleted. " + ex.Message, ex, true);
                                        }
                                    }
                                }
                            }
                        }

                        // ถ้าไม่ใช่ OK status ให้เริ่มเขียน text file
                        foreach (var paperStatus in listPaperStatus)
                        {
                            if (paperStatus.ToLower() != "ok")
                            {
                                try
                                {
                                    using (File.Create(MessageStorageFolder + paperStatus.ToUpper() + "_" + brand.ToUpper() + ".txt"))
                                    {
                                    }
                                }
                                catch (Exception ex)
                                {
                                    new RMSAppException(this, "0500", "CheckPaperStatus failed. " + MessageStorageFolder + paperStatus.ToUpper() + "_" + brand.ToUpper() + ".txt" + " cannot be created. " + ex.Message, ex, true);
                                }
                            }
                        }
                    }
                }
                return(new PaperStatusResult {
                    IsSuccess = true, ListStatus = listPaperStatus
                });
            }
            catch (Exception ex)
            {
                return(new PaperStatusResult {
                    IsSuccess = false, ErrorMessage = ex.Message
                });
            }
        }