<!--

  openHAB nodes for IBM's Node-Red
  https://github.com/pdmangel/node-red-contrib-openhab2
  (c) 2017, Peter De Mangelaere <peter.demangelaere@gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

-->
<script type="text/x-red" data-template-name="openhab2-controller2">
    <div class="form-row">
        <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-config-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-config-input-protocol"><i class="fa fa-unlock-alt"></i> Protocol</label>
        <select id="node-config-input-protocol" name="node-config-input-protocol" style="width: 250px;">
            <option value="http">http</option>
            <option value="https">https</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-config-input-host"><i class="fa fa-globe"></i> Host</label>
        <input type="text" id="node-config-input-host">
    </div>
    <div class="form-row">
        <label for="node-config-input-port"><i class="icon-bookmark"></i> Port</label>
        <input type="text" id="node-config-input-port">
    </div>
    <div class="form-row">
        <label for="node-config-input-path"><i class="fa fa-chevron-right"></i> Path</label>
        <input type="text" id="node-config-input-path">
    </div>
    <div class="form-row">
        <label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
        <input type="text" id="node-config-input-username">
    </div>
    <div class="form-row">
        <label for="node-config-input-password"><i class="fa fa-user-secret"></i> Password</label>
        <input type="password" id="node-config-input-password">
    </div>
    <div class="form-row">
        <label for="node-config-input-ohversion"><i class="fa fa-unlock-alt"></i>openHAB version</label>
        <select id="node-config-input-ohversion" name="node-config-input-ohversion" style="width: 250px;">
            <option value="v2">v1 and v2</option>
            <option value="v3">v3</option>
        </select>
    </div>
    <div class="form-row node-token">
        <label for="node-config-input-token"><i class="fa fa-user-secret"></i> OAuth2 Bearer token</label>
        <input type="password" id="node-config-input-token">
    </div>

</script>

<script type="text/x-red" data-template-name="openhab2-in2">
    <style type="text/css">
        .btn-group {
            width: 70%;
        }
        .multiselect {
            width: 100%;
        }
        .form-row input.multiselect-search {
            width: 100%;
        }
        .multiselect-clear-filter {
            display: none;
        }
        .dropdown-menu {
            width: 100% !important;
        }
        .multiselect-container input[type="checkbox"] {
            display: none;
        }
        .multiselect-container > li > a > label.radio {
            margin: 5px;
            width: 90%;
            height: 100%;
            cursor: pointer;
            font-weight: 400;
            padding: 3px 20px 3px 20px;
        }        
        .multiselect-container label.radio input[type="radio"] {
            display: none;
        }
    </style>
    <link rel="stylesheet" href="static/css/bootstrap-multiselect.css" type="text/css" />
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label>
        <input type="text" id="node-input-controller">
    </div>
    <div class="form-row">
        <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label>
        <select id="node-input-itemname" name="node-input-itemname">
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <input type="text" id="node-input-topic" name="node-input-topic" style="width: 250px;">
    </div>
    <div class="form-tips">
        An 'InitialStateEvent'-typed message (when selected below) will be send whenever this node is (re-)initialised, but also whenever the controller reconnects to the openHAB server.
    </div>
    <div class="form-row">
        <label for="node-input-initialstate"><i class="fa fa-crosshairs"></i> Send initial<br/>(<code>InitialStateEvent</code>)</label>
        <input type="checkbox" id="node-input-initialstate" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
    <div class="form-row">
        <label for="node-input-whenupdated"><i class="fa fa-crosshairs"></i> Send updated<br/>(<code>ItemStateEvent</code>)</label>
        <input type="checkbox" id="node-input-whenupdated" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
    <div class="form-row">
        <label for="node-input-whencommand"><i class="fa fa-crosshairs"></i> Send command<br/>(<code>ItemCommandEvent</code>)</label>
        <input type="checkbox" id="node-input-whencommand" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
    <div class="form-row">
        <label for="node-input-whenchanged"><i class="fa fa-crosshairs"></i> Send changed<br/>(<code>ItemStateChangedEvent</code> or <code>GroupItemStateChangedEvent</code>)</label>
        <input type="checkbox" id="node-input-whenchanged" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
    <div class="form-row node-whenchanged">
        <label for="node-input-changedfrom"><i class="fa fa-tag"></i> From (oldValue)</label>
        <input type="text" id="node-input-changedfrom" placeholder="<anything>">
    </div>
    <div class="form-row node-whenchanged">
        <label for="node-input-changedto"><i class="fa fa-tag"></i> To (value)</label>
        <input type="text" id="node-input-changedto" placeholder="<anything>">
    </div>
