-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (124 loc) · 4.21 KB
/
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
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
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
# Encoding: utf8
# Author: Almir A Braggio
# Date: 30 nov. 2015
# Socket Info.
port_number = 8081
host_name = ''
server_address = ('', port_number)
# Socket Timeout
sock_open_wait = 5 # Try to open the socket, if socket busy
sock_timeout = 600 # To close connection, if lose communication
# MongoDB
mongodb_host = 'mongodb://localhost:27017/'
mongodb_name = 'database'
mongodb_collection = 'posts'
mongoflag = False # Send to database
# Others
printflag = True # Write to standard output
# Library
import sys, time
import json
from gevent import socket
from gevent.server import StreamServer
from pymongo import MongoClient
# Continental VDO Protocol
from protocol_vdo import *
# Suntech ST300 Protocol
from protocol_suntech import *
# Print Standard Function
def printf(string='', end='\n'):
if printflag:
sys.stdout.write(string + end)
sys.stdout.flush()
# Integer Rounding Function
def round_int(value, step):
return int( int((value+step/2 )/step) *step)
# Saving on the database
def db(post={}):
if mongoflag:
# MongoDB connection
db_client = MongoClient(mongodb_host)
# Select database and collection
db = db_client[mongodb_name]
db_posts = db[mongodb_collection]
# Post!
post_id = db_posts.insert(post)
if printflag:
post_id_str = str(post_id)
printf('Post Id Count... "%s"' % db_posts.count())
db_client.close()
return;
# Handle Function
def handle_conn(sock, address):
post = 0
client_ip = address[0]
client_port = address[1]
client_id = id(sock)
printf('New connection from %s:%d (%d)' % (client_ip, client_port, client_id))
# Timeout too close connection, if lose communication
sock.settimeout(sock_timeout)
try:
while True:
msg = ''
msg = sock.recv(4096).decode()
# Null message?
if (len(msg) == 0):
break
try:
# Receiving new message
if msg:
#printf('Message: %s' % msg)
# Suntech Protocol
if (msg.split(';')[0] == 'ST300UEX'):
printf('Suntech ST300R Protocol')
post = protocol_suntech(msg)
elif (msg.split(';')[0] == 'ST300STT'):
printf('Suntech ST300R Default GPS Data')
post = 0 # ignore
# VDO Protocol
elif (msg[0:2] == 'SV'):
printf('Continental VDO Protocol')
post = protocol_vdo(msg)
# Undefined Protocol
else:
printf('Undefined Protocol')
post = 0
# Its a valid data
if (post):
print('Message:')
print(json.dumps(post,sort_keys=True,indent=4,separators=(',',':')))
# Save in database
db(post)
else:
printf('Invalid Data')
else:
printf('No more data from %s:%d (%d)' % (client_ip, client_port, client_id))
break
except KeyboardInterrupt:
raise
except IndexError as err:
printf('Exception IndexError %s' % (str(err),))
except:
printf('Exception %s' % ('unknown',))
raise
except socket.timeout:
printf('Connection timed out')
printf('Closing connection from %s:%d (%d)' % (client_ip, client_port, client_id))
sock.shutdown(socket.SHUT_WR)
sock.close()
while True:
try:
# Create a TCP Socket Server
# Limit to 1,000 simultaneous connections
server = StreamServer(server_address, handle_conn, spawn=1000)
printf('Socket successfully created on port: %s' % port_number)
# Run forever
server.serve_forever()
except KeyboardInterrupt:
break
except:
printf('Socket could not be created')
printf('Waiting for %d seconds' % sock_open_wait)
time.sleep(sock_open_wait)
sys.exit()