Beremiz_service.py
author greg
Thu, 21 Aug 2008 10:35:12 +0200
changeset 206 6b4d58363b80
parent 203 cb9901076a21
child 207 5f4cb3b024cc
permissions -rw-r--r--
fix bug in plugger.py to get URI_Location
add ip authorized in beremiz_service.py
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
        #ip = socket.inet_aton(a)
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    56
        if len(a.split(".")) == 4 or a == "localhost":
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    57
            ip = a
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    58
    elif o in ("-d", "--directory"):
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    59
        # overwrite default working directory
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    60
        WorkingDir = a
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    61
    elif o in ("-p", "--port"):
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    62
        # port: port that the service runs on
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    63
        port = int(a)
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    64
    else:
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    65
        usage()
6b4d58363b80 fix bug in plugger.py to get URI_Location
greg
parents: 203
diff changeset
    66
        sys.exit()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    68
from runtime import PLCObject, ServicePublisher
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    69
import Pyro.core as pyro
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    71
if not os.path.isdir(WorkingDir):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
    os.mkdir(WorkingDir)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
# type: fully qualified service type name
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
type = '_PYRO._tcp.local.'
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
# name: fully qualified service name
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
name = 'First test.%s'%(type)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
# address: IP address as unsigned short, network byte order
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
def gethostaddr(dst = '224.0.1.41'):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
    try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
        s.connect((dst, 7))
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
        (host, port) = s.getsockname()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
        s.close()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
        if host != '0.0.0.0':
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
            return host
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
    except error:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
        pass
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
    return socket.gethostbyname(socket.gethostname())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
# properties: dictionary of properties (or a string holding the bytes for the text field)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
serviceproperties = {'description':'Remote control for PLC'}
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
pyro.initServer()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    96
daemon=pyro.Daemon(host=ip, port=port)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    97
uri = daemon.connect(PLCObject(WorkingDir, daemon),"PLCObject")
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
print "Publish service on local network"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   103
ip_32b = socket.inet_aton(ip)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   104
# Configure and publish service
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   105
service = ServicePublisher.PublishService()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
service.ConfigureService(type, name, ip_32b, port, serviceproperties)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107
service.PublishService()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   108
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   109
daemon.requestLoop()