mirror of
https://codeberg.org/SiB64/turbo_weather.git
synced 2026-06-30 08:19:50 +02:00
Compare commits
No commits in common. "42fb68de4dadb99c9513ac361686d58f973d0d49" and "fc87dd7eada3ed63f7ddceb3c2fb3e2c21e0846c" have entirely different histories.
42fb68de4d
...
fc87dd7ead
3 changed files with 5 additions and 169 deletions
135
src/cmdsocket.py
135
src/cmdsocket.py
|
|
@ -1,135 +0,0 @@
|
||||||
#! /usr/bin/python3
|
|
||||||
|
|
||||||
import sys, os, socket
|
|
||||||
|
|
||||||
class cmd_socket:
|
|
||||||
|
|
||||||
def __init__(self, path=".cmd.socket", **a):
|
|
||||||
self.s = None
|
|
||||||
self.c = []
|
|
||||||
self.path = None
|
|
||||||
if path:
|
|
||||||
self.open(path, **a)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
for c in self.c:
|
|
||||||
c.close()
|
|
||||||
self.c = []
|
|
||||||
self.s.close()
|
|
||||||
self.s = None
|
|
||||||
if self.path:
|
|
||||||
os.unlink(self.path)
|
|
||||||
|
|
||||||
class cmd_receiver(cmd_socket):
|
|
||||||
|
|
||||||
def open(self, path, blocking=False, force=False):
|
|
||||||
if force:
|
|
||||||
try:
|
|
||||||
os.unlink(path)
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
self.blocking=blocking
|
|
||||||
if self.s:
|
|
||||||
raise ValueError("socket is not closed")
|
|
||||||
self.path = path
|
|
||||||
self.s = socket.socket(socket.AF_UNIX)
|
|
||||||
self.s.bind(path)
|
|
||||||
self.s.listen()
|
|
||||||
self.s.setblocking(blocking)
|
|
||||||
self.c = []
|
|
||||||
self.m = b''
|
|
||||||
|
|
||||||
def poll(self):
|
|
||||||
if not self.s:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
c = self.s.accept()[0]
|
|
||||||
self.c.append(c)
|
|
||||||
c.setblocking(self.blocking)
|
|
||||||
self.s.setblocking(False)
|
|
||||||
except BlockingIOError:
|
|
||||||
pass
|
|
||||||
cc = False
|
|
||||||
for i, c in enumerate(self.c):
|
|
||||||
try:
|
|
||||||
m = c.recv(4096)
|
|
||||||
if not m:
|
|
||||||
c.close()
|
|
||||||
self.c[i] = None
|
|
||||||
cc = True
|
|
||||||
else:
|
|
||||||
self.exec(m)
|
|
||||||
except BlockingIOError:
|
|
||||||
pass
|
|
||||||
if cc:
|
|
||||||
self.c = [c for c in self.c if c]
|
|
||||||
if not self.c:
|
|
||||||
self.s.setblocking(self.blocking)
|
|
||||||
|
|
||||||
def exec(self, m):
|
|
||||||
if self.m:
|
|
||||||
m = self.m + m
|
|
||||||
mm = m.split(b'\n')
|
|
||||||
for m in mm[:-1]:
|
|
||||||
self.msg(m)
|
|
||||||
self.tail(mm[-1])
|
|
||||||
|
|
||||||
def tail(self, m):
|
|
||||||
self.m = m
|
|
||||||
if m:
|
|
||||||
print(f"socket {self.path}: got tail {m}", file=sys.stderr)
|
|
||||||
|
|
||||||
def msg(self, m):
|
|
||||||
print(f"socket {self.path}: got message {m}", file=sys.stderr)
|
|
||||||
|
|
||||||
class cmder(cmd_socket):
|
|
||||||
|
|
||||||
def open(self, path):
|
|
||||||
if self.s:
|
|
||||||
raise ValueError("socket is not closed")
|
|
||||||
self.s = socket.socket(socket.AF_UNIX)
|
|
||||||
self.s.connect(path)
|
|
||||||
|
|
||||||
def write(self, m, eol=b'\n'):
|
|
||||||
if isinstance(m, str):
|
|
||||||
m = m.encode()
|
|
||||||
if eol and m[-1:] != eol:
|
|
||||||
m += eol
|
|
||||||
self.s.send(m)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
import getopt, time
|
|
||||||
options, messages = getopt.gnu_getopt(sys.argv[1:], "s:d:rfn")
|
|
||||||
path = ()
|
|
||||||
delay = 0.1
|
|
||||||
receiver = False
|
|
||||||
force = False
|
|
||||||
eol = b'\n'
|
|
||||||
for o,v in options:
|
|
||||||
if o=="-s":
|
|
||||||
path = (v,)
|
|
||||||
if o=="-d":
|
|
||||||
delay = float(v)
|
|
||||||
if o=="-r":
|
|
||||||
receiver = True
|
|
||||||
if o=="-f":
|
|
||||||
force = True
|
|
||||||
if o=="-n":
|
|
||||||
eol = None
|
|
||||||
|
|
||||||
if receiver:
|
|
||||||
s = cmd_receiver(*path, blocking=True, force=force)
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
s.poll()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
s.close()
|
|
||||||
return
|
|
||||||
s = cmder(*path)
|
|
||||||
for m in messages:
|
|
||||||
if delay:
|
|
||||||
time.sleep(delay)
|
|
||||||
s.write(m, eol)
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
|
||||||
main()
|
|
||||||
|
|
@ -12,7 +12,7 @@ class ntc:
|
||||||
The NTC is biases via resistor R₁ from Voltage V₁.
|
The NTC is biases via resistor R₁ from Voltage V₁.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
β = 3940.0 # K
|
β = 3695.0 # K
|
||||||
T25 = 298.16 # K
|
T25 = 298.16 # K
|
||||||
T0 = 273.16 # K
|
T0 = 273.16 # K
|
||||||
R25 = 10 # kΩ
|
R25 = 10 # kΩ
|
||||||
|
|
|
||||||
37
src/turbo.py
37
src/turbo.py
|
|
@ -1,13 +1,12 @@
|
||||||
#! /usr/bin/python3
|
#! /usr/bin/python3
|
||||||
|
|
||||||
import sys, time, getopt, serial, fileinput
|
import sys, time, getopt, serial, fileinput
|
||||||
import pressure, ntc, linear_regression, cmdsocket
|
import pressure, ntc, linear_regression
|
||||||
|
|
||||||
options, files = getopt.gnu_getopt(sys.argv[1:], "xF:s:o:c", ["tty=", "noise", "clock", "socket=", "output="])
|
options, files = getopt.gnu_getopt(sys.argv[1:], "xF:C", ["tty=", "noise", "clock"])
|
||||||
|
|
||||||
tty = None
|
tty = None
|
||||||
socket = None
|
out = sys.stdout
|
||||||
out = None
|
|
||||||
|
|
||||||
do_noise = False
|
do_noise = False
|
||||||
do_clock = False
|
do_clock = False
|
||||||
|
|
@ -19,28 +18,12 @@ for o,v in options:
|
||||||
tty = v
|
tty = v
|
||||||
do_clock = True
|
do_clock = True
|
||||||
|
|
||||||
if o in "--clock":
|
if o in "-C --clock":
|
||||||
do_clock = True
|
do_clock = True
|
||||||
|
|
||||||
if o in "-x --noise":
|
if o in "-x --noise":
|
||||||
do_noise = True
|
do_noise = True
|
||||||
|
|
||||||
if o in "--socket":
|
|
||||||
socket = v
|
|
||||||
|
|
||||||
if o in "--output":
|
|
||||||
if out:
|
|
||||||
raise ValueError("cannot have multiple --outputs")
|
|
||||||
if v=="-":
|
|
||||||
out = sys.stdout
|
|
||||||
elif v=="--":
|
|
||||||
out = sys.stderr
|
|
||||||
else:
|
|
||||||
out = open(v, "a")
|
|
||||||
|
|
||||||
if not out:
|
|
||||||
out = sys.stdout
|
|
||||||
|
|
||||||
if tty and files:
|
if tty and files:
|
||||||
raise ValueError("cannot do tty and files")
|
raise ValueError("cannot do tty and files")
|
||||||
|
|
||||||
|
|
@ -164,17 +147,6 @@ processes = {
|
||||||
b'X': echo,
|
b'X': echo,
|
||||||
}
|
}
|
||||||
|
|
||||||
class batecmd(cmdsocket.cmd_receiver):
|
|
||||||
def msg(self, m):
|
|
||||||
echo(b'r '+m)
|
|
||||||
inp.write(b'\n' + m + b'\n')
|
|
||||||
|
|
||||||
cmd = batecmd(path=None)
|
|
||||||
if socket:
|
|
||||||
if not tty:
|
|
||||||
raise ValueError("cannot have socket without tty")
|
|
||||||
cmd.open(socket, force=True)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
line = inp.readline()
|
line = inp.readline()
|
||||||
|
|
@ -199,4 +171,3 @@ while True:
|
||||||
if tty:
|
if tty:
|
||||||
out.flush()
|
out.flush()
|
||||||
|
|
||||||
cmd.poll()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue