示例#1
0
    def __init__(self):

        # Load configuration file
        self.config = config

        # Make Messenger
        msn = Messenger('', self.config['GUI_PORT'])
        msn.setHandler(self.incoming_msg_handler)
        self.msnThread = Thread(target=msn.start_server)
        self.msnThread.start()

        # GUI
        self.GUI = GUI(msn)

        # Bounds
        self.set_bounds(self.config['SCANNING_AREA'])
        print("Scanning area set to: ", self.bounds)

        self.logger = Logger('.' + self.config['LOG_PATH'],
                             self.config['LOG_SCANNING_FILENAME'],
                             self.config['LOG_SESSION_FILENAME'],
                             self.config['LOG_RESULTS_FILENAME'], self)
示例#2
0
   def __init__(self):
      
      # Load configuration file
      self.config=config

      # Make Messenger
      msn = Messenger('', self.config['GUI_PORT'])
      msn.setHandler(self.incoming_msg_handler)
      self.msnThread = Thread(target=msn.start_server)
      self.msnThread.start()
      
      # GUI
      self.GUI = GUI(msn)
      
      # Bounds
      self.set_bounds(self.config['SCANNING_AREA'])
      print("Scanning area set to: ", self.bounds)
      
      self.logger = Logger('.'+self.config['LOG_PATH'],
                           self.config['LOG_SCANNING_FILENAME'],
                           self.config['LOG_SESSION_FILENAME'],
                           self.config['LOG_RESULTS_FILENAME'],
                           self)
