Commiting state after implementing gating etc before cleaning and refactoring
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
@ -10,6 +11,13 @@ from random import randrange
|
||||
HOST = "127.0.0.1" # Localhost
|
||||
PORT = int(sys.argv[1]) # Port to listen on
|
||||
TOTAL_CH = int(sys.argv[2]) # Number of Channels
|
||||
LOG2FILE = False if len(sys.argv) < 4 else bool(int(sys.argv[3]))
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('counterbox')
|
||||
|
||||
if LOG2FILE:
|
||||
logging.basicConfig(filename=os.path.join(os.getcwd(), 'counterbox_sim.log'), level=logging.INFO)
|
||||
|
||||
|
||||
class CounterBox:
|
||||
@ -26,43 +34,106 @@ class CounterBox:
|
||||
self.elapsed = 0
|
||||
self.monitor = 0
|
||||
|
||||
def resetCounts(self):
|
||||
self.minratechannel = 0
|
||||
self.minrates = [2] * self.total_channels
|
||||
|
||||
self.gate_config = [
|
||||
#(enabled, count high/low),
|
||||
( False, True),
|
||||
( False, True),
|
||||
]
|
||||
|
||||
self.gate = [
|
||||
# high/low
|
||||
False,
|
||||
False,
|
||||
]
|
||||
|
||||
def clearCount(self, counter):
|
||||
self.counts[counter-1] = 0
|
||||
|
||||
def clearCounts(self):
|
||||
self.counts = [0] * self.total_channels
|
||||
self.starttime = time.time()
|
||||
|
||||
def clearTime(self):
|
||||
self.elapsed = 0
|
||||
self.starttime = time.time()
|
||||
|
||||
def getStatus(self):
|
||||
return self.status
|
||||
|
||||
def getCount(self, channel):
|
||||
return self.counts[channel - 1]
|
||||
|
||||
def getCounts(self):
|
||||
return self.counts
|
||||
# The sinqtest box returns a maximum of 8
|
||||
return self.counts[0:min(len(self.counts), 8)]
|
||||
|
||||
def getMonitorCount(self):
|
||||
return self.counts[self.monitor]
|
||||
|
||||
def updateCounts(self):
|
||||
def updateRates(self):
|
||||
for i in range(self.total_channels):
|
||||
self.rates[i] = randrange(5)
|
||||
|
||||
def updateCounts(self):
|
||||
for i in range(self.total_channels):
|
||||
self.counts[i] += self.rates[i]
|
||||
|
||||
def getRunTime(self):
|
||||
elapsed = round(time.time() - self.starttime, 3)
|
||||
self.updateRates()
|
||||
|
||||
# If gating and a low rate threshold are enabled, then the threshold
|
||||
# seems to have precedence and sets the status to 5.
|
||||
# If we are waiting on the gate, then the status is just 1 as normal
|
||||
# after having started a count.
|
||||
|
||||
if self.countmode == 'time':
|
||||
if elapsed < self.presettime:
|
||||
|
||||
if self.minratechannel >= 0 and self.rates[self.minratechannel] < self.minrates[self.minratechannel]:
|
||||
# adjust the starttime, so that it is as if this polling period didn't happen
|
||||
self.starttime += elapsed - self.elapsed
|
||||
self.status = 5
|
||||
return self.elapsed
|
||||
|
||||
self.status = 1
|
||||
|
||||
for i in range(len(self.gate)):
|
||||
# If the gating is enabled and the signal is in the active position
|
||||
if self.gate_config[i][0] and self.gate_config[i][1] != self.gate[i]:
|
||||
self.starttime += elapsed - self.elapsed
|
||||
return self.elapsed
|
||||
|
||||
self.updateCounts()
|
||||
self.elapsed = elapsed
|
||||
else:
|
||||
self.elapsed = self.presettime
|
||||
self.elapsed = self.presettime if self.presettime > 0 else self.elapsed
|
||||
self.status = 0
|
||||
|
||||
elif self.countmode == 'count':
|
||||
if self.getMonitorCount() < self.presetcount:
|
||||
|
||||
if self.minratechannel >= 0 and self.rates[self.minratechannel] < self.minrates[self.minratechannel]:
|
||||
# adjust the starttime, so that it is as if this polling period didn't happen
|
||||
self.starttime += elapsed - self.elapsed
|
||||
self.status = 5
|
||||
return self.elapsed
|
||||
|
||||
self.status = 1
|
||||
|
||||
for i in range(len(self.gate)):
|
||||
# If the gating is enabled and the signal is in the active position
|
||||
if self.gate_config[i][0] and self.gate_config[i][1] != self.gate[i]:
|
||||
self.starttime += elapsed - self.elapsed
|
||||
return self.elapsed
|
||||
|
||||
self.updateCounts()
|
||||
self.elapsed = elapsed
|
||||
|
||||
if self.getMonitorCount() >= self.presetcount:
|
||||
self.counts[self.monitor] = self.presetcount
|
||||
self.counts[self.monitor] = self.presetcount if self.presetcount > 0 else self.counts[self.monitor]
|
||||
self.status = 0
|
||||
|
||||
else:
|
||||
@ -73,18 +144,22 @@ class CounterBox:
|
||||
def stop(self):
|
||||
self.getRunTime()
|
||||
self.status = 0
|
||||
self.presettime = 0
|
||||
self.presetcount = 0
|
||||
|
||||
def startTimePreset(self, presettime):
|
||||
self.countmode = 'time'
|
||||
self.status = 1
|
||||
self.presettime = round(presettime, 3)
|
||||
self.resetCounts()
|
||||
self.clearTime()
|
||||
self.clearCounts()
|
||||
|
||||
def startCountPreset(self, presetcount):
|
||||
self.countmode = 'count'
|
||||
self.status = 1
|
||||
self.presetcount = presetcount
|
||||
self.resetCounts()
|
||||
self.clearTime()
|
||||
self.clearCounts()
|
||||
|
||||
def setMonitorChannel(self, channel):
|
||||
self.monitor = channel - 1
|
||||
@ -94,6 +169,28 @@ class CounterBox:
|
||||
|
||||
def getRate(self, channel):
|
||||
return float(self.rates[channel - 1])
|
||||
|
||||
def getMinRateChannel(self):
|
||||
return self.minratechannel + 1
|
||||
|
||||
def setMinRateChannel(self, channel):
|
||||
self.minratechannel = channel - 1
|
||||
|
||||
def getMinRate(self, channel):
|
||||
return self.minrates[channel - 1]
|
||||
|
||||
def setMinRate(self, channel, rate):
|
||||
self.minrates[channel - 1] = rate
|
||||
|
||||
def getGateStatus(self, channel):
|
||||
return self.gate_config[channel - 1]
|
||||
|
||||
def setGateStatus(self, channel, enable, highlow):
|
||||
self.gate_config[channel - 1] = (enable, highlow)
|
||||
|
||||
def setGate(self, channel, highlow):
|
||||
self.gate[channel - 1] = highlow
|
||||
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
|
||||
@ -104,17 +201,21 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
|
||||
def send(data: str):
|
||||
if data:
|
||||
logger.info(f'SENDING: "{data}"')
|
||||
return conn.sendall(f'{data}\r'.encode())
|
||||
else:
|
||||
logger.info(f'SENDING: ""')
|
||||
return conn.sendall(b'\r')
|
||||
|
||||
def receive():
|
||||
data = conn.recv(1024)
|
||||
if data:
|
||||
# also removes terminator
|
||||
return data.decode('ascii').rstrip()
|
||||
received = data.decode('ascii').rstrip()
|
||||
else:
|
||||
return ''
|
||||
received = ''
|
||||
logger.info(f'RECEIVED: "{received}"')
|
||||
return received
|
||||
|
||||
counterbox = CounterBox(TOTAL_CH)
|
||||
|
||||
@ -141,11 +242,26 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
))
|
||||
)
|
||||
|
||||
elif re.fullmatch(r'RC (\d+)', data):
|
||||
channel = int(re.fullmatch(r'RC (\d+)', data).group(1))
|
||||
count = counterbox.getCount(channel)
|
||||
send(f'{count}')
|
||||
|
||||
elif data == 'RS':
|
||||
send(str(counterbox.getStatus()))
|
||||
|
||||
elif data == 'S':
|
||||
counterbox.stop()
|
||||
send('')
|
||||
|
||||
elif data == 'CT':
|
||||
counterbox.clearTime()
|
||||
send('')
|
||||
|
||||
elif re.fullmatch(r'CC (\d+)', data):
|
||||
counter = int(re.fullmatch(r'CC (\d+)', data).group(1))
|
||||
counterbox.clearCount(counter)
|
||||
send('')
|
||||
|
||||
elif re.fullmatch(r'TP (\d+(\.\d+)?)', data):
|
||||
presettime = float(re.fullmatch(r'TP (\d+(\.\d+)?)', data).group(1))
|
||||
@ -163,13 +279,53 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
elif re.fullmatch(r'PC (\d+)', data):
|
||||
channel = int(re.fullmatch(r'PC (\d+)', data).group(1))
|
||||
counterbox.setMonitorChannel(channel)
|
||||
send('')
|
||||
|
||||
elif data == 'DR':
|
||||
send(str(counterbox.getMinRateChannel()))
|
||||
|
||||
elif re.fullmatch(r'DR (\d+)', data):
|
||||
channel = int(re.fullmatch(r'DR (\d+)', data).group(1))
|
||||
counterbox.setMinRateChannel(channel)
|
||||
send('')
|
||||
|
||||
elif re.fullmatch(r'DL (\d+)', data):
|
||||
channel = int(re.fullmatch(r'DL (\d+)', data).group(1))
|
||||
send('{:.3f}'.format(counterbox.getMinRate(channel)))
|
||||
|
||||
elif re.fullmatch(r'DL (\d+) (\d+(?:.\d+)?)', data):
|
||||
channel, rate = re.fullmatch(r'DL (\d+) (\d+(?:.\d+)?)', data).groups()
|
||||
counterbox.setMinRate(int(channel), float(rate))
|
||||
send('')
|
||||
|
||||
elif re.fullmatch(r'RR (\d+)', data):
|
||||
channel = int(re.fullmatch(r'RR (\d+)', data).group(1))
|
||||
send(counterbox.getRate(channel))
|
||||
|
||||
elif re.fullmatch(r'GT (\d+)', data):
|
||||
channel = int(re.fullmatch(r'GT (\d+)', data).group(1))
|
||||
enabled, highlow = counterbox.getGateStatus(channel)
|
||||
send(f'{int(enabled)} {int(highlow)}')
|
||||
|
||||
elif re.fullmatch(r'GT (\d+) (\d+) (\d+)', data):
|
||||
channel, enable, highlow = re.fullmatch(r'GT (\d+) (\d+) (\d+)', data).groups()
|
||||
channel, enable, highlow = int(channel), bool(int(enable)), bool(int(highlow))
|
||||
counterbox.setGateStatus(channel, enable, highlow)
|
||||
if enable:
|
||||
send(f'Gate {channel} enabled, counting when input = {"high" if highlow else "low"}')
|
||||
else:
|
||||
send(f'Gate {channel} disabled')
|
||||
|
||||
# Only for the simulation
|
||||
elif re.fullmatch(r'GATE (\d+) (\d+)', data):
|
||||
channel, highlow = re.fullmatch(r'GATE (\d+) (\d+)', data).groups()
|
||||
channel, highlow = int(channel), bool(int(highlow))
|
||||
counterbox.setGate(channel, highlow)
|
||||
send('')
|
||||
|
||||
else:
|
||||
send('?2') # Bad command
|
||||
|
||||
except:
|
||||
except Exception as e:
|
||||
logger.exception('Simulation Broke')
|
||||
send('?OV')
|
||||
|
Reference in New Issue
Block a user