opc_ua/opcua_client_maker.py
branchpython3
changeset 3750 f62625418bff
parent 3677 6d9040e07c32
child 3820 46f3ca3f0157
equal deleted inserted replaced
3749:fda6c1a37662 3750:f62625418bff
     1 from __future__ import print_function
     1 
     2 from __future__ import absolute_import
     2 
     3 
     3 
     4 import csv
     4 import csv
     5 
     5 
     6 from opcua import Client
     6 from opcua import Client
     7 from opcua import ua
     7 from opcua import ua
   451         super(OPCUAClientList, self).__init__(self)
   451         super(OPCUAClientList, self).__init__(self)
   452         self.log = log
   452         self.log = log
   453         self.change_callback = change_callback
   453         self.change_callback = change_callback
   454 
   454 
   455     def append(self, value):
   455     def append(self, value):
   456         v = dict(zip(lstcolnames, value))
   456         v = dict(list(zip(lstcolnames, value)))
   457 
   457 
   458         if type(v["IEC"]) != int:
   458         if type(v["IEC"]) != int:
   459             if len(self) == 0:
   459             if len(self) == 0:
   460                 v["IEC"] = 0
   460                 v["IEC"] = 0
   461             else:
   461             else:
   473                 v[n] = t(v[n]) 
   473                 v[n] = t(v[n]) 
   474         except ValueError: 
   474         except ValueError: 
   475             self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"]))
   475             self.log("Variable {} (Id={}) has invalid type\n".format(v["Name"],v["Id"]))
   476             return False
   476             return False
   477 
   477 
   478         if len(self)>0 and v["Id"] in zip(*self)[lstcolnames.index("Id")]:
   478         if len(self)>0 and v["Id"] in list(zip(*self))[lstcolnames.index("Id")]:
   479             self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"]))
   479             self.log("Variable {} (Id={}) already in list\n".format(v["Name"],v["Id"]))
   480             return False
   480             return False
   481 
   481 
   482         list.append(self, [v[n] for n in lstcolnames])
   482         list.append(self, [v[n] for n in lstcolnames])
   483 
   483 
   496             self[direction] = OPCUAClientList(log, change_callback)
   496             self[direction] = OPCUAClientList(log, change_callback)
   497 
   497 
   498     def LoadCSV(self,path):
   498     def LoadCSV(self,path):
   499         with open(path, 'rb') as csvfile:
   499         with open(path, 'rb') as csvfile:
   500             reader = csv.reader(csvfile, delimiter=',', quotechar='"')
   500             reader = csv.reader(csvfile, delimiter=',', quotechar='"')
   501             buf = {direction:[] for direction, _model in self.iteritems()}
   501             buf = {direction:[] for direction, _model in self.items()}
   502             for direction, model in self.iteritems():
   502             for direction, model in self.items():
   503                 self[direction][:] = []
   503                 self[direction][:] = []
   504             for row in reader:
   504             for row in reader:
   505                 direction = row[0]
   505                 direction = row[0]
   506                 # avoids calling change callback whe loading CSV
   506                 # avoids calling change callback whe loading CSV
   507                 list.append(self[direction],row[1:])
   507                 list.append(self[direction],row[1:])
   508 
   508 
   509     def SaveCSV(self,path):
   509     def SaveCSV(self,path):
   510         with open(path, 'wb') as csvfile:
   510         with open(path, 'wb') as csvfile:
   511             for direction, data in self.iteritems():
   511             for direction, data in self.items():
   512                 writer = csv.writer(csvfile, delimiter=',',
   512                 writer = csv.writer(csvfile, delimiter=',',
   513                                 quotechar='"', quoting=csv.QUOTE_MINIMAL)
   513                                 quotechar='"', quoting=csv.QUOTE_MINIMAL)
   514                 for row in data:
   514                 for row in data:
   515                     writer.writerow([direction] + row)
   515                     writer.writerow([direction] + row)
   516 
   516 
   704     INIT_UserPassword("{User}", "{Password}")""".format(**config)
   704     INIT_UserPassword("{User}", "{Password}")""".format(**config)
   705         else:
   705         else:
   706             formatdict["init"] += """
   706             formatdict["init"] += """
   707     INIT_NoAuth()"""
   707     INIT_NoAuth()"""
   708 
   708 
   709         for direction, data in self.iteritems():
   709         for direction, data in self.items():
   710             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   710             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   711             for row in data:
   711             for row in data:
   712                 name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
   712                 name, ua_nsidx, ua_nodeid_type, _ua_node_id, ua_type, iec_number = row
   713                 iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
   713                 iec_type, C_type, iec_size_prefix, ua_type_enum, ua_type = UA_IEC_types[ua_type]
   714                 c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
   714                 c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
   749     config["URI"] = sys.argv[1] if argc>1 else "opc.tcp://localhost:4840"
   749     config["URI"] = sys.argv[1] if argc>1 else "opc.tcp://localhost:4840"
   750 
   750 
   751     if argc > 2:
   751     if argc > 2:
   752         AuthType = sys.argv[2]
   752         AuthType = sys.argv[2]
   753         config["AuthType"] = AuthType
   753         config["AuthType"] = AuthType
   754         for (name, default), value in izip_longest(authParams[AuthType], sys.argv[3:]):
   754         for (name, default), value in zip_longest(authParams[AuthType], sys.argv[3:]):
   755             if value is None:
   755             if value is None:
   756                 if default is None:
   756                 if default is None:
   757                     raise Exception(name+" param expected")
   757                     raise Exception(name+" param expected")
   758                 value = default
   758                 value = default
   759             config[name] = value
   759             config[name] = value