示例#3
0
class Scanner:

    # Incapsulated objects
    GUI = None
    logger = None
    service = None
    config = None
    msnThread = None

    # Stats for overall scanning
    sessionStart = None
    sessionEnd = None
    boxesNinit = 0
    boxesN = 0
    requestsTotal = 0
    resultsTotal = 0
    costTotal = 0
    minTimeInterval = 'INF'
    maxTimeInterval = 0
    sumIntervalsSecs = 0

    # Vars
    bounds = None  #bounds in latitude and longtitude [x, y, x2, y2]

    # ---------------------------------------------------------------------------

    def __init__(self):

        # Load configuration file
        self.config = config

        # Make Messenger
        msn = Messenger('', self.config['GUI_PORT'])
        msn.setHandler(self.incoming_msg_handler)
        self.msnThread = Thread(target=msn.start_server)
        self.msnThread.start()

        # GUI
        self.GUI = GUI(msn)

        # Bounds
        self.set_bounds(self.config['SCANNING_AREA'])
        print("Scanning area set to: ", self.bounds)

        self.logger = Logger('.' + self.config['LOG_PATH'],
                             self.config['LOG_SCANNING_FILENAME'],
                             self.config['LOG_SESSION_FILENAME'],
                             self.config['LOG_RESULTS_FILENAME'], self)

    # Set outer bounds for the scanning
    def set_bounds(self, bounds):
        # Sort the bounds so that the left couple
        # is always on top and left of the right couple
        lat1 = bounds[0]
        lng1 = bounds[1]
        lat2 = bounds[2]
        lng2 = bounds[3]

        if (lat1 < lat2):
            bounds = (lat2, lng1, lat1, lng2)
            lat1cp = lat1
            lat1 = lat2
            lat2 = lat1cp
        if (lng1 > lng2):
            lng1cp = lng1
            lng1 = lng2
            lng2 = lng1cp

        bounds = (lat1, lng1, lat2, lng2)

        self.bounds = bounds
        self.GUI.center_map(bounds[0], bounds[1], bounds[2], bounds[3])

    def set_for_each_box(self, func):
        self.for_each_box = func

    def set_response_handler(self, func):
        self.response_handler = func

    # ---------------------------------------------------------------------------

    # Handling incoming messages from GUI
    def incoming_msg_handler(self, msg):
        if (msg == "PAUSE"):
            print("Client asks to pause application")
        elif (msg == "CLOSE"):
            print("Client asks to close application")

    # ---------------------------------------------------------------------------

    # Start scanning
    def start_scanning(self):
        self.sessionStart = datetime.now()
        logger = self.logger

        # Make a grid of scannable boxes
        grid = Grid(self.bounds, self, self.GUI)
        self.boxesN = len(grid.boxes)
        self.boxesNinit = len(grid.boxes)
        print("Number of boxes to scan(initially): ", self.boxesN)

        # Scan each box
        toScan = list(grid.boxes)
        boxScanStart = None
        isFirstScan = True
        while (toScan):
            box = toScan[0]
            self.GUI.remove_box(box)
            self.GUI.add_box(box, 'yellow')

            # Timing interval between last box scan
            if isFirstScan:
                boxScanStart = datetime.now()
                isFirstScan = False
            else:
                boxScanPrev = copy.deepcopy(boxScanStart)
                boxScanStart = datetime.now()
                tdelta = boxScanStart - boxScanPrev
                secs = tdelta.total_seconds()
                if (secs > self.maxTimeInterval):
                    self.maxTimeInterval = secs
                if (self.minTimeInterval == 'INF'
                        or secs < self.minTimeInterval):
                    self.minTimeInterval = secs
                self.sumIntervalsSecs += secs
            waitTime = self.config['scheduler']['NEXT_SEARCH_WAIT']
            sleep(waitTime)

            # Scan box
            markers = self.service.search(box, logger)

            # Update costs after scan
            self.requestsTotal += 1
            self.costTotal += self.config['service']['request'][
                'COST_PER_REQUEST']
            logger.update_session()

            # Max cost reached
            max_cost_day = self.config['service']['request']['MAX_COST_DAY']
            if max_cost_day != 'INF' and self.costTotal > max_cost_day:
                print("max cost per day reached")

            # Autosplit
            max_results = self.config['service']['response']['MAX_RESULTS']
            if self.config['box']['AUTOSPLIT'] and max_results!='INF' and\
                                                len(markers) >= max_results:
                logger.log_scan(
                    "Response had max possible results. Autosplitting..")
                boxes = grid.splitBoxIn4(box)
                self.boxesN -= 1
                toScan.pop(0)
                self.boxesN += 4
                toScan.insert(0, boxes[0])
                toScan.insert(1, boxes[1])
                toScan.insert(2, boxes[2])
                toScan.insert(3, boxes[3])
                continue

            # Add markers on map
            for marker in markers:
                if (len(marker) >= 2):
                    self.GUI.add_marker(marker[0], marker[1])
                    self.resultsTotal += 1

            # Remove finished box
            self.GUI.remove_box(box)
            self.GUI.add_box(box, 'green')
            toScan.pop(0)

        # Finish
        self.sessionEnd = datetime.now()
        logger.update_session()
        print("Scanning finished.")
        print("Press CTRL+C to stop application.")

    def stop_scanning(self):
        pass

    # ----------------- Setters ---------------------

    # Sets the service to be used for scanning
    def set_service(self, service):
        self.service = service

        # Override config with service values
        rules = service.service
        if (rules):
            for subject in rules:
                for key in rules[subject]:

                    # KEY required
                    if subject == 'authentication' and key == 'REQUIRED':
                        if rules['authentication']['REQUIRED']==True and\
                              (not service.key or len(service.key)<1):
                            print(
                                "The service requires a key, but none was provided."
                            )
                            print("Press CTRL+Z to exit.")
                            exit()

                    # Box limits
                    elif subject == 'box' and (key == 'MAX_X_DISTANCE'
                                               or key == 'MAX_Y_DISTANCE'):
                        if self.config['box']['X_DISTANCE'] > rules['box'][
                                'MAX_X_DISTANCE']:
                            self.config['box']['X_DISTANCE'] = rules['box'][
                                'MAX_X_DISTANCE']
                        if self.config['box']['Y_DISTANCE'] > rules['box'][
                                'MAX_Y_DISTANCE']:
                            self.config['box']['Y_DISTANCE'] = rules['box'][
                                'MAX_Y_DISTANCE']

                    # Min sleep between requests
                    elif subject == 'request' and key == 'MIN_REQUEST_INTERVAL':
                        if self.config['scheduler']['NEXT_SEARCH_WAIT'] < rules[
                                'request']['MIN_REQUEST_INTERVAL']:
                            self.config['scheduler'][
                                'NEXT_SEARCH_WAIT'] = rules['request'][
                                    'MIN_REQUEST_INTERVAL']

                    # Just copy rest of options
                    else:
                        self.config['service'][subject][key] = rules[subject][
                            key]
