Skip to content

Commit

Permalink
Merge pull request #85 from Wakoma/all-devices-produce-shelves
Browse files Browse the repository at this point in the history
Calculate shelf HightUnits from device Height if missing. Don't show devices that create invalid shelves in config
  • Loading branch information
julianstirling authored Jul 10, 2024
2 parents 018b4b3 + 4714a66 commit d6fd69b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nimble_build_system/orchestration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, selected_devices_ids):
all_devices = json.load(devices_file)
def find_device(device_id):
return next((x for x in all_devices if x['ID'] == device_id), None)
selected_devices = [Device(find_device(x)) for x in selected_devices_ids]
selected_devices = [Device(find_device(x), self._rack_params) for x in selected_devices_ids]

self._devices = deepcopy(selected_devices)
self._shelves = self._generate_shelf_list
Expand Down
28 changes: 23 additions & 5 deletions nimble_build_system/orchestration/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import re
from math import ceil

class Device:
"""
Expand Down Expand Up @@ -39,16 +40,24 @@ class Device:
}
"""

def __init__(self, json_node):
def __init__(self, json_node, rack_params):
self.id = json_node['ID']
# self.name = json_node['Name']
self.name = json_node['Hardware']
# self.category = json_node['Category']
self.category = json_node['Type']
self.height_in_u = int(json_node['HeightUnits'])
# self.width = json_node['Width']
self.width = json_node['LengthMm']
self.depth = json_node['Depth']
self.width = get_length_from_str(json_node['LengthMm'])
self.depth = get_length_from_str(json_node['Depth'])
self.height = get_length_from_str(json_node['Height'])

try:
self.height_in_u = int(json_node['HeightUnits'])
except ValueError as exc:
if self.height:
self.height_in_u = ceil((self.height+4)/rack_params.mounting_hole_spacing)
else:
raise RuntimeError("Not enough information provided to generate shelf height") from exc

self.shelf_id = json_node['ShelfId']
self.shelf_type = json_node['Shelf']

Expand All @@ -72,3 +81,12 @@ def shelf_builder_id(self):
#strip of -6 and optionally -s or -t
return match.group(1)
return "generic"

def get_length_from_str(length):
"""
Return the length in mm for a given string should be formatted as
"12.345 mm"
"""
if match := re.match(r'^([0-9]+(?:\.[0-9]+)?) ?mm$', length):
return float(match[1])
return None
32 changes: 32 additions & 0 deletions nimble_build_system/utils/gen_nimble_conf_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import sys
import json
import re

def usage():
"""
Expand Down Expand Up @@ -35,6 +36,11 @@ def main():
servers = []
switches = []
for device in devices:

if not shelf_available(device):
#If a shelf cannot be made for this item then skip it
continue

item = {'value': device['ID'],
'name': device['Brand']+" "+device['Hardware']}
if device['Type'] in ["Access Point", "Router + AP"]:
Expand Down Expand Up @@ -77,5 +83,31 @@ def main():
with open('OrchestratorConfigOptions.json', 'w', encoding="utf-8") as conf_file:
json.dump(conf_dict, conf_file)


def shelf_available(device):
"""
Return True if a shelf can be made for this device if not return False.
"""

#If the HeightUnit is specified, return True if it is an integer. False if specified
# but not understood
if device['HeightUnits']:
try:
int(device['HeightUnits'])
except ValueError:
print(f"Warning: Invalid data in HeightUnits feild for {device['ID']}")
return False
return True

# If HeightUnits is not set then check if Height is set and is a numerical value in mm
if device['Height']:
if re.match(r'^[0-9]+(?:\.[0-9]+)? ?mm$', device['Height']):
return True
print(f"Warning: Invalid data in Height feild for {device['ID']}: ({device['Height']})")
return False

# Neither Height or HeightUnits set. No shelf can be made.
return False

if __name__ == "__main__":
main()

0 comments on commit d6fd69b

Please sign in to comment.