runtime/BACnet_config.py
author Mario de Sousa <msousa@fe.up.pt>
Thu, 28 Jan 2021 14:17:40 +0000
changeset 2721 367eb26972b1
parent 2667 253110ba0fd7
permissions -rw-r--r--
modbus plugin: add (user accessible) transaction counters for servers/slaves
2649
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
#!/usr/bin/env python
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
# This file is part of Beremiz runtime.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
# Copyright (C) 2020: Mario de Sousa
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
# See COPYING.Runtime file for copyrights details.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
# This library is free software; you can redistribute it and/or
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
# modify it under the terms of the GNU Lesser General Public
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
# License as published by the Free Software Foundation; either
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
# version 2.1 of the License, or (at your option) any later version.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
# This library is distributed in the hope that it will be useful,
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
# Lesser General Public License for more details.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
# You should have received a copy of the GNU Lesser General Public
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
# License along with this library; if not, write to the Free Software
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
import json
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
import os
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
import ctypes
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
from formless import annotate, webform
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
# reference to the PLCObject in runtime/PLCObject.py
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
# PLCObject is a singleton, created in runtime/__init__.py
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
_plcobj = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
# reference to the Nevow web server (a.k.a as NS in Beremiz_service.py)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
# (Note that NS will reference the NevowServer.py _module_, and not an object/class)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
_NS = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
# WorkingDir: the directory on which Beremiz_service.py is running, and where 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
#             all the files downloaded to the PLC get stored
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
_WorkingDir = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
# Will contain references to the C functions 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
# (implemented in beremiz/bacnet/runtime/server.c)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
# used to get/set the BACnet specific configuration paramters
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
GetParamFuncs = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
SetParamFuncs = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
# Upon PLC load, this Dictionary is initialised with the BACnet configuration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
# hardcoded in the C file
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
# (i.e. the configuration inserted in Beremiz IDE when project was compiled)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
_DefaultConfiguration = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
# Dictionary that contains the BACnet configuration currently being shown
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
# on the web interface
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
# This configuration will almost always be identical to the current
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
# configuration in the PLC (i.e., the current state stored in the 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
# C variables in the .so file).
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
# The configuration viewed on the web will only be different to the current 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    66
# configuration when the user edits the configuration, and when
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    67
# the user asks to save the edited configuration but it contains an error.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
_WebviewConfiguration = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
# Dictionary that stores the BACnet configuration currently stored in a file
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
# Currently only used to decide whether or not to show the "Delete" button on the
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
# web interface (only shown if _SavedConfiguration is not None)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
_SavedConfiguration = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
# File to which the new BACnet configuration gets stored on the PLC
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
# Note that the stored configuration is likely different to the
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
# configuration hardcoded in C generated code (.so file), so
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
# this file should be persistent across PLC reboots so we can
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
# re-configure the PLC (change values of variables in .so file)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
# before it gets a chance to start running
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
#_BACnetConfFilename = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
_BACnetConfFilename = "/tmp/BeremizBACnetConfig.json"
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
2667
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
    90
class BN_StrippedString(annotate.String):
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
    91
    def __init__(self, *args, **kwargs):
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
    92
        annotate.String.__init__(self, strip = True, *args, **kwargs)
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
    93
