modbus/web_settings.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 17 Jan 2022 15:17:47 +0100
changeset 3407 2f5c30054d50
parent 3163 7cf38cd0d820
child 3733 d1acf20e8e7c
permissions -rw-r--r--
OPC-UA client: "static" class was forgotten for open62541 client structure preventing multiple client.
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
#!/usr/bin/env python
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
# This file is part of Beremiz runtime.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
#
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
# Copyright (C) 2020: Mario de Sousa
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
#
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
# See COPYING.Runtime file for copyrights details.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
#
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
# This library is free software; you can redistribute it and/or
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
# modify it under the terms of the GNU Lesser General Public
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
# License as published by the Free Software Foundation; either
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
# version 2.1 of the License, or (at your option) any later version.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
# This library is distributed in the hope that it will be useful,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
# Lesser General Public License for more details.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
# You should have received a copy of the GNU Lesser General Public
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
# License along with this library; if not, write to the Free Software
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
##############################################################################################
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
# This file implements an extension to the web server embedded in the Beremiz_service.py     #
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
# runtime manager (webserver is in runtime/NevowServer.py).                                  #
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
#                                                                                            #
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
# The extension implemented in this file allows for runtime configuration                    #
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
# of Modbus plugin parameters                                                                #
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
##############################################################################################
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
import json
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
import os
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
import ctypes
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
import string
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
import hashlib
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
from formless import annotate, webform
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
    45
import runtime.NevowServer as NS
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
# Directory in which to store the persistent configurations
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
# Should be a directory that does not get wiped on reboot!
2673
80e7eb44cab4 BACnet and Modbus : fix configuration storage directory
Edouard Tisserant
parents: 2672
diff changeset
    49