</script>

<script type="text/x-red" data-template-name="openhab2-get2">
    <style type="text/css">
        .btn-group {
            width: 70%;
        }
        .multiselect {
            width: 100%;
        }
        .form-row input.multiselect-search {
            width: 100%;
        }
        .multiselect-clear-filter {
            display: none;
        }
        .dropdown-menu {
            width: 100% !important;
        }
        .multiselect-container input[type="checkbox"] {
            display: none;
        }
        .multiselect-container > li > a > label.radio {
            margin: 5px;
            width: 90%;
            height: 100%;
            cursor: pointer;
            font-weight: 400;
            padding: 3px 20px 3px 20px;
        }        
        .multiselect-container label.radio input[type="radio"] {
            display: none;
        }
    </style>
    <link rel="stylesheet" href="static/css/bootstrap-multiselect.css" type="text/css" />
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label>
        <input type="text" id="node-input-controller">
    </div>
    <div class="form-row">
        <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label>
        <select id="node-input-itemname" name="node-input-itemname">
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <input type="text" id="node-input-topic" name="node-input-topic" style="width: 250px;">
    </div>
    <div class="form-row">
        <label for="node-input-sendnull"><i class="fa fa-crosshairs"></i> Send NULL-values</label>
        <input type="checkbox" id="node-input-sendnull" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
</script>

<script type="text/x-red" data-template-name="openhab2-monitor2">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label>
        <input type="text" id="node-input-controller">
    </div>
</script>

<script type="text/x-red" data-template-name="openhab2-out2">
    <style type="text/css">
        .btn-group {
            width: 70%;
        }
        .multiselect {
            width: 100%;
        }
        .form-row input.multiselect-search {
            width: 100%;
        }
        .multiselect-clear-filter {
            display: none;
        }
        .dropdown-menu {
            width: 100% !important;
        }
        .multiselect-container input[type="checkbox"] {
            display: none;
        }
        .multiselect-container > li > a > label.radio {
            margin: 5px;
            width: 90%;
            height: 100%;
            cursor: pointer;
            font-weight: 400;
            padding: 3px 20px 3px 20px;
        }        
        .multiselect-container label.radio input[type="radio"] {
            display: none;
        }
    </style>
    <link rel="stylesheet" href="static/css/bootstrap-multiselect.css" type="text/css" />
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label>
        <input type="text" id="node-input-controller">
    </div>
    <div class="form-row">
        <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label>
        <select id="node-input-itemname" name="node-input-itemname">
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <select id="node-input-topic" name="node-input-topic" style="width: 250px;">
            <option value=""></option>
            <option value="ItemCommand">ItemCommand</option>
            <option value="ItemUpdate">ItemUpdate</option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-payload"><i class="fa fa-envelope"></i> Payload</label>
        <input type="text" id="node-input-payload" placeholder="Payload">
    </div>
    <div class="form-row">
        <label for="node-input-onlywhenchanged"><i class="fa fa-crosshairs"></i> Only when changed</label>
        <input type="checkbox" id="node-input-onlywhenchanged" style="display: inline-block; width: auto; vertical-align: top;">
        </input>
    </div>
</script>

<script type="text/x-red" data-template-name="openhab2-events2">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label>
        <input type="text" id="node-input-controller">
    </div>
</script>

