Skip to content

Commit

Permalink
Revert latest straw prep commit.
Browse files Browse the repository at this point in the history
It is still breaking the Pro 1 LPAL straw-panel transfer
with a None being submitted for the straw_present time_ins.
  • Loading branch information
bamesserly committed Nov 17, 2022
1 parent 9d1730e commit eae3398
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 160 deletions.
14 changes: 7 additions & 7 deletions guis/common/db_classes/procedure.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _startProcedure(cls, station, straw_location):
if c.__mapper_args__["polymorphic_identity"] == station.id:
cls = c
break

procedure = (
cls.query()
.filter(cls.station == station.id)
Expand Down Expand Up @@ -163,7 +163,7 @@ def PanelProcedure(cls, process, panel_number):
return PanelProcedure._startProcedure(station=station, straw_location=panel)

@classmethod
def StrawProcedure(cls, process, pallet_id, pallet_number,timestamp=time(),override_empty_check=None):
def StrawProcedure(cls, process, pallet_id, pallet_number):
# Get Station
station = Station.get_station(stage="straws", step=process)

Expand All @@ -173,12 +173,10 @@ def StrawProcedure(cls, process, pallet_id, pallet_number,timestamp=time(),overr
if station.id == "load":
pallet = StrawLocation.LPAL(pallet_id=pallet_id, number=pallet_number)
else:
pallet = StrawLocation.CPAL(pallet_id=pallet_id, number=pallet_number,override_empty_check=override_empty_check)
pallet = StrawLocation.CPAL(pallet_id=pallet_id, number=pallet_number)

# Use Procedure._startProcedure() to return object.
val = StrawProcedure._startProcedure(station=station, straw_location=pallet)
return val

return StrawProcedure._startProcedure(station=station, straw_location=pallet)

@staticmethod
def _queryStation(station=None, production_stage=None, production_step=None):
Expand Down Expand Up @@ -288,7 +286,9 @@ def _init_details(self):
self.details = dc(id=self.ID(),procedure=self.id)

def _getDetailsClass(self):
pass
# TODO: If additional procedure data is recorded at this station,
# have this function define a mapper class and return it
return None

def isNew(self):
return self.new
Expand Down
18 changes: 2 additions & 16 deletions guis/common/db_classes/procedures_straw.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from datetime import datetime
from guis.common.db_classes.procedure import StrawProcedure
from guis.common.db_classes.straw import Straw
import time


class Prep(StrawProcedure):
Expand All @@ -44,16 +43,6 @@ def __init__(self, station, straw_location, create_key):
station.id == "prep"
), f"Error. Tried to construct prep procedure for a station '{station.id}' not 'prep'."
super().__init__(station, straw_location, create_key)

def _getDetailsClass(self):
class Details(BASE, OBJECT):
__tablename__ = "procedure"
id = Column(Integer, primary_key=True)
station = Column(CHAR(4), ForeignKey("station.id"))
straw_location = Column(Integer, ForeignKey("straw_location.id"))
elapsed_time = Column(Integer, default=0)

return Details

class StrawPrepMeasurement(BASE, OBJECT):
__tablename__ = "measurement_prep"
Expand All @@ -64,15 +53,12 @@ class StrawPrepMeasurement(BASE, OBJECT):
evaluation = Column(BOOLEAN)
timestamp = Column(Integer, default=int(datetime.now().timestamp()))

def __init__(self, procedure, straw_id, paper_pull_grade, evaluation, timestamp=time.time()):
def __init__(self, procedure, straw_id, paper_pull_grade, evaluation):
self.id = self.ID()
self.procedure = procedure.id
self.straw = straw_id
self.paper_pull_grade = paper_pull_grade
self.evaluation = evaluation
self.timestamp = timestamp




class Resistance(StrawProcedure):
Expand Down Expand Up @@ -298,4 +284,4 @@ def setApproximateHumidity(self,humidity):
self.details.approximate_humidity = humidity
"""
"""
56 changes: 3 additions & 53 deletions guis/common/db_classes/straw.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
func,
)
import guis.common.db_classes.straw_location as sl
import time


class Straw(BASE, OBJECT):
Expand All @@ -26,27 +25,25 @@ class Straw(BASE, OBJECT):
id = Column(Integer, primary_key=True)
batch = Column(VARCHAR)
parent = Column(Integer, ForeignKey("straw.id"))
timestamp = Column(Integer, default=time.time())

