Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept: system constants #31

Merged
merged 1 commit into from
Feb 15, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from IoTuring.Entity.Entity import Entity
from IoTuring.Entity.EntityData import EntitySensor
from IoTuring.SystemConsts.SystemConsts import DesktopEnvironment as De

KEY_DE = 'desktop_environment'

Expand All @@ -13,13 +14,6 @@ class DesktopEnvironment(Entity):
def Initialize(self):
self.RegisterEntitySensor(EntitySensor(self, KEY_DE))
# The value for this sensor is static for the entire script run time (set in initialize so other entities can get the value from Postinitialize)
self.SetEntitySensorValue(KEY_DE, self.GetDesktopEnvironment())
self.SetEntitySensorValue(KEY_DE, De.GetDesktopEnvironment())

# If value passed use it else get it from the system
def GetDesktopEnvironment(self):

de = os.environ.get('DESKTOP_SESSION')
if de == None:
de = "base"

return de
8 changes: 4 additions & 4 deletions IoTuring/Entity/Deployments/Lock/Lock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import subprocess
from IoTuring.Entity.Entity import Entity
from IoTuring.Entity.EntityData import EntityCommand
from IoTuring.SystemConsts.SystemConsts import DesktopEnvironment as De
from IoTuring.SystemConsts.SystemConsts import Os

KEY_LOCK = 'lock'

Expand All @@ -21,16 +23,14 @@

class Lock(Entity):
NAME = "Lock"
DEPENDENCIES = ["Os", "DesktopEnvironment"]

def Initialize(self):
self.RegisterEntityCommand(EntityCommand(
self, KEY_LOCK, self.Callback_Lock))

def PostInitialize(self):
self.os = self.GetDependentEntitySensorValue('Os', "operating_system")
self.de = self.GetDependentEntitySensorValue(
'DesktopEnvironment', 'desktop_environment')
self.os = Os.GetOs()
self.de = De.GetDesktopEnvironment()

def Callback_Lock(self, message):
if self.os in commands:
Expand Down
21 changes: 4 additions & 17 deletions IoTuring/Entity/Deployments/Os/Os.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import platform
from IoTuring.Entity.Entity import Entity
from IoTuring.Entity.EntityData import EntitySensor
from IoTuring.Entity import consts

from IoTuring.SystemConsts.SystemConsts import Os

KEY_OS = 'operating_system'


class Os(Entity):
NAME = "Os"
class OperatingSystem(Entity):
NAME = "OperatingSystem"

def Initialize(self):
self.RegisterEntitySensor(EntitySensor(self, KEY_OS))
# The value for this sensor is static for the entire script run time (set in initialize so other entities can get the value from Postinitialize)
self.SetEntitySensorValue(KEY_OS, self.GetOperatingSystem())
self.SetEntitySensorValue(KEY_OS, Os.GetOs())

def Update(self):
pass

def GetOperatingSystem(self):
os = platform.system()
if os == 'Darwin': # It's macOS
return consts.OS_FIXED_VALUE_MACOS
elif os == "Linux":
return consts.OS_FIXED_VALUE_LINUX
elif os == "Windows":
return consts.OS_FIXED_VALUE_WINDOWS
else:
self.Log(self.LOG_WARNING, "Operating system not in the fixed list. Please open a Git issue and warn about this: OS = \"" + os + "\"")
return os
7 changes: 4 additions & 3 deletions IoTuring/Entity/Deployments/Power/Power.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os as sys_os
from IoTuring.Entity.Entity import Entity
from IoTuring.Entity.EntityData import EntityCommand
from IoTuring.SystemConsts.SystemConsts import Os


KEY_SHUTDOWN = 'shutdown'
KEY_REBOOT = 'reboot'
Expand All @@ -27,13 +29,12 @@

class Power(Entity):
NAME = "Power"
DEPENDENCIES = ["Os"]

def Initialize(self):
self.sleep_command = ""

def PostInitialize(self):
self.os = self.GetDependentEntitySensorValue('Os', "operating_system")
self.os = Os.GetOs()
# Check if commands are available for this OS/DE combo, then register them

# Shutdown
Expand All @@ -50,7 +51,7 @@ def PostInitialize(self):
# TODO Update TurnOffMonitors, TurnOnMonitors, LockCommand to use prefix lookup below
# Additional linux checking to find Window Manager: check running X11 for linux
prefix = ''
if self.os == 'Linux' and sys_os.environ.get('DISPLAY'):
if Os.IsLinux() and sys_os.environ.get('DISPLAY'):
prefix = '_X11'
lookup_key = self.os + prefix
if lookup_key in commands_sleep:
Expand Down
9 changes: 5 additions & 4 deletions IoTuring/Entity/Deployments/Temperature/Temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from IoTuring.Entity.Entity import Entity
from IoTuring.Entity.EntityData import EntitySensor
from IoTuring.Entity.ValueFormatter import ValueFormatter
from IoTuring.SystemConsts.SystemConsts import Os


KEY_SENSOR_FORMAT = "{}"
FALLBACK_PACKAGE_LABEL = "package_{}"
Expand Down Expand Up @@ -34,16 +36,15 @@

class Temperature(Entity):
NAME = "Temperature"
DEPENDENCIES = ["Os"]

def PostInitialize(self):
self.specificInitialize = None
self.specificUpdate = None
self.os = self.GetDependentEntitySensorValue('Os', "operating_system")
if(self.os == "Linux"):

if Os.IsLinux():
self.specificInitialize = self.InitLinux
self.specificUpdate = self.UpdateLinux
elif(self.os == "macOS"):
elif Os.IsMacos():
self.specificInitialize = self.InitmacOS
self.specificUpdate = self.UpdatemacOS
else:
Expand Down
40 changes: 40 additions & 0 deletions IoTuring/SystemConsts/SystemConsts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import platform
import os
from IoTuring.Entity import consts


class Os():
OS_NAME = platform.system()

@staticmethod
def GetOs() -> str:
if Os.OS_NAME == 'Darwin': # It's macOS
return consts.OS_FIXED_VALUE_MACOS
elif Os.OS_NAME == "Linux":
return consts.OS_FIXED_VALUE_LINUX
elif Os.OS_NAME == "Windows":
return consts.OS_FIXED_VALUE_WINDOWS
else:
raise Exception(
f"Operating system not in the fixed list. Please open a Git issue and warn about this: OS = {Os.OS_NAME}")

@staticmethod
def IsLinux() -> bool:
return bool(Os.OS_NAME == 'Linux')

@staticmethod
def IsWindows() -> bool:
return bool(Os.OS_NAME == 'Windows')

@staticmethod
def IsMacos() -> bool:
return bool(Os.OS_NAME == 'Darwin')


class DesktopEnvironment():
@staticmethod
def GetDesktopEnvironment() -> str:
de = os.environ.get('DESKTOP_SESSION')
if de == None:
de = "base"
return de