60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
#!/usr/bin/env python
|
|
"""Login/download/start the active application using ScriptEngine online API."""
|
|
|
|
from __future__ import print_function
|
|
|
|
import os
|
|
|
|
from _common import emit, load_payload
|
|
|
|
|
|
def main():
|
|
payload = load_payload()
|
|
project_path = payload.get("project_path")
|
|
target_name = payload.get("target_name", "")
|
|
start_application = bool(payload.get("start_application", False))
|
|
force_download = bool(payload.get("force_download", True))
|
|
|
|
if not project_path:
|
|
raise ValueError("project_path is required.")
|
|
pp = os.path.abspath(os.path.expanduser(project_path))
|
|
if not os.path.exists(pp):
|
|
raise RuntimeError("Project path does not exist: {0}".format(pp))
|
|
if "projects" not in globals() or "online" not in globals():
|
|
raise RuntimeError("Run through CODESYS ScriptEngine (--runscript).")
|
|
|
|
if projects.primary:
|
|
projects.primary.close()
|
|
proj = projects.open(pp, primary=True, allow_readonly=False)
|
|
app = proj.active_application
|
|
if app is None:
|
|
raise RuntimeError("No active application found in project.")
|
|
|
|
online_app = online.create_online_application(app)
|
|
# Try online change first; CODESYS handles download when needed.
|
|
login_mode = OnlineChangeOption.Try if force_download else OnlineChangeOption.Never
|
|
online_app.login(login_mode, True)
|
|
|
|
started = False
|
|
if start_application and online_app.application_state != ApplicationState.run:
|
|
online_app.start()
|
|
started = True
|
|
|
|
online_app.logout()
|
|
proj.close()
|
|
|
|
emit(
|
|
{
|
|
"ok": True,
|
|
"action": "download_to_device",
|
|
"project_path": pp,
|
|
"target_name": target_name,
|
|
"start_application": start_application,
|
|
"started": started,
|
|
}
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|