-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy-car-server.py
executable file
·41 lines (36 loc) · 1.32 KB
/
dummy-car-server.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
#!/usr/bin/python
import json
import math
import random
import sys
import time
import BaseHTTPServer
import SimpleHTTPServer
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
"""Respond to a GET request."""
if self.path == '/get.json':
time.sleep(0.25)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
offset = math.pow(math.sin(time.time() * 0.2) * 0.5 + 0.5, 2)
data = {
'time':time.time(),
'minCellVoltage':2.4 + offset * 0.5,
'avgCellVoltage':2.6 + offset * 0.5,
'maxCellVoltage':2.8 + offset * 0.5,
'loadCurrentAmps':-40 + offset * 300,
'minCellBoardTempC': -20 + offset * 70,
'avgCellBoardTempC': -15 + offset * 70,
'maxCellBoardTempC': -10 + offset * 70,
}
self.wfile.write(json.dumps(data))
else:
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
# BaseHTTPServer.HTTPServer
# HandlerClass.protocol_version = "HTTP/1.1"
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8002), Handler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()