ESP8266Component.qml 26.3 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

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 }

43 44 45 46 47 48
    property real _margins:         ScreenTools.defaultFontPixelHeight
    property real _middleRowWidth:  ScreenTools.defaultFontPixelWidth * 18
    property real _editFieldWidth:  ScreenTools.defaultFontPixelWidth * 18
    property real _labelWidth:      ScreenTools.defaultFontPixelWidth * 10
    property real _statusWidth:     ScreenTools.defaultFontPixelWidth * 5

49
    readonly property string    dialogTitle:    qsTr("controller WiFi Bridge")
50 51
    property int                stStatus:       XMLHttpRequest.UNSENT
    property int                stErrorCount:   0
52
    property bool               stResetCounters:false
dogmaphobic's avatar
dogmaphobic committed
53 54 55 56 57 58

    ESP8266ComponentController {
        id:             controller
        factPanel:      panel
    }

59 60 61 62 63 64 65 66 67 68 69
    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
70 71 72
        var url = "http://"
        url += controller.wifiIPAddress
        url += "/status.json"
73 74 75 76 77 78 79 80 81 82
        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) {
83
                    console.log(qsTr("Error fetching WiFi Bridge Status: %1").arg(objectArray.errors[0].message))
84
                    stErrorCount = stErrorCount + 1
85
                    if(stErrorCount < 2)
86 87
                        timer.start()
                } else {
88 89 90 91 92 93 94 95 96 97 98 99
                    //-- 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
                    timer.start()
100 101 102 103 104 105 106 107 108 109
                }
            }
        }
        req.send()
    }

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

dogmaphobic's avatar
dogmaphobic committed
113
    property Fact wifiMode:     controller.getParameterFact(controller.componentID, "WIFI_MODE",      false) //-- Don't bitch about missing as this is new
