Laurent@814: # -*- coding: utf-8 -*- Laurent@814: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Laurent@814: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Laurent@814: # andrej@1571: # See COPYING file for copyrights details. Laurent@814: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Laurent@814: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Laurent@814: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Laurent@814: Laurent@814: import re Laurent@814: import datetime Laurent@814: Laurent@814: import wx Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Helpers Laurent@814: #------------------------------------------------------------------------------- Laurent@814: andrej@1739: LOCATIONDATATYPES = {"X": ["BOOL"], andrej@1739: "B": ["SINT", "USINT", "BYTE", "STRING"], andrej@1739: "W": ["INT", "UINT", "WORD", "WSTRING"], andrej@1739: "D": ["DINT", "UDINT", "REAL", "DWORD"], andrej@1739: "L": ["LINT", "ULINT", "LREAL", "LWORD"]} Laurent@814: andrej@1736: Laurent@814: def gen_get_function(f): Laurent@814: def get_function(v): Laurent@814: try: Laurent@814: return f(v) Laurent@814: except: Laurent@814: return None Laurent@814: return get_function Laurent@814: andrej@1736: Laurent@878: def gen_get_string(delimiter): Laurent@878: STRING_MODEL = re.compile("%(delimiter)s([^%(delimiter)s]*)%(delimiter)s$" % {"delimiter": delimiter}) andrej@1750: Laurent@878: def get_string(v): Laurent@878: result = STRING_MODEL.match(v) Laurent@878: if result is not None: Laurent@878: return result.group(1) Laurent@878: return None Laurent@878: return get_string Laurent@878: andrej@1749: Laurent@814: getinteger = gen_get_function(int) Laurent@814: getfloat = gen_get_function(float) Laurent@878: getstring = gen_get_string("'") Laurent@878: getwstring = gen_get_string('"') Laurent@814: Laurent@814: SECOND = 1000000 Laurent@814: MINUTE = 60 * SECOND Laurent@814: HOUR = 60 * MINUTE Laurent@814: DAY = 24 * HOUR Laurent@814: Laurent@878: 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]+)?"}) Laurent@878: IEC_DATE_MODEL = re.compile("(?:(?:D|DATE)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})$") Laurent@878: 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]+)?)$") Laurent@878: IEC_TIMEOFDAY_MODEL = re.compile("(?:(?:TOD|TIME_OF_DAY)#)?([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)$") Laurent@814: andrej@1736: andrej@1730: def gettime(v): Laurent@814: result = IEC_TIME_MODEL.match(v.upper()) Laurent@814: if result is not None: Laurent@814: negative, days, hours, minutes, seconds, milliseconds = result.groups() Laurent@814: microseconds = 0 Laurent@814: not_null = False Laurent@814: for value, factor in [(days, DAY), Laurent@814: (hours, HOUR), Laurent@814: (minutes, MINUTE), Laurent@814: (seconds, SECOND), Laurent@814: (milliseconds, 1000)]: Laurent@814: if value is not None: Laurent@814: microseconds += float(value) * factor Laurent@814: not_null = True Laurent@814: if not not_null: Laurent@814: return None Laurent@814: if negative is not None: Laurent@814: microseconds = -microseconds Laurent@814: return datetime.timedelta(microseconds=microseconds) andrej@1730: andrej@1730: else: Laurent@814: return None Laurent@814: andrej@1736: Laurent@814: def getdate(v): Laurent@814: result = IEC_DATE_MODEL.match(v.upper()) Laurent@814: if result is not None: Laurent@814: year, month, day = result.groups() Laurent@814: try: Laurent@814: date = datetime.datetime(int(year), int(month), int(day)) Laurent@814: except ValueError, e: Laurent@814: return None Laurent@814: base_date = datetime.datetime(1970, 1, 1) Laurent@814: return date - base_date andrej@1730: else: Laurent@814: return None Laurent@814: andrej@1736: Laurent@814: def getdatetime(v): Laurent@814: result = IEC_DATETIME_MODEL.match(v.upper()) Laurent@814: if result is not None: Laurent@814: year, month, day, hours, minutes, seconds = result.groups() Laurent@814: try: Laurent@941: date = datetime.datetime(int(year), int(month), int(day), int(hours), int(minutes), int(float(seconds)), int((float(seconds) * SECOND) % SECOND)) Laurent@814: except ValueError, e: Laurent@814: return None Laurent@814: base_date = datetime.datetime(1970, 1, 1) Laurent@814: return date - base_date andrej@1730: else: Laurent@814: return None Laurent@814: andrej@1736: Laurent@814: def gettimeofday(v): Laurent@814: result = IEC_TIMEOFDAY_MODEL.match(v.upper()) Laurent@814: if result is not None: Laurent@814: hours, minutes, seconds = result.groups() Laurent@814: microseconds = 0 Laurent@814: for value, factor in [(hours, HOUR), Laurent@814: (minutes, MINUTE), Laurent@814: (seconds, SECOND)]: Laurent@814: microseconds += float(value) * factor Laurent@814: return datetime.timedelta(microseconds=microseconds) Laurent@814: else: Laurent@814: return None Laurent@814: andrej@1749: Laurent@814: GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False, "0": False, "1": True}.get(x.upper(), None), Laurent@814: "SINT": getinteger, Laurent@814: "INT": getinteger, Laurent@814: "DINT": getinteger, Laurent@814: "LINT": getinteger, Laurent@814: "USINT": getinteger, Laurent@814: "UINT": getinteger, Laurent@814: "UDINT": getinteger, Laurent@814: "ULINT": getinteger, Laurent@814: "BYTE": getinteger, Laurent@814: "WORD": getinteger, Laurent@814: "DWORD": getinteger, Laurent@814: "LWORD": getinteger, Laurent@814: "REAL": getfloat, Laurent@814: "LREAL": getfloat, Laurent@814: "STRING": getstring, Laurent@878: "WSTRING": getwstring, Laurent@814: "TIME": gettime, Laurent@814: "DATE": getdate, Laurent@814: "DT": getdatetime, Laurent@814: "TOD": gettimeofday} Laurent@814: Laurent@814: #------------------------------------------------------------------------------- Laurent@814: # Force Variable Dialog Laurent@814: #------------------------------------------------------------------------------- Laurent@814: andrej@1736: Laurent@814: class ForceVariableDialog(wx.TextEntryDialog): Laurent@814: Laurent@814: def __init__(self, parent, iec_type, defaultValue=""): andrej@1744: wx.TextEntryDialog.__init__(self, parent, message=_("Forcing Variable Value"), andrej@1744: caption=_("Please enter value for a \"%s\" variable:") % iec_type, defaultValue=defaultValue, andrej@1745: style=wx.OK | wx.CANCEL | wx.CENTRE, pos=wx.DefaultPosition) andrej@1730: andrej@1730: self.IEC_Type = iec_type andrej@1730: andrej@1730: self.Bind(wx.EVT_BUTTON, self.OnOK, Laurent@814: self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton()) andrej@1742: self.ValueTextCtrl = self.GetSizer().GetItem(1).GetWindow() andrej@1701: if self.IEC_Type == "BOOL": andrej@1701: self.ToggleButton = wx.ToggleButton(self, label=_("Toggle value")) andrej@1742: value = GetTypeValue[self.IEC_Type](defaultValue) andrej@1701: if value is not None: andrej@1701: self.ToggleButton.SetValue(value) andrej@1701: andrej@1742: border = self.GetSizer().GetItem(1).GetBorder() andrej@1701: self.GetSizer().Insert(before=2, item=self.ToggleButton, andrej@1701: border=border, andrej@1745: flag=wx.LEFT | wx.RIGHT | wx.EXPAND) andrej@1701: self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleBoolValue, self.ToggleButton) andrej@1701: andrej@1701: self.Fit() andrej@1701: andrej@1701: def ToggleBoolValue(self, event): andrej@1742: value = self.ToggleButton.GetValue() andrej@1701: self.ValueTextCtrl.SetValue(unicode(value)) andrej@1701: Laurent@814: def OnOK(self, event): Laurent@814: message = None andrej@1701: value = self.ValueTextCtrl.GetValue() Laurent@814: if value == "": Laurent@814: message = _("You must type a value!") Laurent@814: elif GetTypeValue[self.IEC_Type](value) is None: andrej@1744: message = _("Invalid value \"{a1}\" for \"{a2}\" variable!").format(a1=value, a2=self.IEC_Type) Laurent@814: if message is not None: andrej@1745: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) Laurent@814: dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@814: else: Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: event.Skip() Laurent@814: Laurent@814: def GetValue(self): Laurent@814: return GetTypeValue[self.IEC_Type](wx.TextEntryDialog.GetValue(self))