ESP8266Component.qml 21.2 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*=====================================================================

 QGroundControl Open Source Ground Control Station

 (c) 2009 - 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

 This file is part of the QGROUNDCONTROL project

 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

 ======================================================================*/

import QtQuick          2.5
import QtQuick.Controls 1.2
import QtQuick.Dialogs  1.2
27
import QtQuick.Layouts  1.2
dogmaphobic's avatar
dogmaphobic committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

import QGroundControl               1.0
import QGroundControl.FactSystem    1.0
import QGroundControl.FactControls  1.0
import QGroundControl.Palette       1.0
import QGroundControl.Controls      1.0
import QGroundControl.ScreenTools   1.0
import QGroundControl.Controllers   1.0

QGCView {
    id:         qgcView
    viewPanel:  panel

    QGCPalette { id: palette; colorGroupEnabled: panel.enabled }

    property int                _firstColumn:   ScreenTools.defaultFontPixelWidth * 20
dogmaphobic's avatar
dogmaphobic committed
44
    property int                _secondColumn:  ScreenTools.defaultFontPixelWidth * 18
45
    readonly property string    dialogTitle:    qsTr("controller WiFi Bridge")
46 47 48 49
    property int                stStatus:       XMLHttpRequest.UNSENT
    property int                stErrorCount:   0
    property bool               stEnabled:      false
    property bool               stResetCounters: false
dogmaphobic's avatar
dogmaphobic committed
50 51 52 53 54 55

    ESP8266ComponentController {
        id:             controller
        factPanel:      panel
    }

56 57 58 59 60 61 62 63 64 65 66
    Timer {
        id: timer
    }

    function thisThingHasNoNumberLocaleSupport(n) {
        return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",").replace(",,", ",")
    }

    function updateStatus() {
        timer.stop()
        var req = new XMLHttpRequest;
dogmaphobic's avatar
dogmaphobic committed
67 68 69
        var url = "http://"
        url += controller.wifiIPAddress
        url += "/status.json"
70 71 72 73 74 75 76 77 78 79
        if(stResetCounters) {
            url = url + "?r=1"
            stResetCounters = false
        }
        req.open("GET", url);
        req.onreadystatechange = function() {
            stStatus = req.readyState;
            if (stStatus === XMLHttpRequest.DONE) {
                var objectArray = JSON.parse(req.responseText);
                if (objectArray.errors !== undefined) {
80
                    console.log(qsTr("Error fetching WiFi Bridge Status: %1").arg(objectArray.errors[0].message))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                    stErrorCount = stErrorCount + 1
                    if(stErrorCount < 2 && stEnabled)
                        timer.start()
                } else {
                    if(stEnabled) {
                        //-- This should work but it doesn't
                        //   var n = 34523453.345
                        //   n.toLocaleString()
                        //   "34,523,453.345"
                        vpackets.text   = thisThingHasNoNumberLocaleSupport(objectArray["vpackets"])
                        vsent.text      = thisThingHasNoNumberLocaleSupport(objectArray["vsent"])
                        vlost.text      = thisThingHasNoNumberLocaleSupport(objectArray["vlost"])
                        gpackets.text   = thisThingHasNoNumberLocaleSupport(objectArray["gpackets"])
                        gsent.text      = thisThingHasNoNumberLocaleSupport(objectArray["gsent"])
                        glost.text      = thisThingHasNoNumberLocaleSupport(objectArray["glost"])
                        stErrorCount    = 0
                        wifiStatus.visible = true
                        timer.start()
                    }
                }
            }
        }
        req.send()
    }

    Component.onCompleted: {
        timer.interval = 1000
        timer.repeat = true
        timer.triggered.connect(updateStatus)
    }

dogmaphobic's avatar
dogmaphobic committed
112
    property Fact wifiMode:     controller.getParameterFact(controller.componentID, "WIFI_MODE",      false) //-- Don't bitch about missing as this is new
