144 _server_WebParamListDict = {} |
144 _server_WebParamListDict = {} |
145 _server_WebParamListDict["tcp" ] = TCPserver_parameters |
145 _server_WebParamListDict["tcp" ] = TCPserver_parameters |
146 _server_WebParamListDict["rtu" ] = RTUslave_parameters |
146 _server_WebParamListDict["rtu" ] = RTUslave_parameters |
147 _server_WebParamListDict["ascii"] = [] # (Note: ascii not yet implemented in Beremiz modbus plugin) |
147 _server_WebParamListDict["ascii"] = [] # (Note: ascii not yet implemented in Beremiz modbus plugin) |
148 |
148 |
|
149 WebParamListDictDict = {} |
|
150 WebParamListDictDict['client'] = _client_WebParamListDict |
|
151 WebParamListDictDict['server'] = _server_WebParamListDict |
|
152 |
149 |
153 |
150 #def _CheckPortnumber(port_number): |
154 #def _CheckPortnumber(port_number): |
151 # """ check validity of the port number """ |
155 # """ check validity of the port number """ |
152 # try: |
156 # try: |
153 # portnum = int(port_number) |
157 # portnum = int(port_number) |
193 |
197 |
194 |
198 |
195 def _SetSavedConfiguration(WebNode_id, newConfig): |
199 def _SetSavedConfiguration(WebNode_id, newConfig): |
196 """ Stores a dictionary in a persistant file containing the Modbus parameter configuration """ |
200 """ Stores a dictionary in a persistant file containing the Modbus parameter configuration """ |
197 |
201 |
|
202 # Add the addr_type and node_type to the data that will be saved to file |
|
203 # This allows us to confirm the saved data contains the correct addr_type |
|
204 # when loading from file |
|
205 save_info = {} |
|
206 save_info["addr_type"] = _WebNodeList[WebNode_id]["addr_type"] |
|
207 save_info["node_type"] = _WebNodeList[WebNode_id]["node_type"] |
|
208 save_info["config" ] = newConfig |
|
209 |
198 filename = _WebNodeList[WebNode_id]["filename"] |
210 filename = _WebNodeList[WebNode_id]["filename"] |
199 |
211 |
200 with open(os.path.realpath(filename), 'w') as f: |
212 with open(os.path.realpath(filename), 'w') as f: |
201 json.dump(newConfig, f, sort_keys=True, indent=4) |
213 json.dump(save_info, f, sort_keys=True, indent=4) |
202 |
214 |
203 _WebNodeList[WebNode_id]["SavedConfiguration"] = newConfig |
215 _WebNodeList[WebNode_id]["SavedConfiguration"] = newConfig |
204 |
216 |
205 |
217 |
206 |
218 |
216 |
228 |
217 |
229 |
218 def _GetSavedConfiguration(WebNode_id): |
230 def _GetSavedConfiguration(WebNode_id): |
219 """ |
231 """ |
220 Returns a dictionary containing the Modbus parameter configuration |
232 Returns a dictionary containing the Modbus parameter configuration |
221 that was last saved to file. If no file exists, then return None |
233 that was last saved to file. If no file exists, or file contains |
|
234 wrong addr_type (i.e. 'tcp', 'rtu' or 'ascii' -> does not match the |
|
235 addr_type of the WebNode_id), then return None |
222 """ |
236 """ |
223 filename = _WebNodeList[WebNode_id]["filename"] |
237 filename = _WebNodeList[WebNode_id]["filename"] |
224 try: |
238 try: |
225 #if os.path.isfile(filename): |
239 #if os.path.isfile(filename): |
226 saved_config = json.load(open(filename)) |
240 save_info = json.load(open(filename)) |
227 except Exception: |
241 except Exception: |
228 return None |
242 return None |
229 |
243 |
|
244 if save_info["addr_type"] != _WebNodeList[WebNode_id]["addr_type"]: |
|
245 return None |
|
246 if save_info["node_type"] != _WebNodeList[WebNode_id]["node_type"]: |
|
247 return None |
|
248 if "config" not in save_info: |
|
249 return None |
|
250 |
|
251 saved_config = save_info["config"] |
|
252 |
230 #if _CheckConfiguration(saved_config): |
253 #if _CheckConfiguration(saved_config): |
231 # return saved_config |
254 # return saved_config |
232 #else: |
255 #else: |
233 # return None |
256 # return None |
234 |
257 |
399 _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id) |
422 _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id) |
400 |
423 |
401 |
424 |
402 |
425 |
403 |
426 |
404 def _AddWebNode(C_node_id, WebParamListDict, GetParamFuncs, SetParamFuncs): |
427 def _AddWebNode(C_node_id, node_type, GetParamFuncs, SetParamFuncs): |
405 """ |
428 """ |
406 Load from the compiled code (.so file, aloready loaded into memmory) |
429 Load from the compiled code (.so file, aloready loaded into memmory) |
407 the configuration parameters of a specific Modbus plugin node. |
430 the configuration parameters of a specific Modbus plugin node. |
408 This function works with both client and server nodes, depending on the |
431 This function works with both client and server nodes, depending on the |
409 Get/SetParamFunc dictionaries passed to it (either the client or the server |
432 Get/SetParamFunc dictionaries passed to it (either the client or the server |
410 node versions of the Get/Set functions) |
433 node versions of the Get/Set functions) |
411 """ |
434 """ |
412 WebNode_entry = {} |
435 WebNode_entry = {} |
413 |
436 |
|
437 # Get the config_name from the C code... |
414 config_name = GetParamFuncs["config_name"](C_node_id) |
438 config_name = GetParamFuncs["config_name"](C_node_id) |
|
439 # Get the addr_type from the C code... |
415 # addr_type will be one of "tcp", "rtu" or "ascii" |
440 # addr_type will be one of "tcp", "rtu" or "ascii" |
416 addr_type = GetParamFuncs["addr_type" ](C_node_id) |
441 addr_type = GetParamFuncs["addr_type" ](C_node_id) |
417 # For some operations we cannot use the config name (e.g. filename to store config) |
442 # For some operations we cannot use the config name (e.g. filename to store config) |
418 # because the user may be using characters that are invalid for that purpose ('/' for |
443 # because the user may be using characters that are invalid for that purpose ('/' for |
419 # example), so we create a hash of the config_name, and use that instead. |
444 # example), so we create a hash of the config_name, and use that instead. |
436 WebNode_entry["config_name" ] = config_name |
461 WebNode_entry["config_name" ] = config_name |
437 WebNode_entry["config_hash" ] = config_hash |
462 WebNode_entry["config_hash" ] = config_hash |
438 WebNode_entry["filename" ] = os.path.join(_ModbusConfFiledir, "Modbus_config_" + config_hash + ".json") |
463 WebNode_entry["filename" ] = os.path.join(_ModbusConfFiledir, "Modbus_config_" + config_hash + ".json") |
439 WebNode_entry["GetParamFuncs"] = GetParamFuncs |
464 WebNode_entry["GetParamFuncs"] = GetParamFuncs |
440 WebNode_entry["SetParamFuncs"] = SetParamFuncs |
465 WebNode_entry["SetParamFuncs"] = SetParamFuncs |
441 WebNode_entry["WebParamList" ] = WebParamListDict[addr_type] |
466 WebNode_entry["WebParamList" ] = WebParamListDictDict[node_type][addr_type] |
|
467 WebNode_entry["addr_type" ] = addr_type # 'tcp', 'rtu', or 'ascii' (as returned by C function) |
|
468 WebNode_entry["node_type" ] = node_type # 'client', 'server' |
|
469 |
442 |
470 |
443 # Dictionary that contains the Modbus configuration currently being shown |
471 # Dictionary that contains the Modbus configuration currently being shown |
444 # on the web interface |
472 # on the web interface |
445 # This configuration will almost always be identical to the current |
473 # This configuration will almost always be identical to the current |
446 # configuration in the PLC (i.e., the current state stored in the |
474 # configuration in the PLC (i.e., the current state stored in the |
470 # We first declare a dynamic function to work as callback to obtain the default values for each parameter |
498 # We first declare a dynamic function to work as callback to obtain the default values for each parameter |
471 def __GetWebviewConfigurationValue(ctx, argument): |
499 def __GetWebviewConfigurationValue(ctx, argument): |
472 return _GetWebviewConfigurationValue(ctx, WebNode_id, argument) |
500 return _GetWebviewConfigurationValue(ctx, WebNode_id, argument) |
473 |
501 |
474 webFormInterface = [(name, web_dtype (label=web_label, default=__GetWebviewConfigurationValue)) |
502 webFormInterface = [(name, web_dtype (label=web_label, default=__GetWebviewConfigurationValue)) |
475 for name, web_label, c_dtype, web_dtype in WebParamListDict[addr_type]] |
503 for name, web_label, c_dtype, web_dtype in WebNode_entry["WebParamList"]] |
476 |
504 |
477 # Configure the web interface to include the Modbus config parameters |
505 # Configure the web interface to include the Modbus config parameters |
478 def __OnButtonSave(**kwargs): |
506 def __OnButtonSave(**kwargs): |
479 OnButtonSave(WebNode_id=WebNode_id, **kwargs) |
507 OnButtonSave(WebNode_id=WebNode_id, **kwargs) |
480 |
508 |
569 SetServerParamFuncs[name] = getattr(_plcobj.PLClibraryHandle, ParamFuncName) |
597 SetServerParamFuncs[name] = getattr(_plcobj.PLClibraryHandle, ParamFuncName) |
570 SetServerParamFuncs[name].restype = None |
598 SetServerParamFuncs[name].restype = None |
571 SetServerParamFuncs[name].argtypes = [ctypes.c_int, c_dtype] |
599 SetServerParamFuncs[name].argtypes = [ctypes.c_int, c_dtype] |
572 |
600 |
573 for node_id in range(client_count): |
601 for node_id in range(client_count): |
574 _AddWebNode(node_id, _client_WebParamListDict ,GetClientParamFuncs, SetClientParamFuncs) |
602 _AddWebNode(node_id, "client" ,GetClientParamFuncs, SetClientParamFuncs) |
575 |
603 |
576 for node_id in range(server_count): |
604 for node_id in range(server_count): |
577 _AddWebNode(node_id, _server_WebParamListDict, GetServerParamFuncs, SetServerParamFuncs) |
605 _AddWebNode(node_id, "server", GetServerParamFuncs, SetServerParamFuncs) |
578 |
606 |
579 |
607 |
580 |
608 |
581 |
609 |
582 |
610 |