Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: iso-8859-15 -*-
- import threading
- import socket
- import hashlib
- import time
- from Tkinter import *
- ############################
- ###Variables à configurer###
- ############################
- UDP_IP = "x.x.x.x" #DEST
- UDP_IP_loc = "x.x.x.x" #Local IP
- UDP_PORT = 26010
- PW = "Password"
- compteur = 0
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock_recu = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- #sock_recu.bind((socket.gethostbyname(socket.gethostname()), UDP_PORT))
- sock_recu.bind((UDP_IP_loc, UDP_PORT))
- date_heure = time.strftime('%d/%m/%y %H:%M:%S',time.localtime())
- ############################
- #####Def. des fonctions#####
- ############################
- def dat_heure():
- return time.strftime('%d/%m/%y %H:%M:%S',time.localtime())
- def recep_k8055_info():
- while True:
- data, addr = sock_recu.recvfrom(1024) # buffer size is 1024 bytes
- if(hashlib.sha256(PW).hexdigest() == data.split('|-|')[0]):
- if(data.split('|-|')[1] == "L"):
- liste_ana = eval(data.split('|-|')[2])
- liste_digi = eval(data.split('|-|')[3])
- print dat_heure(), "(" + addr[0] + ")" + " Données reçu: " + str(liste_ana) + str(liste_digi)
- else:
- print dat_heure(), "(" + addr[0] + ")" + " Données reçu: " + data.split('|-|')[1]
- def send_k8055_cmd(event,value1,value2):
- if value1 and value2:
- print dat_heure(), "(" + UDP_IP + ")" + " Commande envoyé:"+event+"("+value1+","+value2+")"
- sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event+"|-|"+value1+"|-|"+value2, (UDP_IP, UDP_PORT))
- elif value1:
- print dat_heure(),"(" + UDP_IP + ")" + " Commande envoyé:"+event+"("+value1+")"
- sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event+"|-|"+value1, (UDP_IP, UDP_PORT))
- else:
- print dat_heure(), "(" + UDP_IP + ")" + " Commande envoyé:"+event+"()"
- sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event, (UDP_IP, UDP_PORT))
- if(event == "end"):
- sock_recu.close()
- print dat_heure(),"Déconnexion du socket"
- a = threading.Thread(None,recep_k8055_info)
- a.start()
- root = Tk( )
- prompt='Appuyez sur une touche du clavier'
- lbl = Label(root, text=prompt, width=len(prompt))
- lbl.pack( )
- class Evenement(Frame):
- def __init__(self, parent=None):
- Frame.__init__( self )
- self.master.title("Touche de direction")
- self.master.geometry("600x150") #La Taille du fenêtre
- self.pack(expand=YES, fill=BOTH)
- lbl=Label(self, text="Appuyez sur une touche de direction")
- lbl["bg"]="blue"
- lbl["font"]=('courier', 20, 'bold')
- lbl.pack(expand=YES, fill=BOTH)
- lbl.focus()
- # Lier un événement aux quatre touches de direction du clavier
- # Avant
- lbl.bind_all("<KeyPress-Up><KeyPress-Right>", self.onFW_Right_Down) # direction droite '-->'
- lbl.bind_all("<KeyPress-Up><KeyPress-Left>", self.onFW_Left_Down) # direction gauche '<--'
- lbl.bind_all("<KeyRelease-Up><KeyRelease-Right>", self.onFW_Right_Up) # direction droite '-->'
- lbl.bind_all("<KeyRelease-Up><KeyRelease-Left>", self.onFW_Left_Up) # direction gauche '<--'
- lbl.bind("<KeyPress-Up>", self.onForward_Down) # direction haut
- lbl.bind("<KeyRelease-Up>", self.onForward_Up) # direction haut
- # Arrière
- lbl.bind("<Down> + <Right>", self.onBA_Right) # direction droite '-->'
- lbl.bind("<Down> + <Left>", self.onBA_Left) # direction gauche '<--'
- lbl.bind("<KeyPress-Down>", self.onBack_Up) # direction bas
- lbl.bind("<KeyRelease-Down>", self.onBack_Down) # direction bas
- # Autre
- lbl.bind("q", self.onEsc) #Quitter
- lbl.bind("<Left> + <Right>", self.on360) # 360 '-->'
- def onFW_Right_Up(self,event):
- send_k8055_cmd("digital_all_off","","")
- def onFW_Right_Down(self,event):
- send_k8055_cmd("digital_write","4","")
- def onFW_Left_Down(self,event):
- send_k8055_cmd("digital_write","1","")
- def onFW_Left_Up(self,event):
- send_k8055_cmd("digital_all_off","","")
- def onForward_Up(self,event):
- send_k8055_cmd("digital_all_off","","")
- def onForward_Down(self,event):
- send_k8055_cmd("digital_write","5","")
- def onBA_Right(self,event):
- send_k8055_cmd("digital_write","8","")
- def onBA_Left(self,event):
- send_k8055_cmd("digital_write","2","")
- def onBack_Down(self,event):
- send_k8055_cmd("digital_all_off","","")
- def onBack_Up(self,event):
- send_k8055_cmd("digital_write","10","")
- def on360(self,event):
- send_k8055_cmd("digital_write","9","")
- def onEsc(self,event):
- send_k8055_cmd("end","","")
- a._Thread__stop()
- sock_recu.close()
- def main(): #Fonction principale
- Evenement().mainloop()
- if __name__=="__main__":
- main()
- a._Thread__stop()
- sock_recu.close()
- print "FIN"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement