#Copyright (C) 2013 Fabian Melendez B. fabmelb@gmail.com
#https://wiki.arcoslab.eie.ucr.ac.cr/doku.php?id=ie0117_proyectos_2013:way_to_shop
#https://github.com/gmocornejos/Way_To_Shop/wiki
#This file is part of Way To Shop.
#Way To Shop is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#Way To Shop is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with Way To Shop. If not, see .
import socket, android, time
droid = android.Android()
# ------------------------------------- Global Variables -------------------------------------- #
username = ""
password = ""
latitude = 00
longitude = 00
# -----------------------------Stablishing the conection with the server------------------------------------- #
# ---- A function to create the socket ---- #
def connectToServer():
#A boolean that indicates if the connection was successful
conection = False
#An int that indicates the amount of connection attempts
count = 0
while (conection==False):
conection = True
try:
#We add an attempt
count += 1
#We define the client instance
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#The address of the server (IP , PORT)
server_address = ('192.168.1.111', 10100)
#We try to connect to the server
client.connect(server_address)
droid.makeToast('Socket created')
#We return the client instance
return client
#In case the connection failed
except:
conection = False
#In case we tried to connect several times
if(count == 15):
droid.makeToast("Connection Error")
#We restart the counter
count = 1
#---------------------------------------- SCREENS METHODS --------------------------------------------------------------#
#--------LOG IN SCREEN --------#
# --DECIDER -- #
def login(client):
while (True):
#Waits for an event
event=droid.eventWait().result
print event
#If a button is clicked
if event["name"]=="click":
#Gets the id of the clicked button
id=event["data"]["id"]
#If it was the login button:
if (id=="login"):
username = droid.fullQueryDetail("username").result["text"]
password = droid.fullQueryDetail("password").result["text"]
#Comunication with the server
message = "login<>"+username+"<>"+password
#For checking
client.sendall(message)
#Waits for an answer
answer = client.recv(100)
#The response depends on the server's answer
if (answer == "login<>success"):
droid.makeToast("Logged In")
droid.fullDismiss()
droid.fullShow(menulayout)
#We execute the menu screen decider
menu(client)
else:
droid.makeToast("Invalid Username or Password, Try Again, or SignUp")
#If it was the sign up button
elif (id=="signup"):
#We close the actual screen
droid.fullDismiss()
#We show the sign screen
droid.fullShow(signlayout)
#Trigger the decider for this screen
sign(client)
# XML code for the graphical layout of the LOGIN SCREEN
loginlayout = """
"""
#--------SIGN SCREEN --------#
# --Decider-- #
def sign(client):
while True:
#Waits for an event
event=droid.eventWait().result
print event
#If a button is clicked
if event["name"]=="click":
#Gets the id of the clicked button
id=event["data"]["id"]
#If it was the signup button:
if id=="signin":
#We extract the information from the textboxes
username = droid.fullQueryDetail("user").result["text"]
password = droid.fullQueryDetail("passw").result["text"]
name = droid.fullQueryDetail("name").result["text"]
email = droid.fullQueryDetail("email").result["text"]
#We comuncate with the server
message = "signup<>"+username+"<>"+password+"<>"+name+"<>"+email
client.sendall(message)
answer = client.recv(100)
if answer == "signup<>success":
droid.makeToast("Welcome! You are now sign up to Way To Shop!")
#We close the current layout
droid.fullDismiss()
#We send the user to the menu screen
droid.fullShow(menulayout)
#The decider
menu(client)
else:
droid.makeToast("Error check your username or password information")
droid.fullDismiss()
droid.fullShow(signlayout)
sign(client)
if id=="back":
droid.fullDismiss()
droid.fullShow(loginlayout)
login(client)
#XML Layout
signlayout = """
"""
#--------MENU SCREEN --------#
# -- Decider -- #
def menu (client):
while True:
#Waits for an event
event=droid.eventWait().result
print event
#If a button is clicked
if event["name"]=="click":
#Gets the id of the clicked button
id=event["data"]["id"]
#If it was the signup button:
if id=="scan":
#We scan the barcode
code = droid.scanBarcode()
#Extract the code
bar = code[1]['extras']['SCAN_RESULT']
#We start a loading alert
droid.dialogCreateSpinnerProgress("Loading", "Please Wait")
droid.dialogShow()
#We start locating the client
droid.startLocating()
time.sleep(10)
location = droid.readLocation().result
#In case that the location failed, we get the last known location
if (location == {}):
location = droid.getLastKnownLocation().result
#When we already have one known location
if (location != {}):
try:
result = location['gps']
except:
result = location['network']
#We extract the latitude and longitude from the lecture
latitude = result['latitude']
longitude = result['longitude']
#address = droid.geocode(latitude, longitude).result
#We stop locating
droid.stopLocating()
#We close the loading alert
droid.dialogDismiss()
#Now we comunicate with the server
#seekitem<>barcode<>latitude%/longitude
client.sendall("seekitem<>"+str(bar)+"<>"+str(latitude)+"%/"+str(longitude))
#We wait for an answer
answer = client.recv(100)
if (answer == "seekitem<>notfound"):
#We ask if the user wants to add the item (By alert Box)
droid.dialogCreateAlert("Item not found", "Would you like to add it?")
droid.dialogSetPositiveButtonText("Yes")
droid.dialogSetNegativeButtonText("No")
droid.dialogShow()
#Wait for a response
response=droid.dialogGetResponse().result
#Close the dialog
droid.dialogDismiss()
if response.has_key("which"):
result=response["which"]
#If the user answered yes we send him to the itemnotfound screen
if result=="positive":
#In case the user wants to add the item, we send him to the item not found screen
droid.fullDismiss()
droid.fullShow(itemnotfoundlayout)
itemNOTfound(client, " ", " ", " ", " " , str(latitude), str(longitude) , True, str(bar))
elif result=="negative":
#In this case we send the user back to the menu
droid.fullDismiss()
droid.fullShow(menulayout)
#We execute the menu decider
menu(client)
#In case the item was found
else:
responseArray = answer.split("<>")
try:
#We extract the information from the message
name = responseArray[1]
brand = responseArray[2]
quantity = responseArray[3]
description = responseArray[4]
image = responseArray[5]
lowestprice = responseArray[6]
lowestpricestore = responseArray[7]
closestlowerprice = responseArray[8]
closestlowerpricestore = responseArray[9]
#And we show it in the item found screen
droid.fullDismiss()
droid.fullShow(itemfoundlayout)
itemfound(name, brand, quantity, lowestprice, lowestpricestore, closestlowerprice, closestlowerpricestore, description, client, str(bar), str(latitude), str(longitude))
except:
#In case it fails
droid.makeToast("Error")
#Back to the menu
droid.fullDismiss()
droid.fullShow(menulayout)
#We execute the menu decider
menu(client)
if id == "waytoshop":
#Not reay yet
exit()
#XML Layout
menulayout = """
"""
#--------ITEM FOUND ---------#
#-- Decider --- #
def itemfound(name, brand, quantity, lwp, lwpst, clwp, clwpst, descrp, client, barcode, latitude, longitude):
#We upate the screen
droid.fullSetProperty("itemname", "text" , name )
droid.fullSetProperty("itembrand", "text" , brand )
droid.fullSetProperty("quantity", "text" , quantity )
droid.fullSetProperty("lp", "text" , lwp )
droid.fullSetProperty("lpstore", "text" , lwpst )
droid.fullSetProperty("clp", "text" , clwp )
droid.fullSetProperty("clpstore", "text" , clwpst )
droid.fullSetProperty("description", "text" , descrp )
while True:
#Waits for an event
event=droid.eventWait().result
print event
#If a button is clicked
if event["name"]=="click":
#Gets the id of the clicked button
id=event["data"]["id"]
#If it was the back button:
if id=="back":
droid.fullDismiss()
droid.fullShow(menulayout)
#Trigger the decider for this screen
menu(client)
if id=="edit":
#In case the user wants to edit the item
#We close the current layout
droid.fullDismiss()
#Show the itemnotfound layout
droid.fullShow(itemnotfoundlayout)
itemNOTfound(client, name, brand, quantity, descrp, latitude, longitude, False, barcode)
if id=="Add":
print "add"
exit()
#XML Layout
itemfoundlayout = """
"""
#--------ITEM NOT FOUND ---------#
#-- Decider --- #
def itemNOTfound(client, name, brand, quantity, descript , latitude, longitude , new, barcode):
droid.fullSetProperty("itemname", "text" , name )
droid.fullSetProperty("itembrand", "text" , brand )
droid.fullSetProperty("quantity", "text" , quantity )
droid.fullSetProperty("descript", "text" , descript )
while True:
#Waits for an event
event=droid.eventWait().result
print event
#If a button is clicked
if event["name"]=="click":
#Gets the id of the clicked button
id=event["data"]["id"]
#If it was the back button:
if id=="back":
droid.fullDismiss()
droid.fullShow(menulayout)
#Trigger the decider for this screen
menu(client)
if id == "submit":
#We extract the information
name = droid.fullQueryDetail("itemname").result["text"]
brand = droid.fullQueryDetail("itembrand").result["text"]
quantity = droid.fullQueryDetail("quantity").result["text"]
price = droid.fullQueryDetail("price").result["text"]
location = droid.fullQueryDetail("store").result["text"]
descript = droid.fullQueryDetail("descript").result["text"]
#We still need to select the picture
#For the store id:
#We comunicate with the server to know wich stores are close
client.sendall("storeinratio<>"+latitude+"%/"+longitude)
answer = client.recv(100)
stores = answer.split("<>")
storesnames = ["New Store"]
storesids = ["-1"]
if len(stores)>1:
for i in range (1, len(stores)-1):
storesnames.append(stores[i].split("%/")[0])
storesids.append(stores[i].split("%/")[1])
#We show the user a list of stores
droid.dialogCreateAlert("Please select the store you are in", "Or submit a new one")
droid.dialogSetItems(storesnames)
droid.dialogShow()
result = droid.dialogGetResponse().result
droid.dialogDismiss()
if result==None:
print "Error"
elif result.has_key("item"):
item = result["item"]
#If the user selects to add a New Store
if(item == 0):
#We ask for the store's name
droid.dialogCreateInput("New Store", "Please write the store's name")
droid.dialogSetPositiveButtonText("Ok")
droid.dialogShow()
storename = droid.dialogGetResponse().result["value"]
droid.dialogDismiss()
#We ask for the store's location
droid.dialogCreateInput("New Store", "Please write the store's location")
droid.dialogSetPositiveButtonText("Ok")
droid.dialogShow()
location = droid.dialogGetResponse().result["value"]
droid.dialogDismiss()
client.sendall("addstore<>"+storename+"<>"+location+"<>"+latitude+"%/"+longitude)
answer = client.recv(100)
droid.makeToast(answer)
if (answer.split("<>")[1]=="success"):
#We extract the store id
storeid = answer.split("<>")[2]
else:
droid.makeToast("Error")
#We send him to the menu
droid.fullDismiss()
droid.fullShow(menulayout)
#We execute the menu screen decider
menu(client)
else:
storeid = storesids[item]
storename = storesnames[item]
#In case is a new item (Depends on the boolean "new")
if (new == True):
client.sendall("submititem<>"+barcode+"<>"+name+"<>"+brand+"<>"+quantity+"<>"+descript+"<>0<>"+price+"<>"+storeid)
print client.recv(100)
if (new == False):
client.sendall("edititem<>"+barcode+"<>"+name+"<>"+brand+"<>"+quantity+"<>"+descript+"<>0<>"+price+"<>"+storeid)
print client.recv(100)
#We send him to the menu
droid.makeToast("Thanks for adding products!")
droid.fullDismiss()
droid.fullShow(menulayout)
menu(client)
if id == "add":
#In case the user wants to add it to one list
#NOT FUNCTIONING NOW
exit()
if id == "picture":
#We take the picture and store it in the default folder
droid.cameraInteractiveCapturePicture("/sdcard/DCIM/Camera")
#XML Layout
itemnotfoundlayout = """
"""
#----------------------------------------------------------------------------------------------#
#------------------------------------A Kind of Main--------------------------------------------#
#----------------------------------------------------------------------------------------------#
#First We Connect To the Server
client = connectToServer()
#We show the loginlayout
droid.fullShow(loginlayout)
#We execute the login screen decider, now on, the app controls itself with the previous methods
login(client)