<script type="text/x-red" data-help-name="openhab2-controller2">
    <p>Configuration node for communication with an openHAB controller.</p>
    <p></p>
    <b>Configuration</b>
    <ul>
        <li><b>Name :</b> Specify a name for the configuration node</li>
        <li><b>Protocol :</b> "http" or "https"</li>
        <li><b>Host :</b> Specify the hostname or ip address</li>
        <li><b>Port :</b> (Optionally) Specify the ip port</li>
        <li><b>Path :</b> (Optionally) Specify the additional base path</li>
        <li><b>Username :</b> (Optionally) Specify the username to authenticate. This is for authenticating to an reverse proxy, not for authenticating with openHAB itself, as openHAB <a href="https://www.openhab.org/docs/installation/security.html#authentication-and-access-control" target="_blank"><i>"does not (yet) support restricting access through HTTP(S) for certain users - there is no authentication in place, nor is there a limitation of functionality or information that different users can access."</i></a></li>
        <li><b>Password :</b> (Optionally) Specify the password to authenticate</li>
        <li><b>openHAB version :</b> "v1 and v2" or "v3".</li>
        <li><b>OAuth2 Bearer token :</b> An OAuth2 token created in openHAB 3, according to the <a href="https://www.openhab.org/docs/configuration/apitokens.html" target="_blank">documentation</a>, with the important note that the token should be filled NOT for the <code>Username</code> or <code>Password</code> field, but <strong>only the</strong> <code>OAuth2 Bearer token</code> <strong>field!</strong> A token looks like this: <code>oh.nodered.CgpQ...</code></li>
    </ul>
</script>

<script type="text/x-red" data-help-name="openhab2-in2">
    <p>Subscribes to state changes for the selected openHAB Item. Items or Groups with a value of NULL are not sent to the output channel.</p>
    <p></p>
    <b>Configuration</b>
    <ul>
        <li><b>Name :</b> Optionally specify a name</li>
        <li><b>Controller :</b> Select the openHAB controller</li>
        <li><b>Item :</b>Select the Item to monitor</li>
        <li><b>Topic :</b> Set .topic for the outgoing message</li>
        <li><b>Send initial :</b>Send message at node and/or the connected controller (openhab2-controller2) startup (type 'InitialStateEvent')</li>
        <li><b>When updated :</b>Send message when node state updates (type 'ItemStateEvent')</li>
        <li><b>When command :</b>Send message when node state receives a command (type 'ItemCommandEvent')</li>
        <li><b>When changed :</b>Send message when node state changes (type 'ItemStateChangedEvent' or 'GroupItemStateChangedEvent')</li>
        <li><b>From :</b>Old state value that should match before outputting a message. Ignored when left empty. Leave this field empty when the above "When command" is checked.</li>
        <li><b>To :</b>New state value that should match before outputting a message. Ignored when left empty.</li>
    </ul>
    <p></p>
    <b>Messages injected in NodeRED flows (1 output channel)</b>
    <ul>
        <li>Channel 1:
            <ul>
                <li><code>msg.item</code> : the item's itemname (not the label)</li>
                <li><code>msg.topic</code> : the item's topic as specified</li>
                <li><code>msg.event</code> : InitialStateEvent | ItemStateEvent | ItemCommandEvent | ItemStateChangedEvent | GroupItemStateChangedEvent</li>
                <li><code>msg.payload</code> : the current state of the selected item</li>
                <li><code>msg.oldValue</code> : the previous state of the selected item</li>
            </ul>
        </li>
    </ul>
</script>

<script type="text/x-red" data-help-name="openhab2-get2">
    <p>Gets an openHAB Item' state on an input message. Items or Groups with a value of NULL are not sent to the output channel.</p>
    <p></p>
    <b>Configuration</b>
    <ul>
        <li><b>Name :</b> Optionally specify a name</li>
        <li><b>Controller :</b> Select the openHAB controller</li>
        <li><b>Item :</b> Optionally select the Item to address. If specified, it overrides the item specified in the incoming message.</li>
        <li><b>Topic :</b> Set .topic for the outgoing message</li>
    </ul>
    <p></p>
    <b>Messages accepted by NodeRED flows (1 input channel)</b>
    <ul>
        <li><code>msg.item</code> optionally the Item to address</li>
    </ul>
    <p></p>
    <b>Messages injected in NodeRED flows (1 output channel)</b>
    <ul>
        <li>Channel 1:
            <p>Outputs the received input message with addition of: <p>
            <ul>
                <li><code>msg.item</code> : the item's itemname (not the label)</li>
                <li><code>msg.topic</code> : the item's topic as specified</li>
                <li><code>msg.event</code> : will be set to "ActualState"</li>
                <li><code>msg.payload</code> : the current state of the selected item</li>
                <li><code>msg.payload_in</code> : copy of incoming message payload</li>
                <li><code>msg.type</code> : returns the type of the item (like <code>String</code> or <code>Group</code>)</li>
                <li><code>msg.groups</code> : contains the list of group(s) the item belongs to. If the item does not belong to any group, the list will be empty.</li>
                <li><code>msg.members</code> : when msg.type is <code>Group</code>, this will contain a list of all members that are part of this group</li>
            </ul>
        </li>
    </ul>
