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

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
cleanup and webthings 1.0 fixes
  • Loading branch information
flatsiedatsie authored Dec 8, 2020
1 parent cdc86b1 commit fba53b0
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions pkg/power_settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Power Settings API handler."""


import functools
import json
import os
import sys
sys.path.append(path.join(path.dirname(path.abspath(__file__)), 'lib'))
import json
from time import sleep
import datetime
import functools
import subprocess


try:
from gateway_addon import APIHandler, APIResponse
#print("succesfully loaded APIHandler and APIResponse from gateway_addon")
Expand All @@ -18,6 +19,18 @@
print = functools.partial(print, flush=True)



_TIMEOUT = 3

_CONFIG_PATHS = [
os.path.join(os.path.expanduser('~'), '.webthings', 'config'),
]

if 'WEBTHINGS_HOME' in os.environ:
_CONFIG_PATHS.insert(0, os.path.join(os.environ['WEBTHINGS_HOME'], 'config'))



class PowerSettingsAPIHandler(APIHandler):
"""Power settings API handler."""

Expand Down Expand Up @@ -116,7 +129,8 @@ def handle_request(self, request):


elif request.path == '/set-ntp':
print("New NTP state = " + str(request.body['ntp']))
if self.DEBUG:
print("New NTP state = " + str(request.body['ntp']))
self.set_ntp_state(request.body['ntp'])
return APIResponse(
status=200,
Expand Down Expand Up @@ -147,7 +161,7 @@ def handle_request(self, request):
)

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

def set_time(self, hours, minutes, seconds=0):
print("Setting the new time")
if self.DEBUG:
print("Setting the new time")

if hours.isdigit() and minutes.isdigit():

Expand All @@ -182,7 +197,8 @@ def set_time(self, hours, minutes, seconds=0):


def set_ntp_state(self,new_state):
print("Setting NTP state")
if self.DEBUG:
print("Setting NTP state")
try:
if new_state:
os.system('sudo timedatectl set-ntp on')
Expand All @@ -195,15 +211,15 @@ def set_ntp_state(self,new_state):


def shutdown(self):
print("Shutting down gateway")
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("Rebooting gateway")
print("Power settings: s rebooting gateway").
try:
os.system('sudo reboot')
except Exception as e:
Expand All @@ -220,7 +236,6 @@ def unload(self):

def run_command(cmd, timeout_seconds=60):
try:

p = subprocess.run(cmd, timeout=timeout_seconds, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)

if p.returncode == 0:
Expand Down

0 comments on commit fba53b0

Please sign in to comment.