def __init__(self, id, batch=None, parent=None, create_key=None, timestamp=time.time()):
def __init__(self, id, batch=None, parent=None, create_key=None):
# Check for authorization with 'create_key'
assert create_key == Straw.__create_key, "You can only make a Straw internally."

self.id = id
self.batch = batch
self.parent = parent
self.timestamp = timestamp
self.commit()

@classmethod
def Straw(cls, id, batch=None, timestamp=time.time()):
def Straw(cls, id, batch=None):

# Try to query the straw
s = cls.queryWithId(id)

# If None is found, construct straw
if s is None:
s = Straw(id, batch, None, cls.__create_key, timestamp)
s = Straw(id, batch, None, cls.__create_key)

# Return straw
return s
Expand All @@ -71,10 +68,6 @@ def exists(cls, straw_id):
def strpBarcode(cls, barcode):
n = int(barcode[2:])
return cls.Straw(n)

def updateBatch(self, batch):
self.batch=batch
self.commit()

def __repr__(self):
return "<Straw(id='%s')>" % (self.id)
Expand Down Expand Up @@ -140,7 +133,6 @@ def _queryVerification(self):
return d

# return list of StrawPositions where this straw is marked present
# example: [<16486600691503178 straw position 38 on 1648660069119168 straw location MN0233>]
def locate(self):
return (
DM.query(sl.StrawPosition) # Get all the straw positions
Expand All @@ -153,48 +145,6 @@ def locate(self):
.all()
)

# same as above but returns ALL locations, not just the current one
# example: [<16499606623611227 straw position 84 on 1649960662278015 straw location LPAL0486 at LPALID01>, <16499477136822270 straw position 84 on 1649947713619747 straw location MN0238>]
def locations(self):
return (
DM.query(sl.StrawPosition) # Get all the straw positions
# .join(sl.StrawPosition, sl.StrawPosition.location == sl.StrawLocation.id) # where straw positions have an entry in the straw location
.join(
sl.StrawPresent, sl.StrawPresent.position == sl.StrawPosition.id
) # that also have straw present entries
.filter(sl.StrawPresent.straw == self.id) # for this straw
.all()
)

# get the "present" occurance in straw_present
def currentPresence(self):
return (
DM.query(
sl.StrawPresent.id,
sl.StrawPresent.position,
sl.StrawPresent.present,
sl.StrawPresent.time_in,
sl.StrawPresent.time_out
)
.filter(sl.StrawPresent.straw == self.id)
.filter(sl.StrawPresent.present == 1)
.all()
)

# get all occurances in straw_position
def presences(self):
return (
DM.query(
sl.StrawPresent.id,
sl.StrawPresent.position,
sl.StrawPresent.present,
sl.StrawPresent.time_in,
sl.StrawPresent.time_out
)
.filter(sl.StrawPresent.straw == self.id)
.all()
)

# remove straw from all locations
def removeFromAllLocations(self):
for position in self.locate():
Expand Down
111 changes: 28 additions & 83 deletions guis/common/db_classes/straw_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)
from sqlalchemy.sql.expression import true, false
import guis.common.db_classes.straw as st
from time import sleep, time
from time import sleep


class StrawLocation(BASE, OBJECT):
Expand Down Expand Up @@ -126,7 +126,7 @@ def __repr__(self):
# an existing number but new id, then you'll just get returned the existing
# pallet when instead you should get an error.
@classmethod
def _construct(cls, number=int(), pallet_id=None, override_empty_check=None):
def _construct(cls, number=int(), pallet_id=None):
assert int(
number
), "Error: attempting to retrieve or create a straw location with a non-integer number."
Expand All @@ -136,25 +136,14 @@ def _construct(cls, number=int(), pallet_id=None, override_empty_check=None):
sl = cls._queryStrawLocation(number)

# If None was found, make a new one.
if override_empty_check is not None:
if sl is None:
sl = cls(
number=number,
pallet_id=pallet_id,
create_key=cls.__create_key,
override_empty_check=override_empty_check
)
else:
if sl is None:
sl = cls(
number=number,
pallet_id=pallet_id,
create_key=cls.__create_key,
)
return sl


if sl is None:
sl = cls(
number=number,
pallet_id=pallet_id,
create_key=cls.__create_key,
)

return sl

## CONSTRUCTORS ##

