author | Edouard Tisserant <edouard@beremiz.fr> |
Fri, 30 Aug 2024 11:50:23 +0200 | |
changeset 4008 | f30573e98600 |
parent 3750 | f62625418bff |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
6 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
8 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
10 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
11 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
12 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
13 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
15 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
16 |
# This program is distributed in the hope that it will be useful, |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
19 |
# GNU General Public License for more details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
20 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
21 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
22 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1365
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
1881
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1839
diff
changeset
|
25 |
|
3750
f62625418bff
automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents:
2437
diff
changeset
|
26 |
|
f62625418bff
automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents:
2437
diff
changeset
|
27 |
|
814 | 28 |
import datetime |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
29 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
30 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
31 |
# ------------------------------------------------------------------------------- |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
32 |
# Date and Time conversion function |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
33 |
# ------------------------------------------------------------------------------- |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
34 |
|
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
35 |
SECOND = 1000000 # Number of microseconds in one second |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
36 |
MINUTE = 60 * SECOND # Number of microseconds in one minute |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
37 |
HOUR = 60 * MINUTE # Number of microseconds in one hour |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
38 |
DAY = 24 * HOUR # Number of microseconds in one day |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
39 |
|
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
40 |
# Date corresponding to Epoch (1970 January the first) |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
41 |
DATE_ORIGIN = datetime.datetime(1970, 1, 1) |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
42 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
43 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
44 |
def get_microseconds(value): |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
45 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
46 |
Function converting time duration expressed in day, second and microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
47 |
into one expressed in microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
48 |
@param value: Time duration to convert |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
49 |
@return: Time duration expressed in microsecond |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
50 |
""" |
1764
d5df428640ff
clean-up: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
51 |
return float(value.days * DAY + |
d5df428640ff
clean-up: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
52 |
value.seconds * SECOND + |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
53 |
value.microseconds) |
814 | 54 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
55 |
|
814 | 56 |
def generate_time(value): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
57 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
58 |
Function converting time duration expressed in day, second and microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
59 |
into a IEC 61131 TIME literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
60 |
@param value: Time duration to convert |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
61 |
@return: IEC 61131 TIME literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
62 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
63 |
microseconds = get_microseconds(value) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
64 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
65 |
# Get absolute microseconds value and save if it was negative |
814 | 66 |
negative = microseconds < 0 |
67 |
microseconds = abs(microseconds) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
68 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
69 |
# TIME literal prefix |
814 | 70 |
data = "T#" |
71 |
if negative: |
|
72 |
data += "-" |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
73 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
74 |
# In TIME literal format, it isn't mandatory to indicate null values |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
75 |
# if no greater non-null values are available. This variable is used to |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
76 |
# inhibit formatting until a non-null value is found |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
77 |
not_null = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
78 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
79 |
for val, format in [ |
2437
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
80 |
(int(microseconds) // DAY, "%dd"), # Days |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
81 |
((int(microseconds) % DAY) // HOUR, "%dh"), # Hours |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
82 |
((int(microseconds) % HOUR) // MINUTE, "%dm"), # Minutes |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
83 |
((int(microseconds) % MINUTE) // SECOND, "%ds")]: # Seconds |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
84 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
85 |
# Add value to TIME literal if value is non-null or another non-null |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
86 |
# value have already be found |
814 | 87 |
if val > 0 or not_null: |
88 |
data += format % val |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
89 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
90 |
# Update non-null variable |
814 | 91 |
not_null = True |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
92 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
93 |
# In any case microseconds have to be added to TIME literal |
814 | 94 |
data += "%gms" % (microseconds % SECOND / 1000.) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
95 |
|
814 | 96 |
return data |
97 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
98 |
|
814 | 99 |
def generate_date(value): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
100 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
101 |
Function converting time duration expressed in day, second and microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
102 |
into a IEC 61131 DATE literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
103 |
@param value: Time duration to convert |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
104 |
@return: IEC 61131 DATE literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
105 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
106 |
return (DATE_ORIGIN + value).strftime("DATE#%Y-%m-%d") |
814 | 107 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
108 |
|
814 | 109 |
def generate_datetime(value): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
110 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
111 |
Function converting time duration expressed in day, second and microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
112 |
into a IEC 61131 DATE_AND_TIME literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
113 |
@param value: Time duration to convert |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
114 |
@return: IEC 61131 DATE_AND_TIME literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
115 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
116 |
return (DATE_ORIGIN + value).strftime("DT#%Y-%m-%d-%H:%M:%S.%f") |
814 | 117 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
118 |
|
814 | 119 |
def generate_timeofday(value): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
120 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
121 |
Function converting time duration expressed in day, second and microseconds |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
122 |
into a IEC 61131 TIME_OF_DAY literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
123 |
@param value: Time duration to convert |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
124 |
@return: IEC 61131 TIME_OF_DAY literal |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
125 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
126 |
microseconds = get_microseconds(value) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
127 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
128 |
# TIME_OF_DAY literal prefix |
814 | 129 |
data = "TOD#" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
130 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
131 |
for val, format in [ |
2437
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
132 |
(int(microseconds) // HOUR, "%2.2d:"), # Hours |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
133 |
((int(microseconds) % HOUR) // MINUTE, "%2.2d:"), # Minutes |
105c20fdeb19
python3 support: pylint, W1619 #(old-division) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset
|
134 |
((int(microseconds) % MINUTE) // SECOND, "%2.2d."), # Seconds |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
135 |
(microseconds % SECOND, "%6.6d")]: # Microseconds |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
136 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
137 |
# Add value to TIME_OF_DAY literal |
814 | 138 |
data += format % val |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
139 |
|
814 | 140 |
return data |
141 |
||
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1737
diff
changeset
|
142 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
143 |
# Dictionary of translation functions from value send by debugger to IEC |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
144 |
# literal stored by type |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
145 |
TYPE_TRANSLATOR = { |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
146 |
"TIME": generate_time, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
147 |
"DATE": generate_date, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
148 |
"DT": generate_datetime, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
149 |
"TOD": generate_timeofday, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
150 |
"STRING": lambda v: "'%s'" % v, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
151 |
"WSTRING": lambda v: '"%s"' % v, |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
152 |
"REAL": lambda v: "%.6g" % v, |
1214
2ef048b5383c
Added support for opening text viewer by default and toggling between GraphicViewer and TextViewer
Laurent Bessard
parents:
1193
diff
changeset
|
153 |
"LREAL": lambda v: "%.6g" % v} |
814 | 154 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
155 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
156 |
# ------------------------------------------------------------------------------- |
814 | 157 |
# Debug Data Consumer Class |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
158 |
# ------------------------------------------------------------------------------- |
814 | 159 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
160 |
|
1831
56b48961cc68
fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
161 |
class DebugDataConsumer(object): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
162 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
163 |
Class that implements an element that consumes debug values |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
164 |
Value update can be inhibited during the time the associated Debug Viewer is |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
165 |
refreshing |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
166 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
167 |
|
814 | 168 |
def __init__(self): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
169 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
170 |
Constructor |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
171 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
172 |
# Debug value and forced flag |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
173 |
self.Value = None |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
174 |
self.Forced = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
175 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
176 |
# Store debug value and forced flag when value update is inhibited |
814 | 177 |
self.LastValue = None |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
178 |
self.LastForced = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
179 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
180 |
# Value IEC data type |
814 | 181 |
self.DataType = None |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
182 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
183 |
# Flag that value update is inhibited |
814 | 184 |
self.Inhibited = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
185 |
|
814 | 186 |
def Inhibit(self, inhibit): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
187 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
188 |
Set flag to inhibit or activate value update |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
189 |
@param inhibit: Inhibit flag |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
190 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
191 |
# Save inhibit flag |
814 | 192 |
self.Inhibited = inhibit |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
193 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
194 |
# When reactivated update value and forced flag with stored values |
814 | 195 |
if not inhibit and self.LastValue is not None: |
196 |
self.SetForced(self.LastForced) |
|
197 |
self.SetValue(self.LastValue) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
198 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
199 |
# Reset stored values |
814 | 200 |
self.LastValue = None |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
201 |
self.LastForced = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
202 |
|
814 | 203 |
def SetDataType(self, data_type): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
204 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
205 |
Set value IEC data type |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
206 |
@param data_type: Value IEC data type |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
207 |
""" |
814 | 208 |
self.DataType = data_type |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
209 |
|
1365
debc97102b23
Added support for optimizing debug, preventing to filling buffers with only the last value for debug data consumers that only show the current state
Laurent Bessard
parents:
1363
diff
changeset
|
210 |
def NewValues(self, tick, values, raw="BOOL"): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
211 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
212 |
Function called by debug thread when a new debug value is available |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
213 |
@param tick: PLC tick when value was captured |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
214 |
@param value: Value captured |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
215 |
@param forced: Forced flag, True if value is forced (default: False) |
1220
16c283246241
Fixed in DebugDataConsumer value translation inhibit support
Laurent Bessard
parents:
1214
diff
changeset
|
216 |
@param raw: Data type of values not translated (default: 'BOOL') |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
217 |
""" |
1365
debc97102b23
Added support for optimizing debug, preventing to filling buffers with only the last value for debug data consumers that only show the current state
Laurent Bessard
parents:
1363
diff
changeset
|
218 |
value, forced = values |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
219 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
220 |
# Translate value to IEC literal |
1220
16c283246241
Fixed in DebugDataConsumer value translation inhibit support
Laurent Bessard
parents:
1214
diff
changeset
|
221 |
if self.DataType != raw: |
1214
2ef048b5383c
Added support for opening text viewer by default and toggling between GraphicViewer and TextViewer
Laurent Bessard
parents:
1193
diff
changeset
|
222 |
value = TYPE_TRANSLATOR.get(self.DataType, str)(value) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
223 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
224 |
# Store value and forced flag when value update is inhibited |
814 | 225 |
if self.Inhibited: |
226 |
self.LastValue = value |
|
227 |
self.LastForced = forced |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
228 |
|
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
229 |
# Update value and forced flag in any other case |
814 | 230 |
else: |
231 |
self.SetForced(forced) |
|
232 |
self.SetValue(value) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
233 |
|
814 | 234 |
def SetValue(self, value): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
235 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
236 |
Update value. |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
237 |
May be overridden by inherited classes |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
238 |
@param value: New value |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
239 |
""" |
814 | 240 |
self.Value = value |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
241 |
|
814 | 242 |
def GetValue(self): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
243 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
244 |
Return current value |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
245 |
@return: Current value |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
246 |
""" |
814 | 247 |
return self.Value |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
248 |
|
814 | 249 |
def SetForced(self, forced): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
250 |
""" |
1193
59c196884fec
Moved and start to rewrite DebugVariablePanel splitting it into multiple files
Laurent Bessard
parents:
1176
diff
changeset
|
251 |
Update Forced flag. |
59c196884fec
Moved and start to rewrite DebugVariablePanel splitting it into multiple files
Laurent Bessard
parents:
1176
diff
changeset
|
252 |
May be overridden by inherited classes |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
253 |
@param forced: New forced flag |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
254 |
""" |
814 | 255 |
self.Forced = forced |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
256 |
|
814 | 257 |
def IsForced(self): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
258 |
""" |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
259 |
Indicate if current value is forced |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
260 |
@return: Current forced flag |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
261 |
""" |
814 | 262 |
return self.Forced |