dogmaphobic's avatar
dogmaphobic committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    property Fact wifiChannel:  controller.getParameterFact(controller.componentID, "WIFI_CHANNEL")
    property Fact hostPort:     controller.getParameterFact(controller.componentID, "WIFI_UDP_HPORT")
    property Fact clientPort:   controller.getParameterFact(controller.componentID, "WIFI_UDP_CPORT")

    QGCViewPanel {
        id:             panel
        anchors.fill:   parent

        Flickable {
            anchors.fill:       parent
            clip:               true
            contentHeight:      innerColumn.height
            contentWidth:       panel.width
            boundsBehavior:     Flickable.StopAtBounds
            flickableDirection: Flickable.VerticalFlick

            Column {
                id:             innerColumn
                width:          panel.width
                spacing:        ScreenTools.defaultFontPixelHeight * 0.5

                QGCLabel {
135
                    text: qsTr("WiFi Bridge Settings")
dogmaphobic's avatar
dogmaphobic committed
136 137 138 139 140
                    font.weight: Font.DemiBold
                }

                Rectangle {
                    width:  parent.width
141
                    height: wifiStatus.visible ? Math.max(wifiCol.height, wifiStatus.height) + (ScreenTools.defaultFontPixelHeight * 2) : wifiCol.height + (ScreenTools.defaultFontPixelHeight * 2)
dogmaphobic's avatar
dogmaphobic committed
142
                    color:  palette.windowShade
143
                    Row {
dogmaphobic's avatar
dogmaphobic committed
144
                        anchors.verticalCenter: parent.verticalCenter
145 146 147 148 149 150 151 152 153 154
                        spacing: ScreenTools.defaultFontPixelWidth
                        Rectangle {
                            height:     parent.height
                            width:      1
                            color:      palette.window
                        }
                        Column {
                            id:                 wifiCol
                            anchors.verticalCenter: parent.verticalCenter
                            spacing:            ScreenTools.defaultFontPixelHeight / 2
dogmaphobic's avatar
dogmaphobic committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                            Row {
                                visible:            wifiMode
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
                                    text:           qsTr("WiFi Mode")
                                    width:          _firstColumn
                                    anchors.baseline: modeField.baseline
                                }
                                QGCComboBox {
                                    id:             modeField
                                    width:          _secondColumn
                                    model:          ["Access Point Mode", "Station Mode"]
                                    currentIndex:   wifiMode ? wifiMode.value : 0
                                    onActivated: {
                                        wifiMode.value = index
                                    }
                                }
                            }
173 174 175
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
176
                                    text:           qsTr("WiFi Channel")
177 178 179 180 181 182
                                    width:          _firstColumn
                                    anchors.baseline: channelField.baseline
                                }
                                QGCComboBox {
                                    id:             channelField
                                    width:          _secondColumn
dogmaphobic's avatar
dogmaphobic committed
183
                                    enabled:        wifiMode && wifiMode.value === 0
184 185 186 187 188 189
                                    model:          controller.wifiChannels
                                    currentIndex:   wifiChannel ? wifiChannel.value - 1 : 0
                                    onActivated: {
                                        wifiChannel.value = index + 1
                                    }
                                }
dogmaphobic's avatar
dogmaphobic committed
190
                            }
191 192 193
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
194
                                    text:           qsTr("WiFi SSID")
195 196 197 198 199 200 201 202 203 204 205
                                    width:          _firstColumn
                                    anchors.baseline: ssidField.baseline
                                    }
                                QGCTextField {
                                    id:             ssidField
                                    width:          _secondColumn
                                    text:           controller.wifiSSID
                                    maximumLength:  16
                                    onEditingFinished: {
                                        controller.wifiSSID = text
                                    }
dogmaphobic's avatar
dogmaphobic committed
206 207
                                }
                            }
208 209 210
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
211
                                    text:           qsTr("WiFi Password")
212 213 214 215 216 217 218 219 220 221 222
                                    width:          _firstColumn
                                    anchors.baseline: passwordField.baseline
                                    }
                                QGCTextField {
                                    id:             passwordField
                                    width:          _secondColumn
                                    text:           controller.wifiPassword
                                    maximumLength:  16
                                    onEditingFinished: {
                                        controller.wifiPassword = text
                                    }
dogmaphobic's avatar
dogmaphobic committed
223
                                }
224 225 226 227
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
228
                                    text:           qsTr("UART Baud Rate")
229 230 231 232 233 234 235 236 237 238 239
                                    width:          _firstColumn
                                    anchors.baseline:   baudField.baseline
                                }
                                QGCComboBox {
                                    id:             baudField
                                    width:          _secondColumn
                                    model:          controller.baudRates
                                    currentIndex:   controller.baudIndex
                                    onActivated: {
                                        controller.baudIndex = index
                                    }
dogmaphobic's avatar
dogmaphobic committed
240 241
                                }
                            }
242 243 244
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
245
                                    text:           qsTr("QGC UDP Port")
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                                    width:          _firstColumn
                                    anchors.baseline: qgcportField.baseline
                                }
                                QGCTextField {
                                    id:             qgcportField
                                    width:          _secondColumn
                                    text:           hostPort ? hostPort.valueString : ""
                                    validator:      IntValidator {bottom: 1024; top: 65535;}
                                    inputMethodHints: Qt.ImhDigitsOnly
                                    onEditingFinished: {
                                        hostPort.value = text
                                    }
                                }
                            }
                        }
                        Rectangle {
                            height:         parent.height
                            width:          1
                            color:          palette.text
                            visible:        wifiStatus.visible
dogmaphobic's avatar
dogmaphobic committed
266
                        }
267 268 269 270 271
                        Column {
                            id:                 wifiStatus
                            anchors.margins:    ScreenTools.defaultFontPixelHeight / 2
                            spacing:            ScreenTools.defaultFontPixelHeight / 2
                            visible:            false
dogmaphobic's avatar
dogmaphobic committed
272
                            QGCLabel {
273
                                text:           qsTr("Bridge/Vehicle Link")
274 275 276 277 278
                                font.weight:    Font.DemiBold
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
279
                                    text:       qsTr("Messages Received")
280
                                    width:      _firstColumn
dogmaphobic's avatar
dogmaphobic committed
281
                                }
282 283
                                QGCLabel {
                                    id:         vpackets
dogmaphobic's avatar
dogmaphobic committed
284 285
                                }
                            }
