graph/test/test.py

83 lines
1.5 KiB
Python
Raw Normal View History

2024-05-15 20:52:13 +00:00
#!/bin/env python3
import requests
from math import sin
from time import sleep
2024-05-16 23:43:31 +00:00
import numpy as np
2024-05-17 19:14:05 +00:00
import pyaudio
2024-05-15 20:52:13 +00:00
2024-05-16 23:43:31 +00:00
def sine():
2024-05-15 20:52:13 +00:00
x = 0.0
while True:
requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={x}&y={sin(x)}")
x += 0.01
sleep(0.01)
2024-05-16 23:43:31 +00:00
def sine_array():
2024-05-17 14:24:47 +00:00
start = 0
while True:
x = np.linspace(start, start+0.5, 100)
y = np.sin(x)
start += 0.5
2024-05-16 23:43:31 +00:00
2024-05-17 14:24:47 +00:00
data = {
"chart": 0,
"series": "R1",
"points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ]
}
2024-05-16 23:43:31 +00:00
2024-05-17 14:24:47 +00:00
r = requests.post("http://0.0.0.0:8080/add", json=data, headers={"Connection":"close"})
sleep(0.25)
2024-05-16 23:43:31 +00:00
def star():
points = [
(2,2),
(3,0),
(0,1),
(4,1),
(1,0),
(2,2)
]
for point in points:
requests.get(f"http://localhost:8080/add?chart=0&series=R1&x={point[0]}&y={point[1]}")
2024-05-17 19:14:05 +00:00
def audio():
p = pyaudio.PyAudio()
stream = p.open(
input=True,
input_device_index=0,
format=pyaudio.paInt16,
channels=1,
rate=44100,
frames_per_buffer=1024
)
stream.start_stream()
while True:
try:
data = stream.read(1024, False)
requests.post(
"http://0.0.0.0:8080/add",
json={
"chart": 0,
"series": "audio",
"points": [ {"x": i, "y": v} for (i, v) in enumerate(data) ]
},
headers={"Connection":"close"})
except KeyboardInterrupt:
break
stream.stop_stream()
stream.close()
p.terminate()
2024-05-16 23:43:31 +00:00
def main():
#sine()
2024-05-17 19:14:05 +00:00
# sine_array()
2024-05-17 14:24:47 +00:00
#star()
2024-05-17 19:14:05 +00:00
audio()
2024-05-15 20:52:13 +00:00
if __name__ == "__main__":
2024-05-16 23:43:31 +00:00
main()