1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-21 23:55:13 -05:00

ScriptsAPI: fixed dump.py and config.py to match v7 requirements

This commit is contained in:
f4exb 2022-08-16 14:53:02 +02:00
parent 0a2fd88ffd
commit 2c3930680f
2 changed files with 38 additions and 87 deletions

View File

@ -18,7 +18,6 @@ from optparse import OptionParser
base_url = "http://127.0.0.1:8091/sdrangel" base_url = "http://127.0.0.1:8091/sdrangel"
app_gui = False app_gui = False
nb_devicesets = 0 nb_devicesets = 0
nb_featuresets = 0
requests_methods = { requests_methods = {
"GET": requests.get, "GET": requests.get,
@ -35,7 +34,7 @@ def getInputOptions():
parser.add_option("-a", "--address", dest="address", help="Address and port. Default: 127.0.0.1:8091", metavar="ADDRESS", type="string") parser.add_option("-a", "--address", dest="address", help="Address and port. Default: 127.0.0.1:8091", metavar="ADDRESS", type="string")
parser.add_option("-j", "--json-file", dest="json_file", help="JSON file containing commands. Mandatory", metavar="FILE", type="string") parser.add_option("-j", "--json-file", dest="json_file", help="JSON file containing commands. Mandatory", metavar="FILE", type="string")
parser.add_option("-i", "--init", dest="initialize", help="Initialize instance before running script", action="store_true") parser.add_option("-i", "--init", dest="initialize", help="Initialize instance before running script", action="store_true")
parser.add_option("-1", "--ignore-first-posts", dest="ignore_first_posts", help="Ignore first deviceset or featureset post in sequence", action="store_true") parser.add_option("-1", "--ignore-first-posts", dest="ignore_first_posts", help="Ignore first deviceset post in sequence", action="store_true")
parser.add_option("-d", "--delay", dest="delay_ms", help="force delay after each command (ms)", metavar="TIME", type="int") parser.add_option("-d", "--delay", dest="delay_ms", help="force delay after each command (ms)", metavar="TIME", type="int")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
@ -49,40 +48,23 @@ def getInputOptions():
def get_instance_details(): def get_instance_details():
global app_gui global app_gui
global nb_devicesets global nb_devicesets
global nb_featuresets
r = requests_methods["GET"](url=base_url) r = requests_methods["GET"](url=base_url)
if r.status_code // 100 == 2: if r.status_code // 100 == 2:
app_gui = r.json()["appname"] == "SDRangel" app_gui = r.json()["appname"] == "SDRangel"
nb_devicesets = r.json()["devicesetlist"]["devicesetcount"] nb_devicesets = r.json()["devicesetlist"]["devicesetcount"]
nb_featuresets = r.json()["featuresetlist"]["featuresetcount"]
# ====================================================================== # ======================================================================
def initialize(): def initialize():
global nb_devicesets global nb_devicesets
global nb_featuresets
for i_ds in reversed(range(nb_devicesets)): for i_ds in reversed(range(nb_devicesets)):
if app_gui and i_ds == 0: r_del = requests_methods["DELETE"](url=f"{base_url}/deviceset")
r_ds = requests_methods["GET"](url=f"{base_url}/deviceset/0") if r_del.status_code // 100 == 2:
if r_ds.status_code // 100 == 2: nb_devicesets -= 1
nb_channels = r_ds.json()["channelcount"] r_fs = requests_methods["GET"](url=f"{base_url}/featureset")
for i_chan in reversed(range(nb_channels)): if r_fs.status_code // 100 == 2:
requests_methods["DELETE"](url=f"{base_url}/deviceset/0/channel/{i_chan}") nb_features = r_fs.json()["featurecount"]
else: for i_feat in reversed(range(nb_features)):
r_del = requests_methods["DELETE"](url=f"{base_url}/deviceset") requests_methods["DELETE"](url=f"{base_url}/featureset/feature/{i_feat}")
if r_del.status_code // 100 == 2:
nb_devicesets -= 1
for i_fs in reversed(range(nb_featuresets)):
if app_gui and i_fs == 0:
r_fs = requests_methods["GET"](url=f"{base_url}/featureset/0")
if r_fs.status_code // 100 == 2:
nb_features = r_fs.json()["featurecount"]
for i_feat in reversed(range(nb_features)):
requests_methods["DELETE"](url=f"{base_url}/featureset/0/feature/{i_feat}")
else:
r_del = requests_methods["DELETE"](url=f"{base_url}/featureset")
if r_del.status_code // 100 == 2:
nb_featuresets -= 1
# ====================================================================== # ======================================================================
def main(): def main():
@ -97,7 +79,6 @@ def main():
if options.initialize: if options.initialize:
initialize() initialize()
nb_featureset_posts = 0
nb_deviceset_posts = 0 nb_deviceset_posts = 0
with open(options.json_file) as json_file: with open(options.json_file) as json_file:
@ -113,11 +94,6 @@ def main():
if nb_deviceset_posts == 1 and options.ignore_first_posts: if nb_deviceset_posts == 1 and options.ignore_first_posts:
print("First deviceset creation ignored") print("First deviceset creation ignored")
continue continue
if endpoint == "/featureset" and http_method == "POST":
nb_featureset_posts += 1
if nb_featureset_posts == 1 and options.ignore_first_posts:
print("First featureset creation ignored")
continue
method = requests_methods.get(http_method, None) if http_method is not None else None method = requests_methods.get(http_method, None) if http_method is not None else None
if method is not None: if method is not None:
request_params = command.get('params', None) request_params = command.get('params', None)

View File

@ -22,18 +22,13 @@ base_url = "http://127.0.0.1:8091/sdrangel"
class Instance: class Instance:
def __init__(self): def __init__(self):
self.devicesets = [] self.devicesets = []
self.featuresets = [] self.features = []
def add_deviceset(self, json_data): def add_deviceset(self, json_data):
ds = DeviceSet(len(self.devicesets), json_data["direction"], json_data["deviceHwType"], json_data) ds = DeviceSet(len(self.devicesets), json_data["direction"], json_data["deviceHwType"], json_data)
self.devicesets.append(ds) self.devicesets.append(ds)
return ds return ds
def add_featureset(self, json_data):
fs = FeatureSet(len(self.featuresets))
self.featuresets.append(fs)
return fs
def add_deviceset_item(self, direction): def add_deviceset_item(self, direction):
return { return {
"endpoint": "/deviceset", "endpoint": "/deviceset",
@ -42,12 +37,21 @@ class Instance:
"msg": f"Add device set" "msg": f"Add device set"
} }
def add_featureset_item(self): def add_feature(self, json_data):
return { feat = Feature(len(self.features), json_data["featureType"], json_data)
"endpoint": "/featureset", self.features.append(feat)
"method": "POST", return feat
"msg": f"Add feature set"
} def delete_all_features_items(self):
nb_features = len(self.features)
items = []
for i_feat in reversed(range(nb_features)):
items.append({
"endpoint": f"/featureset/{self.index}/feature/{i_feat}",
"method": "DELETE",
"msg": f"Delete feature at index {i_feat} in feature set {self.index}"
})
return items
def get_config_items(self): def get_config_items(self):
items = [] items = []
@ -59,11 +63,9 @@ class Instance:
items.append(channel.add_channel_item()) items.append(channel.add_channel_item())
items.append(channel.set_channel_item()) items.append(channel.set_channel_item())
items.append(deviceset.set_spectrum_item()) items.append(deviceset.set_spectrum_item())
for i_fs, featureset in enumerate(self.featuresets): for feature in self.features:
items.append(self.add_featureset_item()) items.append(feature.add_feature_item())
for feature in featureset.features: items.append(feature.set_feature_item())
items.append(feature.add_feature_item())
items.append(feature.set_feature_item())
return items return items
# ====================================================================== # ======================================================================
@ -150,52 +152,30 @@ class Channel:
"msg": f"Setup channel {self.type} at {self.index} in device set {self.ds_index}" "msg": f"Setup channel {self.type} at {self.index} in device set {self.ds_index}"
} }
# ======================================================================
class FeatureSet:
def __init__(self, index):
self.index = index
self.features = []
def add_feature(self, json_data):
feat = Feature(self.index, len(self.features), json_data["featureType"], json_data)
self.features.append(feat)
return feat
def delete_all_features_items(self):
nb_features = len(self.features)
items = []
for i_feat in reversed(range(nb_features)):
items.append({
"endpoint": f"/featureset/{self.index}/feature/{i_feat}",
"method": "DELETE",
"msg": f"Delete feature at index {i_feat} in feature set {self.index}"
})
return items
# ====================================================================== # ======================================================================
class Feature: class Feature:
def __init__(self, fs_index, index, type, settings): def __init__(self, index, type, settings):
self.fs_index = fs_index
self.index = index self.index = index
self.type = type self.type = type
self.feature_settings = settings self.feature_settings = settings
def add_feature_item(self): def add_feature_item(self):
return { return {
"endpoint": f"/featureset/{self.fs_index}/feature", "endpoint": f"/featureset/feature",
"method": "POST", "method": "POST",
"payload": { "payload": {
"featureType": self.type "featureType": self.type
}, },
"msg": f"Add feature {self.type} in feature set index {self.fs_index}" "msg": f"Add feature {self.type} in feature set"
} }
def set_feature_item(self): def set_feature_item(self):
return { return {
"endpoint": f"/featureset/{self.fs_index}/feature/{self.index}/settings", "endpoint": f"/featureset/feature/{self.index}/settings",
"method": "PUT", "method": "PUT",
"payload": self.feature_settings, "payload": self.feature_settings,
"msg": f"Setup feature {self.type} at {self.index} in feature set {self.fs_index}" "msg": f"Setup feature {self.type} at {self.index} in feature set"
} }
@ -235,16 +215,11 @@ def dump():
r_spec = requests.get(url=f"{base_url}/deviceset/{i_deviceset}/spectrum/settings") r_spec = requests.get(url=f"{base_url}/deviceset/{i_deviceset}/spectrum/settings")
if r_spec.status_code // 100 == 2: if r_spec.status_code // 100 == 2:
ds.set_spectrum_settings(r_spec.json()) ds.set_spectrum_settings(r_spec.json())
nb_featuresets = r.json()["featuresetlist"]["featuresetcount"] nb_features = r.json()["featureset"]["featurecount"]
for i_featureset in range(nb_featuresets): for i_feature in range(nb_features):
r_fs = requests.get(url=f"{base_url}/featureset/{i_featureset}") r_feat = requests.get(url=f"{base_url}/featureset/feature/{i_feature}/settings")
if r_fs.status_code // 100 == 2: if r_feat.status_code // 100 == 2:
nb_features = r_fs.json()["featurecount"] instance.add_feature(r_feat.json())
fs = instance.add_featureset(r_fs.json())
for i_feature in range(nb_features):
r_feat = requests.get(url=f"{base_url}/featureset/{i_featureset}/feature/{i_feature}/settings")
if r_feat.status_code // 100 == 2:
fs.add_feature(r_feat.json())
return instance return instance