-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_handler.py
117 lines (91 loc) · 3.26 KB
/
command_handler.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
""" Handles the commands to the Street Smart browser """
import json
import logging
from logger import Logger # pylint: disable=import-error
from logger import log # pylint: disable=import-error
cef_browser = None
@log(level=logging.INFO)
def js_init(args):
""" Calls the cef browser to initialize the StreetSmart api
args is a json string
"""
try:
settings = json.loads(args)
userSettings = settings["userSettings"]
addressSettings = settings["addressSettings"]
configSettings = settings["configSettings"]
cef_browser.ExecuteFunction("initApi", userSettings,
addressSettings, configSettings)
except:
logging.exception("Probleem")
@log(level=logging.DEBUG)
def js_open(args):
''' Calls the javascript function to open a coordinate '''
print("Execute javascript open with ", args)
cef_browser.ExecuteFunction("open", args)
@log(level=logging.DEBUG)
def js_addoverlay(args):
""" Adds an overlay to the Street Smart browser """
geojson, name, srs, sldText, color = args.split('/')
geojson = geojson.replace("__", r"/")
sldText = sldText.replace("__", r"/")
print("Execute addoverlay with {}, {}, {}, {}, {}".format(
geojson, name, srs, sldText, color))
cef_browser.ExecuteFunction(
"addOverlay", geojson, name, srs, sldText, color)
@log(level=logging.DEBUG)
def js_removeoverlay(_):
""" Send command to remove all overlay layers """
cef_browser.ExecuteFunction("removeOverlay")
@log(level=logging.DEBUG)
def js_restartmeasure(args):
""" Calls cef browser to stop and start a measurement
args in ['point', 'polyline', 'polygon']
"""
cef_browser.ExecuteFunction("stopMeasure")
cef_browser.ExecuteFunction("startMeasure", args)
@log(level=logging.DEBUG)
def js_startmeasure(args):
""" Calls cef browser to start a measurement
args in ['point', 'polyline', 'polygon']
"""
cef_browser.ExecuteFunction("startMeasure", args)
@log(level=logging.DEBUG)
def js_stopmeasure(args):
""" Calls cef browser to stop a measurement """
cef_browser.ExecuteFunction("stopMeasure")
@log(level=logging.DEBUG)
def js_getmeasure(args):
""" Calls cef browser for measurement """
cef_browser.ExecuteFunction("getMeasure")
@log(level=logging.INFO)
def stopviewer(args):
"""Closes the browser"""
cef_browser.CloseBrowser(False)
js_commands = {
"initapi": js_init,
"open": js_open,
"overlay": js_addoverlay,
"removeoverlay": js_removeoverlay,
"startmeasure": js_startmeasure,
"restartmeasure": js_restartmeasure,
"stopmeasure": js_stopmeasure,
"getmeasure": js_getmeasure,
"stopviewer": stopviewer,
}
class CommandHandler():
''' Dispatches the commands for the viewer '''
@log(level=logging.INFO)
def __init__(self, browser, log_port=0):
global cef_browser
cef_browser = browser
if log_port != 0:
Logger(__name__, log_port).get()
@staticmethod
@log(print_args=True)
def execute(command):
""" Calls the corresponding function for the given command """
if command[0] in js_commands:
js_commands[command[0]](command[1])
else:
logging.debug("Keyword %s not found", command[0])