+ if ( pcu_state == 0 or pcu_state == "0" ) and pcu.status not in [ 'online', 'good' ]:
+ print "changed status from %s to online" % pcu.status
+ pcu.status = 'online'
+ pcu.last_changed = datetime.now()
+
+ if pcu.status == 'online' and changed_greaterthan(pcu.last_changed, 0.5):
+ #send thank you notice, or on-line notice.
+ print "changed status from %s to good" % pcu.status
+ pcu.status = 'good'
+ # NOTE: do not reset last_changed, or you lose how long it's been up.
+
+ if pcu.status == 'offline' and changed_greaterthan(pcu.last_changed, 2):
+ # send down pcu notice
+ print "changed status from %s to down" % pcu.status
+ pcu.status = 'down'
+ pcu.last_changed = datetime.now()
+
+ if ( pcu.status == 'offline' or pcu.status == 'down' ) and changed_greaterthan(pcu.last_changed, 2*30):
+ print "changed status from %s to down" % pcu.status
+ pcu.status = 'down'
+ pcu.last_changed = datetime.now()
+
+def checkAndRecordState(l_pcus, l_plcpcus):
+ count = 0
+ for pcuname in l_pcus:
+
+ d_pcu = None
+ for pcu in l_plcpcus:
+ if pcu['pcu_id'] == pcuname:
+ d_pcu = pcu
+ break
+ if not d_pcu:
+ continue
+
+ pcuhist = HistoryPCURecord.findby_or_create(plc_pcuid=d_pcu['pcu_id'],
+ if_new_set={'status' : 'offline',
+ 'last_changed' : datetime.now()})
+ pcuhist.last_checked = datetime.now()
+
+ try:
+ # Find the most recent record
+ pcurec = FindbadPCURecord.query.filter(FindbadPCURecord.plc_pcuid==pcuname).first()
+ except:
+ print "COULD NOT FIND FB record for %s" % reboot.pcu_name(d_pcu)
+ import traceback
+ email_exception()
+ print traceback.print_exc()
+ # don't have the info to create a new entry right now, so continue.
+ continue
+
+ if not pcurec:
+ print "none object for pcu %s"% reboot.pcu_name(d_pcu)
+ continue
+
+ check_pcu_state(pcurec, pcuhist)
+
+ count += 1
+ print "%d %35s %s since(%s)" % (count, reboot.pcu_name(d_pcu), pcuhist.status, diff_time(time.mktime(pcuhist.last_changed.timetuple())))
+
+ # NOTE: this commits all pending operations to the DB. Do not remove, or
+ # replace with another operations that also commits all pending ops, such
+ # as session.commit() or flush() or something
+ session.flush()
+ print HistoryPCURecord.query.count()