mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-05-29 21:42:26 -04:00
REST API Python examples: source formatting and exception Python3 compatibility
This commit is contained in:
parent
7e97f62615
commit
5dfc60331c
@ -5,6 +5,7 @@ from optparse import OptionParser
|
||||
|
||||
base_url = "http://127.0.0.1:8091/sdrangel"
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -43,7 +44,7 @@ def main():
|
||||
print("Error adding channel. HTTP: %d" % r.status_code)
|
||||
print json.dumps(r.json(), indent=4, sort_keys=True)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -73,7 +73,7 @@ def main():
|
||||
|
||||
print("All done!")
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -13,6 +13,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -30,6 +31,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -39,6 +41,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -53,6 +56,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -109,7 +113,7 @@ def main():
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -13,6 +13,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -26,6 +27,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -35,6 +37,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -49,6 +52,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -86,7 +90,7 @@ def main():
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -16,6 +16,7 @@ from optparse import OptionParser
|
||||
|
||||
base_url = "http://127.0.0.1:8091/sdrangel"
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -34,6 +35,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def startDevice(deviceIndex):
|
||||
dev_run_url = base_url + ("/deviceset/%d/device/run" % deviceIndex)
|
||||
@ -55,6 +57,7 @@ def startDevice(deviceIndex):
|
||||
else:
|
||||
print("Error getting device %d running state" % deviceIndex)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def stopDevice(deviceIndex):
|
||||
dev_run_url = base_url + ("/deviceset/%d/device/run" % deviceIndex)
|
||||
@ -76,6 +79,7 @@ def stopDevice(deviceIndex):
|
||||
else:
|
||||
print("Error getting device %d running state" % deviceIndex)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setFocus(deviceIndex):
|
||||
dev_focus_url = base_url + ("/deviceset/%d/focus" % deviceIndex)
|
||||
@ -87,6 +91,7 @@ def setFocus(deviceIndex):
|
||||
else:
|
||||
print("Error setting focus on device set %d" % deviceIndex)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -122,7 +127,7 @@ def main():
|
||||
else:
|
||||
print("Error getting device sets configuration")
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -11,6 +11,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
def getHwType():
|
||||
r = requests.get(url=base_url + "/deviceset/0")
|
||||
if r.status_code / 100 == 2:
|
||||
@ -23,6 +24,7 @@ def getHwType():
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def selectRtlSdr():
|
||||
r = requests.put(url=base_url + "/deviceset/0/device", json={"hwType": "RTLSDR"})
|
||||
if r.status_code / 100 == 2:
|
||||
@ -31,6 +33,7 @@ def selectRtlSdr():
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def getRtlSdrSettings():
|
||||
r = requests.get(url=base_url + "/deviceset/0/device/settings")
|
||||
if r.status_code / 100 == 2:
|
||||
@ -44,6 +47,7 @@ def getRtlSdrSettings():
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def patchRtlSdrSettings(settings):
|
||||
new_settings = {"deviceHwType": "RTLSDR", "tx": 0, "rtlSdrSettings": settings}
|
||||
r = requests.patch(url=base_url + "/deviceset/0/device/settings", json=new_settings)
|
||||
@ -52,6 +56,7 @@ def patchRtlSdrSettings(settings):
|
||||
else:
|
||||
print "Error HTTP:", r.status_code
|
||||
|
||||
|
||||
def deviceRun(run):
|
||||
if run:
|
||||
r = requests.post(url=base_url + "/deviceset/0/device/run")
|
||||
@ -83,4 +88,3 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@ -358,10 +358,7 @@ def main():
|
||||
time.sleep(1)
|
||||
setup_audio(options)
|
||||
|
||||
# if options.channel_id == "BFMDemod":
|
||||
# channelsReport(deviceset_url)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -13,6 +13,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -59,6 +60,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -68,6 +70,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -82,6 +85,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -244,8 +248,7 @@ def main():
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -28,8 +28,10 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
class ScanControl:
|
||||
|
||||
def __init__(self, num_channels, channel_step, start_freq, stop_freq, log2_decim):
|
||||
self.channel_shifts = []
|
||||
if num_channels < 2:
|
||||
@ -43,6 +45,7 @@ class ScanControl:
|
||||
self.device_step_freq = 2 * limit + channel_step
|
||||
self.device_sample_rate = (2 * limit + channel_step) * (1 << log2_decim)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -95,6 +98,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setupDevice(scan_control, options):
|
||||
settings = callAPI(deviceset_url + "/device/settings", "GET", None, None, "Get device settings")
|
||||
@ -149,6 +153,7 @@ def setupDevice(scan_control, options):
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def changeDeviceFrequency(fc, options):
|
||||
settings = callAPI(deviceset_url + "/device/settings", "GET", None, None, "Get device settings")
|
||||
@ -168,6 +173,7 @@ def changeDeviceFrequency(fc, options):
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setupChannels(scan_control, options):
|
||||
i = 0
|
||||
@ -212,6 +218,7 @@ def setupChannels(scan_control, options):
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def checkScanning(fc, options, display_message):
|
||||
reports = callAPI(deviceset_url + "/channels/report", "GET", None, None, "Get channels report")
|
||||
@ -235,6 +242,7 @@ def checkScanning(fc, options, display_message):
|
||||
return False # stop scanning
|
||||
return True # continue scanning
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -244,6 +252,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -262,6 +271,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -361,7 +371,7 @@ def main():
|
||||
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -5,6 +5,7 @@ from optparse import OptionParser
|
||||
|
||||
base_url = "http://127.0.0.1:8091/sdrangel"
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -32,6 +33,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def startDevice(deviceIndex):
|
||||
dev_run_url = base_url + ("/deviceset/%d/device/run" % deviceIndex)
|
||||
@ -53,6 +55,7 @@ def startDevice(deviceIndex):
|
||||
else:
|
||||
print("Error getting device %d running state" % deviceIndex)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def stopDevice(deviceIndex):
|
||||
dev_run_url = base_url + ("/deviceset/%d/device/run" % deviceIndex)
|
||||
@ -74,6 +77,7 @@ def stopDevice(deviceIndex):
|
||||
else:
|
||||
print("Error getting device %d running state" % deviceIndex)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -97,7 +101,7 @@ def main():
|
||||
else:
|
||||
print("Error getting device sets configuration")
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -13,6 +13,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -26,6 +27,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -35,6 +37,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -49,6 +52,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def main():
|
||||
try:
|
||||
@ -61,7 +65,7 @@ def main():
|
||||
if settings is None:
|
||||
exit(-1)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
@ -14,6 +14,7 @@ requests_methods = {
|
||||
"DELETE": requests.delete
|
||||
}
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def getInputOptions():
|
||||
|
||||
@ -64,6 +65,7 @@ def getInputOptions():
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def printResponse(response):
|
||||
content_type = response.headers.get("Content-Type", None)
|
||||
@ -73,6 +75,7 @@ def printResponse(response):
|
||||
elif "text/plain" in content_type:
|
||||
print(response.text)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def callAPI(url, method, params, json, text):
|
||||
request_method = requests_methods.get(method, None)
|
||||
@ -87,6 +90,7 @@ def callAPI(url, method, params, json, text):
|
||||
printResponse(r)
|
||||
return None
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setupBladeRFXB200(fc):
|
||||
if fc < 50000:
|
||||
@ -104,6 +108,7 @@ def setupBladeRFXB200(fc):
|
||||
else:
|
||||
return 5 # BLADERF_XB200_AUTO_3DB
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setupDevice(options):
|
||||
settings = callAPI(deviceset_url + "/device/settings", "GET", None, None, "Get device settings")
|
||||
@ -158,6 +163,7 @@ def setupDevice(options):
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
|
||||
# ======================================================================
|
||||
def setupChannel(options):
|
||||
r = callAPI(deviceset_url + "/channel", "POST", None, {"channelType": options.channel_id, "tx": 1}, "Create modulator")
|
||||
@ -278,7 +284,7 @@ def main():
|
||||
if r is None:
|
||||
exit(-1)
|
||||
|
||||
except Exception, msg:
|
||||
except Exception as ex:
|
||||
tb = traceback.format_exc()
|
||||
print >> sys.stderr, tb
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user