Esempio n. 1
0
    def takeUserInput(self):
        print("--------------------------------------------------------------------------")
        print("-----------------------Welcome to Safeway!!-------------------------------")

        enterOption = input("Press 1 to Enter, Press 2 to Exit")
        if enterOption == 1:
            print("-----------------------------------------------------------------------")
            new_customer = ""
            while True:
                print("-----------------------------------------------------------------------")
                print("Enter 1 for creating a customer")
                print("Enter 2 for generating a random customer")
                print("Enter 3 to exit")
                option = input("Please Enter your choice")
                print("\n")

                if option == 1:
                    new_customer = Customer()
                    del new_customer.items[:]
                    while True:
                        print("Enter 1 for Adding Item")
                        print("Enter 2 to see all the Items")
                        print("Enter 3 If finished shopping")

                        option2 = input("Please Enter your choice")
                        if option2 == 1:
                            item = Item.Item()
                            new_customer.items.append(item)
                            print("---------------------New Item Added-------------------------\n")

                        elif option2 == 2:
                            print("Item Name                    Item Price\n")
                            print("-----------------------------------------")
                            for i in new_customer.items:
                                print(str(i.name)+"                        "+ str(i.value))
                            print("\n")

                        elif option2 ==3:
                            message = Message('addcustomer', new_customer)
                            m = message.get_message_json()
                            self.protocol.sendLine(m)
                            print("Customer sent to the queue")
                            break




                if option==2:
                    message = Message('addcustomer', Customer())
                    m = message.get_message_json()
                    self.protocol.sendLine(m)
                    print("Customer sent to the queue")
                    continue

                if option ==3:
                    break

        if enterOption == 2:
            print("Good Bye")
            sys.exit()
    def dataReceived(self, data):
        print data
        try:
            message = json.loads(data)

            if message['msg_type'] == 'addcustomer':
                print "Adding customer"
                customer = Customer()
                customer.object_decoder(message['payload'])
                customer.set_timestamp(time.time())
                self.customerQueueFactory.add_to_queue(customer)
                print "Customer added"

            elif message['msg_type'] == 'nextcustomer':
                print "Sending customer"
                if self.customerQueueFactory.has_customer():
                    customer = self.customerQueueFactory.get_from_queue()
                    wait_time = int(time.time() - customer.get_timestamp())
                    print "Total waiting time for the customer: " + str(
                        wait_time) + " seconds"
                    msg = Message('customer', customer)
                else:
                    msg = Message('no_customer', "")

                msg_json = json.dumps(msg, default=lambda o: o.__dict__)
                print msg_json
                self.transport.write(msg_json)
                print "Customer sent"
        except Exception as e:
            print e.message
            self.transport.write("Dude, you screwed up! :(")
    def dataReceived(self, data):
        print data
        try:
            message = json.loads(data)

            if message['msg_type'] == 'addcustomer':
                print "Adding customer"
                customer = Customer()
                customer.object_decoder(message['payload'])
                customer.set_timestamp(time.time())
                self.customerQueueFactory.add_to_queue(customer)
                print "Customer added"

            elif message['msg_type'] == 'nextcustomer':
                print "Sending customer"
                if self.customerQueueFactory.has_customer():
                    customer = self.customerQueueFactory.get_from_queue()
                    wait_time = int(time.time() - customer.get_timestamp())
                    print "Total waiting time for the customer: " + str(wait_time) + " seconds"
                    msg = Message('customer', customer)
                else:
                    msg = Message('no_customer', "")

                msg_json = json.dumps(msg, default=lambda o: o.__dict__)
                print msg_json
                self.transport.write(msg_json)
                print "Customer sent"
        except Exception as e:
            print e.message
            self.transport.write("Dude, you screwed up! :(")
    def dataReceived(self, data):

        global global_customer
        global has_customer
        try:
            message = json.loads(data)

            if message['msg_type'] == 'customer':
                has_customer = True
                global_customer = Customer()
                global_customer.object_decoder(message['payload'])
                global_customer.set_timestamp(time.time())



            elif message['msg_type'] == 'no_customer':
                has_customer = False

        except Exception as e:
            print e.message
            # self.transport.write("Buddy, you screwed up! in AttendantServer:(")
            pass
Esempio n. 5
0
    def dataReceived(self, data):

        global global_customer
        global has_customer
        try:
            message = json.loads(data)

            if message['msg_type'] == 'customer':
                has_customer = True
                global_customer = Customer()
                global_customer.object_decoder(message['payload'])
                global_customer.set_timestamp(time.time())

            elif message['msg_type'] == 'no_customer':
                has_customer = False

        except Exception as e:
            print e.message
            # self.transport.write("Buddy, you screwed up! in AttendantServer:(")
            pass
Esempio n. 6
0
__author__ = 'priya'

from twisted.internet import protocol, reactor
from twisted.protocols import basic
from twisted.internet.protocol import ClientFactory
from twisted.internet.defer import Deferred
from Actors.Customer import Customer
import json
from Message import Message
from Config import server
import time
global_customer = Customer()
has_customer = False


