Beremiz_service.py
changeset 210 ca3bfcf61192
parent 208 dd630979f628
child 217 f3eb35df4d87
equal deleted inserted replaced
209:08dc3d064cb5 210:ca3bfcf61192
    33            -p, --port port number   - set the port number
    33            -p, --port port number   - set the port number
    34            -h, --help               - print this help text and quit
    34            -h, --help               - print this help text and quit
    35 """%sys.argv[0]
    35 """%sys.argv[0]
    36 
    36 
    37 try:
    37 try:
    38     opts, args = getopt.getopt(sys.argv[1:], "a:d:p:h", ["directory=", "port=", "help"])
    38     opts, args = getopt.getopt(sys.argv[1:], "a:p:h", ["help"])
    39 except getopt.GetoptError, err:
    39 except getopt.GetoptError, err:
    40     # print help information and exit:
    40     # print help information and exit:
    41     print str(err) # will print something like "option -a not recognized"
    41     print str(err) # will print something like "option -a not recognized"
    42     usage()
    42     usage()
    43     sys.exit(2)
    43     sys.exit(2)
    44 
    44 
    45 # default values
    45 # default values
    46 WorkingDir = os.getcwd()
       
    47 ip = ""
    46 ip = ""
    48 port = 3000
    47 port = 3000
    49 
    48 print opts
    50 for o, a in opts:
    49 for o, a in opts:
    51     if o in ("-h", "--help"):
    50     if o in ("-h", "--help"):
    52         usage()
    51         usage()
    53         sys.exit()
    52         sys.exit()
    54     elif o in ("-a", "--address"):
    53     elif o in ("-a", "--address"):
    55         if len(a.split(".")) == 4 or a == "localhost":
    54         if len(a.split(".")) == 4 or a == "localhost":
    56             ip = a
    55             ip = a
    57     elif o in ("-d", "--directory"):
       
    58         # overwrite default working directory
       
    59         WorkingDir = a
       
    60     elif o in ("-p", "--port"):
    56     elif o in ("-p", "--port"):
    61         # port: port that the service runs on
    57         # port: port that the service runs on
    62         port = int(a)
    58         port = int(a)
    63     else:
    59     else:
    64         usage()
    60         usage()
    65         sys.exit()
    61         sys.exit()
       
    62 
       
    63 if len(args) > 1:
       
    64     usage()
       
    65     sys.exit()
       
    66 elif len(args) == 1:
       
    67     WorkingDir = args[0]
       
    68 elif len(args) == 0:
       
    69     WorkingDir = os.getcwd()
       
    70 else:
       
    71     usage()
       
    72     sys.exit()
    66 
    73 
    67 from runtime import PLCObject, ServicePublisher
    74 from runtime import PLCObject, ServicePublisher
    68 import Pyro.core as pyro
    75 import Pyro.core as pyro
    69 
    76 
    70 if not os.path.isdir(WorkingDir):
    77 if not os.path.isdir(WorkingDir):
    91 # properties: dictionary of properties (or a string holding the bytes for the text field)
    98 # properties: dictionary of properties (or a string holding the bytes for the text field)
    92 serviceproperties = {'description':'Remote control for PLC'}
    99 serviceproperties = {'description':'Remote control for PLC'}
    93 
   100 
    94 pyro.initServer()
   101 pyro.initServer()
    95 daemon=pyro.Daemon(host=ip, port=port)
   102 daemon=pyro.Daemon(host=ip, port=port)
    96 uri = daemon.connect(PLCObject(WorkingDir, daemon),"PLCObject")
   103 uri = daemon.connect(PLCObject(WorkingDir, daemon, args),"PLCObject")
    97 
   104 
    98 print "The daemon runs on port :",daemon.port
   105 print "The daemon runs on port :",daemon.port
    99 print "The object's uri is :",uri
   106 print "The object's uri is :",uri
   100 print "The working directory :",WorkingDir
   107 print "The working directory :",WorkingDir
   101 
   108