Beremiz_service.py
author greg
Thu, 21 Aug 2008 13:18:49 +0200
changeset 207 5f4cb3b024cc
parent 206 6b4d58363b80
child 208 dd630979f628
permissions -rw-r--r--
add address ip params
fix configure/publish service
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
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
import os, sys, getopt, socket
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
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    30
%s {[-a ip] [-d path] [-p port]|-h|--help}
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    31
           -a, --address            - authorized ip to connect (x.x.x.x)
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    32
           -d, --directory path     - set the working directory
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    33
           -p, --port port number   - set the port number
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    34
           -h, --help               - print this help text and quit
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    35
"""%sys.argv[0]
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
try:
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    38
    opts, args = getopt.getopt(sys.argv[1:], "a:d:p:h", ["directory=", "port=", "help"])
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    39
except getopt.GetoptError, err:
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
    # print help information and exit:
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    41
    print str(err) # will print something like "option -a not recognized"
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
    usage()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    43
    sys.exit(2)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    45
# default values
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    46
WorkingDir = os.getcwd()
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    47
ip = ""
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    48
port = 3000
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    49
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
for o, a in opts:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
    if o in ("-h", "--help"):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    52
        usage()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    53
        sys.exit()
206
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    54
    elif o in ("-a", "--address"):
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    55
        if len(a.split(".")) == 4 or a == "localhost":
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    56
            ip = a
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    57
    elif o in ("-d", "--directory"):
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    58
        # overwrite default working directory
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    59
        WorkingDir = a
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    60
    elif o in ("-p", "--port"):
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    61
        # port: port that the service runs on
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    62
        port = int(a)
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    63
    else:
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    64
        usage()
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    65
        sys.exit()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    66
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
from runtime import PLCObject, ServicePublisher
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    68
import Pyro.core as pyro
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    69
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
if not os.path.isdir(WorkingDir):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    71
    os.mkdir(WorkingDir)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
# type: fully qualified service type name
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
type = '_PYRO._tcp.local.'
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
# name: fully qualified service name
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
name = 'First test.%s'%(type)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
# address: IP address as unsigned short, network byte order
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
def gethostaddr(dst = '224.0.1.41'):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
    try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
        s.connect((dst, 7))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
        (host, port) = s.getsockname()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
        s.close()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
        if host != '0.0.0.0':
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
            return host
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
    except error:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
        pass
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
    return socket.gethostbyname(socket.gethostname())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
# properties: dictionary of properties (or a string holding the bytes for the text field)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
serviceproperties = {'description':'Remote control for PLC'}
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
pyro.initServer()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
daemon=pyro.Daemon(host=ip, port=port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    96
uri = daemon.connect(PLCObject(WorkingDir, daemon),"PLCObject")
207
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
    97
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    98
print "The daemon runs on port :",daemon.port
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    99
print "The object's uri is :",uri
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   100
print "The working directory :",WorkingDir
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   101
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
# Configure and publish service
207
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   103
# Not publish service if localhost in address params
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   104
print ip
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   105
if ip != "localhost" and ip != "127.0.0.1":    
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   106
    # No ip params -> get host ip
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   107
    if ip == "":
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   108
        ip_32b = socket.inet_aton(gethostaddr(ip))
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   109
    else:
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   110
        ip_32b = ip
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   111
    print "Publish service on local network"
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   112
    service = ServicePublisher.PublishService()
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   113
    service.ConfigureService(type, name, ip_32b, port, serviceproperties)
5f4cb3b024cc add address ip params
greg
parents: 206
diff changeset
   114
    service.PublishService()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   115
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   116
daemon.requestLoop()