</script>

<script type="text/x-red" data-help-name="openhab2-monitor2">
    <p>Monitors the openhab2-controller2 node.</p>
    <p></p>
    <b>Configuration</b>
        <ul>
            <li><b>Name :</b> Optionally specify a name</li>
            <li><b>Controller :</b> Select the openHAB controller</li>
        </ul>
    <p></p>
    <b>Messages injected in NodeRED flows (3 output channels)</b>
    <ul>
        <li>Channel 1:
            <ul>
                <li><code>msg.topic</code> : "ConnectionStatus"</li>
                <li><code>msg.payload</code> : connection status ('ON' or 'OFF')</li>
            </ul>
        </li>
        <li>Channel 2:
            <ul>
                <li><code>msg.topic</code> : "ConnectionError"</li>
                <li><code>msg.payload</code> : error message(s)</li>
            </ul>
        </li>
        <li>Channel 3:
            <ul>
                <li><code>msg.topic</code> : "RawEvent"</li>
                <li><code>msg.payload</code> : raw (unprocessed) event for all items</li>
            </ul>
        </li>
    </ul>
</script>

<script type="text/x-red" data-help-name="openhab2-out2">
    <p>Sends commands or state updates to a selected openHAB Item. You can choose to only write the state value if it differs from the actual state.</p>
    <p></p>
    <b>Configuration</b>
    <ul>
        <li><b>Name :</b> Optionally specify a name</li>
        <li><b>Controller :</b> Select the openHAB controller</li>
        <li><b>Item :</b> Optionally select the Item to address. If specified, it overrides the item specified in the incoming message.</li>
        <li><b>Topic :</b> Optionally select "ItemCommand" or "ItemUpdate". If specified, it overrides the topic specified in the incoming message.</li>
        <li><b>Payload :</b> Optionally specify the command or update value to send to the selected item. If specified, it overrides the value specified in the incoming message.</li>
        <li><b>Only when changed :</b> Optionally check this if you want to ouput the value if it changed in comparison to the actual (current) value.</li>
    </ul>
    <p></p>
    <b>Messages accepted by NodeRED flows (1 input channel)</b>
    <ul>
        <li><code>msg.item</code> optionally the Item to address</li>
        <li><code>msg.topic</code> optionally "ItemCommand", "ItemUpdate"</li>
        <li><code>msg.payload</code> optionally the fixed command or update value to send to the selected item</li>
    </ul>
    <p></p>
    <b>Messages injected in NodeRED flows (1 output channel)</b>
    <ul>
        <li>Channel 1:
            <p>Outputs the received input message in case it was delivered to openhab successfully. In case the 'Only when changed' settings is checked, a message will only be sent when there was an actual change to the value.<p>
        </li>
    </ul>
</script>

<script type="text/x-red" data-help-name="openhab2-events2">
    <p>Watches the event bus.</p>
    <p></p>
    <b>Configuration</b>
    <ul>
        <li><b>Name :</b> Optionally specify a name</li>
        <li><b>Controller :</b> Select the openHAB controller</li>
    </ul>
    <p></p>
    <b>Sample message (1 output channel)</b>
    <code>
    {
        "topic": "openhab/items/TestString/statechanged",
        "payload": {
            "type": "String",
            "value": "A",
            "oldType": "String",
            "oldValue": "B"
        },
        "type": "ItemStateChangedEvent",
        "_msgid": "c437e8981c45b326"
    }
    </code>