class CounterServerFactory(protocol.Factory):
    def __init__(self):
        #print "at server  factory"
        CounterServerProtocol().connectToOtherServer("")
        pass

    def buildProtocol(self, addr):
        return CounterServerProtocol()


class CounterServerProtocol(basic.LineReceiver):
    def __init__(self):
        self.queue_factory = CounterClientFactory()
        pass

    def connectionMade(self):
Esempio n. 7
0
class Bus:
    origin = [18.6183, 73.8768]  # 'army institute of technology, pune'
    destination = [18.6183, 73.8768]  # 'baner, pune'
    route = []  # [{'lat': 18.5726, 'lng': 73.8782}, {'lat': 18.5604, 'lng': 73.8360}]    #wadi and bhosari
    NewRoute = []
    Duration = 00
    Distance = 00

    pp = pprint.PrettyPrinter()

    TimeTable = []

    Gmap = GMap()  # Gmap to get routes
    Customers = []  # List of customers in the bus

    def printBus(self):
        print "origin      : %s" % self.origin
        print "destination : %s" % self.destination
        print "route       : %s" % self.route
        print "Tmax        : %d" % self.Tmax
        print "Duration    : %d" % self.Duration
        print "Distance    : %d" % self.Distance

    def addCustomer(self, pickup, dropoff):
        self.NewCustomer = Customer(pickup, dropoff)

    def accept(self, pickup, dropoff):
        self.addCustomer(pickup, dropoff)
        self.createNewRoute()

        self.directions = self.Gmap.findRouteofBus(self)

        #print "\n\nDirections returned by GMap legs"
        #self.pp.pprint(self.directions)

        self.fillTimeTable(self.directions[0]['legs'])

        print "searching for >>> "
        self.pp.pprint(self.NewCustomer.PickUp)

        t1 = [item for item in self.TimeTable if
              round(item["location"]["lat"], 4) == round(self.NewCustomer.PickUp[0], 4)
              and round(item["location"]["lng"], 4) == round(self.NewCustomer.PickUp[1], 4)]
        t2 = [item for item in self.TimeTable if
              round(item["location"]["lat"], 4) == round(self.NewCustomer.DropOff[0], 4)
              and round(item["location"]["lng"], 4) == round(self.NewCustomer.DropOff[1], 4)]

        if not t1 or not t2:
            print "\n\n>>>No time returned"
            return False

        self.pp.pprint("Pickup loc :")
        self.pp.pprint(t1)
        self.pp.pprint("DropOff loc :")
        self.pp.pprint(t2)
        self.pp.pprint("time diff   :")
        self.pp.pprint(t2[0]["time"] - t1[0]["time"])
        self.pp.pprint("Cust Tmax   :")
        self.pp.pprint(self.NewCustomer.Tmax)

        if t2[0]["time"] - t1[0]["time"] > self.NewCustomer.Tmax:
            return False

        for cust in self.Customers:
            t1 = [item for item in self.TimeTable if round(item["location"]["lat"], 4) == round(cust.PickUp[0], 4)
                  and round(item["location"]["lng"], 4) == round(cust.PickUp[1], 4)]
            t2 = [item for item in self.TimeTable if round(item["location"]["lat"], 4) == round(cust.DropOff[0], 4)
                  and round(item["location"]["lng"], 4) == round(cust.DropOff[1], 4)]

            self.pp.pprint("Pickup loc :")
            self.pp.pprint(t1)
            self.pp.pprint("DropOff loc :")
            self.pp.pprint(t2)
            self.pp.pprint("time diff   :")
            self.pp.pprint(t2[0]["time"] - t1[0]["time"])
            self.pp.pprint("Cust Tmax   :")
            self.pp.pprint(cust.Tmax)

            if t2[0]["time"] - t1[0]["time"] > cust.Tmax:
                return False

        return True

    def createNewRoute(self):
        self.NewRoute = list(self.route)
        self.NewRoute.append(self.NewCustomer.pickup())
        self.NewRoute.append(self.NewCustomer.dropoff())

    def fillTimeTable(self, stops):
        print ">>>> Stops"
        self.TimeTable.append({"location": stops[0]["start_location"], "time": 0})
        for stop in stops:
            self.pp.pprint(stop["distance"])
            # self.pp.pprint(stop['duration'])
            self.TimeTable.append({"location": stop["end_location"],
                                   "time": self.TimeTable[-1]["time"] + stop["duration"]["value"]
            })
            self.pp.pprint(self.TimeTable)

    def to_JSON(self):
        return json.dumps(self, default=lambda o: o.__dict__,
                          sort_keys=True, indent=4)

    def confirm(self):
        self.route = list(self.NewRoute)
        self.Customers.append(self.NewCustomer)
Esempio n. 8
0
 def addCustomer(self, pickup, dropoff):
     self.NewCustomer = Customer(pickup, dropoff)