progress
This commit is contained in:
commit
147f4987e6
|
@ -0,0 +1 @@
|
|||
name = "dagon_planner"
|
|
@ -0,0 +1,5 @@
|
|||
class Cmdline:
|
||||
def __init__(self, s):
|
||||
self.a = s.split("\t")
|
||||
self.start = 1 if self.a[0][0] == ":" else 0
|
||||
self.id = "" if self.start == 0 else self.a[0]
|
|
@ -0,0 +1,81 @@
|
|||
from cmdline import Cmdline
|
||||
|
||||
monoid = 0
|
||||
|
||||
def genid():
|
||||
monoid = monoid + 1
|
||||
return f":{monoid}"
|
||||
|
||||
class Command:
|
||||
def write_start(self):
|
||||
return "" if self.id is None else f"{self.id}\t"
|
||||
def exec(self):
|
||||
pass
|
||||
|
||||
# A set of ids that the command releases
|
||||
def releasing(self):
|
||||
return set()
|
||||
# A set of channels the command accesses
|
||||
def accessing(self):
|
||||
return set()
|
||||
# A dictionary of (id, channel) associated with the command
|
||||
def getids(self):
|
||||
pass
|
||||
def __init__(self, id, is_async):
|
||||
self.id = genid() if id is None else id
|
||||
self.is_async = is_async
|
||||
|
||||
class WaitCommand(Command):
|
||||
def exec(self):
|
||||
ids = self.ids
|
||||
while len(ids) > 0:
|
||||
x = self.getline()
|
||||
if x.id in ids:
|
||||
ids.remove(x.id)
|
||||
|
||||
return None
|
||||
|
||||
def releasing(self):
|
||||
return self.ids
|
||||
|
||||
def __init__(self, ids, getline):
|
||||
Command.__init__(self, None, False)
|
||||
self.ids = ids
|
||||
self.getline = getline
|
||||
|
||||
class RampCommand(Command):
|
||||
def exec(self):
|
||||
s = f"{self.write_start()}RAMP"
|
||||
for (ch, start, step, end, interval) in self.listing:
|
||||
s = s + f"\t{ch}\t{end}\t{start}\t{interval}\t{step}"
|
||||
s = s + "\n"
|
||||
return s
|
||||
|
||||
def getids(self):
|
||||
d = {}
|
||||
for c in self.listing:
|
||||
d[f"{self.id}.{c[0]}"] = c[0]
|
||||
return d
|
||||
|
||||
def accessing(self):
|
||||
return self.listing
|
||||
|
||||
def __init__(self, id, listing):
|
||||
Command.__init__(self, id, True)
|
||||
self.listing = listing
|
||||
|
||||
class SetCommand(Command):
|
||||
def exec(self):
|
||||
s = f"{self.write_start()}SET\t{self.ch}\t{self.val}\n"
|
||||
return set()
|
||||
|
||||
def getids(self):
|
||||
return {self.id : self.ch}
|
||||
|
||||
def accessing(self):
|
||||
return set([self.val])
|
||||
|
||||
def __init__(self, ser, id, ch, val):
|
||||
Command.__init__(self, id, False)
|
||||
self.ch = ch
|
||||
self.val = val
|
|
@ -0,0 +1,33 @@
|
|||
import cmds
|
||||
from serial import Serial
|
||||
|
||||
class InUseException(Exception):
|
||||
def __str__(self):
|
||||
return f"{self.chs} is already used in plan"
|
||||
def __init__(self, chs):
|
||||
self.chs = chs
|
||||
|
||||
class Planner:
|
||||
def exec(self):
|
||||
for c in self.cmds:
|
||||
s = c.exec()
|
||||
if s is not None:
|
||||
self.ser.write(bytes(s, "UTF-8"))
|
||||
|
||||
def add(self, cmd):
|
||||
acc = cmd.accessing()
|
||||
|
||||
if len(acc) > 0:
|
||||
conflict = self.used & acc
|
||||
if len(conflict) > 0:
|
||||
raise InUseException(conflict)
|
||||
if cmd.is_async:
|
||||
self.used |= acc
|
||||
|
||||
self.cmds.append(cmd)
|
||||
|
||||
def __init__(self):
|
||||
self.cmds = []
|
||||
self.used = set()
|
||||
self.waiting_on = dict()
|
||||
self.ser = Serial()
|
Reference in New Issue