winIDEA SDK
Loading...
Searching...
No Matches
webserver.py
1# This script is licensed under BSD License, see file LICENSE.txt, or search for `License` in the SDK online help.
2#
3# (c) TASKING Germany GmbH, 2023
4
5"""
6This script runs a web server, which provides page with target variables
7and their values. Real-time access is used when reading variables via
8isystem.connect.
9"""
10
11import isystem.connect as ic
12import sys
13
14winidea_id = ''
15
16
17if sys.version[0] == '2':
18 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer # Python 2
19elif sys.version[0] == '3':
20 from http.server import BaseHTTPRequestHandler, HTTPServer # Python 3
21else:
22 raise Exception('unknown version')
23
24
25# modify this list to add your variables. The same rules as in winIDEA Watch
26# window apply. For example, you can use modifier to print values in hex, dec,
27# or binary mode:
28# main_loop_counter, h
29# main_loop_counter, d
30# main_loop_counter, b
31targetVars = ['main_loop_counter', 'g_char', 'g_int']
32
33def read_vars():
34 """
35 This function builds contents of a web page, which contains table with
36 variables.
37 """
38 try: # refresh content every three seconds
39 web_str = '<html>\n' + \
40 '<head><meta http-equiv="refresh" content="3"/>\n' + \
41 ' <style>\n' + \
42 ' table, th {\n' \
43 ' border: 2px solid black;\n' + \
44 ' border-collapse: collapse;\n' + \
45 ' }\n' + \
46 ' table, td {\n' \
47 ' border: 1px solid black;\n' + \
48 ' border-collapse: collapse;\n' + \
49 ' }\n' + \
50 ' th, td {\n' + \
51 ' padding: 5px;\n' + \
52 ' }\n' + \
53 '</style>\n' + \
54 '</head><body><code><table><tr><th>Variable</th><th>Value</th></tr>\n'
55 for var in targetVars:
56 py_var = debugCtrl.evaluate(ic.IConnectDebug.fRealTime, var)
57 str_var = py_var.getResult()
58
59 web_str += '<tr><td><b>' + var + '</b></td><td>' + str_var + '</td></tr>\n'
60
61 web_str += '</table></code></body></html>'
62 except Exception as ex:
63 web_str = str(ex)
64
65 return web_str
66
67
68class HttpHandler(BaseHTTPRequestHandler):
69
70 def do_GET(self):
71 """
72 This method processes HTTP request. It always returns
73 the same content, regardless of URL.
74 """
75 try:
76 self.send_response(200)
77 self.send_header('Content-type', 'text/html')
78 self.end_headers()
79 lines = read_vars()
80 self.wfile.write(lines.encode('utf-8'))
81 return
82 except IOError:
83 self.send_error(404, 'File Not Found: ' + self.path)
84
85
86def main():
87 try:
88 server = HTTPServer(('', 8080), HttpHandler)
89 print('Http server is running ...')
90 server.serve_forever()
91 except KeyboardInterrupt:
92 print('Ctrl+C received, terminating the server!')
93 server.socket.close()
94
95
96if __name__ == '__main__':
97 connectionMgr = ic.ConnectionMgr()
98 connectionMgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
99 debugCtrl = ic.CDebugFacade(connectionMgr)
100 main()
101