-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_threaded.py
More file actions
136 lines (120 loc) · 5.29 KB
/
server_threaded.py
File metadata and controls
136 lines (120 loc) · 5.29 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import sys
import socket
import argparse
import threading
import logging
import json
import yaml
import psycopg2
from struct import pack, unpack
from datetime import datetime
FIRST_PART = pack('i', 0)
END_PART = pack('<i', 82)
logging.basicConfig(filename='developer_info.log', level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(message)s')
conf = yaml.load(open('application.yml'), Loader=yaml.BaseLoader)
parser = argparse.ArgumentParser(description = "This is the server for the multithreaded socket.")
parser.add_argument('--host', metavar = 'host', type = str, nargs = '?', default = socket.gethostname())
parser.add_argument('--port', metavar = 'port', type = int, nargs = '?', default = 8443)
args = parser.parse_args()
print("Running the server on: {} and port: {}".format(args.host, args.port))
sck = socket.socket()
sck.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def get_vehicles():
connection = psycopg2.connect(user = conf['db']['user'], password = conf['db']['password'], host = conf['db']['host'], port = conf['db']['port'], database = conf['db']['database'])
cursor = connection.cursor()
cursor.execute('SELECT id, internal_code FROM vehicles ORDER BY id ASC')
x = cursor.fetchall()
if(connection):
cursor.close()
connection.close()
return x
def on_new_client(client, connection):
ip = connection[0]
port = connection[1]
logging.info("The new connection was made from IP: {}, and port: {}!".format(ip, port))
while True:
msg = client.recv(1024)
strMsg = msg.strip().decode('utf-8', 'ignore')
if len(strMsg) > 0:
logging.info("Clean data: {}".format(msg))
handle_message(client, strMsg)
client.close()
logging.info("The client from ip: {}, and port: {}, has gracefully disconnected!".format(ip, port))
def handle_message(client, strMsg):
try:
index = strMsg.find("{")
tmp = strMsg[index:]
loaded_json = json.loads(tmp)
operation = loaded_json['OPERATION']
session_id = loaded_json['SESSION']
if operation == "CONNECT":
#device_id = loaded_json['PARAMETER']['DSNO']
connectionReply(client, session_id)
elif operation == "KEEPALIVE":
reply = json.dumps({"MODULE":"CERTIFICATE","OPERATION":"KEEPALIVE","SESSION":session_id})
client.send(reply.encode('utf-8'))
except Exception as e:
logging.error("Failed fam!: {}".format(e))
print("Failed fam!: {}".format(e))
def connectionReply(client, session_id):
payloadJson = json.dumps({"MODULE":"CERTIFICATE","OPERATION":"CONNECT","RESPONSE":{"DEVTYPE":1,"ERRORCAUSE":"","ERRORCODE":0,"MASKCMD":1,"PRO":"1.0.4","VCODE":""},"SESSION":session_id})
payload = str(payloadJson).replace(" ", "")
pLength = sys.getsizeof(payload)
midPart = pack('>i', pLength)
completeMessage = FIRST_PART + midPart + END_PART + payload.encode('utf-8')
print(completeMessage)
client.send(completeMessage)
#payloadJsonBinary = json.dumps({"MODULE":"CONFIGMODEL","OPERATION":"SET","PARAMETER":{"MDVR":{"KEYS":{"GV":1},"PGDSM":{"PGPS":{"EN":1}},"PIS":{"PC041245T":{"GU":{"EN":1,"IT":5}}},"PSI":{"CG":{"UEM":0}}}},"SESSION":session_id})
#payloadBinary = str(payloadJsonBinary).replace(" ", "")
#pLengthBinary = sys.getsizeof(payloadBinary)
#midPartBinary = pack('>i', pLengthBinary)
#completeMessageBinary = FIRST_PART + midPartBinary + END_PART
#client.send(completeMessageBinary.encode('utf-8'))
try:
sck.bind((args.host, args.port))
sck.listen(5)
#records = get_vehicles()
except Exception as e:
raise SystemExit("We could not bind the server on host: {} to port: {}, because: {}".format(args.host, args.port, e))
while True:
try:
client, ip = sck.accept()
threading._start_new_thread(on_new_client,(client, ip))
except KeyboardInterrupt:
print("Gracefully shutting down the server!")
sys.exit()
except Exception as e:
print("Well I did not anticipate this: {}".format(e))
sck.close()
def handleRegularReports(self, data):
try:
connection = psycopg2.connect(user = conf['db']['user'], password = conf['db']['password'], host = conf['db']['host'], port = conf['db']['port'], database = conf['db']['database'])
cursor = connection.cursor()
size = unpack('>i', data[0:12][4:8])[0] + 12
splitedData = list(self.chunked(size, data))
for msg in splitedData:
if msg[12:13] == b'\x00':
gpsStatus = 'ok'
elif msg[12:13] == b'\x01':
gpsStatus ='bad'
else:
gpsStatus = 'no gps'
longitude = unpack('>i',msg[16:20])[0]/1000000
latitude = unpack('>i',msg[20:24])[0]/1000000
speed = unpack('>i',msg[24:28])[0]/100
angle = unpack('>i',msg[28:32])[0]/100
altitude = unpack('>i',msg[32:36])[0]
date = datetime.strptime(msg[36:].decode('utf-8', 'ignore').rstrip('\x00') + "-05:00", '%Y%m%d%H%M%S%f%z').strftime('%Y/%m/%d %H:%M:%S:%f %z')
finalValues = {'gpsStatus': gpsStatus, 'latitude': latitude, 'longitude': longitude, 'speed': speed, 'angle': angle, 'altitude': altitude, 'date': date}
print(finalValues)
#self.createOnDb(connection, cursor, finalValues)
except (Exception, psycopg2.Error) as error:
if(connection):
logging.error("Failed to insert record into mobile table: {}".format(error))
finally:
if(connection):
cursor.close()
connection.close()
def chunked(self, size, source):
for i in range(0, len(source), size):
yield source[i:i+size]