modbus/web_settings.py
changeset 2670 fd348d79a1f3
parent 2669 be233279d179
child 2672 4bc60e426dd6
equal deleted inserted replaced
2669:be233279d179 2670:fd348d79a1f3
   326     except Exception:
   326     except Exception:
   327         return ""
   327         return ""
   328 
   328 
   329 
   329 
   330 
   330 
   331 
       
   332 def _updateWebInterface(WebNode_id):
       
   333     """
       
   334     Add/Remove buttons to/from the web interface depending on the current state
       
   335        - If there is a saved state => add a delete saved state button
       
   336     """
       
   337 
       
   338     config_hash = _WebNodeList[WebNode_id]["config_hash"]
       
   339     config_name = _WebNodeList[WebNode_id]["config_name"]
       
   340     
       
   341     # Add a "Delete Saved Configuration" button if there is a saved configuration!
       
   342     if _WebNodeList[WebNode_id]["SavedConfiguration"] is None:
       
   343         NS.ConfigurableSettings.delSettings("ModbusConfigDelSaved" + config_hash)
       
   344     else:
       
   345         def __OnButtonDel(**kwargs):
       
   346             return OnButtonDel(WebNode_id = WebNode_id, **kwargs)
       
   347                 
       
   348         NS.ConfigurableSettings.addSettings(
       
   349             "ModbusConfigDelSaved"      + config_hash,  # name (internal, may not contain spaces, ...)
       
   350             _("Modbus Configuration: ") + config_name,  # description (user visible label)
       
   351             [],                                         # fields  (empty, no parameters required!)
       
   352             _("Delete Configuration Stored in Persistent Storage"), # button label
       
   353             __OnButtonDel,                              # callback    
       
   354             "ModbusConfigParm"          + config_hash)  # Add after entry xxxx
       
   355 
       
   356 
       
   357 
       
   358 def OnButtonSave(**kwargs):
   331 def OnButtonSave(**kwargs):
   359     """
   332     """
   360     Function called when user clicks 'Save' button in web interface
   333     Function called when user clicks 'Save' button in web interface
   361     The function will configure the Modbus plugin in the PLC with the values
   334     The function will configure the Modbus plugin in the PLC with the values
   362     specified in the web interface. However, values must be validated first!
   335     specified in the web interface. However, values must be validated first!
   394     # Update the viewable configuration
   367     # Update the viewable configuration
   395     # The PLC may have coerced the values on calling _SetPLCConfiguration()
   368     # The PLC may have coerced the values on calling _SetPLCConfiguration()
   396     # so we do not set it directly to newConfig
   369     # so we do not set it directly to newConfig
   397     _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id)
   370     _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id)
   398 
   371 
   399     # File has just been created => Delete button must be shown on web interface!
   372 
   400     _updateWebInterface(WebNode_id)
   373 
   401 
   374 def OnButtonReset(**kwargs):
   402 
       
   403 
       
   404 
       
   405 def OnButtonDel(**kwargs):
       
   406     """
   375     """
   407     Function called when user clicks 'Delete' button in web interface
   376     Function called when user clicks 'Delete' button in web interface
   408     The function will delete the file containing the persistent
   377     The function will delete the file containing the persistent
   409     Modbus configution
   378     Modbus configution
   410     """
   379     """
   422     _WebNodeList[WebNode_id]["WebviewConfiguration"] = new_config
   391     _WebNodeList[WebNode_id]["WebviewConfiguration"] = new_config
   423     
   392     
   424     # Reset SavedConfiguration
   393     # Reset SavedConfiguration
   425     _WebNodeList[WebNode_id]["SavedConfiguration"] = None
   394     _WebNodeList[WebNode_id]["SavedConfiguration"] = None
   426     
   395     
   427     # File has just been deleted => Delete button on web interface no longer needed!
   396 
   428     _updateWebInterface(WebNode_id)
       
   429 
       
   430 
       
   431 
       
   432 
       
   433 def OnButtonShowCur(**kwargs):
       
   434     """
       
   435     Function called when user clicks 'Show Current PLC Configuration' button in web interface
       
   436     The function will load the current PLC configuration into the web form
       
   437 
       
   438     Note that this function does not get called directly. The real callback
       
   439     function is the dynamic __OnButtonShowCur() function, which will add the 
       
   440     "WebNode_id" argument, and call this function to do the work.
       
   441     """
       
   442     WebNode_id = kwargs.get("WebNode_id", None)
       
   443     
       
   444     _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id)
       
   445     
       
   446 
   397 
   447 
   398 
   448 
   399 
   449 def _AddWebNode(C_node_id, node_type, GetParamFuncs, SetParamFuncs):
   400 def _AddWebNode(C_node_id, node_type, GetParamFuncs, SetParamFuncs):
   450     """
   401     """
   535 
   486 
   536     # Configure the web interface to include the Modbus config parameters
   487     # Configure the web interface to include the Modbus config parameters
   537     def __OnButtonSave(**kwargs):
   488     def __OnButtonSave(**kwargs):
   538         OnButtonSave(WebNode_id=WebNode_id, **kwargs)
   489         OnButtonSave(WebNode_id=WebNode_id, **kwargs)
   539 
   490 
   540     NS.ConfigurableSettings.addSettings(
   491     WebSettings = NS.newExtensionSetting("Modbus "+config_hash)
       
   492 
       
   493     WebSettings.addSettings(
   541         "ModbusConfigParm"          + config_hash,     # name (internal, may not contain spaces, ...)
   494         "ModbusConfigParm"          + config_hash,     # name (internal, may not contain spaces, ...)
   542         _("Modbus Configuration: ") + config_name,     # description (user visible label)
   495         _("Modbus Configuration: ") + config_name,     # description (user visible label)
   543         webFormInterface,                              # fields
   496         webFormInterface,                              # fields
   544         _("Save Configuration to Persistent Storage"), # button label
   497         _("Apply"), # button label
   545         __OnButtonSave)                                # callback   
   498         __OnButtonSave)                                # callback   
   546     
   499     
   547     # Add a "View Current Configuration" button 
   500     def __OnButtonReset(**kwargs):
   548     def __OnButtonShowCur(**kwargs):
   501         return OnButtonReset(WebNode_id = WebNode_id, **kwargs)
   549         OnButtonShowCur(WebNode_id=WebNode_id, **kwargs)
   502             
   550 
   503     def getConfigStatus():
   551     NS.ConfigurableSettings.addSettings(
   504         if WebNode_entry["WebviewConfiguration"] == WebNode_entry["DefaultConfiguration"]:
   552         "ModbusConfigViewCur"       + config_hash, # name (internal, may not contain spaces, ...)
   505             return "Unchanged"
   553         _("Modbus Configuration: ") + config_name,     # description (user visible label)
   506         return "Modified"
   554         [],                                        # fields  (empty, no parameters required!)
   507 
   555         _("Show Current PLC Configuration"),       # button label
   508     WebSettings.addSettings(
   556         __OnButtonShowCur)                         # callback    
   509         "ModbusConfigDelSaved"      + config_hash,  # name (internal, may not contain spaces, ...)
   557 
   510         _("Modbus Configuration: ") + config_name,  # description (user visible label)
   558     # Add the Delete button to the web interface, if required
   511         [ ("status",
   559     _updateWebInterface(WebNode_id)
   512            annotate.String(label=_("Current state"),
       
   513                            immutable=True,
       
   514                            default=lambda *k:getConfigStatus())),
       
   515         ],                                       # fields  (empty, no parameters required!)
       
   516         _("Reset"), # button label
       
   517         __OnButtonReset)
   560 
   518 
   561 
   519 
   562 
   520 
   563 
   521 
   564 def _runtime_modbus_websettings_%(location_str)s_init():
   522 def _runtime_modbus_websettings_%(location_str)s_init():
   565     """
   523     """
   566     Callback function, called (by PLCObject.py) when a new PLC program
   524     Callback function, called (by PLCObject.py) when a new PLC program
   567     (i.e. XXX.so file) is transfered to the PLC runtime
   525     (i.e. XXX.so file) is transfered to the PLC runtime
   568     and loaded into memory
   526     and loaded into memory
   569     """
   527     """
   570     print("_runtime_modbus_websettings_init")
       
   571 
   528 
   572     #PLCObject.LogMessage("Modbus web server extension::OnLoadPLC() Called...")
   529     #PLCObject.LogMessage("Modbus web server extension::OnLoadPLC() Called...")
   573 
   530 
   574     if PLCObject.PLClibraryHandle is None:
   531     if PLCObject.PLClibraryHandle is None:
   575         # PLC was loaded but we don't have access to the library of compiled code (.so lib)?
   532         # PLC was loaded but we don't have access to the library of compiled code (.so lib)?
   649     # Delete the Modbus specific web interface extensions
   606     # Delete the Modbus specific web interface extensions
   650     # (Safe to ask to delete, even if it has not been added!)
   607     # (Safe to ask to delete, even if it has not been added!)
   651     global _WebNodeList    
   608     global _WebNodeList    
   652     for WebNode_entry in _WebNodeList:
   609     for WebNode_entry in _WebNodeList:
   653         config_hash = WebNode_entry["config_hash"]
   610         config_hash = WebNode_entry["config_hash"]
   654         NS.ConfigurableSettings.delSettings("ModbusConfigParm"     + config_hash)
   611         NS.removeExtensionSetting("Modbus "+config_hash)
   655         NS.ConfigurableSettings.delSettings("ModbusConfigViewCur"  + config_hash)  
       
   656         NS.ConfigurableSettings.delSettings("ModbusConfigDelSaved" + config_hash)  
       
   657         
   612         
   658     # Dele all entries...
   613     # Dele all entries...
   659     _WebNodeList = []
   614     _WebNodeList = []
   660 
   615