Пример #1
0
        /// <summary>
        /// Job listener
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="onJob"></param>
        /// <returns></returns>
        public static Listener init(string channel, Action <Exception, Job> onJob)
        {
            return(new Listener(channel, newJobEvent, (dynamic msg) =>
            {
                // Msg validation
                if (msgValidate(msg))
                {
                    // Searching printers for this job
                    List <Printer> printers = Printers.findPrinters((string)msg.document, (int)msg.location, (int)msg.register);
                    if (printers.Count == 0)
                    {
                        log.Info("Skipped job: document: {0}, file: {1}, location: {2}, type: {3}, autoprinted: {4}, register: {5}",
                                 (string)msg.document,
                                 (string)msg.file,
                                 (int)msg.location,
                                 (string)msg.type,
                                 (bool)msg.autoprinted,
                                 (int)msg.register
                                 );
                        return;
                    }

                    foreach (Printer printer in printers)
                    {
                        // Job cration
                        Job job = new Job(
                            (string)msg.document,
                            (string)msg.file,
                            (int)msg.location,
                            (string)msg.type,
                            (bool)msg.autoprinted,
                            (int)msg.register
                            );

                        job.Processing();

                        // Setting job printers
                        job.printer = printer;

                        // New job callback (not sure if it corret and better via event)
                        onJob(null, job);

                        // New job event
                        if (jobAdded != null)
                        {
                            jobAdded(null, job);
                        }

                        job.download((err1) =>
                        {
                            if (err1 == null)
                            {
                                if (jobDownloaded != null)
                                {
                                    jobDownloaded(null, job);
                                }
                                job.print((err2) =>
                                {
                                    if (err2 == null)
                                    {
                                        if (jobPrinted != null)
                                        {
                                            jobPrinted(null, job);
                                        }
                                    }
                                    else
                                    {
                                        job.err = err2;
                                        log.Error("File printing error. File: \"{0}\"; Error details:\n{1}", job.localFilePath, err2);
                                    }
                                });
                            }
                            else
                            {
                                job.err = err1;
                                log.Error("File download error. File: \"{0}\"; Error details:\n{1}", job.file, err1);
                            }
                        });
                    }
                }
                else
                {
                    onJob(new Exception("Wrong job format. Get: " + msg.ToString()), null);
                }
            }));
        }