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