Beremiz_service.py
changeset 217 f3eb35df4d87
parent 210 ca3bfcf61192
child 262 141a7145c099
equal deleted inserted replaced
216:11124e129a28 217:f3eb35df4d87
    20 #
    20 #
    21 #You should have received a copy of the GNU General Public
    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
    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
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import os, sys, getopt, socket
    25 import os, sys, getopt
    26 
    26 
    27 def usage():
    27 def usage():
    28     print """
    28     print """
    29 Usage of Beremiz PLC execution service :\n
    29 Usage of Beremiz PLC execution service :\n
    30 %s {[-a ip] [-d path] [-p port]|-h|--help}
    30 %s {[-n name] [-i ip] [-p port]|-h|--help} working_dir
    31            -a, --address            - authorized ip to connect (x.x.x.x)
    31            -n        - zeroconf service name
    32            -d, --directory path     - set the working directory
    32            -i        - ip of interface to bind to (x.x.x.x)
    33            -p, --port port number   - set the port number
    33            -p        - port number
    34            -h, --help               - print this help text and quit
    34            -h        - print this help text and quit
       
    35            
       
    36            working_dir - directory where are stored PLC files
    35 """%sys.argv[0]
    37 """%sys.argv[0]
    36 
    38 
    37 try:
    39 try:
    38     opts, args = getopt.getopt(sys.argv[1:], "a:p:h", ["help"])
    40     opts, args = getopt.getopt(sys.argv[1:], "i:p:n:h")
    39 except getopt.GetoptError, err:
    41 except getopt.GetoptError, err:
    40     # print help information and exit:
    42     # print help information and exit:
    41     print str(err) # will print something like "option -a not recognized"
    43     print str(err) # will print something like "option -a not recognized"
    42     usage()
    44     usage()
    43     sys.exit(2)
    45     sys.exit(2)
    44 
    46 
    45 # default values
    47 # default values
    46 ip = ""
    48 ip = ""
    47 port = 3000
    49 port = 3000
    48 print opts
    50 name = os.environ[{
       
    51      "linux2":"USER",
       
    52      "win32":"USERNAME",
       
    53      }.get(sys.platform, "USER")]
       
    54 
    49 for o, a in opts:
    55 for o, a in opts:
    50     if o in ("-h", "--help"):
    56     if o == "-h":
    51         usage()
    57         usage()
    52         sys.exit()
    58         sys.exit()
    53     elif o in ("-a", "--address"):
    59     elif o == "-i":
    54         if len(a.split(".")) == 4 or a == "localhost":
    60         if len(a.split(".")) == 4 or a == "localhost":
    55             ip = a
    61             ip = a
    56     elif o in ("-p", "--port"):
    62     elif o == "-p":
    57         # port: port that the service runs on
    63         # port: port that the service runs on
    58         port = int(a)
    64         port = int(a)
       
    65     elif o == "-n":
       
    66         name = a
    59     else:
    67     else:
    60         usage()
    68         usage()
    61         sys.exit()
    69         sys.exit()
    62 
    70 
    63 if len(args) > 1:
    71 if len(args) > 1:
    65     sys.exit()
    73     sys.exit()
    66 elif len(args) == 1:
    74 elif len(args) == 1:
    67     WorkingDir = args[0]
    75     WorkingDir = args[0]
    68 elif len(args) == 0:
    76 elif len(args) == 0:
    69     WorkingDir = os.getcwd()
    77     WorkingDir = os.getcwd()
    70 else:
    78     args=[WorkingDir]
    71     usage()
       
    72     sys.exit()
       
    73 
    79 
    74 from runtime import PLCObject, ServicePublisher
    80 from runtime import PLCObject, ServicePublisher
    75 import Pyro.core as pyro
    81 import Pyro.core as pyro
    76 
    82 
    77 if not os.path.isdir(WorkingDir):
    83 if not os.path.isdir(WorkingDir):
    78     os.mkdir(WorkingDir)
    84     os.mkdir(WorkingDir)
    79 
    85 
    80 # type: fully qualified service type name
       
    81 type = '_PYRO._tcp.local.'
       
    82 # name: fully qualified service name
       
    83 name = 'First test.%s'%(type)
       
    84 # address: IP address as unsigned short, network byte order
       
    85 
       
    86 def gethostaddr(dst = '224.0.1.41'):
       
    87     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
       
    88     try:
       
    89         s.connect((dst, 7))
       
    90         (host, port) = s.getsockname()
       
    91         s.close()
       
    92         if host != '0.0.0.0':
       
    93             return host
       
    94     except error:
       
    95         pass
       
    96     return socket.gethostbyname(socket.gethostname())
       
    97 
       
    98 # properties: dictionary of properties (or a string holding the bytes for the text field)
       
    99 serviceproperties = {'description':'Remote control for PLC'}
       
   100 
    86 
   101 pyro.initServer()
    87 pyro.initServer()
   102 daemon=pyro.Daemon(host=ip, port=port)
    88 daemon=pyro.Daemon(host=ip, port=port)
   103 uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"PLCObject")
    89 uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"PLCObject")
   104 
    90 
   107 print "The working directory :",WorkingDir
    93 print "The working directory :",WorkingDir
   108 
    94 
   109 # Configure and publish service
    95 # Configure and publish service
   110 # Not publish service if localhost in address params
    96 # Not publish service if localhost in address params
   111 if ip != "localhost" and ip != "127.0.0.1":    
    97 if ip != "localhost" and ip != "127.0.0.1":    
   112     # No ip params -> get host ip
       
   113     if ip == "":
       
   114         ip_32b = socket.inet_aton(gethostaddr(ip))
       
   115     else:
       
   116         ip_32b = ip
       
   117     print "Publish service on local network"
    98     print "Publish service on local network"
   118     service = ServicePublisher.PublishService()
    99     service = ServicePublisher.ServicePublisher(name, ip, port)
   119     service.ConfigureService(type, name, ip_32b, port, serviceproperties)
   100 
   120     service.PublishService()
   101 sys.stdout.flush()
   121 
   102 
   122 daemon.requestLoop()
   103 daemon.requestLoop()