</script>

<script type="text/javascript">
    RED.nodes.registerType('openhab2-controller2', {
        category: 'config',
        defaults: {
            name: {
                value: "",
                required: true
            },
            protocol: {
                value: "http",
                required: true,
                validate: RED.validators.regex(/http[s]?/)
            },
            host: {
                value: "localhost",
                required: true
            },
            port: {
                value: 8080,
                required: false,
                validate: RED.validators.number()
            },
            path: {
                value: "",
                required: false
            },
            username: {
                value: "",
                required: false
            },
            password: {
                value: "",
                required: false
            },
            ohversion: {
                value: "v3",
                required: true,
                validate: RED.validators.regex(/v[23]/)
            },
            token: {
                value: "",
                required: false
            }
        },
        paletteLabel: "openhab2-controller2",
        label: function () {
            return this.name || this.host + ":" + this.port;
        },
        oneditprepare: function () {
            $("#node-config-input-ohversion").change(function () {
                if ($("#node-config-input-ohversion").val() == "v3") {
                    $(".node-token").show();
                } else {
                    $(".node-token").hide();
                }
            });
        }
    });
</script>


<script type="text/javascript">
    // some code shared by openhab2-in2 and openhab2-out2

    function openhabEditPrepare(node, allowEmpty) {

        function updateOpenhabItemSelection(cntrlConfig, itemName) {

            var itemSelectionEl = $('#node-input-itemname');

            // itemSelectionEl.children().remove();
            itemSelectionEl.multiselect({
                enableFiltering: true,
                enableCaseInsensitiveFiltering: true,
                maxHeight: 300,
                disableIfEmpty: true,
                nonSelectedText: 'Select item...',
                disabledText: 'No items found...'
            });

            if (cntrlConfig) {
                var config = {
                    name: cntrlConfig.name,
                    protocol: cntrlConfig.protocol,
                    host: cntrlConfig.host,
                    port: cntrlConfig.port,
                    path: cntrlConfig.path,
                    username: cntrlConfig.username,
                    password: cntrlConfig.password,
                    ohversion: cntrlConfig.ohversion,
                    token: cntrlConfig.token,
                }
                //console.log("config = " + JSON.stringify(config));
                // For some Node-Red installations, the relative webroot is changed from "/" to "/endpoint" using the 
                // httpRoot: '/endpoint' setting (in the settings.json on disk). This could break the request below, 
                // so we lookup the `httpNodeRoot` setting to construct the callback url.
                // More info: https://discourse.nodered.org/t/http-endpoint-guidelines/2309/4
                $.getJSON(RED.settings.httpNodeRoot + "openhab3/items", config)
                    .done(function (data) {

                        itemSelectionEl.children().remove();
                        var items = data;

                        items.sort(function (a, b) {
                            if (a.name < b.name)
                                return -1;
                            else if (a.name > b.name)
                                return 1;
                            else
                                return 0;
                        });
                        if (allowEmpty)
                            itemSelectionEl.append('<option value=""></option>');
                        items.forEach(function (item) {
                            itemSelectionEl.append('<option>' + item.name + '</option>');
                            var it = itemSelectionEl.find('option').last();
                            it.attr('value', item.name);
                        });
                        itemSelectionEl.val(itemName);
                        itemSelectionEl.multiselect('rebuild');
                    })
                    .fail(function (e1, e2, e3) {
                        console.log("error : " + JSON.stringify(e1));
                    });
            }
        }

        updateOpenhabItemSelection(RED.nodes.node(node.controller), node.itemname);

        $('#node-input-controller').change(function (ev) {
            updateOpenhabItemSelection(RED.nodes.node($('#node-input-controller').val()), $('#node-input-')
            .val() || node.itemname);
        });
    }
</script>

