Skip to content

Commit 273f01c

Browse files
committedJan 6, 2023
support TB4 Speed
1 parent 8a2f7e9 commit 273f01c

File tree

5 files changed

+82
-264
lines changed

5 files changed

+82
-264
lines changed
 

‎src/getTb.py

+60-234
Original file line numberDiff line numberDiff line change
@@ -1,255 +1,81 @@
1-
# -*- coding: utf-8 -*-
2-
##############################################################################
3-
#
4-
# Module: devServer.py
5-
#
6-
# Description:
7-
# Server socket module, which listens for the client
8-
# Based on the command from Clinet, control device which is connected
9-
# Send the response back to Clinet in JSON format (JSON Object)
10-
#
11-
# Copyright notice:
12-
# This file copyright (c) 2020 by
13-
#
14-
# MCCI Corporation
15-
# 3520 Krums Corners Road
16-
# Ithaca, NY 14850
17-
#
18-
# Released under the MCCI Corporation.
19-
#
20-
# Author:
21-
# Seenivasan V, MCCI Corporation June 2021
22-
#
23-
# Revision history:
24-
# V2.6.0 Wed Apr 20 2022 17:00:00 Seenivasan V
25-
# Module created
26-
##############################################################################
27-
# Built-in imports
281
import json
29-
30-
import sys
312
import os
323

33-
# Own modules
34-
import xml.dom.minidom
35-
36-
DOM_ELEMENT = 1
37-
NODE_TEXT = 3
38-
DOCUMENT_TYPE = 10
39-
40-
# Key Macros
41-
BUSNAME = "_name"
42-
DEVNAME = "device_name_key"
43-
UID = "switch_uid_key"
44-
VENDORNAME = "vendor_name_key"
45-
46-
NODEKEY = "_items"
47-
48-
##############################################################################
49-
# Utilities
50-
##############################################################################
51-
52-
def filterTextNodes(inpDE):
53-
fnodes = []
54-
for elem in inpDE.childNodes:
55-
if elem.nodeType == DOM_ELEMENT:
56-
fnodes.append(elem)
57-
return fnodes
58-
59-
def checkDataType(inpDE):
60-
dtypeIdx = None
61-
for idx, elem in enumerate(inpDE):
62-
if(elem.nodeName == "key"):
63-
if(elem.firstChild.nodeValue == "_dataType"):
64-
dtypeIdx = idx + 1
4+
speed_tag = ['receptacle_1_tag', 'receptacle_2_tag', 'receptacle_3_tag', 'receptacle_4_tag']
5+
swuid_tag = 'switch_uid_key'
6+
tbdata_tag = 'SPThunderboltDataType'
657

66-
dataType = inpDE[dtypeIdx].firstChild.nodeValue
67-
68-
if(dataType == "SPThunderboltDataType"):
69-
return True
70-
return False
71-
72-
def pickArrayElementFromItem(inpDE):
73-
dtypeIdx = None
74-
for idx, elem in enumerate(inpDE):
75-
if(elem.nodeName == "key"):
76-
if(elem.firstChild.nodeValue == "_items"):
77-
dtypeIdx = idx + 1
8+
tbbus = []
789

79-
dataType = inpDE[dtypeIdx].nodeName
80-
if(dataType == "array"):
81-
return inpDE[dtypeIdx]
82-
else:
83-
return None
10+
finalDict = {}
8411

85-
# Function to calculate number of buses
86-
# Number of buses = number of Dict elements in the imAE
87-
def getTotalBuses(imAE):
88-
fnodes = filterTextNodes(imAE)
89-
rnodes = []
90-
for ie in fnodes:
91-
if ie.nodeName == "dict":
92-
rnodes.append(ie)
93-
return rnodes
9412

