########################################################################
### FILE: hwstates.mod
### PURPOSE: Handle hardware (FCB/ACB) state changes
### CONTEXT: "LLAC:" branch
###
### Copyrights (C) 2011 Life Technologies. All rights reserved
########################################################################
### Add event handlers for states
DEFINT EVENTS+ $branch$ $node$ $specfile$
:LLAC:EVENT+ -replaceExisting $branch$-Sensor -id=0x8220 -idMask=0xFFFE -source=$node$
SET index $23u
SET state $(CONF? -ignoreMissing $specfile$ SensorStates ${index})
IF $[ "${state}".startswith("!") ]
SET value $[ $id != 0x8220 ]
SET state ${state:1}
ELSE
SET value $[ $id == 0x8220 ]
IF ${#state}
:STATe:SETTing -createMissing ${state} ${value}
:LLAC:EVENT+ -replaceExisting $branch$-Sensor-Error -id=0x8222 -idMask=0xFFFE -source=$node$
SET index $23u
SET state $[ "$(CONF? -ignoreMissing $specfile$ SensorStates ${index})".lstrip("!") ]
IF ${#state}
INFO "State ${state} is now in error"
:STATe:ERRor= ${state} $device.$[ "$id"[2:] ] "$text"
:LLAC:EVENT+ -replaceExisting $branch$-Solenoid-Disable -id=0x81AB -source=$node$
SET index $23u
SET state $(CONF? -ignoreMissing $specfile$ SolenoidStates ${index})
IF ${#state}
INFO "Got disabled event from solenoid #${index}; clearing state ${state}"
:STATE= ${state} False
:LLAC:EVENT+ -replaceExisting $branch$-Solenoid-Timeout -id=0x81B2 -source=$node$
SET index $23u
SET state $(CONF? -ignoreMissing $specfile$ SolenoidStates ${index})
IF ${#state}
INFO "Got timeout from solenoid #${index}; clearing state ${state}"
:STATE= ${state} False
:LLAC:EVENT+ -replaceExisting $branch$-Solenoid-On -id=0x81B3 -source=$node$
SET index $23u
SET state $(CONF? -ignoreMissing $specfile$ SolenoidStates ${index})
IF ${#state}
INFO "Got ON event from solenoid #${index}; setting state ${state}"
:STATE= ${state} True
:LLAC:EVENT+ -replaceExisting $branch$-Solenoid-Off -id=0x81B4 -source=$node$
SET index $23u
SET state $(CONF? -ignoreMissing $specfile$ SolenoidStates ${index})
IF ${#state}
INFO "Got OFF event from solenoid #${index}; clearing state ${state}"
:STATE= ${state} False
### Remove event handlers for states
DEFINT EVENTS- $branch$
:LLAC:EVENt- -ignoreMissing $branch$-Sensor-Error
:LLAC:EVENt- -ignoreMissing $branch$-Sensor
### Set values for states
DEFINT STATE+ $allows$='' $prevents$='' $state$ $register$=''
UNLess $(:STATe:EXISTS? $state$) ':STATE:NEW $state$'
IF $[ "$register$" != "" ]
:STATe:SETTing $state$ $($register$)
APPend -create UpdateStates "\n\t:STATe:SETTing $state$ \$($register$)"
ITERate -key=dep -delimiter="," "$allows$"
IF ${#dep} ':STATe:DEPendency+ -replaceExisting ${dep} -requires=$state$'
ITERate -key=dep -delimiter="," "$prevents$"
IF ${#dep} ':STATe:DEPendency+ -replaceExisting ${dep} -conflicts=$state$'
### Clear all states
DEFINT STATE~
VAR- -ignoreMissing UpdateStates
### Method to update all state values
DEFINT UPDATESTATES
RUN ${UpdateStates:-}