Ejemplo n.º 1
0
    def start_processing(self):
        logging.debug("ready to open progress")

        self.wizard.next(Progress, message = 'Processing.....')        
        os.chdir(self.wizard.cwd)

        fh, xl_name = tempfile.mkstemp(suffix=".xls", prefix='project_'+datetime.datetime.today().strftime("%Y%m%d_%H%M%S")+"_", dir=os.path.dirname(self.customer_file))
        os.close(fh)
        os.unlink(xl_name)

        logging.debug("XL: "+xl_name)

        copy_customers_dbf = os.path.splitext(os.path.basename(self.customer_file))[0]+"_results_"+datetime.datetime.today().strftime("%Y%m%d_%H%M%S")+".shp"

        maxruntime = self.maxruntime.GetValue()
        if maxruntime == '' : 
            maxruntime = None
        else:
            h,m,s = maxruntime.split(":")
            maxruntime = ((int(h) * 60) + int(m) * 60) + int(s)

        project_id, errors = dbf_to_xl.process(
            customer_file = self.customer_file,
            facility_file = self.facility_file,
            vehicle_file = self.vehicle_file,
            copy_customers_dbf = copy_customers_dbf,
            save_file = xl_name,
            resequence = self.optimization_type.GetSelection() == self.Sequence_Routes,
            params = {
                'parameter_loading_duration_per_unit' : 0,
                'parameter_notification_email' : self.email.GetValue(),
                'parameter_user_data1' : json.dumps({
                    'dir': os.path.dirname(os.path.abspath(self.customer_file)),
                    'user' : os.environ.get( "USERNAME" ),
                    'copy_customers_dbf' : copy_customers_dbf
                    })
                }
            )

        if project_id:
            self.wizard.next(Progress, message = "Initializing....")
            monitor = Route(project_id)

            args = {}
            stop_condition = self.stop_condition.GetSelection()
            if stop_condition == self.Till_Time :
                args['timed'] = maxruntime
            elif stop_condition == self.Till_Nearly_Done :
                args['stop_when_nearly_done'] = True

            rez = monitor.start(**args)
            if 'start' not in rez:
                wx.MessageBox("There has been an error in routing: "+inspect(rez))
                exit(1)
            logging.debug("ready to open monitor optimization page")

            self.wizard.next(Monitor_Optimization_Page, project_id = project_id)

        else: 
            self.wizard.next(Progress, message = "Failed to send to RouteApp:\n%s" % ",".join(errors))
class Monitor_Optimization_Page(wx.Panel):

    def __init__(self, wizard, parent, project_id):
        logging.debug("initiating  monitor optimization page")
        wx.Panel.__init__(self, parent = parent)
        
        self.wizard = wizard
        self.project_id = project_id
        self.SetBackgroundColour(bg_color())

        body_sizer = wx.BoxSizer(orient = wx.VERTICAL)
        self.SetSizer(body_sizer)
        
        self.explain = wx.StaticText(self, -1, "Checking Project ID %s" % self.project_id)       
        body_sizer.Add(self.explain, flag = wx.EXPAND | wx.ALL, border=5)

        body_sizer.AddStretchSpacer()
        self.kill_button = wx.Button(self, -1, 'Cancel Optimization')
        self.kill_button.Disable()
        body_sizer.Add(self.kill_button, flag = wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM | wx.ALL, border=10)
        self.Bind(wx.EVT_BUTTON, self.kill, self.kill_button)

        self.monitor = Route(project_id)
        rez = self.monitor.monitor(observer = lambda(s): self.update_status(s), seconds = 5, wait_initial = True)

    def kill(self, evt):
        self.kill_button.Disable()
        self.monitor.kill_optimization()

    def update_status(self, status):
        logging.debug("test status %s, done? %s" % (inspect(status), status['status'] in Route.Done_Status))
        if 'running' not in status:
            self.wizard.next(Progress, message = "There has been an error in routing: "+inspect(status), cancel_text = 'Exit', cancel_action = Progress.exit)
            return
        elif status['running'] == None:
            logging.debug("status running = none")
            self.monitor.cancel_monitor()
            self.wizard.next(Progress, message = "Not optimizing: "+inspect(status), 
                cancel_text = 'Exit', cancel_action = Progress.exit)

        elif status['status'] in Route.Done_Status:
            self.monitor.cancel_monitor()
            self.wizard.next(Progress, message = "Elapsed Time:  %s\n   Status: %s" % (status['elapsed'],status['status']),
                cancel_text = 'Download', cancel_action = self.download)

        elif status['stopped'] != None:
            self.monitor.cancel_monitor()

            self.wizard.next(Progress, message = " Elapsed Time:  %s\n Status: %s\n Routing: %s" % (status['elapsed'],status['status'], status['stopped']), cancel_text = 'Exit', cancel_action = Progress.exit)

            return
        else:
            self.explain.SetLabel("Elapsed Time:  %s\n Status: %s" % (status['elapsed'],status['status']))

        self.kill_button.Enable()
        self.Layout()

    def download(self, evt):
        self.wizard.next(Download_Page, project_id=self.project_id)
    def __init__(self, wizard, parent, project_id):
        logging.debug("initiating  monitor optimization page")
        wx.Panel.__init__(self, parent = parent)
        
        self.wizard = wizard
        self.project_id = project_id
        self.SetBackgroundColour(bg_color())

        body_sizer = wx.BoxSizer(orient = wx.VERTICAL)
        self.SetSizer(body_sizer)
        
        self.explain = wx.StaticText(self, -1, "Checking Project ID %s" % self.project_id)       
        body_sizer.Add(self.explain, flag = wx.EXPAND | wx.ALL, border=5)

        body_sizer.AddStretchSpacer()
        self.kill_button = wx.Button(self, -1, 'Cancel Optimization')
        self.kill_button.Disable()
        body_sizer.Add(self.kill_button, flag = wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM | wx.ALL, border=10)
        self.Bind(wx.EVT_BUTTON, self.kill, self.kill_button)

        self.monitor = Route(project_id)
        rez = self.monitor.monitor(observer = lambda(s): self.update_status(s), seconds = 5, wait_initial = True)