Skip to content

Commit 94bcde6

Browse files
authored
Save last connection type (file, serial, tcp) [ CPP-495] [CPP-430] (#328)
* Save last connection type into connection_history.yml file This is used so that in the connection screen, the last connection type is remembered. * Use the common python -> rust ConnectionType enum
1 parent fb87111 commit 94bcde6

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

console_backend/src/common_constants.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ pub enum Keys {
287287
RECORDING_FILENAME,
288288
#[strum(serialize = "CONSOLE_VERSION")]
289289
CONSOLE_VERSION,
290+
#[strum(serialize = "PREVIOUS_CONNECTION_TYPE")]
291+
PREVIOUS_CONNECTION_TYPE,
290292
#[strum(serialize = "CONNECTION_MESSAGE")]
291293
CONNECTION_MESSAGE,
292294
#[strum(serialize = "NOTIFICATION")]
@@ -305,6 +307,16 @@ pub enum ConnectionState {
305307
CONNECTING,
306308
}
307309

310+
#[derive(Clone, Debug, Display, EnumString, EnumVariantNames, Eq, Hash, PartialEq)]
311+
pub enum ConnectionType {
312+
#[strum(serialize = "Tcp")]
313+
Tcp,
314+
#[strum(serialize = "File")]
315+
File,
316+
#[strum(serialize = "Serial")]
317+
Serial,
318+
}
319+
308320
#[derive(Clone, Debug, Display, EnumString, EnumVariantNames, Eq, Hash, PartialEq)]
309321
pub enum QTKeys {
310322
#[strum(serialize = "QVariantList")]

console_backend/src/shared_state.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use parking_lot::Mutex;
2222
use serde::{Deserialize, Serialize};
2323
use serialport::FlowControl;
2424

25-
use crate::connection::Connection;
2625
use crate::constants::{
2726
APPLICATION_NAME, APPLICATION_ORGANIZATION, APPLICATION_QUALIFIER, CONNECTION_HISTORY_FILENAME,
2827
DEFAULT_LOG_DIRECTORY, MAX_CONNECTION_HISTORY, MPS,
@@ -38,6 +37,7 @@ use crate::update_tab::UpdateTabUpdate;
3837
use crate::utils::send_conn_state;
3938
use crate::watch::{WatchReceiver, Watched};
4039
use crate::{client_sender::BoxedClientSender, main_tab::logging_stats_thread};
40+
use crate::{common_constants::ConnectionType, connection::Connection};
4141
use crate::{
4242
common_constants::{self as cc, SbpLogging},
4343
status_bar::Heartbeat,
@@ -147,6 +147,9 @@ impl SharedState {
147147
pub fn serial_history(&self) -> IndexMap<String, SerialConfig> {
148148
self.lock().connection_history.serial_configs()
149149
}
150+
pub fn connection_type_history(&self) -> ConnectionType {
151+
self.lock().connection_history.last_connection_type.clone()
152+
}
150153
pub fn update_folder_history(&self, folder: PathBuf) {
151154
let folder = String::from(folder.to_str().expect(CONVERT_TO_STR_FAILURE));
152155
self.lock().connection_history.record_folder(folder);
@@ -713,6 +716,17 @@ pub struct ConnectionHistory {
713716
files: IndexSet<String>,
714717
folders: IndexSet<String>,
715718
serial_configs: IndexMap<String, SerialConfig>,
719+
#[serde(with = "ConnectionTypeDef")]
720+
last_connection_type: ConnectionType,
721+
}
722+
723+
// https://serde.rs/remote-derive.html
724+
#[derive(Serialize, Deserialize)]
725+
#[serde(remote = "ConnectionType")]
726+
enum ConnectionTypeDef {
727+
Tcp,
728+
File,
729+
Serial,
716730
}
717731

718732
impl Default for ConnectionHistory {
@@ -739,6 +753,7 @@ impl ConnectionHistory {
739753
files: IndexSet::new(),
740754
folders,
741755
serial_configs: IndexMap::new(),
756+
last_connection_type: ConnectionType::Serial,
742757
}
743758
}
744759
/// Return the filename of the saved connection history file.
@@ -772,6 +787,7 @@ impl ConnectionHistory {
772787
self.addresses.insert(address);
773788
let diff = i32::max(0, self.addresses.len() as i32 - MAX_CONNECTION_HISTORY);
774789
self.addresses = self.addresses.split_off(diff as usize);
790+
self.last_connection_type = ConnectionType::Tcp;
775791

776792
if let Err(e) = self.save() {
777793
error!("Unable to save connection history, {}.", e);
@@ -786,6 +802,7 @@ impl ConnectionHistory {
786802
self.files.insert(filename);
787803
let diff = i32::max(0, self.files.len() as i32 - MAX_CONNECTION_HISTORY);
788804
self.files = self.files.split_off(diff as usize);
805+
self.last_connection_type = ConnectionType::File;
789806

790807
if let Err(e) = self.save() {
791808
error!("Unable to save connection history, {}.", e);
@@ -818,6 +835,7 @@ impl ConnectionHistory {
818835
self.serial_configs.insert(device, serial);
819836
let diff = i32::max(0, self.serial_configs.len() as i32 - MAX_CONNECTION_HISTORY);
820837
self.serial_configs = self.serial_configs.split_off(diff as usize);
838+
self.last_connection_type = ConnectionType::Serial;
821839

822840
if let Err(e) = self.save() {
823841
error!("Unable to save connection history, {}.", e);
@@ -943,6 +961,7 @@ mod tests {
943961
let first_addy = addresses.first().unwrap();
944962
assert_eq!(host1, first_addy.host);
945963
assert_eq!(port, first_addy.port);
964+
assert_eq!(ConnectionType::Tcp, conn_history.last_connection_type);
946965

947966
conn_history.record_address(host2.clone(), port);
948967
let addresses = conn_history.addresses();
@@ -972,6 +991,7 @@ mod tests {
972991
let files = conn_history.files();
973992
assert_eq!(filename1, files[1]);
974993
assert_eq!(filename2, files[0]);
994+
assert_eq!(ConnectionType::File, conn_history.last_connection_type);
975995

976996
for ele in 0..MAX_CONNECTION_HISTORY {
977997
conn_history.record_file(ele.to_string());
@@ -987,13 +1007,16 @@ mod tests {
9871007
backup_file(bfilename.clone());
9881008

9891009
let mut conn_history = ConnectionHistory::new();
1010+
assert_eq!(ConnectionType::Serial, conn_history.last_connection_type);
9901011

9911012
let configs = conn_history.serial_configs();
9921013
assert_eq!(configs.keys().len(), 0);
9931014

9941015
conn_history.record_serial("/dev/ttyUSB0".to_string(), 115200, FlowControl::Software);
9951016
conn_history.record_serial("/dev/ttyUSB0".to_string(), 115200, FlowControl::None);
9961017

1018+
assert_eq!(ConnectionType::Serial, conn_history.last_connection_type);
1019+
9971020
let configs = conn_history.serial_configs();
9981021

9991022
// Should only store one serial record despite the settings changing

console_backend/src/utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ pub fn refresh_connection_frontend(client_sender: &BoxedClientSender, shared_sta
153153
prevous_files.set(i as u32, filename);
154154
}
155155

156+
let prev_conn_type = shared_state.connection_type_history().to_string();
157+
connection_status.set_previous_connection_type(&*prev_conn_type);
158+
156159
client_sender.send_data(serialize_capnproto_builder(builder));
157160
}
158161

resources/ConnectionScreen.qml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Item {
99
property string tcp_ip: "TCP/IP"
1010
property string serial_usb: "Serial/USB"
1111
property string file: "File"
12+
property string previous_connection_type: ""
1213
property var sources: [tcp_ip, serial_usb, file]
1314
property variant available_baudrates: []
1415
property variant available_devices: []
@@ -79,23 +80,25 @@ Item {
7980
Layout.alignment: Qt.AlignTop
8081

8182
RadioButton {
82-
id: tcpRadio
83+
id: serialRadio
8384

84-
checked: true
85-
text: tcp_ip
85+
checked: previous_connection_type == "Serial"
86+
text: serial_usb
8687
onToggled: dialogRect.forceActiveFocus()
8788
}
8889

8990
RadioButton {
90-
id: serialRadio
91+
id: tcpRadio
9192

92-
text: serial_usb
93+
checked: previous_connection_type == "TCP"
94+
text: tcp_ip
9395
onToggled: dialogRect.forceActiveFocus()
9496
}
9597

9698
RadioButton {
9799
id: fileRadio
98100

101+
checked: previous_connection_type == "File"
99102
text: file
100103
onToggled: dialogRect.forceActiveFocus()
101104
}
@@ -370,6 +373,7 @@ Item {
370373
previous_ports = connectionData.previous_ports;
371374
previous_files = connectionData.previous_files;
372375
previous_serial_configs = connectionData.previous_serial_configs;
376+
previous_connection_type = connectionData.previous_connection_type;
373377
if (!last_used_serial_device && connectionData.last_used_serial_device) {
374378
// Set the default selected to the last used
375379
last_used_serial_device = connectionData.last_used_serial_device;

src/main/resources/base/console_backend.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ struct ConnectionStatus {
158158
previousHosts @3: List(Text);
159159
previousPorts @4: List(UInt16);
160160
previousFiles @5: List(Text);
161+
previousConnectionType @10: Text;
161162
previousSerialConfigs @6: List(SerialRequest);
162163
lastSerialDevice: union {
163164
port @7 :Text;

swiftnav_console/connection.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from PySide2.QtCore import Property, QObject, Slot
77

8-
from .constants import Keys, QTKeys, ConnectionState
8+
from .constants import Keys, QTKeys, ConnectionState, ConnectionType
99

1010
CONNECTION: Dict[str, Any] = {
1111
Keys.AVAILABLE_PORTS: [],
@@ -20,6 +20,7 @@
2020
Keys.LAST_USED_SERIAL_DEVICE: None,
2121
Keys.PREVIOUS_SERIAL_CONFIGS: [],
2222
Keys.CONSOLE_VERSION: str,
23+
Keys.PREVIOUS_CONNECTION_TYPE: "",
2324
}
2425

2526

@@ -36,6 +37,7 @@ class ConnectionData(QObject): # pylint: disable=too-many-instance-attributes d
3637
_last_used_serial_device: str
3738
_previous_serial_configs: List[List[Any]] = []
3839
_console_version: str = ""
40+
_previous_connection_type: ConnectionType = ConnectionType.Serial
3941
_connection_message: str = ""
4042

4143
def get_available_ports(self) -> List[str]:
@@ -128,6 +130,14 @@ def set_console_version(self, console_version: str) -> None:
128130

129131
console_version = Property(str, get_console_version, set_console_version)
130132

133+
def get_previous_connection_type(self) -> ConnectionType:
134+
return self._previous_connection_type
135+
136+
def set_previous_connection_type(self, previous_connection_type: ConnectionType) -> None:
137+
self._previous_connection_type = previous_connection_type
138+
139+
previous_connection_type = Property(str, get_previous_connection_type, set_previous_connection_type)
140+
131141
def get_connection_message(self) -> str:
132142
return self._connection_message
133143

@@ -150,6 +160,7 @@ def fill_data(self, cp: ConnectionData) -> ConnectionData: # pylint:disable=no-
150160
cp.set_last_used_serial_device(CONNECTION[Keys.LAST_USED_SERIAL_DEVICE])
151161
cp.set_previous_serial_configs(CONNECTION[Keys.PREVIOUS_SERIAL_CONFIGS])
152162
cp.set_console_version(CONNECTION[Keys.CONSOLE_VERSION])
163+
cp.set_previous_connection_type(CONNECTION[Keys.PREVIOUS_CONNECTION_TYPE])
153164
cp.set_connection_message(CONNECTION[Keys.CONNECTION_MESSAGE])
154165
CONNECTION[Keys.CONNECTION_MESSAGE] = ""
155166
return cp

swiftnav_console/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class Keys(str, Enum):
136136
PREVIOUS_SERIAL_CONFIGS = "PREVIOUS_SERIAL_CONFIGS"
137137
RECORDING_FILENAME = "RECORDING_FILENAME"
138138
CONSOLE_VERSION = "CONSOLE_VERSION"
139+
PREVIOUS_CONNECTION_TYPE = "PREVIOUS_CONNECTION_TYPE"
139140
CONNECTION_MESSAGE = "CONNECTION_MESSAGE"
140141
NOTIFICATION = "NOTIFICATION"
141142

@@ -147,6 +148,12 @@ class ConnectionState(str, Enum):
147148
CONNECTING = "CONNECTING"
148149

149150

151+
class ConnectionType(str, Enum):
152+
Tcp = "Tcp"
153+
File = "File"
154+
Serial = "Serial"
155+
156+
150157
class QTKeys(str, Enum):
151158
QVARIANTLIST = "QVariantList"
152159
QVARIANT = "QVariant"

swiftnav_console/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import console_backend.server # type: ignore # pylint: disable=import-error,no-name-in-module
2626

27-
from .constants import ApplicationMetadata, ConnectionState, Keys, Tabs, QTKeys
27+
from .constants import ApplicationMetadata, ConnectionState, ConnectionType, Keys, Tabs, QTKeys
2828

2929
from .log_panel import (
3030
LOG_PANEL,
@@ -382,6 +382,7 @@ def receive_messages(app_, backend, messages):
382382
[entry.device, entry.baudrate, entry.flowControl] for entry in m.connectionStatus.previousSerialConfigs
383383
]
384384
CONNECTION[Keys.CONSOLE_VERSION] = m.connectionStatus.consoleVersion
385+
CONNECTION[Keys.PREVIOUS_CONNECTION_TYPE] = ConnectionType(m.connectionStatus.previousConnectionType)
385386
elif m.which == Message.Union.LoggingBarStatus:
386387
LOGGING_BAR[Keys.PREVIOUS_FOLDERS][:] = m.loggingBarStatus.previousFolders
387388
LOGGING_BAR[Keys.CSV_LOGGING] = m.loggingBarStatus.csvLogging

0 commit comments

Comments
 (0)