95-
def getBusProperties(sbe):
96-
busparam = []
97-
busidx = []
98-
rnodes = filterTextNodes(sbe)
99-
for idx, elem in enumerate(rnodes):
100-
if(elem.nodeName == "key"):
101-
if(elem.firstChild.nodeValue == BUSNAME):
102-
busidx.append(idx+1)
103-
elif(elem.firstChild.nodeValue == DEVNAME):
104-
busidx.append(idx+1)
105-
elif(elem.firstChild.nodeValue == UID):
106-
busidx.append(idx+1)
107-
elif(elem.firstChild.nodeValue == VENDORNAME):
108-
busidx.append(idx+1)
109-
for idx in busidx:
110-
busparam.append(rnodes[idx].firstChild.nodeValue)
111-
return busparam
13+
def grabData(gbus, finalDict):
14+
childs = None
15+
if swuid_tag in gbus:
16+
bdict = {}
17+
bdict['name'] = gbus['_name']
18+
bdict['deviceName'] = gbus['device_name_key']
19+
bdict['vendorName'] = gbus['vendor_name_key']
11220

113-
def getTotalBusNames(tbe):
114-
busparam = []
115-
for ide in tbe:
116-
busparam.append(getBusProperties(ide))
117-
return busparam
118-
119-
def getBusHubPtrs(sbe):
120-
sbushubptrs = []
121-
busidx = None
122-
hae = None
123-
rnodes = filterTextNodes(sbe)
124-
for idx, elem in enumerate(rnodes):
125-
if(elem.nodeName == "key"):
126-
if(elem.firstChild.nodeValue == NODEKEY):
127-
busidx = idx+1
128-
if(busidx != None):
129-
dataType = rnodes[busidx].nodeName
130-
if(dataType == "array"):
131-
hae = rnodes[busidx]
132-
if(hae != None):
133-
hrnodes = filterTextNodes(hae)
134-
for elem in hrnodes:
135-
if elem.nodeName == "dict":
136-
sbushubptrs.append(elem)
21+
for stag in speed_tag:
22+
if stag in gbus:
23+
bdict['speed'] = gbus[stag]['current_speed_key']
24+
# print(stag)
25+
break
26+
27+
if '_items' in gbus.keys():
28+
childs = gbus['_items']
29+
nchild = []
30+
for ielem in childs:
31+
nchild.append(ielem[swuid_tag])
32+
bdict['child'] = nchild
33+
else:
34+
bdict['child'] = []
35+
childs = []
36+
finalDict[gbus[swuid_tag]] = bdict
13737

138-
return sbushubptrs
139-
140-
def getTotalBusHubPtrs(tbe):
141-
tbushubptrs = []
142-
for ide in tbe:
143-
tbushubptrs.append(getBusHubPtrs(ide))
144-
return tbushubptrs
145-
146-
def fetchNameAndItem(she):
147-
nhe = None
148-
chn = None
149-
nameidx = None
150-
itemidx = None
151-
for idx, elem in enumerate(she):
152-
if(elem.nodeName == "key"):
153-
if(elem.firstChild.nodeValue == BUSNAME):
154-
nameidx = idx + 1
155-
elif(elem.firstChild.nodeValue == NODEKEY):
156-
itemidx = idx + 1
157-
elif(elem.firstChild.nodeValue == UID):
158-
uididx = idx + 1
159-
160-
chn = she[nameidx].firstChild.nodeValue
161-
uid = she[uididx].firstChild.nodeValue
162-
nodename = chn+","+uid
163-
if(itemidx != None):
164-
nhe = she[itemidx]
165-
return nodename, nhe
166-
167-
def getDictNodeFromArray(inpDN):
168-
dnode = None
169-
rnodes = filterTextNodes(inpDN)
170-
for idx, elem in enumerate(rnodes):
171-
if(elem.nodeName == "dict"):
172-
dnode = elem
173-
return dnode
174-
175-
def getBusHubs(sbhptr):
176-
sbushubs = []
177-
if(len(sbhptr) == 0):
178-
return sbushubs
38+
kilist = list(gbus.keys())
39+
for ikey in kilist:
40+
if ikey != "_items":
41+
del gbus[ikey]
17942

180-
# Hub pointer is a Dict Element which may contains nested hubs and ports
181-
# In which there should be a name key and item key which is optional
182-
# if name key found, append the name string to the list
183-
# then if item key found do the nested operation until no item key found
43+
return childs
18444

185-
# Remove the Text Nodes
186-
trhptr = []
187-
for elem in sbhptr:
188-
trhptr.append(filterTextNodes(elem))
18945