_ModbusConfFiledir = WorkingDir
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    51
# List of all Web Extension Setting nodes we are handling.
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    52
# One WebNode each for:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    53
#   - Modbus TCP client 
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    54
#   - Modbus TCP server
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    55
#   - Modbus RTU client
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    56
#   - Modbus RTU slave
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    57
# configured in the loaded PLC (i.e. the .so file loaded into memory)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    58
# Each entry will be a dictionary. See _AddWebNode() for the details
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    59
# of the data structure in each entry.
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    60
_WebNodeList = []
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    61
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    62
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    63
2666
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    64
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    65
class MB_StrippedString(annotate.String):
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    66
    def __init__(self, *args, **kwargs):
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    67
        annotate.String.__init__(self, strip = True, *args, **kwargs)
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    68
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
    69
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    70
class MB_StopBits(annotate.Choice):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    71
    _choices = [0, 1, 2]
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    72
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    73
    def coerce(self, val, configurable):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    74
        return int(val)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    75
    def __init__(self, *args, **kwargs):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    76
        annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    77
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    78
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    79
class MB_Baud(annotate.Choice):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    80
    _choices = [110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    81
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    82
    def coerce(self, val, configurable):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    83
        return int(val)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    84
    def __init__(self, *args, **kwargs):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    85
        annotate.Choice.__init__(self, choices = self._choices, *args, **kwargs)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    86
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    87
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    88
class MB_Parity(annotate.Choice):
2660
ddbf029f5682 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2658
diff changeset
    89
    # For more info on what this class really does, have a look at the code in
ddbf029f5682 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2658
diff changeset
    90
    # file twisted/nevow/annotate.py
ddbf029f5682 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2658
diff changeset
    91
    # grab this code from $git clone https://github.com/twisted/nevow/
ddbf029f5682 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2658
diff changeset
    92
    # 
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    93
    # Warning: do _not_ name this variable choice[] without underscore, as that name is
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    94
    # already used for another similar variable by the underlying class annotate.Choice
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    95
    _choices = [  0,      1,      2  ]
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    96
    _label   = ["none", "odd", "even"]
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    97
    
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
    98
    def choice_to_label(self, key):
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
    99
        #PLCObject.LogMessage("Modbus web server extension::choice_to_label()  " + str(key))
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   100
        return self._label[key]
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   101
    
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   102
    def coerce(self, val, configurable):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   103
        """Coerce a value with the help of an object, which is the object
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   104
        we are configuring.
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   105
        """
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   106
        # Basically, make sure the value the user introduced is valid, and transform
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   107
        # into something that is valid if necessary or mark it as an error 
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   108
        # (by raising an exception ??).
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   109
        #
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   110
        # We are simply using this functions to transform the input value (a string)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   111
        # into an integer. Note that although the available options are all
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   112
        # integers (0, 1 or 2), even though what is shown on the user interface
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   113
        # are actually strings, i.e. the labels), these parameters are for some 
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   114
        # reason being parsed as strings, so we need to map them back to an
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   115
        # integer.
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   116
        #
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   117
        #PLCObject.LogMessage("Modbus web server extension::coerce  " + val )
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   118
        return int(val)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   119
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   120
    def __init__(self, *args, **kwargs):
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   121
        annotate.Choice.__init__(self, 
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   122
                                 choices   = self._choices,
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   123
                                 stringify = self.choice_to_label,
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   124
                                 *args, **kwargs)
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   125
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   126
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   127
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   128
# Parameters we will need to get from the C code, but that will not be shown
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   129
# on the web interface. Common to all modbus entry types (client/server, tcp/rtu/ascii)
2658
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   130
#
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   131
# The annotate type entry is basically useless and is completely ignored.
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   132
# We kee that entry so that this list can later be correctly merged with the
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   133
# following lists...
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   134
General_parameters = [
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   135
    #    param. name       label                        ctype type         annotate type
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   136
    # (C code var name)   (used on web interface)      (C data type)       (web data type)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   137
    #                                                                      (annotate.String,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   138
    #                                                                       annotate.Integer, ...)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   139
    ("config_name"      , _("")                      , ctypes.c_char_p,    annotate.String),
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   140
    ("addr_type"        , _("")                      , ctypes.c_char_p,    annotate.String)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   141
    ]                                                                      
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   142
                                                                           
2661
9d08e3bba629 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2660
diff changeset
   143
# Parameters we will need to get from the C code, and that _will_ be shown
9d08e3bba629 add comment
Mario de Sousa <msousa@fe.up.pt>
parents: 2660
diff changeset
   144
# on the web interface.
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   145
TCPclient_parameters = [                                                   
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   146
    #    param. name       label                        ctype type         annotate type
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   147
    # (C code var name)   (used on web interface)      (C data type)       (web data type)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   148
    #                                                                      (annotate.String,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   149
    #                                                                       annotate.Integer, ...)
2666
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   150
    ("host"             , _("Remote IP Address")     , ctypes.c_char_p,    MB_StrippedString),
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   151
    ("port"             , _("Remote Port Number")    , ctypes.c_char_p,    MB_StrippedString),
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   152
    ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer )
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   153
    ]
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   154
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   155
RTUclient_parameters = [                                                   
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   156
    #    param. name       label                        ctype type         annotate type
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   157
    # (C code var name)   (used on web interface)      (C data type)       (web data type)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   158
    #                                                                      (annotate.String,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   159
    #                                                                       annotate.Integer, ...)
2666
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   160
    ("device"           , _("Serial Port")           , ctypes.c_char_p,    MB_StrippedString),
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   161
    ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   162
    ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   163
    ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   164
    ("comm_period"      , _("Invocation Rate (ms)")  , ctypes.c_ulonglong, annotate.Integer)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   165
    ]
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   166
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   167
TCPserver_parameters = [                                                   
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   168
    #    param. name       label                        ctype type         annotate type
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   169
    # (C code var name)   (used on web interface)      (C data type)       (web data type)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   170
    #                                                                      (annotate.String,
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   171
    #                                                                       annotate.Integer, ...)
2666
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   172
    ("host"             , _("Local IP Address")      , ctypes.c_char_p,    MB_StrippedString),
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   173
    ("port"             , _("Local Port Number")     , ctypes.c_char_p,    MB_StrippedString),
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   174
    ("slave_id"         , _("Slave ID")              , ctypes.c_ubyte,     annotate.Integer )
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   175
    ]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   176
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   177
RTUslave_parameters = [                                                   
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   178
    #    param. name       label                        ctype type         annotate type
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   179
    # (C code var name)   (used on web interface)      (C data type)       (web data type)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   180
    #                                                                      (annotate.String,
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   181
    #                                                                       annotate.Integer, ...)
2666
5f48d5e60a81 Modbus plugin, web interface: strip leading and trailing spaces from string parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2665
diff changeset
   182
    ("device"           , _("Serial Port")           , ctypes.c_char_p,    MB_StrippedString),
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   183
    ("baud"             , _("Baud Rate")             , ctypes.c_int,       MB_Baud         ),
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   184
    ("parity"           , _("Parity")                , ctypes.c_int,       MB_Parity       ),
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   185
    ("stop_bits"        , _("Stop Bits")             , ctypes.c_int,       MB_StopBits     ),
2691
6e4a95621080 Modbus: fixed typo in ctype typing.
Edouard Tisserant
parents: 2690
diff changeset
   186
    ("slave_id"         , _("Slave ID")              , ctypes.c_ubyte,     annotate.Integer)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   187
    ]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   188
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   189
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   190
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   191
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   192
# Dictionary containing List of Web viewable parameters
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   193
# Note: the dictionary key must be the same as the string returned by the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   194
# __modbus_get_ClientNode_addr_type()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   195
# __modbus_get_ServerNode_addr_type()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   196
# functions implemented in C (see modbus/mb_runtime.c)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   197
_client_WebParamListDict = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   198
_client_WebParamListDict["tcp"  ] = TCPclient_parameters
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   199
_client_WebParamListDict["rtu"  ] = RTUclient_parameters
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   200
_client_WebParamListDict["ascii"] = []  # (Note: ascii not yet implemented in Beremiz modbus plugin)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   201
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   202
_server_WebParamListDict = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   203
_server_WebParamListDict["tcp"  ] = TCPserver_parameters
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   204
_server_WebParamListDict["rtu"  ] = RTUslave_parameters
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   205
_server_WebParamListDict["ascii"] = []  # (Note: ascii not yet implemented in Beremiz modbus plugin)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   207
WebParamListDictDict = {}
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   208
WebParamListDictDict['client'] = _client_WebParamListDict
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   209
WebParamListDictDict['server'] = _server_WebParamListDict
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   210
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   211
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   212
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   213
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   214
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   215
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   216
def _SetModbusSavedConfiguration(WebNode_id, newConfig):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
    """ Stores a dictionary in a persistant file containing the Modbus parameter configuration """