286 287 288
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
289
                                    text:       qsTr("Messages Lost")
290 291 292 293 294 295 296 297 298
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    id:         vlost
                                }
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
299
                                    text:       qsTr("Messages Sent")
300 301 302 303 304 305 306 307 308 309 310
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    id:         vsent
                                }
                            }
                            Rectangle {
                                height:         1
                                width:          parent.width
                                color:          palette.text
                            }
dogmaphobic's avatar
dogmaphobic committed
311
                            QGCLabel {
312
                                text:           qsTr("Bridge/QGC Link")
313
                                font.weight:    Font.DemiBold
dogmaphobic's avatar
dogmaphobic committed
314
                            }
315 316 317
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
318
                                    text:       qsTr("Messages Received")
319 320 321 322
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    id:         gpackets
dogmaphobic's avatar
dogmaphobic committed
323 324
                                }
                            }
325 326 327
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
328
                                    text:       qsTr("Messages Lost")
329 330 331 332 333 334 335 336 337
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    id:         glost
                                }
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
338
                                    text:       qsTr("Messages Sent")
339 340 341 342 343 344 345 346 347 348 349
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    id:         gsent
                                }
                            }
                            Rectangle {
                                height:         1
                                width:          parent.width
                                color:          palette.text
                            }
dogmaphobic's avatar
dogmaphobic committed
350
                            QGCLabel {
351
                                text:           qsTr("QGC/Bridge Link")
352 353 354 355 356
                                font.weight:    Font.DemiBold
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
357
                                    text:       qsTr("Messages Received")
358 359 360 361 362 363 364 365 366
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    text:       controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesReceived) : 0
                                }
                            }
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
367
                                    text:       qsTr("Messages Lost")
368 369 370 371 372
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    text:       controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesLost) : 0
                                }
dogmaphobic's avatar
dogmaphobic committed
373
                            }
374 375 376
                            Row {
                                spacing: ScreenTools.defaultFontPixelWidth
                                QGCLabel {
377
                                    text:       qsTr("Messages Sent")
378 379 380 381
                                    width:      _firstColumn
                                }
                                QGCLabel {
                                    text:       controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesSent) : 0
dogmaphobic's avatar
dogmaphobic committed
382 383 384 385 386 387 388 389
                                }
                            }
                        }
                    }
                }
                Row {
                    spacing: ScreenTools.defaultFontPixelWidth * 1.5
                    QGCButton {
390
                        text:   qsTr("Restore Defaults")
dogmaphobic's avatar
dogmaphobic committed
391 392 393 394 395 396
                        width:  ScreenTools.defaultFontPixelWidth * 16
                        onClicked: {
                            controller.restoreDefaults()
                        }
                    }
                    QGCButton {
397
                        text:           qsTr("Restart WiFi Bridge")
dogmaphobic's avatar
dogmaphobic committed
398 399 400 401 402 403 404 405 406 407
                        enabled:        !controller.busy
                        width:          ScreenTools.defaultFontPixelWidth * 16
                        onClicked: {
                            rebootDialog.visible = true
                        }
                        MessageDialog {
                            id:         rebootDialog
                            visible:    false
                            icon:       StandardIcon.Warning
                            standardButtons: StandardButton.Yes | StandardButton.No
408 409
                            title:      qsTr("Reboot WiFi Bridge")
                            text:       qsTr("This will restart the WiFi Bridge so the settings you've changed can take effect. Note that you may have to change your computer WiFi settings and QGroundControl link settings to match these changes. Are you sure you want to restart it?")
dogmaphobic's avatar
dogmaphobic committed
410 411 412 413 414 415 416 417 418
                            onYes: {
                                controller.reboot()
                                rebootDialog.visible = false
                            }
                            onNo: {
                                rebootDialog.visible = false
                            }
                        }
                    }
419
                    QGCButton {
dogmaphobic's avatar
dogmaphobic committed
420 421
                        text:       stEnabled ? qsTr("Hide Status") : qsTr("Show Status")
                        width:      ScreenTools.defaultFontPixelWidth * 16
422 423 424 425 426 427 428 429 430 431 432
                        onClicked: {
                            stEnabled = !stEnabled
                            if(stEnabled)
                                updateStatus()
                            else {
                                wifiStatus.visible = false
                                timer.stop()
                            }
                        }
                    }
                    QGCButton {
433
                        text:       qsTr("Reset Counters")
434 435 436 437 438 439 440 441 442 443
                        visible:    stEnabled
                        enabled:    stEnabled
                        width:      ScreenTools.defaultFontPixelWidth * 16
                        onClicked: {
                            stResetCounters = true;
                            updateStatus()
                            if(controller.vehicle)
                                controller.vehicle.resetCounters()
                        }
                    }
dogmaphobic's avatar
dogmaphobic committed
444 445 446 447 448
                }
            }
        }
    }
}