Beremiz_service.py
changeset 206 6b4d58363b80
parent 203 cb9901076a21
child 207 5f4cb3b024cc
equal deleted inserted replaced
205:ee8d1f4528ef 206:6b4d58363b80
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import os, sys, getopt, socket
    25 import os, sys, getopt, socket
    26 
    26 
    27 def usage():
    27 def usage():
    28     print "\nUsage of Beremiz PLC execution service :"
    28     print """
    29     print "\n   %s [PLC path]\n"%sys.argv[0]
    29 Usage of Beremiz PLC execution service :\n
       
    30 %s {[-a ip] [-d path] [-p port]|-h|--help}
       
    31            -a, --address            - authorized ip to connect (x.x.x.x)
       
    32            -d, --directory path     - set the working directory
       
    33            -p, --port port number   - set the port number
       
    34            -h, --help               - print this help text and quit
       
    35 """%sys.argv[0]
    30 
    36 
    31 try:
    37 try:
    32     opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
    38     opts, args = getopt.getopt(sys.argv[1:], "a:d:p:h", ["directory=", "port=", "help"])
    33 except getopt.GetoptError:
    39 except getopt.GetoptError, err:
    34     # print help information and exit:
    40     # print help information and exit:
       
    41     print str(err) # will print something like "option -a not recognized"
    35     usage()
    42     usage()
    36     sys.exit(2)
    43     sys.exit(2)
       
    44 
       
    45 # default values
       
    46 WorkingDir = os.getcwd()
       
    47 ip = ""
       
    48 port = 3000
    37 
    49 
    38 for o, a in opts:
    50 for o, a in opts:
    39     if o in ("-h", "--help"):
    51     if o in ("-h", "--help"):
    40         usage()
    52         usage()
    41         sys.exit()
    53         sys.exit()
    42 
    54     elif o in ("-a", "--address"):
    43 if len(args) > 1:
    55         #ip = socket.inet_aton(a)
    44     usage()
    56         if len(a.split(".")) == 4 or a == "localhost":
    45     sys.exit()
    57             ip = a
    46 elif len(args) == 1:
    58     elif o in ("-d", "--directory"):
    47     WorkingDir = args[0]
    59         # overwrite default working directory
    48 elif len(args) == 0:
    60         WorkingDir = a
    49     WorkingDir = os.getcwd()
    61     elif o in ("-p", "--port"):
    50 else:
    62         # port: port that the service runs on
    51     usage()
    63         port = int(a)
    52     sys.exit()
    64     else:
       
    65         usage()
       
    66         sys.exit()
    53 
    67 
    54 from runtime import PLCObject, ServicePublisher
    68 from runtime import PLCObject, ServicePublisher
    55 import Pyro.core as pyro
    69 import Pyro.core as pyro
    56 
    70 
    57 if not os.path.isdir(WorkingDir):
    71 if not os.path.isdir(WorkingDir):
    73             return host
    87             return host
    74     except error:
    88     except error:
    75         pass
    89         pass
    76     return socket.gethostbyname(socket.gethostname())
    90     return socket.gethostbyname(socket.gethostname())
    77 
    91 
    78 ip = gethostaddr()
       
    79 # port: port that the service runs on
       
    80 port = 3000
       
    81 # properties: dictionary of properties (or a string holding the bytes for the text field)
    92 # properties: dictionary of properties (or a string holding the bytes for the text field)
    82 serviceproperties = {'description':'Remote control for PLC'}
    93 serviceproperties = {'description':'Remote control for PLC'}
    83 
    94 
    84 pyro.initServer()
    95 pyro.initServer()
    85 daemon=pyro.Daemon(host=ip, port=port)
    96 daemon=pyro.Daemon(host=ip, port=port)