Beispiel #1
0
        static void Main(string[] args)
        {
            LoadSettings();
            StartPolling();
            Aptoma.Init();

            if (SAVEOUTPUT)
            {
                Log("ATTENTION! SAVEOUTPUT is set to true. All files will be saved in " + OUTPUTDIR);
            }

            if (DEBUG)
            {
                Log("PROGRAM IS IN DEBUG MODE! Info will not be sent to Aptoma");
            }

            Console.Write("Press <Escape> to exit...\n");
            while (Console.ReadKey().Key != ConsoleKey.Escape)
            {
            }
        }
Beispiel #2
0
        static void Poll(object sender, ElapsedEventArgs e)
        {
            if (!WORKING)
            {
                WORKING = true;

                FILELIST = new List <string>(Directory.GetFiles(INPUTDIR).OrderBy(d => new FileInfo(d).CreationTime));

                if (FILELIST.Count > 0)
                {
                    System.Threading.Thread.Sleep(2000); // Short pause to ensure all files in list are readable

                    foreach (string file in FILELIST)
                    {
                        bool skipFile = false;

                        string[] fileSplit = file.Split('\\');
                        string   fileName  = fileSplit[fileSplit.Length - 1];

                        string[] fileNameSplit = fileName.Split('.');
                        string   extension     = fileNameSplit[fileNameSplit.Length - 1].ToLower();

                        try
                        {
                            if (extension.Substring(0, 1).ToLower().Equals("p"))
                            {
                                Log("Processing pdl file: " + fileName);
                                string json = ConvertPDLtoJSON(file);

                                if (!DEBUG)
                                {
                                    string[] response = Aptoma.PostPage(json);
                                    if (response[0].Equals("OK"))
                                    {
                                        Log("Page successfully uploaded");
                                    }
                                    else
                                    {
                                        Log("Error uploading page!");
                                        Log("Server response: " + response[0]);
                                        Log("Moving " + fileName + " to error folder.");
                                        File.Copy(file, ERRORDIR + "\\" + fileName, true);
                                    }
                                }

                                if (SAVEOUTPUT)
                                {
                                    SaveOutput(fileName + ".txt", json);
                                }
                            }
                            else if (extension.Equals("xml"))
                            {
                                Log("Processing xml file: " + fileName);
                                string json = ConvertXMLToJson(file);

                                if (!DEBUG)
                                {
                                    string[] response = Aptoma.PostEdition(json);
                                    if (response[0].Equals("OK"))
                                    {
                                        Log("Edition successfully uploaded");
                                    }
                                    else
                                    {
                                        Log("Error uploading edition!");
                                        Log("Server response: " + response[0]);
                                        Log("Moving " + fileName + " to error folder.");
                                        File.Copy(file, ERRORDIR + "\\" + fileName, true);
                                    }
                                }

                                if (SAVEOUTPUT)
                                {
                                    SaveOutput(fileName + ".txt", json);
                                }
                            }
                            else if (extension.Equals("jpg"))
                            {
                                Log("Processing jpg file: " + fileName);
                                string xml = ImageMeta.GetImageXml(file);

                                if (!DEBUG)
                                {
                                    string[] response = Aptoma.PostImage(xml);
                                    if (response[0].Equals("OK"))
                                    {
                                        Log("Image successfully uploaded");
                                    }
                                    else
                                    {
                                        Log("Error uploading image!");
                                        Log("Server response: " + response[0]);
                                        Log("Moving " + fileName + " to error folder.");
                                        File.Copy(file, ERRORDIR + "\\" + fileName, true);
                                    }
                                }

                                if (SAVEOUTPUT)
                                {
                                    SaveOutput(fileName + ".txt", xml);
                                }
                            }
                            else
                            {
                                Log("Unknown fileformat: " + extension);
                                Log("Moving " + fileName + " to error folder.");
                                File.Copy(file, ERRORDIR + "\\" + fileName, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log(ex.ToString());
                            //Log(ex.Message);
                            Log("Skipping file...");
                            skipFile = true;
                        }


                        if (!skipFile)
                        {
                            try
                            {
                                File.Delete(file);
                            }
                            catch (Exception ex)
                            {
                                Log(ex.Message);
                            }
                        }
                    }
                }

                WORKING = false;
            }
            else
            {
                Log("Already processing files. Skipping poll.");
            }
        }