ESP8266Component.qml 24.6 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9 10


11 12
import QtQuick          2.3
import QtQuick.Controls 1.2
dogmaphobic's avatar
dogmaphobic committed
13
import QtQuick.Dialogs  1.2
14
import QtQuick.Layouts  1.2
dogmaphobic's avatar
dogmaphobic committed
15 16 17 18 19 20 21 22 23

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

24
Item {
dogmaphobic's avatar
dogmaphobic committed
25

26
    property real _margins:         ScreenTools.defaultFontPixelHeight
27 28 29 30 31
    property real _middleRowWidth:  ScreenTools.defaultFontPixelWidth * 18
    property real _editFieldWidth:  ScreenTools.defaultFontPixelWidth * 16
    property real _labelWidth:      ScreenTools.defaultFontPixelWidth * 10
    property real _statusWidth:     ScreenTools.defaultFontPixelWidth * 6
    property real _smallFont:       ScreenTools.smallFontPointSize
32

33
    readonly property string    dialogTitle:    qsTr("controller WiFi Bridge")
34 35
    property int                stStatus:       XMLHttpRequest.UNSENT
    property int                stErrorCount:   0
36
    property bool               stResetCounters:false
dogmaphobic's avatar
dogmaphobic committed
37 38

    ESP8266ComponentController {
39
        id: controller
dogmaphobic's avatar
dogmaphobic committed
40 41
    }

42 43 44 45 46 47 48 49 50 51 52
    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
53 54 55
        var url = "http://"
        url += controller.wifiIPAddress
        url += "/status.json"
56 57 58 59 60 61 62 63 64 65
        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) {
66
                    console.log(qsTr("Error fetching WiFi Bridge Status: %1").arg(objectArray.errors[0].message))
67
                    stErrorCount = stErrorCount + 1
68
                    if(stErrorCount < 2)
69 70
                        timer.start()
                } else {
71 72 73 74 75 76 77 78 79 80 81 82
                    //-- 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()
83 84 85 86 87 88 89 90 91 92
                }
            }
        }
        req.send()
    }

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

dogmaphobic's avatar
dogmaphobic committed
96
    property Fact wifiMode:     controller.getParameterFact(controller.componentID, "WIFI_MODE",      false) //-- Don't bitch about missing as this is new
dogmaphobic's avatar
dogmaphobic committed
97 98 99 100
    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")

101
    Item {
dogmaphobic's avatar
dogmaphobic committed
102 103 104 105
        id:             panel
        anchors.fill:   parent

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