-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifiservice.py
111 lines (99 loc) · 3.85 KB
/
wifiservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import socket
from os.path import sep
from keydb_simulator import key_code_parser
import fnmatch
import os
import time
import subprocess
from kivy.config import Config
class WiFiService:
server_sock = None
client_sock = None
client_addr = None
connected = None
profile_path = None
wifi_available = None
def __init__(self, profile_path, port):
self.connected = False
self.profile_path = profile_path
self.wifi_available = check_wifi_availability()
self.port = port
self.client_addr = None
def start_wifi_service(self):
while True:
if self.wifi_available:
self.server_sock = socket.socket()
self.server_sock.bind(("", int(self.port)))
self.server_sock.listen(1)
print "WIFI Started Service"
self.client_sock, self.client_addr = self.server_sock.accept()
self.connected = True
print self.client_addr
self.client_sock.send("Successfully Connected to RacePadServer".center(1024, '\''))
profile_count = 0
for profile in os.listdir(self.profile_path):
if fnmatch.fnmatch(profile, "*.xml"):
profile_count += 1
self.client_sock.send(str(profile_count).center(1024, '\''))
time.sleep(0.2)
for profile in os.listdir(self.profile_path):
if fnmatch.fnmatch(profile, "*.xml"):
try:
send = self.client_sock.send(profile.center(1024, '\''))
except:
break
xml_file = open(self.profile_path + sep + profile, "rb")
file_bytes = xml_file.read()
xml_file.close()
self.client_sock.send(file_bytes.center(1024, '\''))
time.sleep(0.5)
try:
while True:
data = self.client_sock.recv(1024)
if len(data) == 0:
break
codes = data.split('~')
for code in codes:
key_code_parser(code)
except IOError:
pass
self.connected = False
print("disconnected")
self.client_sock.close()
self.server_sock.close()
def connection_status(self):
return self.connected
def get_client_address(self):
if self.connected:
return self.client_addr
return None
def update_profiles(self):
Config.set("kivy", "update_profiles", 0)
Config.write()
profile_count = 0
for profile in os.listdir(self.profile_path):
if fnmatch.fnmatch(profile, "*.xml"):
profile_count += 1
self.client_sock.send(str(profile_count).center(1024, '\''))
print "updating"
time.sleep(0.2)
print(profile_count)
for profile in os.listdir(self.profile_path):
if fnmatch.fnmatch(profile, "*.xml"):
try:
self.client_sock.send(profile.center(1024, '\''))
except:
break
xml_file = open(self.profile_path + sep + profile, "rb")
file_bytes = xml_file.read()
xml_file.close()
self.client_sock.send(file_bytes.center(1024, '\''))
print profile+" Sending Successs"
time.sleep(0.5)
print "updated"
def check_wifi_availability():
output = subprocess.check_output("netsh wlan show drivers", shell=True)
if len(output) > 50:
return True
else:
return False