Beremiz_service.py
author Laurent Bessard
Thu, 10 Oct 2013 17:04:23 +0200
changeset 1355 9183fb765f16
parent 1270 aa9bc3e6181d
child 1434 6e0cd0ceabb7
permissions -rwxr-xr-x
Fixed bug when modifying power rail in LD Viewer
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     1
#!/usr/bin/env python
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     2
# -*- coding: utf-8 -*-
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     3
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     6
#
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     8
#
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
     9
#See COPYING file for copyrights details.
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    10
#
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    11
#This library is free software; you can redistribute it and/or
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    12
#modify it under the terms of the GNU General Public
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    13
#License as published by the Free Software Foundation; either
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    15
#
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    19
#General Public License for more details.
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    20
#
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    21
#You should have received a copy of the GNU General Public
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    22
#License along with this library; if not, write to the Free Software
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    24
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    25
import os, sys, getopt
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
    26
from threading import Thread
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    27
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    28
def usage():
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    29
    print """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    30
Usage of Beremiz PLC execution service :\n
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    31
%s {[-n servicename] [-i IP] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    32
           -n        - zeroconf service name (default:disabled)
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    33
           -i        - IP address of interface to bind to (default:localhost)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    34
           -p        - port number default:3000
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    35
           -h        - print this help text and quit
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    36
           -a        - autostart PLC (0:disable 1:enable) (default:0)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    37
           -x        - enable/disable wxTaskbarIcon (0:disable 1:enable) (default:1)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    38
           -t        - enable/disable Twisted web interface (0:disable 1:enable) (default:1)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    39
           
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    40
           working_dir - directory where are stored PLC files
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    41
"""%sys.argv[0]
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    42
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    43
try:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    44
    opts, argv = getopt.getopt(sys.argv[1:], "i:p:n:x:t:a:h")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    45
except getopt.GetoptError, err:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    46
    # print help information and exit:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    47
    print str(err) # will print something like "option -a not recognized"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    48
    usage()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    49
    sys.exit(2)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    50
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    51
# default values
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    52
given_ip = None
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    53
port = 3000
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    54
servicename = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    55
autostart = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    56
enablewx = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    57
havewx = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    58
enabletwisted = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    59
havetwisted = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    60
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    61
for o, a in opts:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    62
    if o == "-h":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    63
        usage()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    64
        sys.exit()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    65
    elif o == "-i":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    66
        if len(a.split(".")) == 4 or a == "localhost":
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    67
            given_ip = a
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    68
        else:
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    69
            usage()
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
    70
            sys.exit()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    71
    elif o == "-p":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    72
        # port: port that the service runs on
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    73
        port = int(a)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    74
    elif o == "-n":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    75
        servicename = a
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    76
    elif o == "-x":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    77
        enablewx = int(a)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    78
    elif o == "-t":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    79
        enabletwisted = int(a)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    80
    elif o == "-a":
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    81
        autostart = int(a)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    82
    else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    83
        usage()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    84
        sys.exit()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    85
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    86
if len(argv) > 1:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    87
    usage()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    88
    sys.exit()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    89
elif len(argv) == 1:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    90
    WorkingDir = argv[0]
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    91
    os.chdir(WorkingDir)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    92
