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

works with sparkworks v2 api #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion telegram/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM "bitnami/python:2"
RUN pip install --upgrade pip
RUN pip install python-telegram-bot
RUN pip install requests
RUN pip install sparkworks
RUN pip install sparkworks==2.0.0
RUN pip install redis
COPY bot.py /app/bot.py
COPY utils.py /app/utils.py
Expand Down
16 changes: 12 additions & 4 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"What is the temperature in the school?\n" \
"What is the power consumption of the school building?\n\n"

phenomena = s.phenomena()


def start(bot, update):
keyboard = []
for key in utils.locations:
Expand All @@ -56,7 +59,7 @@ def button(bot, update):
if query.data.startswith('location'):
keyboard = []
for school in utils.locations[query.data]['schools']:
keyboard.append([InlineKeyboardButton(school['name'], callback_data='school-' + str(school['id']))])
keyboard.append([InlineKeyboardButton(school['name'], callback_data='school-' + school['uuid'])])

reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(text="Please choose your school: ".format(query.data),
Expand Down Expand Up @@ -97,8 +100,11 @@ def handle_message(bot, update):

if "list" in messageParts:
text = "Sensed properties: \n"
props = []
for property in properties:
text += "- %s\n" % (property['property'].title())
props.append(property['property'].title())
for property in sorted(set(props)):
text += "- %s\n" % (property)
elif "school" in messageParts:
text = "Current School: \n"
text += "- %s\n" % (utils.getSchoolNameFromId(schooId).title())
Expand All @@ -107,8 +113,10 @@ def handle_message(bot, update):
logger.info('Requested "%s" returned "%s"', message, resource)
if resource is not None:
text = ""
bot.send_message(chat_id=update.message.chat_id, text="Retrieving data for %s..." % (utils.getSchoolNameFromId(schooId).title()))
latest = s.latest(resource)
bot.send_message(chat_id=update.message.chat_id,
text="Retrieving data for %s..." % (utils.getSchoolNameFromId(schooId).title()))
resource_uuid = resource['resourceUuid']
latest = s.latest(resource_uuid)
divi1 = 1
divi2 = 1
try:
Expand Down
70 changes: 36 additions & 34 deletions telegram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@
import re

locations = {'location-1': {'name': 'greece',
'schools': [{'id': 19640, 'name': "Γυμνάσιο Πενταβρύσου Καστοριάς"},
{'id': 156886, 'name': "1ο Επαγγελματικό Λύκειο Πατρών"},
{'id': 155877, 'name': "2ο Δημοτικό Σχολείο Παραλίας Πατρών "},
{'id': 27827, 'name': "8ο Γυμνάσιο Πατρών"},
{'id': 506265, 'name': "1o Δημοτικό Σχολείο Ψυχικού Αττικής"},
{'id': 141587, 'name': "1o Γυμνάσιο Ραφήνας"},
{'id': 144024, 'name': "Δημοτικό Σχολείο Λυγιάς"},
{'id': 155851, 'name': "5ο Δημοτικό Σχολείο Νέας Σμύρνης"},
{'id': 157185, 'name': "Ελληνογερμανική Αγωγή"},
{'id': 144242, 'name': "1ο Γυμνάσιο Ν. Φιλαδέλφειας"},
{'id': 141611, 'name': "Πειραματικό Γυμνάσιο Πατρών"},
{'id': 204, 'name': "Πειραματικό Γυμνάσιο Πανεπιστημίου Πατρών"},
{'id': 195, 'name': "Πειραματικό Λύκειο Πανεπιστημίου Πατρών"},
{'id': 3206, 'name': "Πειραματικό Δημοτικό Σχολείο Πανεπιστημίου Πατρών"},
{'id': 155877, 'name': "2ο Δημοτικό Σχολείο Παραλίας Πατρών"},
{'id': 155849, 'name': "6ο Δημοτικό Σχολείο Καισαριανής"},
{'id': 155865, 'name': "46ο Δημοτικό Σχολείο Πατρών"},
{'id': 144243, 'name': "Δημοτικό Σχολείο Μεγίστης"},
{'id': 157089, 'name': "1ο Εργαστηριακό Κέντρο Πατρών"}]},
'location-2': {'name': 'sweeden', 'schools': [{'id': 159705, 'name': "Soderhamn"}, ]},
'schools': [{'uuid': 'd3d8bac4-1ac8-4e7c-891e-03168eff7226', 'name': '46ο Δημοτικό Σχολείο Πατρών'},
{'uuid': 'f065a043-5fb0-454f-bf3f-bbdcb3926fce', 'name': '8ο Γυμνάσιο Βόλου'},
{'uuid': '2951e56d-ad8f-4ea6-bf4d-4f6d46060ab0', 'name': 'Πειραματικό Δημοτικό Σχολείο Πανεπιστημίου Πατρών'},
{'uuid': '8eb0b364-af94-4536-aa5b-12ccf4549e1f', 'name': 'Γυμνάσιο Πενταβρύσου Καστοριάς'},
{'uuid': '79a262dc-e0ca-4ce4-80f3-73afe2a6a1d4', 'name': '2ο Δημοτικό Σχολείο Παραλίας Πατρών'},
{'uuid': '107beec7-859b-4d49-8da7-fbccd39188db', 'name': 'Πειραματικό Γυμνάσιο Πατρών'},
{'uuid': 'f7e14a41-6358-4d48-be6e-28145c16ffa3', 'name': 'Δημοτικό Σχολείο Λυγιάς'},
{'uuid': 'a138ad38-0a48-46e6-b15b-e95ee70af3ea', 'name': '1ο Γυμνάσιο Ν. Φιλαδέλφειας'},
{'uuid': '0876e4a2-9e1a-4fc3-b04a-ff19dff1314b', 'name': '8ο Γυμνάσιο Πατρών'},
{'uuid': 'a63e16b7-84b7-4c46-bb0d-29443a7cce13', 'name': '2o Γυμνάσιο Ν. Ιωνίας'},
{'uuid': '672b3677-4981-42b7-bacf-bd87e42f547a', 'name': '1o Δημοτικό Σχολείο Ψυχικού Αττικής'},
{'uuid': '1e443c40-2090-4ee1-82f2-554fd29219d8', 'name': '1ο Επαγγελματικό Λύκειο Πατρών'},
{'uuid': '1d200eb3-9533-461d-8929-17c75bd0f470', 'name': '5ο Δημοτικό Σχολείο Νέας Σμύρνης'},
{'uuid': '2d930a5f-6819-4815-a126-0825294fee62', 'name': '3o ΓΕΛ Ν. Φιλαδέλφειας'},
{'uuid': '1614db70-b727-47aa-99bf-bbb228f1fed2', 'name': 'Δημοτικό Σχολείο Φιλοθέης'},
{'uuid': '4b5d3819-aa82-4e69-86ca-990c32668ee3', 'name': 'Πειραματικό Γυμνάσιο Πανεπιστημίου Πατρών'},
{'uuid': '4e76caa6-90a2-4d81-a0f8-7d7e18406e8f', 'name': '1o Γυμνάσιο Ραφήνας'},
{'uuid': 'ec51bc2e-ec3a-41e3-8ef8-60fc3bbe0226', 'name': '1ο Εργαστηριακό Κέντρο Πατρών'},
{'uuid': 'bcb7d3d5-ad05-46fa-9a8d-42cc5dbaf34b', 'name': 'Talos'},
{'uuid': 'b70e0ce2-7f6c-4c30-8159-92be56230fff', 'name': '6ο Δημοτικό Σχολείο Καισαριανής'},
{'uuid': '9728d857-7fd7-444e-96f4-def7137c837c', 'name': 'Ελληνογερμανική Αγωγή'},
{'uuid': 'e7103f6a-67f6-4f4a-95df-2566df48c0e4', 'name': 'Δημοτικό Σχολείο Μεγίστης'},
{'uuid': '3bf1856a-1a61-4db1-a40a-dd217116370f', 'name': 'Πειραματικό Λύκειο Πανεπιστημίου Πατρών'}
]},
'location-2': {'name': 'sweeden', 'schools': [{'uuid': 'a1b8fc98-675f-4302-a007-cb67e4863477', 'name': 'Soderhamn'}, ]},
'location-3': {'name': 'italy',
'schools': [{'id': 155076, 'name': "Gramsci-Keynes School"},
{'id': 155077, 'name': "Sapienza"}]}}
'schools': [{'uuid': 'd3537dbe-7123-4986-993c-fa1a44f76b08', 'name': 'Gramsci-Keynes School'},
{'uuid': '7cd850f5-2096-44ac-a404-78972f67b10c', 'name': 'Sapienza'}]}}

schoolNames = []
for item in locations['location-1']['schools']:
Expand All @@ -43,23 +48,20 @@
def getSchoolStrId(id):
return id.replace('school-', '')

def getSchoolNameFromId(uuid):
return [item for item in schoolNames if item["uuid"] == getSchoolStrId(uuid)][0]["name"]

def getSchoolIntId(id):
return int(getSchoolStrId(id))


def getSchoolNameFromId(id):
return [item for item in schoolNames if item["id"] == getSchoolIntId(id)][0]["name"]


def getSiteProperties(s, idStr):
def getSiteProperties(s, group_uuid):
properties = []
resources = s.siteResources({'id': getSchoolIntId('school-' + idStr)})
resources = s.groupResources(group_uuid)
print "resources", len(resources)
for resource in resources:
if resource['uri'].startswith('site-' + idStr):
# TODO: add + group_uuid
if resource['systemName'].startswith('site-'):
properties.append(
{'resourceId': resource['resourceId'], 'uri': resource['uri'],
'property': resource['property'].lower()})
{'resourceUuid': resource['uuid'], 'systemName': resource['systemName'],
'property': s.phenomenonByUuid(resource['phenomenonUuid'])['name'].lower()})
return properties


Expand Down