Skip to content

Commit 098975c

Browse files
Add way to spoof device data for development
1 parent 031f72d commit 098975c

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed

mechanical/components/cadquery/tray_6in.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@
2727
# "dual-ssd" - for 2x 2.5" SSD
2828
# "raspi" - for Raspberry Pi
2929

30-
device_id = "Raspberry_Pi_4B"
30+
31+
32+
# For development purposes if there is not a device in the devices database
33+
# with the paramters you want set the device id to "dummy-<shelf type>-<h>u"
34+
# for example "dummy-dual-ssd-4u"
35+
#
36+
# You can also pass a dictionary of device data to the `dummy_device_data`
37+
# keyword argument of `create_shelf_for` if you need other device data to be
38+
# set.
39+
40+
device_id = "dummy-raspi-2u"
3141

3242

3343
def create_6in_shelf(device_id) -> cad.Body:

nimble_build_system/cad/shelf.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ def create_shelf_for(device_id: str,
1818
assembly_key: str='Shelf',
1919
position: tuple[float, float, float]=(0,0,0),
2020
color: str='dodgerblue1',
21-
rack_params: RackParameters|None = None):
21+
rack_params: RackParameters|None = None,
22+
dummy_device_data:dict|None=None):
23+
2224

2325
if not rack_params:
2426
rack_params = RackParameters()
25-
device = Device(device_id, rack_params)
27+
28+
#Dummy is used for development purposes
29+
if device_id.startswith("dummy-"):
30+
device = Device(device_id, rack_params, dummy=True, dummy_data=dummy_device_data)
31+
else:
32+
device = Device(device_id, rack_params)
2633

2734
#TODO. We have shelf_id, shekf_key, shelf_type, and shelf_builder_id,
2835
# None of which are explained well, and the neither the id or the key

nimble_build_system/orchestration/device.py

+51-15
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,33 @@ class Device:
6161
}
6262
"""
6363

64-
def __init__(self, device_id, rack_params):
65-
66-
json_node = find_device(device_id)
67-
68-
self.id = json_node['ID']
69-
# self.name = json_node['Name']
70-
self.name = json_node['Hardware']
71-
# self.category = json_node['Category']
72-
self.category = json_node['Type']
73-
self.width = get_length_from_str(json_node['LengthMm'])
74-
self.depth = get_length_from_str(json_node['Depth'])
75-
self.height = get_length_from_str(json_node['Height'])
64+
def __init__(self, device_id, rack_params, dummy=False, dummy_data:dict|None=None):
65+
66+
self.dummy = dummy
67+
if not dummy:
68+
device_dict = find_device(device_id)
69+
else:
70+
device_dict = generate_dummy_device_dict(device_id, dummy_data)
71+
72+
self.id = device_dict['ID']
73+
# self.name = device_dict['Name']
74+
self.name = device_dict['Hardware']
75+
# self.category = device_dict['Category']
76+
self.category = device_dict['Type']
77+
self.width = get_length_from_str(device_dict['LengthMm'])
78+
self.depth = get_length_from_str(device_dict['Depth'])
79+
self.height = get_length_from_str(device_dict['Height'])
7680

7781
try:
78-
self.height_in_u = int(json_node['HeightUnits'])
82+
self.height_in_u = int(device_dict['HeightUnits'])
7983
except ValueError as exc:
8084
if self.height:
8185
self.height_in_u = ceil((self.height+4)/rack_params.mounting_hole_spacing)
8286
else:
8387
raise RuntimeError("Not enough information provided to generate shelf height") from exc
8488

85-
self.shelf_id = json_node['ShelfId']
86-
self.shelf_type = json_node['Shelf']
89+
self.shelf_id = device_dict['ShelfId']
90+
self.shelf_type = device_dict['Shelf']
8791

8892
@property
8993
def shelf_key(self):
@@ -123,3 +127,35 @@ def get_length_from_str(length):
123127
if match := re.match(r'^([0-9]+(?:\.[0-9]+)?) ?mm$', length):
124128
return float(match[1])
125129
return None
130+
131+
def generate_dummy_device_dict(device_id:str, dummy_data:dict|None=None):
132+
"""
133+
Create a dummy database record from a device id. If id is in the pattern
134+
`dummy-<type>-<h>u` then the shelf type and height in u are set from this.
135+
All device dictionary items that are used in the Device class can be set
136+
by entering the desired key and value in the dummy_data parameter
137+
"""
138+
if dummy_data is None:
139+
dummy_data = {}
140+
141+
device_dict = {'ID': device_id}
142+
143+
if match := re.match(r'^dummy-(.+)-([0-9])+u$', device_id):
144+
default_shelf_id = match[1]+'-6'
145+
default_height_in_u = match[2]
146+
else:
147+
print(f"dummy device string `{device_id}` doesn't match pattern "
148+
"`dummy-<type>-<h>u` shelf type and height in u may not have "
149+
"been set as intended")
150+
default_shelf_id = "generic"
151+
default_height_in_u = "2"
152+
153+
device_dict['Hardware'] = dummy_data.get('Hardware', "Dummy")
154+
device_dict['Type'] = dummy_data.get('Type', "Dummy")
155+
device_dict['LengthMm'] = dummy_data.get('LengthMm', "100mm")
156+
device_dict['Depth'] = dummy_data.get('Depth', "100mm")
157+
device_dict['Height'] = dummy_data.get('Height', "30mm")
158+
device_dict['Shelf'] = dummy_data.get('Shelf', "Dummy")
159+
device_dict['HeightUnits'] = dummy_data.get('HeightUnits', default_height_in_u)
160+
device_dict['ShelfId'] = dummy_data.get('ShelfId', default_shelf_id )
161+
return device_dict

0 commit comments

Comments
 (0)