190-
hname, nitem = fetchNameAndItem(trhptr[0])
191-
192-
# Fetch Name and Item
193-
for she in trhptr:
194-
hname, nitem = fetchNameAndItem(she)
195-
sbushubs.append(hname)
196-
while(nitem != None):
197-
rnodes = getDictNodeFromArray(nitem)
198-
rrnodes = filterTextNodes(rnodes)
199-
hname, nitem = fetchNameAndItem(rrnodes)
200-
sbushubs.append(hname)
201-
return sbushubs
46+
def handleBusTree(gbus, tbdict):
47+
doflg = True
48+
while doflg:
49+
gchild = grabData(gbus, tbdict)
50+
if gchild == None:
51+
doflg = False
52+
else:
53+
# print(len(gchild))
54+
if len(gchild) > 0:
55+
gbus = gchild[0]
56+
else:
57+
doflg = False
58+
20259

20360
def scan_tb():
204-
buses = [] # contains Dict Elements
205-
busNames = [] # [["bus0 busname", "deviceName", "UID", "Vendor Name"],["bus1 busname", "deviceName", "UID", "Vendor Name"]]
206-
busHubPtrs = [] # [[bus_0 hub1, hub2,...,hubn],[bus_1 hub1, hub2,...,hubn]]
207-
busHubs = []
208-
61+
tbbus = []
20962
tbdict = {}
210-
211-
xmldoc = os.popen("system_profiler -xml SPThunderboltDataType")
212-
# Use the parse() function to load and parse an XML file
213-
xmlobj = xml.dom.minidom.parseString(xmldoc.read())
21463

215-
childobj = None
216-
child = xmlobj.childNodes
217-
childobj = child[1]
64+
# f = open('myspeed.json')
65+
# mytb = json.load(f)
21866

219-
# Plist should contains 2 DOM text nodes and one array element
220-
fnodes = filterTextNodes(childobj)
221-
mAE = fnodes[0]
222-
223-
# This DOM Element (array) contains 2 DOM text nodes and one DOM Element (dict)
224-
fnodes = filterTextNodes(mAE)
225-
mDE = fnodes[0]
226-
227-
fmDE = filterTextNodes(mDE)
228-
if checkDataType(fmDE):
229-
230-
imAE = pickArrayElementFromItem(fmDE)
231-
232-
buses = getTotalBuses(imAE)
233-
busNames = getTotalBusNames(buses)
234-
235-
busHubPtrs = getTotalBusHubPtrs(buses)
236-
237-
for hptr in busHubPtrs:
238-
busHubs.append(getBusHubs(hptr))
67+
xmldoc = os.popen("system_profiler -json SPThunderboltDataType")
68+
mytb = json.load(xmldoc)
69+
# Use the parse() function to load and parse an XML file
70+
# xmlobj = xml.dom.minidom.parseString(xmldoc.read())
23971

240-
fbnames = []
241-
for bus in busNames:
242-
try:
243-
fbnames.append(bus[0])
244-
except:
245-
pass
72+
if tbdata_tag in mytb.keys():
73+
tbbuses = mytb[tbdata_tag]
24674

247-
for idx, hubs in enumerate(busHubs):
248-
try:
249-
tbdict[fbnames[idx]] = hubs
250-
except:
251-
pass
252-
else:
253-
print("Thunderbolt Data type fail")
75+
for tbus in tbbuses:
76+
tbbus.append(tbus)
25477

78+
for i in range(len(tbbus)):
79+
handleBusTree(tbbus[i], tbdict)
80+
# print(tbdict)
25581
return tbdict

‎src/logWindow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def UsbThread(self):
289289
thControl.get_tree_change(self.top)
290290
except:
291291
# print message
292-
self.print_on_log("USB Read Error!")
292+
self.print_on_log("rror!")
293293
self.wait_flg = False
294294
self.btn_ref.Enable()
295295

‎src/uiGlobals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# GLOBAL VARIABLES
2929
##############################################################################
3030
APP_NAME = "Cricket"
31-
APP_VERSION = "3.0.0"
31+
APP_VERSION = "3.0.0-1"
3232

3333
# StatusBar ID
3434
SB_PORT_ID = 0

‎src/uiMainApp.py

+1
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ def read_serial(self, param):
11331133
return False
11341134

11351135
def get_usb_tree(self):
1136+
# thControl.get_tree_change(self)
11361137
try:
11371138
thControl.get_tree_change(self)
11381139
except:

0 commit comments

Comments
 (0)