2675
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   218
    WebNode_entry = _WebNodeList[WebNode_id]
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   219
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   220
    if WebNode_entry["DefaultConfiguration"] == newConfig:
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   221
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   222
        _DelModbusSavedConfiguration(WebNode_id)
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   223
        WebNode_entry["ModbusSavedConfiguration"] = None
2675
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   224
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   225
    else:
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   226
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   227
        # Add the addr_type and node_type to the data that will be saved to file
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   228
        # This allows us to confirm the saved data contains the correct addr_type
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   229
        # when loading from file
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   230
        save_info = {}
2690
4de6a587f7f9 MODBUS: Fix saving of websettings.
Edouard Tisserant
parents: 2686
diff changeset
   231
        save_info["addr_type"] = WebNode_entry["addr_type"]
2675
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   232
        save_info["node_type"] = WebNode_entry["node_type"]
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   233
        save_info["config"   ] = newConfig
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   234
        
2675
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   235
        filename = WebNode_entry["filename"]
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   236
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   237
        with open(os.path.realpath(filename), 'w') as f:
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   238
            json.dump(save_info, f, sort_keys=True, indent=4)
3ca5611d6308 Modbus: Delete stored configuration when identical to default configuration.
Edouard Tisserant
parents: 2673
diff changeset
   239
            
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   240
        WebNode_entry["ModbusSavedConfiguration"] = newConfig
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   241
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   242
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   243
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   244
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   245
def _DelModbusSavedConfiguration(WebNode_id):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   246
    """ Deletes the file cotaining the persistent Modbus configuration """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   247
    filename = _WebNodeList[WebNode_id]["filename"]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   248
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   249
    if os.path.exists(filename):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
        os.remove(filename)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   251
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   252
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   253
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   254
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   255
def _GetModbusSavedConfiguration(WebNode_id):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   256
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   257
    Returns a dictionary containing the Modbus parameter configuration
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   258
    that was last saved to file. If no file exists, or file contains 
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   259
    wrong addr_type (i.e. 'tcp', 'rtu' or 'ascii' -> does not match the
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   260
    addr_type of the WebNode_id), then return None
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   261
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   262
    filename = _WebNodeList[WebNode_id]["filename"]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   263
    try:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   264
        #if os.path.isfile(filename):
