diff --git a/.gitignore b/.gitignore index 0407309ebbfb28ef7d7cb42da341289a61042564..dd4af9ec8ced1a805c0749a8f17347cec9b0686b 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ build .vscode/* store.json -scripts/homeone/event_header_generator/generated/ +scripts/event_header_generator/generated/ diff --git a/scripts/event_header_generator/EventFunctions.cpp.template b/scripts/event_header_generator/EventFunctions.cpp.template new file mode 100644 index 0000000000000000000000000000000000000000..18f7690704c127e2bd5eb516097a08868e505565 --- /dev/null +++ b/scripts/event_header_generator/EventFunctions.cpp.template @@ -0,0 +1,59 @@ +/* Copyright (c) 2018 Skyward Experimental Rocketry + * Authors: Luca Erbetta + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + ****************************************************************************** + * THIS FILE IS AUTOGENERATED. DO NOT EDIT. * + ****************************************************************************** + */ + +// Generated from: {sheet_link} +// Autogen date: {date} + + +#include "Events.h" +#include "Topics.h" + +#include <map> + +namespace HomeoneBoard +{{ + +string getEventString(uint8_t event) +{{ + static const map<uint8_t, string> event_string_map {{ +{event_map_data} + }}; + auto it = event_string_map.find(event); + return it == event_string_map.end() ? "EV_UNKNOWN" : it->second; +}} + +string getTopicString(uint8_t topic) +{{ + static const map<uint8_t, string> topic_string_map{{ +{topic_map_data} + }}; + auto it = topic_string_map.find(topic); + return it == topic_string_map.end() ? "TOPIC_UNKNOWN" : it->second; +}} + +}} \ No newline at end of file diff --git a/scripts/event_header_generator/Events.h.template b/scripts/event_header_generator/Events.h.template new file mode 100644 index 0000000000000000000000000000000000000000..2c4937a1fdd509e3307baf666120b77fdd067fec --- /dev/null +++ b/scripts/event_header_generator/Events.h.template @@ -0,0 +1,67 @@ +/* Copyright (c) 2018 Skyward Experimental Rocketry + * Authors: Luca Erbetta + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + ****************************************************************************** + * THIS FILE IS AUTOGENERATED. DO NOT EDIT. * + ****************************************************************************** + */ + +// Generated from: {sheet_link} +// Autogen date: {date} + +#ifndef SRC_SHARED_BOARDS_HOMEONE_EVENTS_H +#define SRC_SHARED_BOARDS_HOMEONE_EVENTS_H + +#include <cstdint> +#include <string> + +#include "events/Event.h" +#include "events/EventBroker.h" +#include "EventClasses.h" +#include "Topics.h" + +using std::string; +using std::map; + +namespace HomeoneBoard +{{ +/** + * Definition of all events in the Homeone Board software + * Refer to section 5.1.1 of the Software Design Document. + */ +enum Events : uint8_t +{{ +{enum_data} +}}; + +/** + * @brief Returns the name of the provided event + * + * @param event + * @return string + */ +string getEventString(uint8_t event); + +}} + +#endif /* SRC_SHARED_BOARDS_HOMEONE_EVENTS_H */ diff --git a/scripts/event_header_generator/README.md b/scripts/event_header_generator/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f87e46c67ebcec406b02c58e10dbe91b00a0c8d3 --- /dev/null +++ b/scripts/event_header_generator/README.md @@ -0,0 +1,14 @@ +# Events Generator Script + +This script generates the Events.h and Topics.h heander file from a GoogleSheets document on Google Drive. + +To execute the script: + +``` +pip install --upgrade google-api-python-client +pip install oauth2client +python scripts/homeone/event_header_generator/event_generator.py +``` + +The first time a browser window should open asking for your credentials (use the Skyward ones). +If it doesn't and only gives you an error, try executing it by clicking on the icon. \ No newline at end of file diff --git a/scripts/event_header_generator/Topics.h.template b/scripts/event_header_generator/Topics.h.template new file mode 100644 index 0000000000000000000000000000000000000000..43e72f7302673cf61bbb3a6605681feb08cf7236 --- /dev/null +++ b/scripts/event_header_generator/Topics.h.template @@ -0,0 +1,61 @@ +/* Copyright (c) 2018 Skyward Experimental Rocketry + * Authors: Luca Erbetta + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + ****************************************************************************** + * THIS FILE IS AUTOGENERATED. DO NOT EDIT. * + ****************************************************************************** + */ + +// Generated from: {sheet_link} +// Autogen date: {date} + +#ifndef SRC_SHARED_BOARDS_HOMEONE_TOPICS_H +#define SRC_SHARED_BOARDS_HOMEONE_TOPICS_H + +#include <stdint.h> +#include <string> + +using std::map; +using std::string; + +namespace HomeoneBoard +{{ +/** + * Definition of various event topics to use in the EventBroker + */ +enum Topics : uint8_t +{{ +{enum_data} +}}; + +/** + * @brief Returns the name of the provided event + * + * @param event + * @return string + */ +string getTopicString(uint8_t topic); + +}} // namespace HomeoneBoard + +#endif /* SRC_SHARED_BOARDS_HOMEONE_TOPICS_H_ */ diff --git a/scripts/event_header_generator/credentials.json b/scripts/event_header_generator/credentials.json new file mode 100644 index 0000000000000000000000000000000000000000..dae318afc5661116b82fe75f80f4316e538e670a --- /dev/null +++ b/scripts/event_header_generator/credentials.json @@ -0,0 +1 @@ +{"installed":{"client_id":"1025168905991-tv31etsgm3lecodc5c798shqciekad40.apps.googleusercontent.com","project_id":"homeone-event-ge-1540834105298","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://www.googleapis.com/oauth2/v3/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"Yhaf67HuHR4DyXZKNXWu2lre","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} \ No newline at end of file diff --git a/scripts/event_header_generator/event_generator.py b/scripts/event_header_generator/event_generator.py new file mode 100644 index 0000000000000000000000000000000000000000..cbfe9ece20ade7da0bd27f1892055f9c42d57465 --- /dev/null +++ b/scripts/event_header_generator/event_generator.py @@ -0,0 +1,202 @@ +#!/usr/bin/python3 + +# Copyright (c) 2018 Skyward Experimental Rocketry +# Authors: Luca Erbetta +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# + +from googleapiclient.discovery import build +from httplib2 import Http +from oauth2client import file, client, tools + +import re +import datetime +import sys +from os.path import join +import os + +OUTPUT_FOLDER = "generated" +SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly' + +service = None + +# Spreadsheet file used to generate the events. The can be found in the link +# to the spreadsheet, for example: +# https://docs.google.com/spreadsheets/d/1msICDqJtSseSP_JAoAPoKIzpKlI6bI2n8lwws1X2hz4/ +# --> +# 1msICDqJtSseSP_JAoAPoKIzpKlI6bI2n8lwws1X2hz4 +SPREADSHEET_ID = '1msICDqJtSseSP_JAoAPoKIzpKlI6bI2n8lwws1X2hz4' +EVENTS_RANGE_NAME = 'EventList!A2:A' +TOPICS_RANGE_NAME = 'Topics!B3:B' + + +def auth(): + try: + store = file.Storage('store.json') + creds = store.get() + if not creds or creds.invalid: + flow = client.flow_from_clientsecrets('credentials.json', SCOPES) + creds = tools.run_flow(flow, store) + + global service + service = build('sheets', 'v4', http=creds.authorize(Http())) + return True + except: + print("Authentication error:", sys.exc_info()[0]) + return False + + +def load_events(): + result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID, + range=EVENTS_RANGE_NAME).execute() + lines = result.get('values', []) + + # Event names start with EV_ and contains only uppercase letters or underscores + re_event = re.compile(r'(?P<ev>^EV_([A-Z_]+)+)') + + # Only return lines with valid events, remove additional data + events = [] + for l in lines: + m = re_event.match(l[0]) + if m is not None: + events.append(m.group('ev')) + else: + print("Skipped line containing invalid event: {}".format(l)) + + return events + + +def load_topics(): + result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID, + range=TOPICS_RANGE_NAME).execute() + lines = result.get('values', []) + re_topics = re.compile(r'(?P<topic>^TOPIC_([A-Z_]+)+)') + + # Only return lines with valid topics, remove additional data + topics = [] + for l in lines: + m = re_topics.match(l[0]) + if m is not None: + topics.append(m.group('topic')) + else: + print("Skipped line containing invalid topics: {}".format(l)) + + return topics + + +def has_duplicates(lst): + if len(lst) != len(set(lst)): + return True + return False + + + +print("Homeone on-board software event header generator v0.2") +print("Google sheets API auth in progress...") + +if auth(): + print("Auth successfull.") +else: + exit() + +#directory = os.path.dirname(OUTPUT_FOLDER) +if not os.path.exists(OUTPUT_FOLDER): + os.mkdir(OUTPUT_FOLDER) + +print("Reading from: https://docs.google.com/spreadsheets/d/12TecOmDd7Uot-MvXkCbhDJRU48-XO6s5ChKDlr4AOvI") + +events = load_events() +topics = load_topics() + +# Check duplicates +if has_duplicates(events): + print("Duplicate events found! Terminating.") + exit() + +if has_duplicates(topics): + print("Duplicate topics found! Terminating.") + exit() + +print("{} events loaded.".format(len(events))) +print("{} topics loaded.".format(len(topics))) + +enum_str = "" +event_map_str = "" + +date = datetime.datetime.now() +link = "https://docs.google.com/spreadsheets/d/{id}".format(id=SPREADSHEET_ID) + + +# Events.h generation + +print("Generating Events.h...") + +for e in events: + endl = ",\n" if e != events[-1] else "" + enum_str += " " + e + \ + (" = EV_FIRST_SIGNAL" if e == events[0] else "") + endl + event_map_str += " {{ {event}, {string} }}{endl}".format( + event=e, string='"' + e + '"', endl=endl) + +with open('Events.h.template', 'r') as template_file: + template = template_file.read() + +template = template.format(sheet_link=link, date=date, + enum_data=enum_str) + +with open(join(OUTPUT_FOLDER, 'Events.h'), 'w') as header_file: + header_file.write(template) + +print("Events.h successfully generated.") +# Topics.h generation + +print("Generating Topics.h...") +enum_str = "" +topic_map_str = "" + +for t in topics: + endl = ",\n" if t != topics[-1] else "" + enum_str += " " + t + endl + topic_map_str += " {{ {topics}, {string} }}{endl}".format( + topics=t, string='"' + t + '"', endl=endl) + +with open('Topics.h.template', 'r') as template_file: + template = template_file.read() + +template = template.format(sheet_link=link, date=date, + enum_data=enum_str) + +with open(join(OUTPUT_FOLDER, 'Topics.h'), 'w') as header_file: + header_file.write(template) + +with open('EventFunctions.cpp.template', 'r') as cpp_template_file: + cpp = cpp_template_file.read() + +cpp = cpp.format(sheet_link=link, date=date, + event_map_data=event_map_str, topic_map_data=topic_map_str) + +with open(join(OUTPUT_FOLDER, 'EventFunctions.cpp'), 'w') as cpp_file: + cpp_file.write(cpp) + +print("Topics.h successfully generated.") + +print() +print("All files successfully generated. Please move Events.h, Topics.h, EventFunction.cpp into your project sources.") +print(".... Done.")