20
20
ERR_DEV = 2
21
21
22
22
CONSOLE_PORT_TABLE = "CONSOLE_PORT"
23
+ LINE_KEY = "LINE"
23
24
BAUD_KEY = "baud_rate"
24
25
DEVICE_KEY = "remote_device"
25
26
FLOW_KEY = "flow_control"
@@ -46,22 +47,42 @@ def run_command(cmd):
46
47
sys .exit (ERR_CMD )
47
48
return output
48
49
49
- # returns a sorted list of all devices (whose name matches DEVICE_PREFIX)
50
+ # returns a sorted list of all devices
50
51
def getAllDevices ():
52
+ config_db = ConfigDBConnector ()
53
+ config_db .connect ()
54
+
55
+ # Querying CONFIG_DB to get configured console ports
56
+ keys = config_db .get_keys (CONSOLE_PORT_TABLE )
57
+ devices = []
58
+ for k in keys :
59
+ device = config_db .get_entry (CONSOLE_PORT_TABLE , k )
60
+ device [LINE_KEY ] = k
61
+ devices .append (device )
62
+
63
+ # Querying device directory to get all available console ports
51
64
cmd = "ls " + DEVICE_PREFIX + "*"
52
65
output = run_command (cmd )
53
66
54
- devices = output .split ('\n ' )
55
- devices = list (filter (lambda dev : re .match (DEVICE_PREFIX + r"\d+" , dev ) != None , devices ))
56
- devices .sort (key = lambda dev : int (dev [len (DEVICE_PREFIX ):]))
57
-
67
+ availableTtys = output .split ('\n ' )
68
+ availableTtys = list (filter (lambda dev : re .match (DEVICE_PREFIX + r"\d+" , dev ) != None , availableTtys ))
69
+ for tty in availableTtys :
70
+ k = tty [len (DEVICE_PREFIX ):]
71
+ if k not in keys :
72
+ device = { LINE_KEY : k }
73
+ devices .append (device )
74
+
75
+ devices .sort (key = lambda dev : int (dev [LINE_KEY ]))
58
76
return devices
59
77
60
78
# exits if inputted line number does not correspond to a device
61
79
# input: linenum
62
80
def checkDevice (linenum ):
63
- devices = getAllDevices ()
64
- if DEVICE_PREFIX + str (linenum ) not in devices :
81
+ config_db = ConfigDBConnector ()
82
+ config_db .connect ()
83
+
84
+ entry = config_db .get_entry (CONSOLE_PORT_TABLE , str (linenum ))
85
+ if not entry :
65
86
click .echo ("Line number {} does not exist" .format (linenum ))
66
87
sys .exit (ERR_DEV )
67
88
@@ -120,12 +141,9 @@ def getLineNumber(target, deviceBool):
120
141
config_db .connect ()
121
142
122
143
devices = getAllDevices ()
123
- linenums = list (map (lambda dev : dev [len (DEVICE_PREFIX ):], devices ))
124
-
125
- for linenum in linenums :
126
- entry = config_db .get_entry (CONSOLE_PORT_TABLE , linenum )
127
- if DEVICE_KEY in entry and entry [DEVICE_KEY ] == target :
128
- return linenum
144
+ for device in devices :
145
+ if DEVICE_KEY in device and device [DEVICE_KEY ] == target :
146
+ return device [LINE_KEY ]
129
147
130
148
click .echo ("Device {} does not exist" .format (target ))
131
149
sys .exit (ERR_DEV )
0 commit comments