elif len(argv) == 0:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    93
    WorkingDir = os.getcwd()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    94
    argv=[WorkingDir]
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    95
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    96
import __builtin__
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    97
if __name__ == '__main__':
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    98
    __builtin__.__dict__['_'] = lambda x: x
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
    99
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   100
if enablewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   101
    try:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   102
        import wx, re
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   103
        from threading import Thread, currentThread
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   104
        from types import *
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   105
        havewx = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   106
    except:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   107
        print "Wx unavailable !"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   108
        havewx = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   109
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   110
    if havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   111
        app=wx.App(redirect=False)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   112
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   113
        # Import module for internationalization
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   114
        import gettext
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   115
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   116
        CWD = os.path.split(os.path.realpath(__file__))[0]
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   117
        def Bpath(*args):
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   118
            return os.path.join(CWD,*args)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   119
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   120
        # Get folder containing translation files
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   121
        localedir = os.path.join(CWD,"locale")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   122
        # Get the default language
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   123
        langid = wx.LANGUAGE_DEFAULT
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   124
        # Define translation domain (name of translation files)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   125
        domain = "Beremiz"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   126
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   127
        # Define locale for wx
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   128
        loc = __builtin__.__dict__.get('loc', None)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   129
        if loc is None:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   130
            loc = wx.Locale(langid)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   131
            __builtin__.__dict__['loc'] = loc
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   132
        # Define location for searching translation files
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   133
        loc.AddCatalogLookupPathPrefix(localedir)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   134
        # Define locale domain
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   135
        loc.AddCatalog(domain)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   136
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   137
        def unicode_translation(message):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   138
            return wx.GetTranslation(message).encode("utf-8")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   139
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   140
        if __name__ == '__main__':
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   141
            __builtin__.__dict__['_'] = wx.GetTranslation#unicode_translation
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   142
        
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   143
        defaulticon = wx.Image(Bpath("images", "brz.png"))
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   144
        starticon = wx.Image(Bpath("images", "icoplay24.png"))
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   145
        stopicon = wx.Image(Bpath("images", "icostop24.png"))
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   146
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   147
        class ParamsEntryDialog(wx.TextEntryDialog):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   148
            if wx.VERSION < (2, 6, 0):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   149
                def Bind(self, event, function, id = None):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   150
                    if id is not None:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   151
                        event(self, id, function)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   152
                    else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   153
                        event(self, function)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   154
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   155
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   156
            def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   157
                               style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   158
                wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   159
                
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   160
                self.Tests = []
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   161
                if wx.VERSION >= (2, 8, 0):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   162
                    self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId())
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   163
                elif wx.VERSION >= (2, 6, 0):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   164
                    self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   165
                else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   166
                    self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   167
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   168
            def OnOK(self, event):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   169
                value = self.GetValue()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   170
                texts = {"value" : value}
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   171
                for function, message in self.Tests:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   172
                    if not function(value):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   173
                        message = wx.MessageDialog(self, message%texts, _("Error"), wx.OK|wx.ICON_ERROR)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   174
                        message.ShowModal()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   175
                        message.Destroy()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   176
                        return
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   177
                self.EndModal(wx.ID_OK)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   178
                event.Skip()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   179
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   180
            def GetValue(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   181
                return self.GetSizer().GetItem(1).GetWindow().GetValue()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   182
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   183
            def SetTests(self, tests):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   184
                self.Tests = tests
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   185
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   186
        class BeremizTaskBarIcon(wx.TaskBarIcon):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   187
            TBMENU_START = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   188
            TBMENU_STOP = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   189
            TBMENU_CHANGE_NAME = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   190
            TBMENU_CHANGE_PORT = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   191
            TBMENU_CHANGE_INTERFACE = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   192
            TBMENU_LIVE_SHELL = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   193
            TBMENU_WXINSPECTOR = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   194
            TBMENU_CHANGE_WD = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   195
            TBMENU_QUIT = wx.NewId()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   196
            
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   197
            def __init__(self, pyroserver, level):
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   198
                wx.TaskBarIcon.__init__(self)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   199
                self.pyroserver = pyroserver
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   200
                # Set the image
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   201
                self.UpdateIcon(None)
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   202
                self.level = level
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   203
                
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   204
                # bind some events
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   205
                self.Bind(wx.EVT_MENU, self.OnTaskBarStartPLC, id=self.TBMENU_START)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   206
                self.Bind(wx.EVT_MENU, self.OnTaskBarStopPLC, id=self.TBMENU_STOP)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   207
                self.Bind(wx.EVT_MENU, self.OnTaskBarChangeName, id=self.TBMENU_CHANGE_NAME)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   208
                self.Bind(wx.EVT_MENU, self.OnTaskBarChangeInterface, id=self.TBMENU_CHANGE_INTERFACE)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   209
                self.Bind(wx.EVT_MENU, self.OnTaskBarLiveShell, id=self.TBMENU_LIVE_SHELL)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   210
                self.Bind(wx.EVT_MENU, self.OnTaskBarWXInspector, id=self.TBMENU_WXINSPECTOR)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   211
                self.Bind(wx.EVT_MENU, self.OnTaskBarChangePort, id=self.TBMENU_CHANGE_PORT)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   212
                self.Bind(wx.EVT_MENU, self.OnTaskBarChangeWorkingDir, id=self.TBMENU_CHANGE_WD)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   213
                self.Bind(wx.EVT_MENU, self.OnTaskBarQuit, id=self.TBMENU_QUIT)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   214
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   215
            def CreatePopupMenu(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   216
                """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   217
                This method is called by the base class when it needs to popup
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   218
                the menu for the default EVT_RIGHT_DOWN event.  Just create
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   219
                the menu how you want it and return it from this function,
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   220
                the base class takes care of the rest.
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   221
                """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   222
                menu = wx.Menu()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   223
                menu.Append(self.TBMENU_START, _("Start PLC"))
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   224
                menu.Append(self.TBMENU_STOP, _("Stop PLC"))
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   225
                if self.level==1:
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   226
                    menu.AppendSeparator()
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   227
                    menu.Append(self.TBMENU_CHANGE_NAME, _("Change Name"))
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   228
                    menu.Append(self.TBMENU_CHANGE_INTERFACE, _("Change IP of interface to bind"))
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   229
                    menu.Append(self.TBMENU_CHANGE_PORT, _("Change Port Number"))
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   230
                    menu.Append(self.TBMENU_CHANGE_WD, _("Change working directory"))
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   231
                    menu.AppendSeparator()
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   232
                    menu.Append(self.TBMENU_LIVE_SHELL, _("Launch a live Python shell"))
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   233
                    menu.Append(self.TBMENU_WXINSPECTOR, _("Launch WX GUI inspector"))
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   234
                menu.AppendSeparator()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   235
                menu.Append(self.TBMENU_QUIT, _("Quit"))
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   236
                return menu
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   237
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   238
            def MakeIcon(self, img):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   239
                """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   240
                The various platforms have different requirements for the
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   241
                icon size...
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   242
                """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   243
                if "wxMSW" in wx.PlatformInfo:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   244
                    img = img.Scale(16, 16)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   245
                elif "wxGTK" in wx.PlatformInfo:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   246
                    img = img.Scale(22, 22)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   247
                # wxMac can be any size upto 128x128, so leave the source img alone....
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   248
                icon = wx.IconFromBitmap(img.ConvertToBitmap() )
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   249
                return icon
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   250
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   251
            def OnTaskBarStartPLC(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   252
                if self.pyroserver.plcobj is not None: 
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   253
                    self.pyroserver.plcobj.StartPLC()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   254
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   255
            def OnTaskBarStopPLC(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   256
                if self.pyroserver.plcobj is not None:
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   257
                    Thread(target=self.pyroserver.plcobj.StopPLC).start()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   258
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   259
            def OnTaskBarChangeInterface(self, evt):
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   260
                dlg = ParamsEntryDialog(None, _("Enter the IP of the interface to bind"), defaultValue=self.pyroserver.ip_addr)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   261
                dlg.SetTests([(re.compile('\d{1,3}(?:\.\d{1,3}){3}$').match, _("IP is not valid!")),
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   262
                               ( lambda x :len([x for x in x.split(".") if 0 <= int(x) <= 255]) == 4, _("IP is not valid!"))
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   263
                               ])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   264
                if dlg.ShowModal() == wx.ID_OK:
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   265
                    self.pyroserver.ip_addr = dlg.GetValue()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   266
                    self.pyroserver.Stop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   267
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   268
            def OnTaskBarChangePort(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   269
                dlg = ParamsEntryDialog(None, _("Enter a port number "), defaultValue=str(self.pyroserver.port))
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   270
                dlg.SetTests([(UnicodeType.isdigit, _("Port number must be an integer!")), (lambda port : 0 <= int(port) <= 65535 , _("Port number must be 0 <= port <= 65535!"))])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   271
                if dlg.ShowModal() == wx.ID_OK:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   272
                    self.pyroserver.port = int(dlg.GetValue())
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   273
                    self.pyroserver.Stop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   274
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   275
            def OnTaskBarChangeWorkingDir(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   276
                dlg = wx.DirDialog(None, _("Choose a working directory "), self.pyroserver.workdir, wx.DD_NEW_DIR_BUTTON)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   277
                if dlg.ShowModal() == wx.ID_OK:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   278
                    self.pyroserver.workdir = dlg.GetPath()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   279
                    self.pyroserver.Stop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   280
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   281
            def OnTaskBarChangeName(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   282
                dlg = ParamsEntryDialog(None, _("Enter a name "), defaultValue=self.pyroserver.name)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   283
                dlg.SetTests([(lambda name : len(name) is not 0 , _("Name must not be null!"))])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   284
                if dlg.ShowModal() == wx.ID_OK:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   285
                    self.pyroserver.name = dlg.GetValue()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   286
                    self.pyroserver.Restart()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   287
            
959
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   288
            def _LiveShellLocals(self):
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   289
                if self.pyroserver.plcobj is not None:
1049
ebf53b5f0777 Fixed inspector
Edouard Tisserant
parents: 1046
diff changeset
   290
                    return {"locals":self.pyroserver.plcobj.python_runtime_vars}
959
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   291
                else:
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   292
                    return {}
1046
103467ccb22f Fixed bug Beremiz_service process not stopped when quitting
Laurent Bessard
parents: 1045
diff changeset
   293
            
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   294
            def OnTaskBarLiveShell(self, evt):
959
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   295
                from wx import py
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   296
                frame = py.crust.CrustFrame(**self._LiveShellLocals())
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   297
                frame.Show()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   298
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   299
            def OnTaskBarWXInspector(self, evt):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   300
                # Activate the widget inspection tool
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   301
                from wx.lib.inspection import InspectionTool
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   302
                if not InspectionTool().initialized:
959
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   303
                    InspectionTool().Init(**self._LiveShellLocals())
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   304
046aeae0d71c Python shell and wx inspector now both available even when PLC not started
Edouard Tisserant
parents: 867
diff changeset
   305
                wnd = wx.GetApp()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   306
                InspectionTool().Show(wnd, True)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   307
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   308
            def OnTaskBarQuit(self, evt):
1121
d3838e8f1b90 Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents: 1067
diff changeset
   309
                if wx.Platform == '__WXMSW__':
d3838e8f1b90 Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents: 1067
diff changeset
   310
                    Thread(target=self.pyroserver.Quit).start()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   311
                self.RemoveIcon()
1121
d3838e8f1b90 Fixed Beremiz_service not closing on Windows
Laurent Bessard
parents: 1067
diff changeset
   312
                wx.CallAfter(wx.GetApp().ExitMainLoop)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   313
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   314
            def UpdateIcon(self, plcstatus):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   315
                if plcstatus is "Started" :
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   316
                    currenticon = self.MakeIcon(starticon)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   317
                elif plcstatus is "Stopped":
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   318
                    currenticon = self.MakeIcon(stopicon)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   319
                else:
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   320
                    currenticon = self.MakeIcon(defaulticon)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   321
                self.SetIcon(currenticon, "Beremiz Service")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   322
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   323
from runtime import PLCObject, PLCprint, ServicePublisher
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   324
import Pyro.core as pyro
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   325
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   326
if not os.path.isdir(WorkingDir):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   327
    os.mkdir(WorkingDir)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   328
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   329
def default_evaluator(tocall, *args, **kwargs):
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   330
    try:
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   331
        res=(tocall(*args,**kwargs), None)
1051
847d68c3e7ff Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents: 1049
diff changeset
   332
    except Exception:
847d68c3e7ff Extended exception info from evaluator. Problems in python runtime init/cleanup code now more readable
Edouard Tisserant
parents: 1049
diff changeset
   333
        res=(None, sys.exc_info())
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   334
    return res
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   335
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   336
class Server():
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   337
    def __init__(self, servicename, ip_addr, port, workdir, argv, autostart=False, statuschange=None, evaluator=default_evaluator, website=None):
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   338
        self.continueloop = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   339
        self.daemon = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   340
        self.servicename = servicename
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   341
        self.ip_addr = ip_addr
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   342
        self.port = port
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   343
        self.workdir = workdir
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   344
        self.argv = argv
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   345
        self.plcobj = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   346
        self.servicepublisher = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   347
        self.autostart = autostart
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   348
        self.statuschange = statuschange
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   349
        self.evaluator = evaluator
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   350
        self.website = website
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   351
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   352
    def Loop(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   353
        while self.continueloop:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   354
            self.Start()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   355
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   356
    def Restart(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   357
        self.Stop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   358
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   359
    def Quit(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   360
        self.continueloop = False
1045
a220a27defe5 Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents: 1034
diff changeset
   361
        if self.plcobj is not None:
a220a27defe5 Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents: 1034
diff changeset
   362
            self.plcobj.UnLoadPLC()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   363
        self.Stop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   364
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   365
    def Start(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   366
        pyro.initServer()
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   367
        self.daemon=pyro.Daemon(host=self.ip_addr, port=self.port)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   368
        self.plcobj = PLCObject(self.workdir, self.daemon, self.argv, self.statuschange, self.evaluator, self.website)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   369
        uri = self.daemon.connect(self.plcobj,"PLCObject")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   370
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   371
        print "Pyro port :",self.port
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   372
        print "Pyro object's uri :",uri
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   373
        print "Current working directory :",self.workdir
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   374
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   375
        # Configure and publish service
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   376
        # Not publish service if localhost in address params
648
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 644
diff changeset
   377
        if (self.servicename is not None and 
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 644
diff changeset
   378
            self.ip_addr is not None and 
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 644
diff changeset
   379
            self.ip_addr != "localhost" and 
73295e742da2 Avoid starting Zeroconf if ip unspecified or set to localhost. Pick one interface address when given IP is 0.0.0.0
Edouard Tisserant
parents: 644
diff changeset
   380
            self.ip_addr != "127.0.0.1"):
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   381
            print "Publishing service on local network"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   382
            self.servicepublisher = ServicePublisher.ServicePublisher()
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   383
            self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   384
        
1034
078bf153fb8c Fix bug when launching Beremiz_service with autostart option and working dir doesn't contain any program
Laurent Bessard
parents: 1027
diff changeset
   385
        if self.autostart and self.plcobj.GetPLCstatus()[0] != "Empty":
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   386
            self.plcobj.StartPLC()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   387
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   388
        sys.stdout.flush()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   389
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   390
        self.daemon.requestLoop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   391
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   392
    def Stop(self):
1045
a220a27defe5 Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents: 1034
diff changeset
   393
        if self.plcobj is not None:
a220a27defe5 Runtime now unloads and cleanup PLC before exit (created threads was preventing exit)
Edouard Tisserant
parents: 1034
diff changeset
   394
            self.plcobj.StopPLC()
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   395
        if self.servicepublisher is not None:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   396
            self.servicepublisher.UnRegisterService()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   397
            self.servicepublisher = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   398
        self.daemon.shutdown(True)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   399
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   400
if enabletwisted:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   401
    import warnings
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   402
    with warnings.catch_warnings():
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   403
        warnings.simplefilter("ignore")
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   404
        try:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   405
            from threading import Thread, currentThread
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   406
            if havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   407
                from twisted.internet import wxreactor
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   408
                wxreactor.install()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   409
            from twisted.internet import reactor, task
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   410
            from twisted.python import log, util
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   411
            from nevow import rend, appserver, inevow, tags, loaders, athena
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   412
            from nevow.page import renderer
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   413
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   414
            havetwisted = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   415
        except:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   416
            print "Twisted unavailable !"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   417
            havetwisted = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   418
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   419
if havetwisted:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   420
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   421
    xhtml_header = '''<?xml version="1.0" encoding="utf-8"?>
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   422
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   423
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   424
'''
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   425
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   426
    class PLCHMI(athena.LiveElement):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   427
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   428
        initialised = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   429
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   430
        def HMIinitialised(self, result):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   431
            self.initialised = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   432
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   433
        def HMIinitialisation(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   434
            self.HMIinitialised(None)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   435
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   436
    class DefaultPLCStartedHMI(PLCHMI):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   437
        docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[                                    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   438
                                             tags.h1["PLC IS NOW STARTED"],
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   439
                                             ])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   440
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   441
    class PLCStoppedHMI(PLCHMI):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   442
        docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   443
                                             tags.h1["PLC IS STOPPED"],
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   444
                                             ])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   445
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   446
    class MainPage(athena.LiveElement):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   447
        jsClass = u"WebInterface.PLC"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   448
        docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   449
                                                        tags.div(id='content')[                         
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   450
                                                        tags.div(render = tags.directive('PLCElement')),
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   451
                                                        ]])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   452
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   453
        def __init__(self, *a, **kw):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   454
            athena.LiveElement.__init__(self, *a, **kw)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   455
            self.pcl_state = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   456
            self.HMI = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   457
            self.resetPLCStartedHMI()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   458
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   459
        def setPLCState(self, state):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   460
            self.pcl_state = state
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   461
            if self.HMI is not None:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   462
                self.callRemote('updateHMI')
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   463
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   464
        def setPLCStartedHMI(self, hmi):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   465
            self.PLCStartedHMIClass = hmi
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   466
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   467
        def resetPLCStartedHMI(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   468
            self.PLCStartedHMIClass = DefaultPLCStartedHMI
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   469
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   470
        def getHMI(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   471
            return self.HMI
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   472
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   473
        def HMIexec(self, function, *args, **kwargs):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   474
            if self.HMI is not None:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   475
                getattr(self.HMI, function, lambda:None)(*args, **kwargs)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   476
        athena.expose(HMIexec)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   477
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   478
        def resetHMI(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   479
            self.HMI = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   480
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   481
        def PLCElement(self, ctx, data):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   482
            return self.getPLCElement()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   483
        renderer(PLCElement)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   484
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   485
        def getPLCElement(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   486
            self.detachFragmentChildren()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   487
            if self.pcl_state:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   488
                f = self.PLCStartedHMIClass()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   489
            else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   490
                f = PLCStoppedHMI()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   491
            f.setFragmentParent(self)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   492
            self.HMI = f
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   493
            return f
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   494
        athena.expose(getPLCElement)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   495
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   496
        def detachFragmentChildren(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   497
            for child in self.liveFragmentChildren[:]:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   498
                child.detach()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   499
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   500
    class WebInterface(athena.LivePage):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   501
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   502
        docFactory = loaders.stan([tags.raw(xhtml_header),
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   503
                                   tags.html(xmlns="http://www.w3.org/1999/xhtml")[
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   504
                                       tags.head(render=tags.directive('liveglue')),
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   505
                                       tags.body[
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   506
                                           tags.div[
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   507
                                                   tags.div( render = tags.directive( "MainPage" ))
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   508
                                                   ]]]])
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   509
        MainPage = MainPage()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   510
        PLCHMI = PLCHMI
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   511
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   512
        def __init__(self, plcState=False, *a, **kw):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   513
            super(WebInterface, self).__init__(*a, **kw)
719
db54ccc96309 refactoring
Edouard Tisserant
parents: 648
diff changeset
   514
            self.jsModules.mapping[u'WebInterface'] = util.sibpath(__file__, os.path.join('runtime', 'webinterface.js'))
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   515
            self.plcState = plcState
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   516
            self.MainPage.setPLCState(plcState)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   517
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   518
        def getHMI(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   519
            return self.MainPage.getHMI()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   520
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   521
        def LoadHMI(self, hmi, jsmodules):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   522
            for name, path in jsmodules.iteritems():
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   523
                self.jsModules.mapping[name] = os.path.join(WorkingDir, path)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   524
            self.MainPage.setPLCStartedHMI(hmi)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   525
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   526
        def UnLoadHMI(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   527
            self.MainPage.resetPLCStartedHMI()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   528
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   529
        def PLCStarted(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   530
            self.plcState = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   531
            self.MainPage.setPLCState(True)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   532
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   533
        def PLCStopped(self):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   534
            self.plcState = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   535
            self.MainPage.setPLCState(False)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   536
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   537
        def renderHTTP(self, ctx):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   538
            """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   539
            Force content type to fit with SVG
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   540
            """
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   541
            req = inevow.IRequest(ctx)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   542
            req.setHeader('Content-type', 'application/xhtml+xml')
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   543
            return super(WebInterface, self).renderHTTP(ctx)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   544
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   545
        def render_MainPage(self, ctx, data):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   546
            f = self.MainPage
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   547
            f.setFragmentParent(self)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   548
            return ctx.tag[f]
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   549
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   550
        def child_(self, ctx):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   551
            self.MainPage.detachFragmentChildren()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   552
            return WebInterface(plcState=self.plcState)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   553
            
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   554
        def beforeRender(self, ctx):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   555
            d = self.notifyOnDisconnect()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   556
            d.addErrback(self.disconnected)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   557
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   558
        def disconnected(self, reason):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   559
            self.MainPage.resetHMI()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   560
            #print reason
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   561
            #print "We will be called back when the client disconnects"
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   562
        
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   563
    if havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   564
        reactor.registerWxApp(app)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   565
    website = WebInterface()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   566
    site = appserver.NevowSite(website)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   567
    
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   568
    website_port = 8009
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   569
    listening = False
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   570
    while not listening:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   571
        try:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   572
            reactor.listenTCP(website_port, site)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   573
            listening = True
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   574
        except:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   575
            website_port += 1
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   576
    print "Http interface port :",website_port
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   577
else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   578
    website = None
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   579
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   580
if havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   581
    from threading import Semaphore
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   582
    wx_eval_lock = Semaphore(0)
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   583
    main_thread = currentThread()
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   584
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   585
    def statuschange(status):
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   586
        wx.CallAfter(taskbar_instance.UpdateIcon,status)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   587
        
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   588
    def wx_evaluator(obj, *args, **kwargs):
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   589
        tocall,args,kwargs = obj.call
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   590
        obj.res = default_evaluator(tocall, *args, **kwargs)
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   591
        wx_eval_lock.release()
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   592
        
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   593
    def evaluator(tocall, *args, **kwargs):
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   594
        global main_thread
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   595
        if(main_thread == currentThread()):
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   596
            # avoid dead lock if called from the wx mainloop 
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   597
            return default_evaluator(tocall, *args, **kwargs)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   598
        else:
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   599
            o=type('',(object,),dict(call=(tocall, args, kwargs), res=None))
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   600
            wx.CallAfter(wx_evaluator,o)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   601
            wx_eval_lock.acquire()
867
06495975e8a4 Added caching for python eval (avoid compiling when same code called, but still execute). Cleaned up some evaluator related code.
Edouard Tisserant
parents: 850
diff changeset
   602
            return o.res
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   603
    
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   604
    pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, statuschange, evaluator, website)
835
8145be14a2ae Fixed runtime GUI freeze when stopping PLC from the menu. Added -x 2 argument to Beremiz_service.py to restrict content of systray icon menu.
Edouard Tisserant
parents: 719
diff changeset
   605
    taskbar_instance = BeremizTaskBarIcon(pyroserver, enablewx)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   606
else:
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 641
diff changeset
   607
    pyroserver = Server(servicename, given_ip, port, WorkingDir, argv, autostart, website=website)
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   608
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   609
# Exception hooks s
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   610
import threading, traceback
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   611
def LogException(*exp):
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   612
    if pyroserver.plcobj is not None:
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   613
        pyroserver.plcobj.LogMessage(0,'\n'.join(traceback.format_exception(*exp)))
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   614
    else:
1270
aa9bc3e6181d Fix bug when LogException handler
Laurent Bessard
parents: 1121
diff changeset
   615
        traceback.print_exception(*exp)
1067
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   616
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   617
sys.excepthook = LogException
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   618
def installThreadExcepthook():
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   619
    init_old = threading.Thread.__init__
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   620
    def init(self, *args, **kwargs):
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   621
        init_old(self, *args, **kwargs)
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   622
        run_old = self.run
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   623
        def run_with_except_hook(*args, **kw):
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   624
            try:
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   625
                run_old(*args, **kw)
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   626
            except (KeyboardInterrupt, SystemExit):
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   627
                raise
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   628
            except:
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   629
                sys.excepthook(*sys.exc_info())
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   630
        self.run = run_with_except_hook
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   631
    threading.Thread.__init__ = init
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   632
installThreadExcepthook()
4f460c1dffb5 Added exception hook to Beremiz_service, so that exception go in PLC log. Extended Beremiz.py exception hook to threads. Stripped images embedded in Beremiz_service.py, user real images instead.
Edouard Tisserant
parents: 1051
diff changeset
   633
641
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   634
if havetwisted or havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   635
    pyro_thread=Thread(target=pyroserver.Loop)
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   636
    pyro_thread.start()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   637
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   638
    if havetwisted:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   639
        reactor.run()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   640
    elif havewx:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   641
        app.MainLoop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   642
else:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   643
    try :
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   644
        pyroserver.Loop()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   645
    except KeyboardInterrupt,e:
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   646
        pass
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   647
pyroserver.Quit()
b4eae24d05bf Fixed encoding of Beremiz_service.py
Edouard Tisserant
parents: 622
diff changeset
   648
sys.exit(0)