示例#4
0
#init game
pygame.init()
screen = pygame.display.set_mode((config._SCREEN_WIDTH, config._SCREEN_HEIGHT))
pygame.display.set_caption("ESCAPE")
pygame.mixer.init()
pygame.mixer.music.load(config._ASSET_DIR + "theme.mp3")
pygame.mixer.music.play(-1, 0.0)
running = True

#init classes and stuff
spaceship = player.player(
    (config._STARTING_X, config._STARTING_Y),
    config._ASSET_DIR + config._SHIPS_DIR + config._SPACESHIP1[0])
config._PLAYER = spaceship
gui = GUI.GUI()

counter = 0
config.initEverything()


def getUserInput():
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.event.pump()
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        spaceship.vertical += 1
    #handle arguments
    debug = False
    noGUI = False
    includeUiLayer = False
    includeSRS = False
    for i in range(len(sys.argv)):
        if sys.argv[i] == '-debug':
            debug = True
            logging.basicConfig(
                filename=path + os.path.sep + 'dcs-hotas-kneeboard.log',
                filemode='w',
                level=logging.DEBUG,
                format='%(asctime)s - %(levelname)s - %(message)s')
            logging.getLogger('dcs-hotas-kneeboard')
            logging.debug('Logger initialized')
        if sys.argv[i] == '-noGUI':
            noGUI = True
        if sys.argv[i] == '-UiLayer':
            includeUiLayer = True
        if sys.argv[i] == '-SRS':
            includeSRS = True

    if noGUI:
        #run main
        main()
    else:
        #run GUI
        app = wx.App()
        GUI(None, debug, checkForUpdate())
        app.MainLoop()
