kopia lustrzana https://github.com/projecthorus/pysondehub
run black and make output unbuffered
rodzic
b2e3f80549
commit
6302131f7c
|
@ -1,9 +1,11 @@
|
|||
import sondehub
|
||||
|
||||
|
||||
def on_message(message):
|
||||
print(message)
|
||||
|
||||
test = sondehub.Stream(sondes=["R3320848"], on_message=on_message)
|
||||
#test = sondehub.Stream(on_message=on_message)
|
||||
|
||||
#test = sondehub.Stream(sondes=["R3320848"], on_message=on_message)
|
||||
test = sondehub.Stream(on_message=on_message)
|
||||
while 1:
|
||||
pass
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "sondehub"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
description = "SDK to access SondeHub open data"
|
||||
authors = ["Michaela <git@michaela.lgbt>"]
|
||||
readme = "README.md"
|
||||
|
|
|
@ -3,7 +3,8 @@ from urllib.parse import urlparse
|
|||
import http.client
|
||||
import json
|
||||
|
||||
class Stream():
|
||||
|
||||
class Stream:
|
||||
def __init__(self, sondes: list = ["#"], on_connect=None, on_message=None):
|
||||
self.mqttc = mqtt.Client(transport="websockets")
|
||||
self._sondes = sondes
|
||||
|
@ -17,6 +18,7 @@ class Stream():
|
|||
(result, mid) = self.mqttc.subscribe(f"sondes/{sonde}", 0)
|
||||
if result != mqtt.MQTT_ERR_SUCCESS:
|
||||
self.ws_connect()
|
||||
|
||||
def remove_sonde(self, sonde):
|
||||
self._sondes.remove(sonde)
|
||||
(result, mid) = self.mqttc.unsubscribe(f"sondes/{sonde}", 0)
|
||||
|
@ -27,14 +29,16 @@ class Stream():
|
|||
url = self.get_url()
|
||||
urlparts = urlparse(url)
|
||||
headers = {
|
||||
"Host": "{0:s}".format(urlparts.netloc),
|
||||
"Host": "{0:s}".format(urlparts.netloc),
|
||||
}
|
||||
|
||||
self.mqttc.on_message = self._on_message # self.on_message
|
||||
|
||||
self.mqttc.on_message = self._on_message # self.on_message
|
||||
self.mqttc.on_connect = self._on_connect
|
||||
self.mqttc.on_disconnect = self._on_disconnect
|
||||
|
||||
self.mqttc.ws_set_options(path="{}?{}".format(urlparts.path, urlparts.query), headers=headers)
|
||||
self.mqttc.ws_set_options(
|
||||
path="{}?{}".format(urlparts.path, urlparts.query), headers=headers
|
||||
)
|
||||
try:
|
||||
self.mqttc.tls_set()
|
||||
except ValueError:
|
||||
|
@ -54,12 +58,13 @@ class Stream():
|
|||
def _on_message(self, mqttc, obj, msg):
|
||||
if self.on_message:
|
||||
self.on_message(json.loads(msg.payload))
|
||||
|
||||
def _on_connect(self, mqttc, obj, flags, rc):
|
||||
if mqtt.MQTT_ERR_SUCCESS != rc:
|
||||
self.ws_connect()
|
||||
if self.on_connect:
|
||||
self.on_connect()
|
||||
|
||||
|
||||
def _on_disconnect(self, client, userdata, rc):
|
||||
self.ws_connect()
|
||||
|
||||
|
|
|
@ -1,20 +1,39 @@
|
|||
import json
|
||||
import sondehub
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
unbuffered = os.fdopen(sys.stdout.fileno(), "wb", 0)
|
||||
|
||||
|
||||
def on_message(message):
|
||||
print(json.dumps(message))
|
||||
unbuffered.write(json.dumps(message).encode())
|
||||
unbuffered.write("\n".encode())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Sondehub CLI')
|
||||
parser.add_argument('--serial', dest="sondes", default=["#"],nargs="*", help="Filter to sonde serial", type=str, action="append")
|
||||
|
||||
parser = argparse.ArgumentParser(description="Sondehub CLI")
|
||||
parser.add_argument(
|
||||
"--serial",
|
||||
dest="sondes",
|
||||
default=["#"],
|
||||
nargs="*",
|
||||
help="Filter to sonde serial",
|
||||
type=str,
|
||||
action="append",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
if len(args.sondes) > 1: # we need to drop the default value if the user specifies sepcific sondes
|
||||
if (
|
||||
len(args.sondes) > 1
|
||||
): # we need to drop the default value if the user specifies sepcific sondes
|
||||
args.sondes = args.sondes[1:]
|
||||
sondes = [item for sublist in args.sondes for item in sublist]
|
||||
test = sondehub.Stream(on_message=on_message, sondes=sondes)
|
||||
while 1:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
|
Ładowanie…
Reference in New Issue