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