Skip to content

Commit

Permalink
0.0.4
Browse files Browse the repository at this point in the history
- better handling of situation where a source does not have a value yet (e.g. after a gateway reboot)
- centered response text
  • Loading branch information
flatsiedatsie authored Jul 20, 2020
1 parent 13b0f4b commit 531928d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
4 changes: 4 additions & 0 deletions css/extension.css
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@
}


.extension-followers-response-data{
text-align:center;
}



@media screen and (min-width: 1025px) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
},
"short_name": "Followers",
"version": "0.0.3",
"version": "0.0.4",
"web_accessible_resources": [
"css/*.css",
"images/*.svg",
Expand Down
53 changes: 30 additions & 23 deletions pkg/followers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def clock(self):
#print("not enabled")
continue


#print("")

api_get_result = self.api_get( '/things/' + str(item['thing1']) + '/properties/' + str(item['property1']))
#print("detail: " + str(item['thing1']))

Expand All @@ -224,11 +225,17 @@ def clock(self):
#print("API GET was succesfull")
original_value = api_get_result[key]
#if self.DEBUG:
# print("type of original_value variable is: " + str(type(original_value)))
# print("got original value from API: " + str(original_value))


if original_value is "":
#print("original value is an empty string.") # this happens if the gateway has just been rebooted, and the property doesn not have a value yet.
continue

if min(float(item['limit1']), float(item['limit2'])) <= float(original_value) <= max(float(item['limit1']), float(item['limit2'])):
#if original_value in range(float(item['limit1']), float(item['limit2'])):
output = str( translate(original_value, item['limit1'], item['limit2'], item['limit3'], item['limit4']) )
output = translate(original_value, item['limit1'], item['limit2'], item['limit3'], item['limit4'])
#print("got translated output: " + str(output))


Expand All @@ -238,12 +245,13 @@ def clock(self):

if item['previous_value'] is not get_int_or_float(output):

item['previous_value'] = get_int_or_float(output)
try:
item['previous_value'] = get_int_or_float(output)

if self.DEBUG:
print("new value, will update via API: " + str(item['previous_value']))
if self.DEBUG:
print("new value, will update via API: " + str(item['previous_value']))

try:

data_to_put = { str(item['property2']) : get_int_or_float(output) }
#print(str(data_to_put))
api_put_result = self.api_put( '/things/' + str(item['thing2']) + '/properties/' + str(item['property2']), data_to_put )
Expand Down Expand Up @@ -310,7 +318,6 @@ def handle_request(self, request):
content=json.dumps({'state' : "Internal error: no thing data", 'items' : []}),
)



elif request.path == '/update_items':
try:
Expand All @@ -330,7 +337,6 @@ def handle_request(self, request):
content=json.dumps("Error updating items: " + str(ex)),
)



else:
return APIResponse(
Expand Down Expand Up @@ -360,8 +366,6 @@ def handle_request(self, request):
)




def unload(self):
self.running = False
if self.DEBUG:
Expand Down Expand Up @@ -392,9 +396,9 @@ def api_get(self, api_path):
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + str(self.token),
}, verify=False, timeout=3)
if self.DEBUG:
print("API GET: " + str(r.status_code) + ", " + str(r.reason))
}, verify=False, timeout=2)
#if self.DEBUG:
# print("API GET: " + str(r.status_code) + ", " + str(r.reason))

if r.status_code != 200:
if self.DEBUG:
Expand All @@ -415,12 +419,12 @@ def api_get(self, api_path):
def api_put(self, api_path, json_dict):
"""Sends data to the WebThings Gateway API."""

if self.DEBUG:
print("PUT > api_path = " + str(api_path))
print("PUT > json dict = " + str(json_dict))
print("PUT > self.server = " + str(self.server))
#if self.DEBUG:
#print("PUT > api_path = " + str(api_path))
#print("PUT > json dict = " + str(json_dict))
#print("PUT > self.server = " + str(self.server))
#print("PUT > self.token = " + str(self.token))


headers = {
'Accept': 'application/json',
Expand All @@ -432,15 +436,18 @@ def api_put(self, api_path, json_dict):
json=json_dict,
headers=headers,
verify=False,
timeout=5
timeout=2
)
if self.DEBUG:
print("API PUT: " + str(r.status_code) + ", " + str(r.reason))
#if self.DEBUG:
#print("API PUT: " + str(r.status_code) + ", " + str(r.reason))

if r.status_code != 200:
print("Error communicating: " + str(r.status_code))
#if self.DEBUG:
# print("Error communicating: " + str(r.status_code))
return {"error": str(r.status_code)}
else:
if self.DEBUG:
print("API PUT response: " + str(r.text))
return json.loads(r.text)

except Exception as ex:
Expand Down Expand Up @@ -518,4 +525,4 @@ def get_int_or_float(v):
if number_as_float == number_as_int:
return number_as_int
else:
return float( int( number_as_float * 100) / 100)
return float( int( number_as_float * 1000) / 1000)

0 comments on commit 531928d

Please sign in to comment.