Skip to content

Commit

Permalink
Updates to python apps/devices
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-long committed Nov 16, 2024
1 parent 92ec5ad commit 9c5d2c7
Show file tree
Hide file tree
Showing 11 changed files with 687 additions and 216 deletions.
13 changes: 9 additions & 4 deletions apps/audibleAlerts/audibleAlerts/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def reaction_handler(self, new_message, element_name, transition, utterance_choi
return
value = new_message[element_name]
self.log.debug(f"Judging reaction for {element_name} change to {repr(value)} using {transition}")
self.log.debug(f"before check {self.latch_transitions=}")
last_value = self.latch_transitions.get(transition)
self.log.debug(f"{new_message}\n{transition.compare(value)=}, last value was {last_value}, {value != last_value=} {(not transition.compare(last_value))=}")
if transition.compare(value) and (
Expand All @@ -104,19 +105,23 @@ def reaction_handler(self, new_message, element_name, transition, utterance_choi
(not transition.compare(last_value))
):
self.latch_transitions[transition] = value
self.log.debug(f"after update {self.latch_transitions=}")
self.log.debug(f"latched {transition=} with {value=}")
last_transition_ts = self.per_transition_cooldown_ts.get(transition, 0)
sec_since_trigger = time.time() - last_transition_ts
debounce_expired = sec_since_trigger > transition.debounce_sec
self.log.debug(f"Debounced {sec_since_trigger=}")
self.log.debug(f"Checking for debounce: {sec_since_trigger=} {debounce_expired=}")
if debounce_expired:
utterance = choice(utterance_choices)
self.log.debug(f"Submitting speech request: {utterance}")
self.enqueue_speech_request(utterance)
else:
self.log.debug(f"Would have spoken, but it's only been {sec_since_trigger=}")
elif transition.compare(last_value) and not transition.compare(value):
# un-latch, so next time we change to a value that compares True we trigger again:
self.log.debug(f"un-latch {transition}, so next time we change to a value that compares True we trigger again. ({last_value=} {value=})")
self.log.debug(f"before {self.latch_transitions=}")
del self.latch_transitions[transition]
self.log.debug(f"after {self.latch_transitions=}")
else:
self.log.debug(f"Got {new_message.device}.{new_message.name} but {transition=} did not match")

Expand Down Expand Up @@ -169,7 +174,7 @@ def handle_soundboard_switch(self, prop: properties.IndiProperty, new_message):

def load_personality(self, personality_name):
personality_file = os.path.join(HERE, "personalities", f"{personality_name}.xml")
for cb, device_name, property_name in self._cb_handles:
for cb, device_name, property_name, _ in self._cb_handles:
try:
self.client.unregister_callback(cb, device_name=device_name, property_name=property_name)
except ValueError:
Expand Down Expand Up @@ -202,7 +207,7 @@ def load_personality(self, personality_name):
device_name=device_name,
property_name=property_name
)
self._cb_handles.add((cb, device_name, property_name))
self._cb_handles.add((cb, device_name, property_name, t))
self.log.debug(f"Registered reaction handler on {device_name=} {property_name=} {element_name=} using transition {t}")
for idx, utterance in enumerate(reaction.transitions[t]):
self.log.debug(f"{reaction.indi_id}: {t}: {utterance}")
Expand Down
6 changes: 6 additions & 0 deletions apps/audibleAlerts/audibleAlerts/personalities/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<speak><voice name="larynx:karen_savage-glow_tts">Entregue todas sus empanadas al jefe.</voice></speak>
<speak>We, at Mag <say-as interpret-as="spell-out">AOX</say-as>, recommend Mag <say-as interpret-as="spell-out">AOX</say-as>.</speak>
<file name="not_jared.wav" />
<speak><voice name="larynx:lisa-glow_tts">non è consentito il cappuccino dopo mezzogiorno</voice></speak>
</random-utterances>
<soundboard>
<button name="NotJared">
Expand All @@ -37,6 +38,11 @@
<file name="clown.wav" />
</button>
</soundboard>
<react-to indi-id="fwfpm.filterName.cmc">
<transition value="On">
<speak><voice name="larynx:lisa-glow_tts">non è consentito il cappuccino dopo mezzogiorno</voice></speak>
</transition>
</react-to>
<react-to indi-id="observers.obs_on.toggle">
<transition value="On">
<speak>Saving data for {observers.current_observer.pfoa}.</speak>
Expand Down
5 changes: 3 additions & 2 deletions apps/dbIngest/dbIngest.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ def setup(self):
self.fs_observer.start()

def rescan_files(self):
search_paths = [self.config.common_path_prefix / name for name in self.config.data_dirs]
with self.conn.cursor() as cur:
ingest.update_file_inventory(cur, self.config.hostname, self.config.data_dirs)
self.log.info(f"Completed startup rescan of file inventory for {self.config.hostname}")
ingest.update_file_inventory(cur, self.config.hostname, search_paths)
self.log.info(f"Completed startup rescan of file inventory for {self.config.hostname} from {search_paths}")

def ingest_line(self, line):
# avoid infinite loop of modifying log file and getting notified of the modification
Expand Down
1 change: 1 addition & 0 deletions python/magaox/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
import upath

class StateCodes(Enum):
FAILURE = -20 # The application has failed should be used when m_shutdown is set for an error.
Expand Down
5 changes: 3 additions & 2 deletions python/magaox/db/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def batch_file_origins(cur: psycopg.Cursor, records: list[FileOrigin]):
''', [(rec.origin_host, rec.origin_path, rec.creation_time, rec.modification_time, rec.size_bytes) for rec in records])
cur.execute("COMMIT")

def identify_new_files(cur: psycopg.Cursor, this_host: str, paths: list[str]):
def identify_new_files(cur: psycopg.Cursor, this_host: str, paths: list[pathlib.Path]):
'''Returns the paths from ``paths`` that are not already part of the ``file_origins`` table'''
if len(paths) == 0:
return []
Expand All @@ -64,7 +64,7 @@ def identify_new_files(cur: psycopg.Cursor, this_host: str, paths: list[str]):
INSERT INTO on_disk_files (path)
VALUES (%s)
'''
cur.executemany(query, [(x,) for x in paths])
cur.executemany(query, [(x.as_posix(),) for x in paths])
# execute_values(cur, query, )
log.debug(f"Loaded {len(paths)} paths into temporary table for new file identification")

Expand Down Expand Up @@ -126,6 +126,7 @@ def update_file_inventory(cur: psycopg.Cursor, host: str, data_dirs: list[pathli
cur.execute("BEGIN")
for prefix in data_dirs:
for dirpath, dirnames, filenames in os.walk(prefix):
dirpath = pathlib.Path(dirpath)
log.info(f"Checking for new files in {dirpath}")
new_files = identify_new_files(cur, host, [dirpath / fn for fn in filenames])
if len(new_files) == 0:
Expand Down
9 changes: 3 additions & 6 deletions python/magaox/indi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ def log_level_to_label(levelno):

class MagAOXLogFormatter(logging.Formatter):
def __init__(self, fmt='%(asctime)s %(levelname)s %(message)s (%(name)s:%(funcName)s:%(lineno)d)'):
super().__init__(
fmt=fmt,
datefmt='%Y-%m-%dT%H:%M:%S.%f000',
)
# def formatTime(self, record, datefmt):
# return datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).strftime()
super().__init__(fmt=fmt)
def formatTime(self, record, datefmt):
return datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f') + '000'

class NDJSONLogFormatter(MagAOXLogFormatter):
def __init__(self):
Expand Down
Empty file added python/magaox/pack/__init__.py
Empty file.
Loading

0 comments on commit 9c5d2c7

Please sign in to comment.