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