示例#6
0
class Scanner:
   
   # Incapsulated objects
   GUI       = None
   logger    = None
   service   = None
   config    = None
   msnThread = None
   
   # Stats for overall scanning
   sessionStart    = None
   sessionEnd      = None
   boxesNinit      = 0
   boxesN          = 0
   requestsTotal   = 0
   resultsTotal    = 0
   costTotal       = 0
   minTimeInterval = 'INF'
   maxTimeInterval = 0
   sumIntervalsSecs = 0
   
   # Vars
   bounds    = None    #bounds in latitude and longtitude [x, y, x2, y2]
   
   
   # ---------------------------------------------------------------------------
   
   
   def __init__(self):
      
      # Load configuration file
      self.config=config

      # Make Messenger
      msn = Messenger('', self.config['GUI_PORT'])
      msn.setHandler(self.incoming_msg_handler)
      self.msnThread = Thread(target=msn.start_server)
      self.msnThread.start()
      
      # GUI
      self.GUI = GUI(msn)
      
      # Bounds
      self.set_bounds(self.config['SCANNING_AREA'])
      print("Scanning area set to: ", self.bounds)
      
      self.logger = Logger('.'+self.config['LOG_PATH'],
                           self.config['LOG_SCANNING_FILENAME'],
                           self.config['LOG_SESSION_FILENAME'],
                           self.config['LOG_RESULTS_FILENAME'],
                           self)


   # Set outer bounds for the scanning
   def set_bounds(self, bounds):
      # Sort the bounds so that the left couple
      # is always on top and left of the right couple
      lat1=bounds[0]
      lng1=bounds[1]
      lat2=bounds[2]
      lng2=bounds[3]
      
      if (lat1<lat2):
         bounds=(lat2, lng1, lat1, lng2)
         lat1cp = lat1
         lat1 = lat2
         lat2 = lat1cp
      if (lng1>lng2):
         lng1cp = lng1
         lng1 = lng2
         lng2 = lng1cp
         
      bounds=(lat1, lng1, lat2, lng2)
         
      self.bounds=bounds
      self.GUI.center_map(bounds[0], bounds[1], bounds[2], bounds[3])

   def set_for_each_box(self, func):
      self.for_each_box=func
      
   def set_response_handler(self, func):
      self.response_handler=func
      

   # ---------------------------------------------------------------------------


   # Handling incoming messages from GUI
   def incoming_msg_handler(self, msg):
      if (msg == "PAUSE"):
         print("Client asks to pause application")
      elif (msg == "CLOSE"):
         print("Client asks to close application")
        
               
   # ---------------------------------------------------------------------------


   # Start scanning
   def start_scanning(self):
      self.sessionStart=datetime.now()
      logger=self.logger

      # Make a grid of scannable boxes
      grid=Grid(self.bounds, self, self.GUI) 
      self.boxesN     = len(grid.boxes)
      self.boxesNinit = len(grid.boxes)
      print("Number of boxes to scan(initially): ", self.boxesN)

      # Scan each box
      toScan=list(grid.boxes)
      boxScanStart = None
      isFirstScan    = True
      while(toScan):
         box=toScan[0]
         self.GUI.remove_box(box)
         self.GUI.add_box(box, 'yellow')

         # Timing interval between last box scan
         if isFirstScan:
            boxScanStart = datetime.now()
            isFirstScan  = False
         else:
            boxScanPrev  = copy.deepcopy(boxScanStart)
            boxScanStart = datetime.now()
            tdelta = boxScanStart - boxScanPrev
            secs   = tdelta.total_seconds()
            if (secs > self.maxTimeInterval):
               self.maxTimeInterval = secs
            if (self.minTimeInterval=='INF' or secs < self.minTimeInterval):
               self.minTimeInterval = secs
            self.sumIntervalsSecs+=secs
         waitTime = self.config['scheduler']['NEXT_SEARCH_WAIT']
         sleep(waitTime)
         
         # Scan box
         markers = self.service.search(box, logger)

         # Update costs after scan
         self.requestsTotal +=1
         self.costTotal += self.config['service']['request']['COST_PER_REQUEST']
         logger.update_session()

         # Max cost reached
         max_cost_day = self.config['service']['request']['MAX_COST_DAY']
         if max_cost_day!='INF' and self.costTotal > max_cost_day:
            print("max cost per day reached")
         
         # Autosplit
         max_results = self.config['service']['response']['MAX_RESULTS']
         if self.config['box']['AUTOSPLIT'] and max_results!='INF' and\
                                             len(markers) >= max_results:
            logger.log_scan("Response had max possible results. Autosplitting..")
            boxes=grid.splitBoxIn4(box)
            self.boxesN-=1
            toScan.pop(0)
            self.boxesN+=4
            toScan.insert(0, boxes[0])
            toScan.insert(1, boxes[1])
            toScan.insert(2, boxes[2])
            toScan.insert(3, boxes[3])
            continue

         # Add markers on map
         for marker in markers:
            if (len(marker)>=2):
               self.GUI.add_marker(marker[0], marker[1])
               self.resultsTotal+=1

         # Remove finished box
         self.GUI.remove_box(box)
         self.GUI.add_box(box, 'green')
         toScan.pop(0)

      # Finish
      self.sessionEnd=datetime.now()
      logger.update_session()
      print("Scanning finished.")
      print("Press CTRL+C to stop application.")


   def stop_scanning(self):
      pass


   # ----------------- Setters ---------------------

   # Sets the service to be used for scanning
   def set_service(self, service):
      self.service = service
      
      # Override config with service values
      rules = service.service
      if (rules):
         for subject in rules:
            for key in rules[subject]:

               # KEY required
               if subject=='authentication' and key=='REQUIRED':
                  if rules['authentication']['REQUIRED']==True and\
                        (not service.key or len(service.key)<1):
                     print("The service requires a key, but none was provided.")
                     print("Press CTRL+Z to exit.")
                     exit()
               
               # Box limits
               elif subject=='box' and (key=='MAX_X_DISTANCE' or key=='MAX_Y_DISTANCE'):
                  if self.config['box']['X_DISTANCE'] > rules['box']['MAX_X_DISTANCE']:
                     self.config['box']['X_DISTANCE'] = rules['box']['MAX_X_DISTANCE']
                  if self.config['box']['Y_DISTANCE'] > rules['box']['MAX_Y_DISTANCE']:
                     self.config['box']['Y_DISTANCE'] = rules['box']['MAX_Y_DISTANCE']

               # Min sleep between requests
               elif subject=='request' and key=='MIN_REQUEST_INTERVAL':
                  if self.config['scheduler']['NEXT_SEARCH_WAIT'] < rules['request']['MIN_REQUEST_INTERVAL']:
                     self.config['scheduler']['NEXT_SEARCH_WAIT'] = rules['request']['MIN_REQUEST_INTERVAL']
               
               # Just copy rest of options
               else:
                  self.config['service'][subject][key] = rules[subject][key]
示例#7
0
from classes import GUI

GUI()