author | Edouard Tisserant <edouard@beremiz.fr> |
Fri, 11 Oct 2024 10:34:15 +0200 | |
changeset 4026 | a3cf9f635952 |
parent 3812 | 92e528718733 |
permissions | -rw-r--r-- |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
3 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
4 |
import os |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
5 |
import sys |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
6 |
from functools import wraps |
3619
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
7 |
from threading import Timer |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
8 |
from datetime import datetime |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
9 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
import click |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
import fake_wx |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
13 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
14 |
from ProjectController import ProjectController |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
15 |
from LocalRuntimeMixin import LocalRuntimeMixin |
3619
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
16 |
from runtime.loglevels import LogLevelsCount, LogLevels |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
17 |
|
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
19 |
class Log: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
20 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
def __init__(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
self.crlfpending = False |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
def write(self, s): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
25 |
if s: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
if self.crlfpending: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
27 |
sys.stdout.write("\n") |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
28 |
sys.stdout.write(s) |
3548
da7b461f54b7
CLI: Explicitely wait for user interrupt (-k) and flush stdout each line.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3539
diff
changeset
|
29 |
sys.stdout.flush() |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
self.crlfpending = 0 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
def write_error(self, s): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
if s: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
34 |
self.write("Error: "+s) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
35 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
36 |
def write_warning(self, s): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
37 |
if s: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
self.write("Warning: "+s) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
39 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
40 |
def flush(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
41 |
sys.stdout.flush() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
43 |
def isatty(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
44 |
return False |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
def progress(self, s): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
47 |
if s: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
48 |
sys.stdout.write(s+"\r") |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
49 |
self.crlfpending = True |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
50 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
def with_project_loaded(func): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
@wraps(func) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
def func_wrapper(self, *args, **kwargs): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
if not self.HasOpenedProject(): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
if self.check_and_load_project(): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |
return 1 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
self.apply_config() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
return func(self, *args, **kwargs) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
60 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
61 |
return func_wrapper |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
62 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
63 |
def connected(func): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
64 |
@wraps(func) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
65 |
def func_wrapper(self, *args, **kwargs): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
66 |
if self._connector is None: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
67 |
if self.session.uri: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
68 |
self.BeremizRoot.setURI_location(self.session.uri) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
69 |
if not self._Connect(): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
70 |
return 1 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
71 |
return func(self, *args, **kwargs) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
72 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
73 |
return func_wrapper |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
74 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
75 |
class CLIController(LocalRuntimeMixin, ProjectController): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
76 |
def __init__(self, session): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
77 |
self.session = session |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
78 |
log = Log() |
3539
c2eec6aae07e
CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3501
diff
changeset
|
79 |
LocalRuntimeMixin.__init__(self, log, use_gui=False) |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
80 |
ProjectController.__init__(self, None, log) |
3619
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
81 |
|
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
82 |
def _SetConnector(self, connector, update_status=True): |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
83 |
self._connector = connector |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
84 |
self.previous_log_count = [None]*LogLevelsCount |
3812
92e528718733
CLI: simpler status polling, fixing pyro5 thread
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3787
diff
changeset
|
85 |
if connector is None and update_status: |
3619
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
86 |
self.UpdateMethodsFromPLCStatus() |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
87 |
|
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
88 |
def UpdatePLCLog(self, log_count): |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
89 |
connector = self._connector |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
90 |
new_messages = [] |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
91 |
if connector: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
92 |
for level, count, prev in zip( |
3750
f62625418bff
automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents:
3619
diff
changeset
|
93 |
range(LogLevelsCount), log_count, self.previous_log_count): |
3619
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
94 |
if count is not None and prev != count: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
95 |
if prev is None: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
96 |
dump_end = max(-1, count - 10) |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
97 |
else: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
98 |
dump_end = prev - 1 |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
99 |
for msgidx in range(count-1, dump_end, -1): |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
100 |
message = connector.GetLogMessage(level, msgidx) |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
101 |
if message is not None: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
102 |
msg, _tick, tv_sec, tv_nsec = message |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
103 |
date = datetime.utcfromtimestamp(tv_sec + tv_nsec * 1e-9) |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
104 |
txt = "%s at %s: %s\n" % (LogLevels[level], date.isoformat(' '), msg) |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
105 |
new_messages.append((date,txt)) |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
106 |
else: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
107 |
break |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
108 |
self.previous_log_count[level] = count |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
109 |
new_messages.sort() |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
110 |
for date, txt in new_messages: |
f75b11506fb2
CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3548
diff
changeset
|
111 |
self.logger.write(txt) |
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
112 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
113 |
def check_and_load_project(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
114 |
if not os.path.isdir(self.session.project_home): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
115 |
self.logger.write_error( |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
116 |
_("\"%s\" is not a valid Beremiz project\n") % self.session.project_home) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
117 |
return True |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
118 |
|
3787
56ceda9889f8
CLIController: use absolute project path
GP Orcullo <kinsamanka@gmail.com>
parents:
3775
diff
changeset
|
119 |
if not os.path.isabs(self.session.project_home): |
56ceda9889f8
CLIController: use absolute project path
GP Orcullo <kinsamanka@gmail.com>
parents:
3775
diff
changeset
|
120 |
self.session.project_home = os.path.join(os.getcwd(), self.session.project_home) |
56ceda9889f8
CLIController: use absolute project path
GP Orcullo <kinsamanka@gmail.com>
parents:
3775
diff
changeset
|
121 |
|
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
122 |
errmsg, error = self.LoadProject(self.session.project_home, self.session.buildpath) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
123 |
if error: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
124 |
self.logger.write_error(errmsg) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
125 |
return True |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
126 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
127 |
def apply_config(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
128 |
for k,v in self.session.config: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
129 |
self.SetParamsAttribute("BeremizRoot."+k, v) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
130 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
131 |
@with_project_loaded |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
132 |
def build_project(self, target): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
133 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
134 |
if target: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
135 |
self.SetParamsAttribute("BeremizRoot.TargetType", target) |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
136 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
137 |
return 0 if self._Build() else 1 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
138 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
139 |
@with_project_loaded |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
140 |
@connected |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
141 |
def transfer_project(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
142 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
143 |
return 0 if self._Transfer() else 1 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
144 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
145 |
@with_project_loaded |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
146 |
@connected |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
147 |
def run_project(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
148 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
149 |
return 0 if self._Run() else 1 |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
150 |
|
3775 | 151 |
@with_project_loaded |
152 |
@connected |
|
153 |
def stop_project(self): |
|
154 |
||
155 |
return 0 if self._Stop() else 1 |
|
156 |
||
3501
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
157 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
158 |
def finish(self): |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
159 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
160 |
self._Disconnect() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
161 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
162 |
if not self.session.keep: |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
163 |
self.KillLocalRuntime() |
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
164 |
|
fa291393aac7
Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
165 |