-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_main.py
72 lines (57 loc) · 1.94 KB
/
xml_main.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
from server.socket_server import BasicChatServer
import database.python_database.object_xml_operations as o2x
import thread
import json
import time
import server.debug as debug
database_path = "/database/database.xml"
class TCPParser(object):
"""Doc"""
dispatch = {
'add_member': o2x.add_member
# TODO: Add all functions
}
def mes(client, message):
# Message will be sent to TCPParser
# To check with dispatcher and execute function.
# FIXME: Each function has differrent attributes, Cant execute them together.
# TODO: Execute Authentication Test here. Check is user allowed
# IDEA: Different user types to execute group of functions. Allow some users to execute some etc. remove_member
debug.INFO("Socket: " + str(client) + " Message is: " + str(message))
data = None
try:
data = json.loads(message)
except ValueError, e:
return
id = data["id"]
type = data["request_type"]
content = data["content"]
if type == "door_clearance":
(level, person) = o2x.get_level(content, "rfid", "database/python_database/database.xml")
has_clearance = False
found = level != -1
if found:
has_clearance = level <= 3
_dict = {"id":id, "result":has_clearance}
if not found:
_dict["status"] = "ERROR"
_dict["content"] = "User Not Found"
else:
_dict["status"] = "OK"
_dict["content"] = str(person.name)
json_str = json.dumps(_dict)
send_to_client(client, json_str)
file = open("request_log.txt", 'a')
file.write(json.dumps(data) + "\n")
file.close()
def send_to_client(client, message):
client.send(message + "\n")
debug.INFO("Sending Back: " + message)
def main():
server = BasicChatServer(mes)
thread.start_new_thread(server.run,())
# send_to_client(server)
while True:
time.sleep(10)
if __name__ == "__main__":
main()