dogmaphobic's avatar
dogmaphobic committed
114 115 116 117 118 119 120 121 122
    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 {
123 124 125 126
            clip:                                       true
            anchors.fill:                               parent
            contentHeight:                              mainCol.height
            flickableDirection:                         Flickable.VerticalFlick
dogmaphobic's avatar
dogmaphobic committed
127
            Column {
128 129 130 131
                id:                                     mainCol
                spacing:                                _margins
                anchors.horizontalCenter:               parent.horizontalCenter
                Item { width: 1; height: _margins * 0.5; }
dogmaphobic's avatar
dogmaphobic committed
132
                QGCLabel {
133 134
                    text:                               qsTr("ESP WiFi Bridge Settings")
                    font.weight:                        Font.DemiBold
dogmaphobic's avatar
dogmaphobic committed
135 136
                }
                Rectangle {
137 138 139
                    color:                              palette.windowShade
                    width:                              statusLayout.width  + _margins * 4
                    height:                             settingsRow.height  + _margins * 2
140
                    Row {
141 142 143 144 145 146 147 148 149 150 151 152
                        id:                             settingsRow
                        spacing:                        _margins
                        anchors.centerIn:               parent
                        Item { width: _margins * 0.5; height: 1; }
                        QGCColoredImage {
                            color:                      palette.text
                            height:                     ScreenTools.defaultFontPixelWidth * 10
                            width:                      ScreenTools.defaultFontPixelWidth * 12
                            mipmap:                     true
                            fillMode:                   Image.PreserveAspectFit
                            source:                     wifiMode ? (wifiMode.value === 0 ? "/qmlimages/APMode.svg" : "/qmlimages/StationMode.svg") : "/qmlimages/APMode.svg"
                            anchors.verticalCenter:     parent.verticalCenter
153
                        }
154
                        Item { width: _margins * 0.5; height: 1; }
155
                        Column {
156 157
                            spacing:                        _margins * 0.5
                            anchors.verticalCenter:         parent.verticalCenter
dogmaphobic's avatar
dogmaphobic committed
158
                            Row {
159
                                visible:                    wifiMode
dogmaphobic's avatar
dogmaphobic committed
160
                                QGCLabel {
161 162 163
                                    text:                   qsTr("WiFi Mode")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       modeField.baseline
dogmaphobic's avatar
dogmaphobic committed
164 165
                                }
                                QGCComboBox {
166 167 168 169
                                    id:                     modeField
                                    width:                  _editFieldWidth
                                    model:                  ["Access Point Mode", "Station Mode"]
                                    currentIndex:           wifiMode ? wifiMode.value : 0
dogmaphobic's avatar
dogmaphobic committed
170 171 172 173 174
                                    onActivated: {
                                        wifiMode.value = index
                                    }
                                }
                            }
175 176
                            Row {
                                QGCLabel {
177 178 179
                                    text:                   qsTr("WiFi Channel")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       channelField.baseline
180 181
                                }
                                QGCComboBox {
182 183 184 185 186
                                    id:                     channelField
                                    width:                  _editFieldWidth
                                    enabled:                wifiMode && wifiMode.value === 0
                                    model:                  controller.wifiChannels
                                    currentIndex:           wifiChannel ? wifiChannel.value - 1 : 0
187 188 189 190
                                    onActivated: {
                                        wifiChannel.value = index + 1
                                    }
                                }
dogmaphobic's avatar
dogmaphobic committed
191
                            }
192 193
                            Row {
                                QGCLabel {
194 195 196 197
                                    text:                   qsTr("WiFi AP SSID")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       ssidField.baseline
                                }
198
                                QGCTextField {
199 200 201 202
                                    id:                     ssidField
                                    width:                  _editFieldWidth
                                    text:                   controller.wifiSSID
                                    maximumLength:          16
203 204 205
                                    onEditingFinished: {
                                        controller.wifiSSID = text
                                    }
dogmaphobic's avatar
dogmaphobic committed
206 207
                                }
                            }
208 209
                            Row {
                                QGCLabel {
210 211 212 213
                                    text:                   qsTr("WiFi AP Password")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       passwordField.baseline
                                }
214
                                QGCTextField {
215 216 217 218
                                    id:                     passwordField
                                    width:                  _editFieldWidth
                                    text:                   controller.wifiPassword
                                    maximumLength:          16
219 220 221
                                    onEditingFinished: {
                                        controller.wifiPassword = text
                                    }
dogmaphobic's avatar
dogmaphobic committed
222
                                }
223 224 225
                            }
                            Row {
                                QGCLabel {
226 227 228
                                    text:                   qsTr("WiFi STA SSID")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       stassidField.baseline
229
                                }
230 231 232 233 234 235 236 237
                                QGCTextField {
                                    id:                     stassidField
                                    width:                  _editFieldWidth
                                    text:                   controller.wifiSSIDSta
                                    maximumLength:          16
                                    enabled:                wifiMode && wifiMode.value === 1
                                    onEditingFinished: {
                                        controller.wifiSSIDSta = text
238
                                    }
dogmaphobic's avatar
dogmaphobic committed
239 240
                                }
                            }
241 242
                            Row {
                                QGCLabel {
243 244 245
                                    text:                   qsTr("WiFi STA Password")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       passwordStaField.baseline
246 247
                                }
                                QGCTextField {
248 249 250 251 252
                                    id:                     passwordStaField
                                    width:                  _editFieldWidth
                                    text:                   controller.wifiPasswordSta
                                    maximumLength:          16
                                    enabled:                wifiMode && wifiMode.value === 1
253
                                    onEditingFinished: {
254
                                        controller.wifiPasswordSta = text
255 256 257 258 259
                                    }
                                }
                            }
                            Row {
                                QGCLabel {
260 261 262
                                    text:                   qsTr("UART Baud Rate")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       baudField.baseline
263
                                }
264 265 266 267 268 269 270 271
                                QGCComboBox {
                                    id:                     baudField
                                    width:                  _editFieldWidth
                                    model:                  controller.baudRates
                                    currentIndex:           controller.baudIndex
                                    onActivated: {
                                        controller.baudIndex = index
                                    }
272
                                }
dogmaphobic's avatar
dogmaphobic committed
273
                            }
274 275
                            Row {
                                QGCLabel {
276 277 278
                                    text:                   qsTr("QGC UDP Port")
                                    width:                  _middleRowWidth
                                    anchors.baseline:       qgcportField.baseline
279
                                }
280 281 282 283 284 285 286 287 288
                                QGCTextField {
                                    id:                     qgcportField
                                    width:                  _editFieldWidth
                                    text:                   hostPort ? hostPort.valueString : ""
                                    validator:              IntValidator {bottom: 1024; top: 65535;}
                                    inputMethodHints:       Qt.ImhDigitsOnly
                                    onEditingFinished: {
                                        hostPort.value = text
                                    }
dogmaphobic's avatar
dogmaphobic committed
289 290 291 292 293
                                }
                            }
                        }
                    }
                }
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                QGCLabel {
                    text:                               qsTr("ESP WiFi Bridge Status")
                    font.weight:                        Font.DemiBold
                }
                Rectangle {
                    color:                              palette.windowShade
                    width:                              statusLayout.width  + _margins * 4
                    height:                             statusLayout.height + _margins * 2
                    GridLayout {
                       id:                              statusLayout
                       columns:                         3
                       columnSpacing:                   _margins * 2
                       anchors.centerIn:                parent
                       QGCLabel {
                           text:                        qsTr("Bridge/Vehicle Link")
                           Layout.alignment:            Qt.AlignHCenter
                       }
                       QGCLabel {
                           text:                        qsTr("Bridge/QGC Link")
                           Layout.alignment:            Qt.AlignHCenter
                       }
                       QGCLabel {
                           text:                        qsTr("QGC/Bridge Link")
                           Layout.alignment:            Qt.AlignHCenter
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Received")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      vpackets
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               text:                    qsTr("Messages Received")
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      gpackets
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               text:                    qsTr("Messages Received")
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               text:                    controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesReceived) : 0
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Lost")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      vlost
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Lost")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      glost
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Lost")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               text:                    controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesLost) : 0
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Sent")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      vsent
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Sent")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               id:                      gsent
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                       Row {
                           spacing:                     _margins
                           QGCLabel {
                               text:                    qsTr("Messages Sent")
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               width:                   _labelWidth
                           }
                           QGCLabel {
                               text:                    controller.vehicle ? thisThingHasNoNumberLocaleSupport(controller.vehicle.messagesSent) : 0
                               width:                   _statusWidth
                               horizontalAlignment:     Text.AlignRight
                               font.pixelSize:          ScreenTools.smallFontPixelSize
                               font.family:             "Monospace"
                           }
                       }
                    }
                }
dogmaphobic's avatar
dogmaphobic committed
456
                Row {
457 458
                    spacing:                            _margins
                    anchors.horizontalCenter:           parent.horizontalCenter
dogmaphobic's avatar
dogmaphobic committed
459
                    QGCButton {
460 461
                        text:                           qsTr("Restore Defaults")
                        width:                          ScreenTools.defaultFontPixelWidth * 16
dogmaphobic's avatar
dogmaphobic committed
462 463 464 465 466
                        onClicked: {
                            controller.restoreDefaults()
                        }
                    }
                    QGCButton {
467 468 469
                        text:                           qsTr("Restart WiFi Bridge")
                        enabled:                        !controller.busy
                        width:                          ScreenTools.defaultFontPixelWidth * 16
dogmaphobic's avatar
dogmaphobic committed
470 471 472 473 474 475 476 477
                        onClicked: {
                            rebootDialog.visible = true
                        }
                        MessageDialog {
                            id:         rebootDialog
                            visible:    false
                            icon:       StandardIcon.Warning
                            standardButtons: StandardButton.Yes | StandardButton.No
478 479
                            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
480 481 482 483 484 485 486 487 488
                            onYes: {
                                controller.reboot()
                                rebootDialog.visible = false
                            }
                            onNo: {
                                rebootDialog.visible = false
                            }
                        }
                    }
489
                    QGCButton {
490 491
                        text:                           qsTr("Reset Counters")
                        width:                          ScreenTools.defaultFontPixelWidth * 16
492 493 494 495 496 497 498
                        onClicked: {
                            stResetCounters = true;
                            updateStatus()
                            if(controller.vehicle)
                                controller.vehicle.resetCounters()
                        }
                    }
dogmaphobic's avatar
dogmaphobic committed
499 500 501 502 503
                }
            }
        }
    }
}