Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions smibhid/lib/hid.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from ulogging import uLogger
from asyncio import get_event_loop
from slack_api import Wrapper
from display import Display
from space_state import SpaceState
from error_handling import ErrorHandler
from module_config import ModuleConfig
from display import Display
from networking import WirelessNetwork

class HID:

Expand All @@ -15,8 +16,10 @@ def __init__(self) -> None:
self.log = uLogger("HID")
self.version = "1.1.1"
self.loop_running = False
self.display = Display()
self.spaceState = SpaceState(self.display)
self.moduleConfig = ModuleConfig(Display(), WirelessNetwork())
self.moduleConfig.enable_network_status_monitor()
self.display = self.moduleConfig.get_display()
self.spaceState = SpaceState(self.moduleConfig)
self.errorHandler = ErrorHandler("HID")
self.errorHandler.configure_display(self.display)

Expand Down
22 changes: 22 additions & 0 deletions smibhid/lib/module_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from display import Display
from networking import WirelessNetwork
from asyncio import create_task

class ModuleConfig:
"""
Create a new instance of the module configurations for use in child modules of HID.
Dependency injection to ensure we are managing module configurations in a single place without the use of singletons.
"""
def __init__(self, display: Display, wifi: WirelessNetwork) -> None:
self.display = display
self.wifi = wifi

def enable_network_status_monitor(self) -> None:
create_task(self.wifi.network_status_monitor())

def get_display(self) -> Display:
return self.display

def get_wifi(self) -> WirelessNetwork:
return self.wifi

14 changes: 14 additions & 0 deletions smibhid/lib/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self) -> None:
self.subnet = "Unknown"
self.gateway = "Unknown"
self.dns = "Unknown"
self.state = "Unknown"

self.configure_wifi()

Expand All @@ -55,6 +56,19 @@ def configure_wifi(self) -> None:
self.hostname = "smibhid-" + self.mac[-5:]
self.log.info(f"Setting hostname to {self.hostname}")
network.hostname(self.hostname)
self.log.info("MAC: " + self.mac)

async def network_status_monitor(self) -> None:
while True:
status = self.dump_status()
if status == 3:
self.state = "Connected"
elif status >= 0:
self.state = "Connecting"
else:
self.state = "Disconnected"
self.log.info(f"Network status: {self.state}")
await sleep(5)

def dump_status(self):
status = self.wlan.status()
Expand Down
6 changes: 3 additions & 3 deletions smibhid/lib/slack_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from lib.ulogging import uLogger
import lib.uaiohttpclient as httpclient
from lib.networking import WirelessNetwork
from networking import WirelessNetwork
from config import WEBSERVER_HOST, WEBSERVER_PORT
import gc
from json import loads
Expand All @@ -9,9 +9,9 @@ class Wrapper:
"""
API wrapper for the REST API accepting comands to pass to the local slack server socket.
"""
def __init__(self) -> None:
def __init__(self, network: WirelessNetwork) -> None:
self.log = uLogger("Slack API")
self.wifi = WirelessNetwork()
self.wifi = network
self.event_api_base_url = "http://" + WEBSERVER_HOST + ":" + WEBSERVER_PORT + "/smib/event/"

async def async_space_open(self) -> None:
Expand Down
9 changes: 5 additions & 4 deletions smibhid/lib/space_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
from button import Button
from asyncio import Event, create_task, sleep, wait_for
from constants import OPEN, CLOSED
from display import Display
from slack_api import Wrapper
from module_config import ModuleConfig
from error_handling import ErrorHandler

class SpaceState:
def __init__(self, display: Display) -> None:
def __init__(self, module_config: ModuleConfig) -> None:
"""
Pass an asyncio event object to error_event and use a coroutine to
monitor for event triggers to handle errors in space state checking
by querying the is_in_error_state attribute.
"""
self.log = uLogger("SpaceState")
self.display = display
self.slack_api = Wrapper()
self.display = module_config.get_display()
self.wifi = module_config.get_wifi()
self.slack_api = Wrapper(self.wifi)
self.space_open_button_event = Event()
self.space_closed_button_event = Event()
self.open_button = Button(config.SPACE_OPEN_BUTTON, "Space_open", self.space_open_button_event)
Expand Down