2649
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
BACnet_parameters = [
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97
    #    param. name             label                                            ctype type      annotate type
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    98
    # (C code var name)         (used on web interface)                          (C data type)    (web data type)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    99
    #                                                                                             (annotate.String,
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   100
    #                                                                                              annotate.Integer, ...)
2667
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
   101
    ("network_interface"      , _("Network Interface")                         , ctypes.c_char_p, BN_StrippedString),
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
   102
    ("port_number"            , _("UDP Port Number")                           , ctypes.c_char_p, BN_StrippedString),
2649
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   103
    ("comm_control_passwd"    , _("BACnet Communication Control Password")     , ctypes.c_char_p, annotate.String),
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   104
    ("device_id"              , _("BACnet Device ID")                          , ctypes.c_int,    annotate.Integer),
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   105
    ("device_name"            , _("BACnet Device Name")                        , ctypes.c_char_p, annotate.String),
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   106
    ("device_location"        , _("BACnet Device Location")                    , ctypes.c_char_p, annotate.String),
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   107
    ("device_description"     , _("BACnet Device Description")                 , ctypes.c_char_p, annotate.String),
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   108
    ("device_appsoftware_ver" , _("BACnet Device Application Software Version"), ctypes.c_char_p, annotate.String)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   109
    ]
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   110
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   111
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   112
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   113
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   114
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   115
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   116
def _CheckPortnumber(port_number):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   117
    """ check validity of the port number """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   118
    try:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   119
        portnum = int(port_number)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   120
        if (portnum < 0) or (portnum > 65535):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   121
           raise Exception
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   122
    except Exception:    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   123
        return False
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   124
        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   125
    return True    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   126
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   127
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   128
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   129
def _CheckDeviceID(device_id):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   130
    """ 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   131
    # check validity of the Device ID 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   132
    # NOTE: BACnet device (object) IDs are 22 bits long (not counting the 10 bits for the type ID)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   133
    #       so the Device instance ID is limited from 0 to 22^2-1 = 4194303
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   134
    #       However, 4194303 is reserved for special use (similar to NULL pointer), so last
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   135
    #       valid ID becomes 4194302
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   136
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   137
    try:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   138
        devid = int(device_id)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   139
        if (devid < 0) or (devid > 4194302):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   140
            raise Exception
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   141
    except Exception:    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   142
        return False
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   143
        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   144
    return True    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   145
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   146
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   147
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   148
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   149
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   150
def _CheckConfiguration(BACnetConfig):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   151
    res = True    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   152
    res = res and _CheckPortnumber(BACnetConfig["port_number"])
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   153
    res = res and _CheckDeviceID  (BACnetConfig["device_id"])
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   154
    return res
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   155
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   156
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   157
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   158
def _CheckWebConfiguration(BACnetConfig):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   159
    res = True
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   160
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   161
    # check the port number
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   162
    if not _CheckPortnumber(BACnetConfig["port_number"]):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   163
        raise annotate.ValidateError(
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   164
            {"port_number": "Invalid port number: " + str(BACnetConfig["port_number"])},
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   165
            _("BACnet configuration error:"))
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   166
        res = False
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   167
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   168
    if not _CheckDeviceID(BACnetConfig["device_id"]):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   169
        raise annotate.ValidateError(
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   170
            {"device_id": "Invalid device ID: " + str(BACnetConfig["device_id"])},
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   171
            _("BACnet configuration error:"))
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   172
        res = False
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   173
        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   174
    return res
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   175
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   176
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   177
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   178
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   179
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   180
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   181
def _SetSavedConfiguration(BACnetConfig):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   182
    """ Stores in a file a dictionary containing the BACnet parameter configuration """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   183
    with open(os.path.realpath(_BACnetConfFilename), 'w') as f:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   184
        json.dump(BACnetConfig, f, sort_keys=True, indent=4)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   185
    global _SavedConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   186
    _SavedConfiguration = BACnetConfig
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   187
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   188
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   189
def _DelSavedConfiguration():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   190
    """ Deletes the file cotaining the persistent BACnet configuration """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   191
    if os.path.exists(_BACnetConfFilename):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   192
        os.remove(_BACnetConfFilename)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   193
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   194
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   195
def _GetSavedConfiguration():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   196
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   197
    # Returns a dictionary containing the BACnet parameter configuration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   198
    # that was last saved to file. If no file exists, then return None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   199
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   200
    try:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   201
        #if os.path.isfile(_BACnetConfFilename):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   202
        saved_config = json.load(open(_BACnetConfFilename))
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   203
    except Exception:    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   204
        return None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   205
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   206
    if _CheckConfiguration(saved_config):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   207
        return saved_config
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   208
    else:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   209
        return None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   210
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   211
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   212
def _GetPLCConfiguration():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   213
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   214
    # Returns a dictionary containing the current BACnet parameter configuration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   215
    # stored in the C variables in the loaded PLC (.so file)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   216
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   217
    current_config = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   218
    for par_name, x1, x2, x3 in BACnet_parameters:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   219
        value = GetParamFuncs[par_name]()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   220
        if value is not None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   221
            current_config[par_name] = value
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   222
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   223
    return current_config
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   224
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   225
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   226
def _SetPLCConfiguration(BACnetConfig):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   227
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   228
    # Stores the BACnet parameter configuration into the
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   229
    # the C variables in the loaded PLC (.so file)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   230
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   231
    for par_name in BACnetConfig:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   232
        value = BACnetConfig[par_name]
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   233
        #_plcobj.LogMessage("BACnet web server extension::_SetPLCConfiguration()  Setting "
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   234
        #                       + par_name + " to " + str(value) )
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   235
        if value is not None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   236
            SetParamFuncs[par_name](value)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   237
    # update the configuration shown on the web interface
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   238
    global _WebviewConfiguration 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   239
    _WebviewConfiguration = _GetPLCConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   240
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   241
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   242
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   243
def _GetWebviewConfigurationValue(ctx, argument):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   244
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   245
    # Callback function, called by the web interface (NevowServer.py)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   246
    # to fill in the default value of each parameter
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   247
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   248
    try:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   249
        return _WebviewConfiguration[argument.name]
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   250
    except Exception:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   251
        return ""
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   252
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   253
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   254
# The configuration of the web form used to see/edit the BACnet parameters
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   255
webFormInterface = [(name, web_dtype (label=web_label, default=_GetWebviewConfigurationValue)) 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   256
                    for name, web_label, c_dtype, web_dtype in BACnet_parameters]
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   257
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   258
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   259
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   260
def _updateWebInterface():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   261
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   262
    # Add/Remove buttons to/from the web interface depending on the current state
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   263
    #
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   264
    #  - If there is a saved state => add a delete saved state button
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   265
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   266
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   267
    # Add a "Delete Saved Configuration" button if there is a saved configuration!
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   268
    if _SavedConfiguration is None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   269
        _NS.ConfigurableSettings.delSettings("BACnetConfigDelSaved")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   270
    else:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   271
        _NS.ConfigurableSettings.addSettings(
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   272
            "BACnetConfigDelSaved",                   # name
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   273
            _("BACnet Configuration"),                # description
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   274
            [],                                       # fields  (empty, no parameters required!)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   275
            _("Delete Configuration Stored in Persistent Storage"), # button label
2667
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
   276
            OnButtonDel,                              # callback    
253110ba0fd7 Bacnet plugin web configuration: strip leading and trailing spaces for net. interface and port numb.
Mario de Sousa <msousa@fe.up.pt>
parents: 2649
diff changeset
   277
            "BACnetConfigParm")                       # Add after entry xxxx
2649
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   278
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   279
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   280
def OnButtonSave(**kwargs):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   281
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   282
    # Function called when user clicks 'Save' button in web interface
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   283
    # The function will configure the BACnet plugin in the PLC with the values
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   284
    # specified in the web interface. However, values must be validated first!
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   285
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   286
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   287
    #_plcobj.LogMessage("BACnet web server extension::OnButtonSave()  Called")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   288
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   289
    newConfig = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   290
    for par_name, x1, x2, x3 in BACnet_parameters:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   291
        value = kwargs.get(par_name, None)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   292
        if value is not None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   293
            newConfig[par_name] = value
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   294
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   295
    global _WebviewConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   296
    _WebviewConfiguration = newConfig
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   297
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   298
    # First check if configuration is OK.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   299
    if not _CheckWebConfiguration(newConfig):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   300
        return
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   301
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   302
    # store to file the new configuration so that 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   303
    # we can recoup the configuration the next time the PLC
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   304
    # has a cold start (i.e. when Beremiz_service.py is retarted)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   305
    _SetSavedConfiguration(newConfig)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   306
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   307
    # Configure PLC with the current BACnet parameters
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   308
    _SetPLCConfiguration(newConfig)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   309
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   310
    # File has just been created => Delete button must be shown on web interface!
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   311
    _updateWebInterface()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   312
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   313
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   314
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   315
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   316
def OnButtonDel(**kwargs):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   317
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   318
    # Function called when user clicks 'Delete' button in web interface
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   319
    # The function will delete the file containing the persistent
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   320
    # BACnet configution
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   321
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   322
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   323
    # Delete the file
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   324
    _DelSavedConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   325
    # Set the current configuration to the default (hardcoded in C)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   326
    _SetPLCConfiguration(_DefaultConfiguration)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   327
    # Reset global variable
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   328
    global _SavedConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   329
    _SavedConfiguration = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   330
    # File has just been deleted => Delete button on web interface no longer needed!
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   331
    _updateWebInterface()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   332
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   333
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   334
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   335
def OnButtonShowCur(**kwargs):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   336
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   337
    # Function called when user clicks 'Show Current PLC Configuration' button in web interface
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   338
    # The function will load the current PLC configuration into the web form
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   339
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   340
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   341
    global _WebviewConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   342
    _WebviewConfiguration = _GetPLCConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   343
    # File has just been deleted => Delete button on web interface no longer needed!
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   344
    _updateWebInterface()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   345
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   346
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   347
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   348
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   349
def OnLoadPLC():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   350
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   351
    # Callback function, called (by PLCObject.py) when a new PLC program
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   352
    # (i.e. XXX.so file) is transfered to the PLC runtime
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   353
    # and oaded into memory
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   354
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   355
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   356
    #_plcobj.LogMessage("BACnet web server extension::OnLoadPLC() Called...")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   357
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   358
    if _plcobj.PLClibraryHandle is None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   359
        # PLC was loaded but we don't have access to the library of compiled code (.so lib)?
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   360
        # Hmm... This shold never occur!! 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   361
        return  
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   362
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   363
    # Get the location (in the Config. Node Tree of Beremiz IDE) the BACnet plugin
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   364
    # occupies in the currently loaded PLC project (i.e., the .so file)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   365
    # If the "__bacnet_plugin_location" C variable is not present in the .so file,
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   366
    # we conclude that the currently loaded PLC does not have the BACnet plugin
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   367
    # included (situation (2b) described above init())
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   368
    try:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   369
        location = ctypes.c_char_p.in_dll(_plcobj.PLClibraryHandle, "__bacnet_plugin_location")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   370
    except Exception:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   371
        # Loaded PLC does not have the BACnet plugin => nothing to do
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   372
        #   (i.e. do _not_ configure and make available the BACnet web interface)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   373
        return
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   374
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   375
    # Map the get/set functions (written in C code) we will be using to get/set the configuration parameters
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   376
    for name, web_label, c_dtype, web_dtype in BACnet_parameters:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   377
        GetParamFuncName = "__bacnet_" + location.value + "_get_ConfigParam_" + name
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   378
        SetParamFuncName = "__bacnet_" + location.value + "_set_ConfigParam_" + name
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   379
        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   380
        GetParamFuncs[name]          = getattr(_plcobj.PLClibraryHandle, GetParamFuncName)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   381
        GetParamFuncs[name].restype  = c_dtype
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   382
        GetParamFuncs[name].argtypes = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   383
        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   384
        SetParamFuncs[name]          = getattr(_plcobj.PLClibraryHandle, SetParamFuncName)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   385
        SetParamFuncs[name].restype  = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   386
        SetParamFuncs[name].argtypes = [c_dtype]
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   387
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   388
    # Default configuration is the configuration done in Beremiz IDE
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   389
    # whose parameters get hardcoded into C, and compiled into the .so file
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   390
    # We read the default configuration from the .so file before the values
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   391
    # get changed by the user using the web server, or by the call (further on)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   392
    # to _SetPLCConfiguration(SavedConfiguration)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   393
    global _DefaultConfiguration 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   394
    _DefaultConfiguration = _GetPLCConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   395
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   396
    # Show the current PLC configuration on the web interface        
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   397
    global _WebviewConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   398
    _WebviewConfiguration = _GetPLCConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   399
 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   400
    # Read from file the last used configuration, which is likely
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   401
    # different to the hardcoded configuration.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   402
    # We Reset the current configuration (i.e., the config stored in the 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   403
    # variables of .so file) to this saved configuration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   404
    # so the PLC will start off with this saved configuration instead
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   405
    # of the hardcoded (in Beremiz C generated code) configuration values.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   406
    #
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   407
    # Note that _SetPLCConfiguration() will also update 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   408
    # _WebviewConfiguration , if necessary.
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   409
    global _SavedConfiguration
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   410
    _SavedConfiguration  = _GetSavedConfiguration()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   411
    if _SavedConfiguration is not None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   412
        if _CheckConfiguration(_SavedConfiguration):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   413
            _SetPLCConfiguration(_SavedConfiguration)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   414
            
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   415
    # Configure the web interface to include the BACnet config parameters
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   416
    _NS.ConfigurableSettings.addSettings(
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   417
        "BACnetConfigParm",                # name
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   418
        _("BACnet Configuration"),         # description
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   419
        webFormInterface,                  # fields
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   420
        _("Save Configuration to Persistent Storage"),  # button label
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   421
        OnButtonSave)                      # callback    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   422
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   423
    # Add a "View Current Configuration" button 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   424
    _NS.ConfigurableSettings.addSettings(
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   425
        "BACnetConfigViewCur",                    # name
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   426
        _("BACnet Configuration"),                # description
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   427
        [],                                       # fields  (empty, no parameters required!)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   428
        _("Show Current PLC Configuration"),      # button label
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   429
        OnButtonShowCur)                          # callback    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   430
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   431
    # Add the Delete button to the web interface, if required
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   432
    _updateWebInterface()
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   433
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   434
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   435
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   436
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   437
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   438
def OnUnLoadPLC():
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   439
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   440
    # Callback function, called (by PLCObject.py) when a PLC program is unloaded from memory
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   441
    """
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   442
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   443
    #_plcobj.LogMessage("BACnet web server extension::OnUnLoadPLC() Called...")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   444
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   445
    # Delete the BACnet specific web interface extensions
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   446
    # (Safe to ask to delete, even if it has not been added!)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   447
    _NS.ConfigurableSettings.delSettings("BACnetConfigParm")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   448
    _NS.ConfigurableSettings.delSettings("BACnetConfigViewCur")  
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   449
    _NS.ConfigurableSettings.delSettings("BACnetConfigDelSaved")  
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   450
    GetParamFuncs = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   451
    SetParamFuncs = {}
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   452
    _WebviewConfiguration = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   453
    _SavedConfiguration   = None
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   454
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   455
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   456
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   457
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   458
# The Beremiz_service.py service, along with the integrated web server it launches
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   459
# (i.e. Nevow web server, in runtime/NevowServer.py), will go through several states
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   460
# once started:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   461
#  (1) Web server is started, but no PLC is loaded
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   462
#  (2) PLC is loaded (i.e. the PLC compiled code is loaded)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   463
#         (a) The loaded PLC includes the BACnet plugin
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   464
#         (b) The loaded PLC does not have the BACnet plugin
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   465
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   466
# During (1) and (2a):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   467
#     we configure the web server interface to not have the BACnet web configuration extension
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   468
# During (2b) 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   469
#     we configure the web server interface to include the BACnet web configuration extension
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   470
#
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   471
# plcobj    : reference to the PLCObject defined in PLCObject.py
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   472
# NS        : reference to the web server (i.e. the NevowServer.py module)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   473
# WorkingDir: the directory on which Beremiz_service.py is running, and where 
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   474
#             all the files downloaded to the PLC get stored, including
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   475
#             the .so file with the compiled C generated code
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   476
def init(plcobj, NS, WorkingDir):
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   477
    #plcobj.LogMessage("BACnet web server extension::init(plcobj, NS, " + WorkingDir + ") Called")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   478
    global _WorkingDir
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   479
    _WorkingDir = WorkingDir
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   480
    global _plcobj
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   481
    _plcobj = plcobj
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   482
    global _NS
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   483
    _NS = NS
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   484
    global _BACnetConfFilename
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   485
    if _BACnetConfFilename is None:
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   486
        _BACnetConfFilename = os.path.join(WorkingDir, "BACnetConfig.json")
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   487
    
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   488
    _plcobj.RegisterCallbackLoad  ("BACnet_Settins_Extension", OnLoadPLC)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   489
    _plcobj.RegisterCallbackUnLoad("BACnet_Settins_Extension", OnUnLoadPLC)
db68cb0e6bdc BACnet plugin: Add web interface for online parameter configuration
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
   490
    OnUnLoadPLC() # init is called before the PLC gets loaded...  so we make sure we have the correct state