Beremiz_service.py
changeset 203 cb9901076a21
child 206 6b4d58363b80
equal deleted inserted replaced
202:cd81a7a6e55c 203:cb9901076a21
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of Beremiz, a Integrated Development Environment for
       
     5 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import os, sys, getopt, socket
       
    26 
       
    27 def usage():
       
    28     print "\nUsage of Beremiz PLC execution service :"
       
    29     print "\n   %s [PLC path]\n"%sys.argv[0]
       
    30 
       
    31 try:
       
    32     opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
       
    33 except getopt.GetoptError:
       
    34     # print help information and exit:
       
    35     usage()
       
    36     sys.exit(2)
       
    37 
       
    38 for o, a in opts:
       
    39     if o in ("-h", "--help"):
       
    40         usage()
       
    41         sys.exit()
       
    42 
       
    43 if len(args) > 1:
       
    44     usage()
       
    45     sys.exit()
       
    46 elif len(args) == 1:
       
    47     WorkingDir = args[0]
       
    48 elif len(args) == 0:
       
    49     WorkingDir = os.getcwd()
       
    50 else:
       
    51     usage()
       
    52     sys.exit()
       
    53 
       
    54 from runtime import PLCObject, ServicePublisher
       
    55 import Pyro.core as pyro
       
    56 
       
    57 if not os.path.isdir(WorkingDir):
       
    58     os.mkdir(WorkingDir)
       
    59 
       
    60 # type: fully qualified service type name
       
    61 type = '_PYRO._tcp.local.'
       
    62 # name: fully qualified service name
       
    63 name = 'First test.%s'%(type)
       
    64 # address: IP address as unsigned short, network byte order
       
    65 
       
    66 def gethostaddr(dst = '224.0.1.41'):
       
    67     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       
    68     try:
       
    69         s.connect((dst, 7))
       
    70         (host, port) = s.getsockname()
       
    71         s.close()
       
    72         if host != '0.0.0.0':
       
    73             return host
       
    74     except error:
       
    75         pass
       
    76     return socket.gethostbyname(socket.gethostname())
       
    77 
       
    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)
       
    82 serviceproperties = {'description':'Remote control for PLC'}
       
    83 
       
    84 pyro.initServer()
       
    85 daemon=pyro.Daemon(host=ip, port=port)
       
    86 uri = daemon.connect(PLCObject(WorkingDir, daemon),"PLCObject")
       
    87 print "The daemon runs on port :",daemon.port
       
    88 print "The object's uri is :",uri
       
    89 print "The working directory :",WorkingDir
       
    90 print "Publish service on local network"
       
    91 
       
    92 ip_32b = socket.inet_aton(ip)
       
    93 # Configure and publish service
       
    94 service = ServicePublisher.PublishService()
       
    95 service.ConfigureService(type, name, ip_32b, port, serviceproperties)
       
    96 service.PublishService()
       
    97 
       
    98 daemon.requestLoop()