Ignore:
Timestamp:
03/30/12 15:26:43 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

Fleet retrieval and gate selection works, started new connection handling and the fsuipc simulator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/web.py

    r48 r51  
    77import threading
    88import sys
     9import urllib
    910import urllib2
    1011import hashlib
     
    102103#------------------------------------------------------------------------------
    103104
     105class Plane(object):
     106    """Information about an airplane in the fleet."""
     107    def __init__(self, s):
     108        """Build a plane info based on the given string.
     109
     110        The string consists of three, space-separated fields.
     111        The first field is the tail number, the second field is the gate
     112        number, the third field is the plane's status as a character."""
     113        try:
     114            words = s.split(" ")
     115            tailNumber = words[0]
     116            self.tailNumber = tailNumber
     117
     118            status = words[2] if len(words)>2 else None
     119            self.status = const.PLANE_HOME if status=="H" else \
     120                          const.PLANE_AWAY if status=="A" else \
     121                          const.PLANE_PARKING if status=="P" else \
     122                          const.PLANE_UNKNOWN
     123
     124            gateNumber = words[1] if len(words)>1 else ""
     125            self.gateNumber = gateNumber if gateNumber else None
     126
     127        except:
     128            print >> sys.stderr, "Plane string is invalid: '" + s + "'"
     129            self.tailNumber = None
     130
     131    def __repr__(self):
     132        """Get the representation of the plane object."""
     133        s = "<Plane: %s %s" % (self.tailNumber,
     134                               "home" if self.status==const.PLANE_HOME else \
     135                               "away" if self.status==const.PLANE_AWAY else \
     136                               "parking" if self.status==const.PLANE_PARKING \
     137                               else "unknown")
     138        if self.gateNumber is not None:
     139            s += " (gate " + self.gateNumber + ")"
     140        s += ">"
     141        return s
     142       
     143
     144#------------------------------------------------------------------------------
     145
     146class Fleet(object):
     147    """Information about the whole fleet."""
     148    def __init__(self, f):
     149        """Construct the fleet information by reading the given file object."""
     150        self._planes = {}
     151        while True:
     152            line = readline(f)
     153            if not line or line == "#END": break
     154
     155            plane = Plane(line)
     156            if plane.tailNumber is not None:
     157                self._planes[plane.tailNumber] = plane       
     158
     159    def isGateConflicting(self, plane):
     160        """Check if the gate of the given plane conflicts with another plane's
     161        position."""
     162        for p in self._planes.itervalues():
     163            if p.tailNumber!=plane.tailNumber and \
     164               p.status==const.PLANE_HOME and \
     165               p.gateNumber==plane.gateNumber:
     166                return True
     167
     168        return False
     169
     170    def getOccupiedGateNumbers(self):
     171        """Get a set containing the numbers of the gates occupied by planes."""
     172        gateNumbers = set()
     173        for p in self._planes.itervalues():
     174            if p.status==const.PLANE_HOME and p.gateNumber:
     175                gateNumbers.add(p.gateNumber)
     176        return gateNumbers
     177       
     178    def __getitem__(self, tailNumber):
     179        """Get the plane with the given tail number.
     180
     181        If the plane is not in the fleet, None is returned."""
     182        return self._planes[tailNumber] if tailNumber in self._planes else None
     183
     184    def __repr__(self):
     185        """Get the representation of the fleet object."""
     186        return self._planes.__repr__()
     187       
     188#------------------------------------------------------------------------------
     189
    104190class Result(object):
    105191    """A result object.
     
    161247    iso88592decoder = codecs.getdecoder("iso-8859-2")
    162248   
    163     def __init__(self, pilotID, password, callback):
     249    def __init__(self, callback, pilotID, password):
    164250        """Construct the login request with the given pilot ID and
    165251        password."""
     
    205291                                    flight2.departureTime))
    206292
     293        f.close()
     294
    207295        return result
    208296       
     297#------------------------------------------------------------------------------
     298
     299class GetFleet(Request):
     300    """Request to get the fleet from the website."""
     301   
     302    def __init__(self, callback):
     303        """Construct the fleet request."""
     304        super(GetFleet, self).__init__(callback)
     305
     306    def run(self):
     307        """Perform the login request."""
     308        url = "http://www.virtualairlines.hu/onlinegates_get.php"
     309
     310        f = urllib2.urlopen(url)
     311        result = Result()
     312        result.fleet = Fleet(f)
     313        f.close()
     314       
     315        return result
     316
     317#------------------------------------------------------------------------------
     318
     319class UpdatePlane(Request):
     320    """Update the status of one of the planes in the fleet."""
     321    def __init__(self, callback, tailNumber, status, gateNumber = None):
     322        """Construct the request."""
     323        super(UpdatePlane, self).__init__(callback)
     324        self._tailNumber = tailNumber
     325        self._status = status
     326        self._gateNumber = gateNumber
     327
     328    def run(self):
     329        """Perform the plane update."""
     330        url = "http://www.virtualairlines.hu/onlinegates_set.php"
     331
     332        status = "H" if self._status==const.PLANE_HOME else \
     333                 "A" if self._status==const.PLANE_AWAY else \
     334                 "P" if self._status==const.PLANE_PARKING else ""
     335
     336        gateNumber = self._gateNumber if self._gateNumber else ""
     337
     338        data = urllib.urlencode([("lajstrom", self._tailNumber),
     339                                 ("status", status),
     340                                 ("kapu", gateNumber)])
     341       
     342        f = urllib2.urlopen(url, data)
     343        line = readline(f)
     344       
     345        result = Result()
     346        result.success = line == "OK"
     347
     348        return result
     349           
    209350#------------------------------------------------------------------------------
    210351
     
    223364        self.daemon = True
    224365
    225     def login(self, pilotID, password, callback):
     366    def login(self, callback, pilotID, password):
    226367        """Enqueue a login request."""
    227         self._addRequest(Login(pilotID, password, callback))
    228 
     368        self._addRequest(Login(callback, pilotID, password))
     369
     370    def getFleet(self, callback):
     371        """Enqueue a fleet retrieval request."""
     372        self._addRequest(GetFleet(callback))
     373       
     374    def updatePlane(self, callback, tailNumber, status, gateNumber = None):
     375        """Update the status of the given plane."""       
     376        self._addRequest(UpdatePlane(callback, tailNumber, status, gateNumber))
     377       
    229378    def run(self):
    230379        """Process the requests."""
     
    255404    handler.start()
    256405
    257     handler.login("P096", "V5fwj", callback)
     406    #handler.login(callback, "P096", "V5fwj")
     407    #handler.getFleet(callback)
     408    # Plane: HA-LEG home (gate 67)
     409    handler.updatePlane(callback, "HA-LQC", const.PLANE_AWAY, "72")
    258410    time.sleep(3)   
    259 
    260 #------------------------------------------------------------------------------
     411    handler.getFleet(callback)
     412    time.sleep(3)   
     413
     414#------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.