Expand All @@ -165,9 +154,9 @@ def Panel(number):
# TODO check here (not in construct) for existing pallets with this number
# but a different id.
@staticmethod
def CPAL(number=int(), pallet_id=None, override_empty_check=None):
return CuttingPallet._construct(number=number, pallet_id=pallet_id, override_empty_check=override_empty_check)
def CPAL(number=int(), pallet_id=None):
return CuttingPallet._construct(number=number, pallet_id=pallet_id)

# TODO check here (not in construct) for existing pallets with this number
# but a different id.
@staticmethod
Expand Down Expand Up @@ -363,34 +352,6 @@ def addStraw(self, straw, position, commit=True):
straw_present.commit()

return straw_present

def add_historical_straw(self, straw, position, commit=True, timestamp=time()):
# Get or create target position
target_position = (
self._queryStrawPositions()
.filter(StrawPosition.position_number == position)
.one_or_none()
)
if not target_position:
logger.warning(
f"Adding straw {straw} to position {self.location_type}{self.number} - {position}, which doesn't exist in the DB. This is weird, and you should let Ben know."
)
logger.warning(
"Anways, I'm creating the position and adding the straw to it."
)
target_position = StrawPosition(location=self.id, position=position)
target_position.commit()

straw_present = StrawPresent(
straw=straw, position=target_position.id, present=False, time_in=timestamp
)

if commit:
sleep(0.4) # so the order of events in the DB is clear
straw_present.commit()
logger.debug(f"Straw ST{straw_present.straw} added to {target_position}.")

return straw_present

# Safely transfer a straw from previous location(s) to target location.
#
Expand Down Expand Up @@ -451,7 +412,6 @@ def forceAddStraw(self, straw, position, commit=True):
sleep(0.4) # so the order of events in the DB is clear
straw_present.commit()
logger.debug(f"Straw ST{straw_present.straw} added to {target_position}.")


return straw_present

Expand Down Expand Up @@ -635,20 +595,18 @@ def queryByNumber(cls, number):

class Pallet(StrawLocation):
def __init__(
self, location_type=None, number=int(), pallet_id=None, create_key=None, override_empty_check=None
self, location_type=None, number=int(), pallet_id=None, create_key=None
):
if override_empty_check is not True:
assert self._palletIsEmpty(
pallet_id
), f"Unable to create pallet {number}: pallet {pallet_id} is not empty."

assert self._palletIsEmpty(
pallet_id
), f"Unable to create pallet {number}: pallet {pallet_id} is not empty."
super().__init__(
location_type=location_type,
number=number,
pallet_id=pallet_id,
create_key=create_key,
)

@property
def palletBarcode(self):
return self.getLocationType().palletBarcode(self.pallet_id)
Expand Down Expand Up @@ -689,32 +647,25 @@ def __init__(
create_key=create_key,
)


class CuttingPallet(Pallet):
__mapper_args__ = {"polymorphic_identity": "CPAL"}

def __init__(
self, location_type=None, number=int(), pallet_id=None, create_key=None, override_empty_check=None
self, location_type=None, number=int(), pallet_id=None, create_key=None
):
if override_empty_check is not None:
super().__init__(
location_type="CPAL",
number=number,
pallet_id=pallet_id,
create_key=create_key,
override_empty_check=override_empty_check,
)
else:
super().__init__(
location_type="CPAL",
number=number,
pallet_id=pallet_id,
create_key=create_key,
)

super().__init__(
location_type="CPAL",
number=number,
pallet_id=pallet_id,
create_key=create_key,
)

@classmethod
def save_to_db(self, straws_passed_list, pallet_id, pallet_number):
logger = logging.getLogger("root")
#assert len(straws_passed_list) == 24

# Get a cpal in the DB
try:
cpal = self.CPAL(pallet_id=pallet_id, number=pallet_number)
Expand Down Expand Up @@ -829,18 +780,12 @@ class StrawPresent(BASE, OBJECT):
straw = Column(Integer, ForeignKey("straw.id"))
position = Column(Integer, ForeignKey("straw_position.id"))
present = Column(BOOLEAN)
time_in = Column(Integer)
time_out = Column(Integer)

def __init__(self, straw, position, present=true(), time_in=None, time_out=None):
def __init__(self, straw, position, present=true()):
self.id = self.IncrementID()
self.straw = straw
self.position = position
self.present = present
if time_in != None:
self.time_in = time_in
if time_out != None:
self.time_out = time_out

def __repr__(self):
return "<StrawPresent(id='%s',straw'%s',position='%s')>" % (
Expand Down
Loading

0 comments on commit eae3398

Please sign in to comment.