2690
4de6a587f7f9 MODBUS: Fix saving of websettings.
Edouard Tisserant
parents: 2686
diff changeset
   265
        save_info = json.load(open(os.path.realpath(filename)))
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   266
    except Exception:    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   267
        return None
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   268
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   269
    if save_info["addr_type"] != _WebNodeList[WebNode_id]["addr_type"]:
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   270
        return None
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   271
    if save_info["node_type"] != _WebNodeList[WebNode_id]["node_type"]:
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   272
        return None
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   273
    if "config" not in save_info:
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   274
        return None
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   275
    
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   276
    saved_config = save_info["config"]
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   277
    
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   278
    #if _CheckConfiguration(saved_config):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   279
    #    return saved_config
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   280
    #else:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   281
    #    return None
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   282
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   283
    return saved_config
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   284
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   285
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   286
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   287
def _GetModbusPLCConfiguration(WebNode_id):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   288
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   289
    Returns a dictionary containing the current Modbus parameter configuration
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   290
    stored in the C variables in the loaded PLC (.so file)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   291
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   292
    current_config = {}
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   293
    C_node_id      = _WebNodeList[WebNode_id]["C_node_id"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   294
    WebParamList   = _WebNodeList[WebNode_id]["WebParamList"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   295
    GetParamFuncs  = _WebNodeList[WebNode_id]["GetParamFuncs"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   296
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   297
    for par_name, x1, x2, x3 in WebParamList:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   298
        value = GetParamFuncs[par_name](C_node_id)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   299
        if value is not None:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   300
            current_config[par_name] = value
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   301
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   302
    return current_config
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   303
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   304
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   305
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   306
def _SetModbusPLCConfiguration(WebNode_id, newconfig):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   307
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   308
    Stores the Modbus parameter configuration into the
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   309
    the C variables in the loaded PLC (.so file)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   310
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   311
    C_node_id      = _WebNodeList[WebNode_id]["C_node_id"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   312
    SetParamFuncs  = _WebNodeList[WebNode_id]["SetParamFuncs"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   313
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   314
    for par_name in newconfig:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   315
        value = newconfig[par_name]
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   316
        if value is not None:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   317
            SetParamFuncs[par_name](C_node_id, value)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   318
            
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   319
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   320
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   321
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   322
def _GetModbusWebviewConfigurationValue(ctx, WebNode_id, argument):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   323
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   324
    Callback function, called by the web interface (NevowServer.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   325
    to fill in the default value of each parameter of the web form
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   326
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   327
    Note that the real callback function is a dynamically created function that
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   328
    will simply call this function to do the work. It will also pass the WebNode_id 
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   329
    as a parameter.
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   330
    """    
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   331
    try:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   332
        return _WebNodeList[WebNode_id]["WebviewConfiguration"][argument.name]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
    except Exception:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   334
        return ""
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   335
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   336
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   337
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   338
def OnModbusButtonSave(**kwargs):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   339
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   340
    Function called when user clicks 'Save' button in web interface
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   341
    The function will configure the Modbus plugin in the PLC with the values
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   342
    specified in the web interface. However, values must be validated first!
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   343
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   344
    Note that this function does not get called directly. The real callback
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   345
    function is the dynamic __OnButtonSave() function, which will add the 
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   346
    "WebNode_id" argument, and call this function to do the work.
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   347
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   348
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   349
    #PLCObject.LogMessage("Modbus web server extension::OnModbusButtonSave()  Called")
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   350
    
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   351
    newConfig    = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   352
    WebNode_id   =  kwargs.get("WebNode_id", None)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   353
    WebParamList = _WebNodeList[WebNode_id]["WebParamList"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   354
    
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   355
    for par_name, x1, x2, x3 in WebParamList:
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   356
        value = kwargs.get(par_name, None)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   357
        if value is not None:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   358
            newConfig[par_name] = value
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   359
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   360
    # First check if configuration is OK.
2658
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   361
    # Note that this is not currently required, as we use drop down choice menus
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   362
    # for baud, parity and sop bits, so the values should always be correct!
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   363
    #if not _CheckWebConfiguration(newConfig):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   364
    #    return
2658
fdca999c0c1a Modbus plugin web configuration: fix comments, and delete un-used code
Mario de Sousa <msousa@fe.up.pt>
parents: 2657
diff changeset
   365
    
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   366
    # store to file the new configuration so that 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   367
    # we can recoup the configuration the next time the PLC
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   368
    # has a cold start (i.e. when Beremiz_service.py is retarted)
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   369
    _SetModbusSavedConfiguration(WebNode_id, newConfig)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   370
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   371
    # Configure PLC with the current Modbus parameters
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   372
    _SetModbusPLCConfiguration(WebNode_id, newConfig)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   373
2665
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   374
    # Update the viewable configuration
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   375
    # The PLC may have coerced the values on calling _SetModbusPLCConfiguration()
2665
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   376
    # so we do not set it directly to newConfig
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   377
    _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetModbusPLCConfiguration(WebNode_id)
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   378
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   379
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   380
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   381
def OnModbusButtonReset(**kwargs):
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   382
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   383
    Function called when user clicks 'Delete' button in web interface
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   384
    The function will delete the file containing the persistent
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   385
    Modbus configution
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   386
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   387
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   388
    WebNode_id = kwargs.get("WebNode_id", None)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   389
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
    # Delete the file
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   391
    _DelModbusSavedConfiguration(WebNode_id)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   392
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   393
    # Set the current configuration to the default (hardcoded in C)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   394
    new_config = _WebNodeList[WebNode_id]["DefaultConfiguration"]
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   395
    _SetModbusPLCConfiguration(WebNode_id, new_config)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   396
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   397
    #Update the webviewconfiguration
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   398
    _WebNodeList[WebNode_id]["WebviewConfiguration"] = new_config
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   399
    
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   400
    # Reset ModbusSavedConfiguration
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   401
    _WebNodeList[WebNode_id]["ModbusSavedConfiguration"] = None
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   402
    
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   403
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   404
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   405
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   406
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   407
def _AddWebNode(C_node_id, node_type, GetParamFuncs, SetParamFuncs):
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   408
    """
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   409
    Load from the compiled code (.so file, aloready loaded into memmory)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   410
    the configuration parameters of a specific Modbus plugin node.
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   411
    This function works with both client and server nodes, depending on the
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   412
    Get/SetParamFunc dictionaries passed to it (either the client or the server
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   413
    node versions of the Get/Set functions)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   414
    """
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   415
    WebNode_entry = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   416
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   417
    # Get the config_name from the C code...
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   418
    config_name = GetParamFuncs["config_name"](C_node_id)
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   419
    # Get the addr_type from the C code...
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   420
    # addr_type will be one of "tcp", "rtu" or "ascii"
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   421
    addr_type   = GetParamFuncs["addr_type"  ](C_node_id)   
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   422
    # For some operations we cannot use the config name (e.g. filename to store config)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   423
    # because the user may be using characters that are invalid for that purpose ('/' for
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   424
    # example), so we create a hash of the config_name, and use that instead.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   425
    config_hash = hashlib.md5(config_name).hexdigest()
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   426
    
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   427
    #PLCObject.LogMessage("Modbus web server extension::_AddWebNode("+str(C_node_id)+") config_name="+config_name)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   428
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   429
    # Add the new entry to the global list
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   430
    # Note: it is OK, and actually necessary, to do this _before_ seting all the parameters in WebNode_entry
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   431
    #       WebNode_entry will be stored as a reference, so we can later insert parameters at will.
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   432
    global _WebNodeList
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   433
    _WebNodeList.append(WebNode_entry)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   434
    WebNode_id = len(_WebNodeList) - 1
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   435
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   436
    # store all WebNode relevant data for future reference
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   437
    #
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   438
    # Note that "WebParamList" will reference one of:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   439
    #  - TCPclient_parameters, TCPserver_parameters, RTUclient_parameters, RTUslave_parameters
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   440
    WebNode_entry["C_node_id"    ] = C_node_id
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   441
    WebNode_entry["config_name"  ] = config_name 
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   442
    WebNode_entry["config_hash"  ] = config_hash
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   443
    WebNode_entry["filename"     ] = os.path.join(_ModbusConfFiledir, "Modbus_config_" + config_hash + ".json")
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   444
    WebNode_entry["GetParamFuncs"] = GetParamFuncs
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   445
    WebNode_entry["SetParamFuncs"] = SetParamFuncs
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   446
    WebNode_entry["WebParamList" ] = WebParamListDictDict[node_type][addr_type] 
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   447
    WebNode_entry["addr_type"    ] = addr_type  # 'tcp', 'rtu', or 'ascii' (as returned by C function)
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   448
    WebNode_entry["node_type"    ] = node_type  # 'client', 'server'
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   449
        
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   450
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   451
    # Dictionary that contains the Modbus configuration currently being shown
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   452
    # on the web interface
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   453
    # This configuration will almost always be identical to the current
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   454
    # configuration in the PLC (i.e., the current state stored in the 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   455
    # C variables in the .so file).
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   456
    # The configuration viewed on the web will only be different to the current 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   457
    # configuration when the user edits the configuration, and when
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   458
    # the user asks to save an edited configuration that contains an error.
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   459
    WebNode_entry["WebviewConfiguration"] = None
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   460
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   461
    # Upon PLC load, this Dictionary is initialised with the Modbus configuration
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   462
    # hardcoded in the C file
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   463
    # (i.e. the configuration inserted in Beremiz IDE when project was compiled)
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   464
    WebNode_entry["DefaultConfiguration"] = _GetModbusPLCConfiguration(WebNode_id)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   465
    WebNode_entry["WebviewConfiguration"] = WebNode_entry["DefaultConfiguration"]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   466
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   467
    # Dictionary that stores the Modbus configuration currently stored in a file
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   468
    # Currently only used to decide whether or not to show the "Delete" button on the
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   469
    # web interface (only shown if "ModbusSavedConfiguration" is not None)
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   470
    SavedConfig = _GetModbusSavedConfiguration(WebNode_id)
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   471
    WebNode_entry["ModbusSavedConfiguration"] = SavedConfig
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   472
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   473
    if SavedConfig is not None:
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   474
        _SetModbusPLCConfiguration(WebNode_id, SavedConfig)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   475
        WebNode_entry["WebviewConfiguration"] = SavedConfig
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   476
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   477
    # Define the format for the web form used to show/change the current parameters
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   478
    # We first declare a dynamic function to work as callback to obtain the default values for each parameter
2657
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   479
    # Note: We transform every parameter into a string
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   480
    #       This is not strictly required for parameters of type annotate.Integer that will correctly
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   481
    #           accept the default value as an Integer python object
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   482
    #       This is obviously also not required for parameters of type annotate.String, that are
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   483
    #           always handled as strings.
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   484
    #       However, the annotate.Choice parameters (and all parameters that derive from it,
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   485
    #           sucn as Parity, Baud, etc.) require the default value as a string
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   486
    #           even though we store it as an integer, which is the data type expected
41c34e7d196d Modbus plugin web configuration: use drop down choices for baud, parity and stop bits.
Mario de Sousa <msousa@fe.up.pt>
parents: 2656
diff changeset
   487
    #           by the set_***() C functions in mb_runtime.c
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   488
    def __GetWebviewConfigurationValue(ctx, argument):
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   489
        return str(_GetModbusWebviewConfigurationValue(ctx, WebNode_id, argument))
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   490
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   491
    webFormInterface = [(name, web_dtype (label=web_label, default=__GetWebviewConfigurationValue)) 
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   492
                    for name, web_label, c_dtype, web_dtype in WebNode_entry["WebParamList"]]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   493
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   494
    # Configure the web interface to include the Modbus config parameters
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   495
    def __OnButtonSave(**kwargs):
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   496
        OnModbusButtonSave(WebNode_id=WebNode_id, **kwargs)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   497
2672
4bc60e426dd6 Runtime extrension web settings: distinguish display string and token when calling NS.newExtensionSetting, thus allowing meaningful name to be displayed instead of an id.
Edouard Tisserant
parents: 2670
diff changeset
   498
    WebSettings = NS.newExtensionSetting("Modbus #"+ str(WebNode_id), config_hash)
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   499
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   500
    WebSettings.addSettings(
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   501
        "ModbusConfigParm"          + config_hash,     # name (internal, may not contain spaces, ...)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   502
        _("Modbus Configuration: ") + config_name,     # description (user visible label)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   503
        webFormInterface,                              # fields
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   504
        _("Apply"), # button label
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   505
        __OnButtonSave)                                # callback   
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   506
    
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   507
    def __OnButtonReset(**kwargs):
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   508
        return OnModbusButtonReset(WebNode_id = WebNode_id, **kwargs)
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   509
            
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   510
    def getModbusConfigStatus():
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   511
        if WebNode_entry["WebviewConfiguration"] == WebNode_entry["DefaultConfiguration"]:
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   512
            return "Unchanged"
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   513
        return "Modified"
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   514
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   515
    WebSettings.addSettings(
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   516
        "ModbusConfigDelSaved"      + config_hash,  # name (internal, may not contain spaces, ...)
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   517
        _("Modbus Configuration: ") + config_name,  # description (user visible label)
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   518
        [ ("status",
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   519
           annotate.String(label=_("Current state"),
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   520
                           immutable=True,
2686
703ebf57508a Modbus and Bacnet websettings : Rename variables and functions to avoid name collisions.
Edouard Tisserant
parents: 2676
diff changeset
   521
                           default=lambda *k:getModbusConfigStatus())),
2670
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   522
        ],                                       # fields  (empty, no parameters required!)
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   523
        _("Reset"), # button label
fd348d79a1f3 BACnet and Modbus : Simpler configuration management. NevowServer.py now allows each extension to create and delete multiple configuration forms in the setting page, deprecating delSettings and addAfter.
Edouard Tisserant
parents: 2669
diff changeset
   524
        __OnButtonReset)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   525
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   526
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   527
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   528
2703
32ffdb32b14e Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2691
diff changeset
   529
def _runtime_%(location_str)s_modbus_websettings_init():
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   530
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   531
    Callback function, called (by PLCObject.py) when a new PLC program
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   532
    (i.e. XXX.so file) is transfered to the PLC runtime
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   533
    and loaded into memory
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
    """
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   535
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   536
    #PLCObject.LogMessage("Modbus web server extension::OnLoadPLC() Called...")
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   537
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   538
    if PLCObject.PLClibraryHandle is None:
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   539
        # PLC was loaded but we don't have access to the library of compiled code (.so lib)?
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   540
        # Hmm... This shold never occur!! 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   541
        return  
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   542
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   543
    # Get the number of Modbus Client and Servers (Modbus plugin)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   544
    # configured in the currently loaded PLC project (i.e., the .so file)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   545
    # If the "__modbus_plugin_client_node_count" 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   546
    # or the "__modbus_plugin_server_node_count" C variables 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   547
    # are not present in the .so file we conclude that the currently loaded 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   548
    # PLC does not have the Modbus plugin included (situation (2b) described above init())
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   549
    try:
2676
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   550
        # XXX TODO : stop reading from PLC .so file. This code is template code
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   551
        #            that can use modbus extension build data, such as client node count.
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   552
        client_count = ctypes.c_int.in_dll(PLCObject.PLClibraryHandle, "__modbus_plugin_client_node_count").value
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   553
        server_count = ctypes.c_int.in_dll(PLCObject.PLClibraryHandle, "__modbus_plugin_server_node_count").value
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   554
    except Exception:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   555
        # Loaded PLC does not have the Modbus plugin => nothing to do
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   556
        #   (i.e. do _not_ configure and make available the Modbus web interface)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   557
        return
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   558
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   559
    if client_count < 0: client_count = 0
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   560
    if server_count < 0: server_count = 0
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   561
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   562
    if (client_count == 0) and (server_count == 0):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   563
        # The Modbus plugin in the loaded PLC does not have any client and servers configured
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   564
        #  => nothing to do (i.e. do _not_ configure and make available the Modbus web interface)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   565
        return
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   566
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   567
    # Map the get/set functions (written in C code) we will be using to get/set the configuration parameters
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   568
    # Will contain references to the C functions (implemented in beremiz/modbus/mb_runtime.c)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   569
    GetClientParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   570
    SetClientParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   571
    GetServerParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   572
    SetServerParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   573
2676
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   574
    # XXX TODO : stop reading from PLC .so file. This code is template code
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   575
    #            that can use modbus extension build data
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   576
    for name, web_label, c_dtype, web_dtype in TCPclient_parameters + RTUclient_parameters + General_parameters:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   577
        ParamFuncName                      = "__modbus_get_ClientNode_" + name        
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   578
        GetClientParamFuncs[name]          = getattr(PLCObject.PLClibraryHandle, ParamFuncName)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   579
        GetClientParamFuncs[name].restype  = c_dtype
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   580
        GetClientParamFuncs[name].argtypes = [ctypes.c_int]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   581
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   582
    for name, web_label, c_dtype, web_dtype in TCPclient_parameters + RTUclient_parameters:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   583
        ParamFuncName                      = "__modbus_set_ClientNode_" + name
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   584
        SetClientParamFuncs[name]          = getattr(PLCObject.PLClibraryHandle, ParamFuncName)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   585
        SetClientParamFuncs[name].restype  = None
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   586
        SetClientParamFuncs[name].argtypes = [ctypes.c_int, c_dtype]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   587
2676
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   588
    # XXX TODO : stop reading from PLC .so file. This code is template code
b276d05bdb09 Modbus/BACnet : add some comments so that we don't forget some pending optimization.
Edouard Tisserant
parents: 2675
diff changeset
   589
    #            that can use modbus extension build data
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   590
    for name, web_label, c_dtype, web_dtype in TCPserver_parameters + RTUslave_parameters + General_parameters:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   591
        ParamFuncName                      = "__modbus_get_ServerNode_" + name        
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   592
        GetServerParamFuncs[name]          = getattr(PLCObject.PLClibraryHandle, ParamFuncName)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   593
        GetServerParamFuncs[name].restype  = c_dtype
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   594
        GetServerParamFuncs[name].argtypes = [ctypes.c_int]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   595
        
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   596
    for name, web_label, c_dtype, web_dtype in TCPserver_parameters + RTUslave_parameters:
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   597
        ParamFuncName                      = "__modbus_set_ServerNode_" + name
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   598
        SetServerParamFuncs[name]          = getattr(PLCObject.PLClibraryHandle, ParamFuncName)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   599
        SetServerParamFuncs[name].restype  = None
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   600
        SetServerParamFuncs[name].argtypes = [ctypes.c_int, c_dtype]
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   601
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   602
    for node_id in range(client_count):
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   603
        _AddWebNode(node_id, "client" ,GetClientParamFuncs, SetClientParamFuncs)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   604
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   605
    for node_id in range(server_count):
2656
049b919b7505 Modbus plugin web configuration: save node type and addr type to persistant storage
Mario de Sousa <msousa@fe.up.pt>
parents: 2655
diff changeset
   606
        _AddWebNode(node_id, "server", GetServerParamFuncs, SetServerParamFuncs)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   607
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   608
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   609
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   610
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   611
2703
32ffdb32b14e Python Runtime: order of execution of extension's init() and cleanup() now reflects order of appearance of extensions in configuration tree.
Edouard Tisserant
parents: 2691
diff changeset
   612
def _runtime_%(location_str)s_modbus_websettings_cleanup():
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   613
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   614
    Callback function, called (by PLCObject.py) when a PLC program is unloaded from memory
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   615
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   616
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   617
    #PLCObject.LogMessage("Modbus web server extension::OnUnLoadPLC() Called...")
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   618
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   619
    # Delete the Modbus specific web interface extensions
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   620
    # (Safe to ask to delete, even if it has not been added!)
2672
4bc60e426dd6 Runtime extrension web settings: distinguish display string and token when calling NS.newExtensionSetting, thus allowing meaningful name to be displayed instead of an id.
Edouard Tisserant
parents: 2670
diff changeset
   621
    global _WebNodeList
4bc60e426dd6 Runtime extrension web settings: distinguish display string and token when calling NS.newExtensionSetting, thus allowing meaningful name to be displayed instead of an id.
Edouard Tisserant
parents: 2670
diff changeset
   622
    for index, WebNode_entry in enumerate(_WebNodeList):
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   623
        config_hash = WebNode_entry["config_hash"]
2672
4bc60e426dd6 Runtime extrension web settings: distinguish display string and token when calling NS.newExtensionSetting, thus allowing meaningful name to be displayed instead of an id.
Edouard Tisserant
parents: 2670
diff changeset
   624
        NS.removeExtensionSetting(config_hash)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   625
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   626
    # Dele all entries...
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   627
    _WebNodeList = []
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   628