1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-04 16:01:14 -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"
app_gui = False
nb_devicesets = 0
nb_featuresets = 0
requests_methods = {
"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("-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("-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")
(options, args) = parser.parse_args()
@ -49,40 +48,23 @@ def getInputOptions():
def get_instance_details():
global app_gui
global nb_devicesets
global nb_featuresets
r = requests_methods["GET"](url=base_url)
if r.status_code // 100 == 2:
app_gui = r.json()["appname"] == "SDRangel"
nb_devicesets = r.json()["devicesetlist"]["devicesetcount"]
nb_featuresets = r.json()["featuresetlist"]["featuresetcount"]
# ======================================================================
def initialize():
global nb_devicesets
global nb_featuresets
for i_ds in reversed(range(nb_devicesets)):
if app_gui and i_ds == 0:
r_ds = requests_methods["GET"](url=f"{base_url}/deviceset/0")
if r_ds.status_code // 100 == 2:
nb_channels = r_ds.json()["channelcount"]
for i_chan in reversed(range(nb_channels)):
requests_methods["DELETE"](url=f"{base_url}/deviceset/0/channel/{i_chan}")
else:
r_del = requests_methods["DELETE"](url=f"{base_url}/deviceset")
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")
r_fs = requests_methods["GET"](url=f"{base_url}/featureset")
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
requests_methods["DELETE"](url=f"{base_url}/featureset/feature/{i_feat}")
# ======================================================================
def main():
@ -97,7 +79,6 @@ def main():
if options.initialize:
initialize()
nb_featureset_posts = 0
nb_deviceset_posts = 0
with open(options.json_file) as json_file:
@ -113,11 +94,6 @@ def main():
if nb_deviceset_posts == 1 and options.ignore_first_posts:
print("First deviceset creation ignored")
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
if method is not None:
request_params = command.get('params', None)

View File

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