modbus/web_settings.py
author Edouard Tisserant
Mon, 15 Jun 2020 14:34:00 +0200
changeset 2674 a8975ff44cd1
parent 2673 80e7eb44cab4
child 2675 3ca5611d6308
permissions -rw-r--r--
BACnet: prevent saving configuration that is same as default, delete it if so.
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     ),
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   186
    ("slave_id"         , _("Slave ID")              , ctypes.c_ulonglong, annotate.Integer)
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
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   216
def _SetSavedConfiguration(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 """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   218
    
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
   219
    # Add the addr_type and node_type to the data that will be saved to file
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
   220
    # This allows us to confirm the saved data contains the correct 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
   221
    # when loading from file
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
   222
    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
   223
    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
   224
    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
   225
    save_info["config"   ] = newConfig
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
   226
    
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   227
    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
   228
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   229
    with open(os.path.realpath(filename), 'w') as f:
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
   230
        json.dump(save_info, f, sort_keys=True, indent=4)
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
        
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   232
    _WebNodeList[WebNode_id]["SavedConfiguration"] = newConfig
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   233
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   234
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   235
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   236
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   237
def _DelSavedConfiguration(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
   238
    """ 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
   239
    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
   240
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   241
    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
   242
        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
   243
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   244
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   245
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   246
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   247
def _GetSavedConfiguration(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
   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
    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
   250
    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
   251
    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
   252
    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
   253
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   254
    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
   255
    try:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   256
        #if os.path.isfile(filename):
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
   257
        save_info = json.load(open(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
   258
    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
   259
        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
   260
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
   261
    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
   262
        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
   263
    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
   264
        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
   265
    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
   266
        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
   267
    
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
   268
    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
   269
    
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   270
    #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
   271
    #    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
   272
    #else:
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   273
    #    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
   274
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   275
    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
   276
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   277
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   278
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   279
def _GetPLCConfiguration(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
   280
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   281
    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
   282
    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
   283
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   284
    current_config = {}
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   285
    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
   286
    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
   287
    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
   288
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   289
    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
   290
        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
   291
        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
   292
            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
   293
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   294
    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
   295
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   296
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   297
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   298
def _SetPLCConfiguration(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
   299
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   300
    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
   301
    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
   302
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   303
    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
   304
    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
   305
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   306
    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
   307
        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
   308
        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
   309
            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
   310
            
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   311
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   312
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   313
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   314
def _GetWebviewConfigurationValue(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
   315
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   316
    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
   317
    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
   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
    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
   320
    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
   321
    as a parameter.
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   322
    """    
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
    try:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   324
        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
   325
    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
   326
        return ""
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   327
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   328
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   329
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   330
def OnButtonSave(**kwargs):
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   331
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   332
    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
   333
    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
   334
    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
   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
    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
   337
    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
   338
    "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
   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
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   341
    #PLCObject.LogMessage("Modbus web server extension::OnButtonSave()  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
   342
    
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   343
    newConfig    = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   344
    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
   345
    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
   346
    
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   347
    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
   348
        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
   349
        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
   350
            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
   351
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   352
    # 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
   353
    # 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
   354
    # 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
   355
    #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
   356
    #    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
   357
    
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   358
    # 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
   359
    # 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
   360
    # has a cold start (i.e. when Beremiz_service.py is retarted)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   361
    _SetSavedConfiguration(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
   362
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   363
    # Configure PLC with the current Modbus parameters
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   364
    _SetPLCConfiguration(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
   365
2665
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   366
    # Update the viewable configuration
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   367
    # The PLC may have coerced the values on calling _SetPLCConfiguration()
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   368
    # so we do not set it directly to newConfig
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   369
    _WebNodeList[WebNode_id]["WebviewConfiguration"] = _GetPLCConfiguration(WebNode_id)
978cf239c689 Modbus plugin web interface: show the keyword "#ANY#" on TCP servers
Mario de Sousa <msousa@fe.up.pt>
parents: 2661
diff changeset
   370
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
   371
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
   372
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
   373
def OnButtonReset(**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
   374
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   375
    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
   376
    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
   377
    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
   378
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   379
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   380
    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
   381
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   382
    # Delete the file
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   383
    _DelSavedConfiguration(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
   384
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   385
    # 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
   386
    new_config = _WebNodeList[WebNode_id]["DefaultConfiguration"]
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   387
    _SetPLCConfiguration(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
   388
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   389
    #Update the webviewconfiguration
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   390
    _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
   391
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   392
    # Reset SavedConfiguration
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   393
    _WebNodeList[WebNode_id]["SavedConfiguration"] = 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
   394
    
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
   395
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   396
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   397
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   398
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
   399
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
   400
    """
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   401
    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
   402
    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
   403
    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
   404
    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
   405
    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
   406
    """
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   407
    WebNode_entry = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   408
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
   409
    # 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
   410
    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
   411
    # 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
   412
    # 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
   413
    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
   414
    # 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
   415
    # 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
   416
    # 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
   417
    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
   418
    
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   419
    #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
   420
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   421
    # 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
   422
    # 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
   423
    #       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
   424
    global _WebNodeList
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   425
    _WebNodeList.append(WebNode_entry)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   426
    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
   427
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   428
    # 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
   429
    #
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   430
    # 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
   431
    #  - 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
   432
    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
   433
    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
   434
    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
   435
    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
   436
    WebNode_entry["GetParamFuncs"] = GetParamFuncs
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   437
    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
   438
    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
   439
    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
   440
    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
   441
        
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   442
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   443
    # 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
   444
    # 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
   445
    # 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
   446
    # 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
   447
    # 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
   448
    # 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
   449
    # 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
   450
    # 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
   451
    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
   452
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   453
    # 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
   454
    # 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
   455
    # (i.e. the configuration inserted in Beremiz IDE when project was compiled)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   456
    WebNode_entry["DefaultConfiguration"] = _GetPLCConfiguration(WebNode_id)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   457
    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
   458
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   459
    # 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
   460
    # Currently only used to decide whether or not to show the "Delete" button on the
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   461
    # web interface (only shown if "SavedConfiguration" is not None)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   462
    SavedConfig = _GetSavedConfiguration(WebNode_id)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   463
    WebNode_entry["SavedConfiguration"] = 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
   464
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   465
    if SavedConfig is not None:
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   466
        _SetPLCConfiguration(WebNode_id, SavedConfig)
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   467
        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
   468
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   469
    # 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
   470
    # 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
   471
    # 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
   472
    #       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
   473
    #           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
   474
    #       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
   475
    #           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
   476
    #       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
   477
    #           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
   478
    #           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
   479
    #           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
   480
    def __GetWebviewConfigurationValue(ctx, argument):
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
   481
        return str(_GetWebviewConfigurationValue(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
   482
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   483
    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
   484
                    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
   485
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   486
    # 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
   487
    def __OnButtonSave(**kwargs):
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   488
        OnButtonSave(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
   489
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
   490
    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
   491
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
   492
    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
   493
        "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
   494
        _("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
   495
        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
   496
        _("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
   497
        __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
   498
    
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
    def __OnButtonReset(**kwargs):
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
        return OnButtonReset(WebNode_id = WebNode_id, **kwargs)
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
   501
            
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
   502
    def getConfigStatus():
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
   503
        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
   504
            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
   505
        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
   506
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
    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
   508
        "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
   509
        _("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
   510
        [ ("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
   511
           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
   512
                           immutable=True,
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
                           default=lambda *k:getConfigStatus())),
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
        ],                                       # 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
   515
        _("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
   516
        __OnButtonReset)
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   517
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   518
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   519
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   520
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   521
def _runtime_modbus_websettings_%(location_str)s_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
   522
    """
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   523
    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
   524
    (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
   525
    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
   526
    """
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   527
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   528
    #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
   529
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   530
    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
   531
        # 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
   532
        # 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
   533
        return  
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   534
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   535
    # 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
   536
    # 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
   537
    # 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
   538
    # 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
   539
    # 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
   540
    # 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
   541
    try:
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   542
        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
   543
        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
   544
    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
   545
        # 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
   546
        #   (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
   547
        return
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   548
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   549
    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
   550
    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
   551
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   552
    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
   553
        # 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
   554
        #  => 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
   555
        return
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   556
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   557
    # 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
   558
    # 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
   559
    GetClientParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   560
    SetClientParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   561
    GetServerParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   562
    SetServerParamFuncs = {}
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   563
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   564
    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
   565
        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
   566
        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
   567
        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
   568
        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
   569
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   570
    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
   571
        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
   572
        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
   573
        SetClientParamFuncs[name].restype  = None
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   574
        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
   575
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   576
    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
   577
        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
   578
        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
   579
        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
   580
        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
   581
        
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   582
    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
   583
        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
   584
        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
   585
        SetServerParamFuncs[name].restype  = None
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   586
        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
   587
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   588
    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
   589
        _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
   590
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   591
    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
   592
        _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
   593
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   594
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   595
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   596
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   597
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
def _runtime_modbus_websettings_%(location_str)s_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
   599
    """
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   600
    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
   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
2669
be233279d179 BACnet and Modbus: Remove additional loading and unloading, use the one already in place for extensions.
Edouard Tisserant
parents: 2666
diff changeset
   603
    #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
   604
    
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   605
    # 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
   606
    # (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
   607
    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
   608
    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
   609
        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
   610
        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
   611
        
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   612
    # Dele all entries...
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
   613
    _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
   614