-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring(data): Refactoring inter-process data passing WIP
- Loading branch information
1 parent
980a3cc
commit 137c54f
Showing
11 changed files
with
167 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
from foosball.models import TrackerResult, Score, Team, Result | ||
|
||
@dataclass | ||
class ScoreAnalyzerResultData: | ||
score: Score | ||
team_scored: Team | ||
|
||
|
||
ScoreAnalyzerResult = Result[ScoreAnalyzerResultData] | ||
|
||
|
||
class AbstractAnalyzer(ABC): | ||
|
||
def __init__(self, name: str = "UnknownAnalyzer", **kwargs): | ||
self.name = name | ||
self.logger = logging.getLogger(name) | ||
|
||
@abstractmethod | ||
def analyze(self, track_result: TrackerResult, timestamp: datetime) -> dict: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import traceback | ||
|
||
from .ScoreAnalyzer import ScoreAnalyzer | ||
from ... import hooks | ||
from ...hooks import generate_goal_webhook | ||
from ...models import Team, TrackerResult | ||
from ...pipe.BaseProcess import BaseProcess, Msg | ||
|
||
|
||
class Analyzer(BaseProcess): | ||
def close(self): | ||
pass | ||
|
||
def __init__(self, audio: bool = False, webhook: bool = False, goal_grace_period_sec: float = 1.0, *args, **kwargs): | ||
super().__init__(name="Analyzer") | ||
self.kwargs = kwargs | ||
self.analyzers = [ScoreAnalyzer(goal_grace_period_sec, args, kwargs)] | ||
# TODO: catch up here | ||
self.effects = [] | ||
self.audio = audio | ||
self.webhook = webhook | ||
|
||
def call_hooks(self, team: Team) -> None: | ||
if self.audio: | ||
hooks.play_random_sound('goal') | ||
if self.webhook: | ||
hooks.webhook(generate_goal_webhook(team)) | ||
|
||
def process(self, msg: Msg) -> Msg: | ||
track_result: TrackerResult = msg.kwargs['Tracker'] | ||
data = track_result.data | ||
|
||
for a in self.analyzers: | ||
try: | ||
ar = a.analyze(track_result, msg.timestamp) | ||
msg.add(a.name, ar, info=ar.info) | ||
except Exception as e: | ||
self.logger.error("Error in Analyzer - analyzers ", e) | ||
traceback.print_exc() | ||
# TODO: catch up here | ||
for e in self.effects: | ||
try: | ||
e.invoke(track_result, msg.timestamp) | ||
except Exception as e: | ||
self.logger.error("Error in Analyzer - effects ", e) | ||
traceback.print_exc() | ||
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from abc import abstractmethod, ABC | ||
|
||
|
||
class Effect(ABC): | ||
|
||
@abstractmethod | ||
def invoke(self, *args, **kwargs): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.