author | laurent |
Tue, 17 Jan 2012 16:44:00 +0100 | |
changeset 625 | b7062a7018ec |
parent 564 | 5024d42e1050 |
child 714 | 131ea7f237b9 |
permissions | -rw-r--r-- |
469 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
4 |
#based on the plcopen standard. |
|
5 |
# |
|
6 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
7 |
# |
|
8 |
#See COPYING file for copyrights details. |
|
9 |
# |
|
10 |
#This library is free software; you can redistribute it and/or |
|
11 |
#modify it under the terms of the GNU General Public |
|
12 |
#License as published by the Free Software Foundation; either |
|
13 |
#version 2.1 of the License, or (at your option) any later version. |
|
14 |
# |
|
15 |
#This library is distributed in the hope that it will be useful, |
|
16 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
#General Public License for more details. |
|
19 |
# |
|
20 |
#You should have received a copy of the GNU General Public |
|
21 |
#License along with this library; if not, write to the Free Software |
|
22 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
||
24 |
import wx |
|
519
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
25 |
import re |
516 | 26 |
import datetime |
469 | 27 |
|
28 |
#------------------------------------------------------------------------------- |
|
29 |
# Force Variable Dialog |
|
30 |
#------------------------------------------------------------------------------- |
|
31 |
||
32 |
LOCATIONDATATYPES = {"X" : ["BOOL"], |
|
33 |
"B" : ["SINT", "USINT", "BYTE", "STRING"], |
|
34 |
"W" : ["INT", "UINT", "WORD", "WSTRING"], |
|
35 |
"D" : ["DINT", "UDINT", "REAL", "DWORD"], |
|
36 |
"L" : ["LINT", "ULINT", "LREAL", "LWORD"]} |
|
37 |
||
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
38 |
def gen_get_function(f): |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
39 |
def get_function(v): |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
40 |
try: |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
41 |
return f(v) |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
42 |
except: |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
43 |
return None |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
44 |
return get_function |
469 | 45 |
|
476 | 46 |
getinteger = gen_get_function(int) |
47 |
getfloat = gen_get_function(float) |
|
48 |
getstring = gen_get_function(str) |
|
469 | 49 |
|
519
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
50 |
SECOND = 1000000 |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
51 |
MINUTE = 60 * SECOND |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
52 |
HOUR = 60 * MINUTE |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
53 |
DAY = 24 * HOUR |
525
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
54 |
|
564
5024d42e1050
Adding custom cell editor for interval value in task informations of ResourceEditor
laurent
parents:
536
diff
changeset
|
55 |
IEC_TIME_MODEL = re.compile("(?:(?:T|TIME)#)?(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M(?!S)_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?" % {"float": "[0-9]+(?:\.[0-9]+)?"}) |
525
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
56 |
IEC_DATE_MODEL = re.compile("(?:(?:D|DATE)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})") |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
57 |
IEC_DATETIME_MODEL = re.compile("(?:(?:DT|DATE_AND_TIME)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)") |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
58 |
IEC_TIMEOFDAY_MODEL = re.compile("(?:(?:TOD|TIME_OF_DAY)#)?([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)") |
519
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
59 |
|
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
60 |
def gettime(v): |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
61 |
result = IEC_TIME_MODEL.match(v.upper()) |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
62 |
if result is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
63 |
negative, days, hours, minutes, seconds, milliseconds = result.groups() |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
64 |
microseconds = 0 |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
65 |
not_null = False |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
66 |
for value, factor in [(days, DAY), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
67 |
(hours, HOUR), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
68 |
(minutes, MINUTE), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
69 |
(seconds, SECOND), |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
70 |
(milliseconds, 1000)]: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
71 |
if value is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
72 |
microseconds += float(value) * factor |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
73 |
not_null = True |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
74 |
if not not_null: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
75 |
return None |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
76 |
if negative is not None: |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
77 |
microseconds = -microseconds |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
78 |
return datetime.timedelta(microseconds=microseconds) |
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
79 |
|
722714c04dcd
Adding support for displaying and forcing TIME variables according to IEC 61131 literal format
laurent
parents:
516
diff
changeset
|
80 |
else: |
516 | 81 |
return None |
82 |
||
525
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
83 |
def getdate(v): |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
84 |
result = IEC_DATE_MODEL.match(v.upper()) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
85 |
if result is not None: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
86 |
year, month, day = result.groups() |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
87 |
try: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
88 |
date = datetime.datetime(int(year), int(month), int(day)) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
89 |
except ValueError, e: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
90 |
return None |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
91 |
base_date = datetime.datetime(1970, 1, 1) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
92 |
return date - base_date |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
93 |
else: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
94 |
return None |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
95 |
|
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
96 |
def getdatetime(v): |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
97 |
result = IEC_DATETIME_MODEL.match(v.upper()) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
98 |
if result is not None: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
99 |
year, month, day, hours, minutes, seconds = result.groups() |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
100 |
try: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
101 |
date = datetime.datetime(int(year), int(month), int(day), int(hours), int(minutes), int(float(seconds)), int((float(second) * SECOND) % SECOND)) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
102 |
except ValueError, e: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
103 |
return None |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
104 |
base_date = datetime.datetime(1970, 1, 1) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
105 |
return date - base_date |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
106 |
else: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
107 |
return None |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
108 |
|
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
109 |
def gettimeofday(v): |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
110 |
result = IEC_TIMEOFDAY_MODEL.match(v.upper()) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
111 |
if result is not None: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
112 |
hours, minutes, seconds = result.groups() |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
113 |
microseconds = 0 |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
114 |
for value, factor in [(hours, HOUR), |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
115 |
(minutes, MINUTE), |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
116 |
(seconds, SECOND)]: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
117 |
microseconds += float(value) * factor |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
118 |
return datetime.timedelta(microseconds=microseconds) |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
119 |
else: |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
120 |
return None |
516 | 121 |
|
536
288324dddfb8
Adding '0' and '1' as possible value to force boolean variables
laurent
parents:
525
diff
changeset
|
122 |
GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False, "0": False, "1": True}.get(x.upper(), None), |
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
123 |
"SINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
124 |
"INT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
125 |
"DINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
126 |
"LINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
127 |
"USINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
128 |
"UINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
129 |
"UDINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
130 |
"ULINT": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
131 |
"BYTE": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
132 |
"WORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
133 |
"DWORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
134 |
"LWORD": getinteger, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
135 |
"REAL": getfloat, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
136 |
"LREAL": getfloat, |
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
137 |
"STRING": getstring, |
516 | 138 |
"WSTRING": getstring, |
525
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
139 |
"TIME": gettime, |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
140 |
"DATE": getdate, |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
141 |
"DT": getdatetime, |
e8d5ab0855d3
Adding support for debugging and forcing DATE, DT and TOD in Beremiz interface
laurent
parents:
519
diff
changeset
|
142 |
"TOD": gettimeofday} |
469 | 143 |
|
144 |
||
145 |
class ForceVariableDialog(wx.TextEntryDialog): |
|
146 |
||
147 |
if wx.VERSION < (2, 6, 0): |
|
148 |
def Bind(self, event, function, id = None): |
|
149 |
if id is not None: |
|
150 |
event(self, id, function) |
|
151 |
else: |
|
152 |
event(self, function) |
|
153 |
||
479
2fab0eefa66e
Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents:
476
diff
changeset
|
154 |
def __init__(self, parent, iec_type, defaultValue=""): |
469 | 155 |
wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), |
479
2fab0eefa66e
Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents:
476
diff
changeset
|
156 |
caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = defaultValue, |
469 | 157 |
style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition) |
158 |
||
159 |
self.IEC_Type = iec_type |
|
160 |
if wx.VERSION >= (2, 8, 0): |
|
161 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) |
|
162 |
elif wx.VERSION >= (2, 6, 0): |
|
163 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) |
|
164 |
else: |
|
165 |
self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
166 |
||
167 |
def OnOK(self, event): |
|
168 |
value = self.GetSizer().GetItem(1).GetWindow().GetValue() |
|
169 |
if value == "": |
|
170 |
message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
171 |
message.ShowModal() |
|
172 |
message.Destroy() |
|
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
173 |
elif GetTypeValue[self.IEC_Type](value) is None: |
469 | 174 |
message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR) |
175 |
message.ShowModal() |
|
176 |
message.Destroy() |
|
177 |
else: |
|
178 |
self.EndModal(wx.ID_OK) |
|
179 |
||
180 |
def GetValue(self): |
|
475
9ca03fdff80f
ForceVariableDialog return a python value instead of string
laurent
parents:
469
diff
changeset
|
181 |
return GetTypeValue[self.IEC_Type](self.GetSizer().GetItem(1).GetWindow().GetValue()) |