Beremiz_service.py
author etisserant
Fri, 22 Aug 2008 15:30:09 +0200
changeset 217 f3eb35df4d87
parent 210 ca3bfcf61192
child 262 141a7145c099
permissions -rw-r--r--
Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
#General Public License for more details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    25
import os, sys, getopt
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    27
def usage():
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    28
    print """
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    29
Usage of Beremiz PLC execution service :\n
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    30
%s {[-n name] [-i ip] [-p port]|-h|--help} working_dir
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    31
           -n        - zeroconf service name
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    32
           -i        - ip of interface to bind to (x.x.x.x)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    33
           -p        - port number
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    34
           -h        - print this help text and quit
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    35
           
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    36
           working_dir - directory where are stored PLC files
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    37
"""%sys.argv[0]
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    39
try:
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    40
    opts, args = getopt.getopt(sys.argv[1:], "i:p:n:h")
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    41
except getopt.GetoptError, err:
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
    # print help information and exit:
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    43
    print str(err) # will print something like "option -a not recognized"
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
    usage()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    45
    sys.exit(2)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    46
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    47
# default values
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    48
ip = ""
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    49
port = 3000
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    50
name = os.environ[{
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    51
     "linux2":"USER",
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    52
     "win32":"USERNAME",
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    53
     }.get(sys.platform, "USER")]
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    54
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    55
for o, a in opts:
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    56
    if o == "-h":
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    57
        usage()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    58
        sys.exit()
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    59
    elif o == "-i":
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    60
        if len(a.split(".")) == 4 or a == "localhost":
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    61
            ip = a
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    62
    elif o == "-p":
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    63
        # port: port that the service runs on
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    64
        port = int(a)
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    65
    elif o == "-n":
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    66
        name = a
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    67
    else:
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    68
        usage()
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    69
        sys.exit()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
210
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    71
if len(args) > 1:
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    72
    usage()
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    73
    sys.exit()
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    74
elif len(args) == 1:
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    75
    WorkingDir = args[0]
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    76
elif len(args) == 0:
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    77
    WorkingDir = os.getcwd()
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    78
    args=[WorkingDir]
210
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    79
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
from runtime import PLCObject, ServicePublisher
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
import Pyro.core as pyro
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
if not os.path.isdir(WorkingDir):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
    os.mkdir(WorkingDir)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
pyro.initServer()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
daemon=pyro.Daemon(host=ip, port=port)
210
ca3bfcf61192 Fixed oddities in arguments/options parsing and passing to plugins initialization...
etisserant
parents: 208
diff changeset
    89
uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"PLCObject")
207
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
    90
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
print "The daemon runs on port :",daemon.port
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
print "The object's uri is :",uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
print "The working directory :",WorkingDir
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
# Configure and publish service
207
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
    96
# Not publish service if localhost in address params
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
    97
if ip != "localhost" and ip != "127.0.0.1":    
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
    98
    print "Publish service on local network"
217
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
    99
    service = ServicePublisher.ServicePublisher(name, ip, port)
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
   100
f3eb35df4d87 Now, Beremiz launch Beremiz_service at startup, with a one-time workin dir
etisserant
parents: 210
diff changeset
   101
sys.stdout.flush()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   103
daemon.requestLoop()