<script type="text/javascript">
    $.getScript('static/js/bootstrap-multiselect.js');
    RED.nodes.registerType('openhab2-in2', {
        category: 'home automation',
        color: '#00cc00',
        defaults: {
            name: {
                value: ""
            },
            controller: {
                value: "",
                type: "openhab2-controller2",
                required: true
            },
            itemname: {
                value: "",
                required: true
            },
            topic: {
                value: "",
                required: false
            },
            initialstate: {
                value: false
            },
            whenupdated: {
                value: true
            },
            whencommand: {
                value: true
            },
            whenchanged: {
                value: false
            },
            changedfrom: {
                value: "",
                required: false
            },
            changedto: {
                value: "",
                required: false
            }
        },
        inputs: 0,
        outputs: 1,
        icon: "node-red-contrib-openhab2.png",
        paletteLabel: "openhab2-in2",
        label: function () {
            return (this.name || this.itemname || "openhab2 in2");
        },
        oneditprepare: function () {
            openhabEditPrepare(this, false);
            $("#node-input-whenchanged").change(function () {
                if ($(this).is(":checked")) {
                    $(".node-whenchanged").show();
                } else {
                    $(".node-whenchanged").hide();
                    $('#node-input-changedfrom').val('');
                    $('#node-input-changedto').val('');
                }
            });
            if (this.whenchanged === "true" || this.whenchanged === true) {
                $("#node-input-whenchanged").prop("checked", true);
            } else {
                $("#node-input-whenchanged").prop("checked", false);
            }
        }
    });
</script>

<script type="text/javascript">
    RED.nodes.registerType('openhab2-get2', {
        category: 'home automation',
        color: '#00cc00',
        defaults: {
            name: {
                value: ""
            },
            controller: {
                value: "",
                type: "openhab2-controller2",
                required: true
            },
            itemname: {
                value: "",
                required: false
            },
            topic: {
                value: "",
                required: false
            },
            sendnull: {
                value: false
            }
        },
        inputs: 1,
        outputs: 1,
        icon: "node-red-contrib-openhab2.png",
        paletteLabel: "openhab2-get2",
        label: function () {
            return (this.name || this.itemname || "openhab2 get2");
        },
        oneditprepare: function () {
            openhabEditPrepare(this, true);
        }
    });
</script>

<script type="text/javascript">
    RED.nodes.registerType('openhab2-monitor2', {
        category: 'home automation',
        color: '#00cc00',
        defaults: {
            name: {
                value: ""
            },
            controller: {
                value: "",
                type: "openhab2-controller2",
                required: true
            }
        },
        inputs: 0,
        outputs: 3,
        icon: "node-red-contrib-openhab2.png",
        paletteLabel: "openhab2-monitor",
        label: function () {
            return (this.name || "openhab2 monitor");
        },
        oneditprepare: function () {}
    });
</script>

<script type="text/javascript">
    RED.nodes.registerType('openhab2-out2', {
        category: 'home automation',
        color: '#00cc00',
        defaults: {
            name: {
                value: ""
            },
            controller: {
                value: "",
                type: "openhab2-controller2",
                required: true
            },
            itemname: {
                value: "",
                required: false
            },
            topic: {
                value: "",
                required: false
            },
            payload: {
                value: "",
                required: false
            },
            onlywhenchanged: {
                value: false
            },
        },
        inputs: 1,
        outputs: 1,
        icon: "node-red-contrib-openhab2.png",
        paletteLabel: "openhab2-out2",
        label: function () {
            return (this.name || this.itemname || "openhab2 out2");
        },
        oneditprepare: function () {
            openhabEditPrepare(this, true);
        }
    });
</script>

<script type="text/javascript">
    RED.nodes.registerType('openhab2-events2', {
        category: 'home automation',
        color: '#0fdd0f',
        defaults: {
            name: {
                value: ""
            },
            controller: {
                value: "",
                type: "openhab2-controller2",
                required: true
            },
            itemname: {
                value: "",
                required: false
            },
        },
        inputs: 0,
        outputs: 1,
        icon: "node-red-contrib-openhab2.png",
        paletteLabel: "openhab2-events2",
        label: function () {
            return (this.name || this.itemname || "openhab2 events2");
        },
        oneditprepare: function () {
            openhabEditPrepare(this, true);
        }
    });
</script>