Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
flatsiedatsie committed Jan 30, 2022
1 parent 74660e3 commit 007d534
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions pkg/power_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, verbose=False):
if self.DEBUG:
print("self.manager_proxy = " + str(self.manager_proxy))
print("Created new API HANDLER: " + str(manifest['id']))


print(str(self.user_profile))

except Exception as e:
print("Failed to init UX extension API handler: " + str(e))

Expand All @@ -74,7 +78,7 @@ def handle_request(self, request):
if request.method != 'POST':
return APIResponse(status=404)

if request.path == '/init' or request.path == '/set-time' or request.path == '/set-ntp' or request.path == '/shutdown' or request.path == '/reboot':
if request.path == '/init' or request.path == '/set-time' or request.path == '/set-ntp' or request.path == '/shutdown' or request.path == '/reboot' or request.path == '/restart':

try:
if request.path == '/init':
Expand Down Expand Up @@ -118,7 +122,8 @@ def handle_request(self, request):
content=json.dumps("Time set"),
)
except Exception as ex:
print("Error setting time: " + str(ex))
if self.DEBUG:
print("Error setting time: " + str(ex))
return APIResponse(
status=500,
content_type='application/json',
Expand Down Expand Up @@ -148,11 +153,20 @@ def handle_request(self, request):

elif request.path == '/reboot':
self.reboot()
return APIResponse(
status=200,
content_type='application/json',
content=json.dumps("Rebooting"),
)

elif request.path == '/restart':
self.restart()
return APIResponse(
status=200,
content_type='application/json',
content=json.dumps("Restarting"),
)

else:
return APIResponse(
status=500,
Expand All @@ -161,7 +175,8 @@ def handle_request(self, request):
)

except Exception as ex:
print("Power settings server error: " + str(ex))
if self.DEBUG:
print("Power settings server error: " + str(ex))
return APIResponse(
status=500,
content_type='application/json',
Expand All @@ -172,7 +187,8 @@ def handle_request(self, request):
return APIResponse(status=404)

except Exception as e:
print("Failed to handle UX extension API request: " + str(e))
if self.DEBUG:
print("Failed to handle UX extension API request: " + str(e))
return APIResponse(
status=500,
content_type='application/json',
Expand All @@ -188,7 +204,8 @@ def set_time(self, hours, minutes, seconds=0):
the_date = str(datetime.datetime.now().strftime('%Y-%m-%d'))

time_command = "sudo date --set '" + the_date + " " + str(hours) + ":" + str(minutes) + ":00'"
print("new set date command: " + str(time_command))
if self.DEBUG:
print("new set date command: " + str(time_command))

try:
os.system(time_command)
Expand All @@ -202,30 +219,43 @@ def set_ntp_state(self,new_state):
try:
if new_state:
os.system('sudo timedatectl set-ntp on')
print("Network time turned on")
if self.DEBUG:
print("Network time turned on")
else:
os.system('sudo timedatectl set-ntp off')
print("Network time turned off")
if self.DEBUG:
print("Network time turned off")
except Exception as e:
print("Error changing NTP state: " + str(e))


def shutdown(self):
print("Power settings: shutting down gateway")
if self.DEBUG:
print("Power settings: shutting down gateway")
try:
os.system('sudo shutdown now')
except Exception as e:
print("Error shutting down: " + str(e))


def reboot(self):
print("Power settings: s rebooting gateway")
if self.DEBUG:
print("Power settings: rebooting gateway")
try:
os.system('sudo reboot')
except Exception as e:
print("Error rebooting: " + str(e))


def restart(self):
if self.DEBUG:
print("Power settings: restarting gateway")
try:
os.system('sudo systemctl restart webthings-gateway.service')
except Exception as e:
print("Error rebooting: " + str(e))



def unload(self):
if self.DEBUG:
Expand Down

0 comments on commit 007d534

Please sign in to comment.