diff --git a/catfeeder-app/TLSAdapter.py b/catfeeder-app/TLSAdapter.py new file mode 100644 index 0000000..6c59c05 --- /dev/null +++ b/catfeeder-app/TLSAdapter.py @@ -0,0 +1,11 @@ +import ssl +import sys +import requests +from requests.adapters import HTTPAdapter +from requests.packages.urllib3.poolmanager import PoolManager + +class Tls12HttpAdapter(HTTPAdapter): + def init_poolmanager(self, connections, maxsize, block=False): + self.poolmanager = PoolManager( + num_pools=connections, maxsize=maxsize, + block=block, ssl_version=ssl.PROTOCOL_TLSv1_2) diff --git a/catfeeder-app/commonLibs.py b/catfeeder-app/commonLibs.py index a6c15c4..7978bea 100644 --- a/catfeeder-app/commonLibs.py +++ b/catfeeder-app/commonLibs.py @@ -15,3 +15,9 @@ def __init__(self): ### def GetLogFilepath(self): return self.common_names['log_filepath'] + + ### + # @brief: Return the path for the logfile + ### + def GetFQDN(self): + return self.common_names['FQDN'] diff --git a/catfeeder-app/src/catfeeder/__pycache__/__init__.cpython-36.pyc b/catfeeder-app/src/catfeeder/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index 5f7b71b..0000000 Binary files a/catfeeder-app/src/catfeeder/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/catfeeder-app/src/catfeeder/__pycache__/__main__.cpython-36.pyc b/catfeeder-app/src/catfeeder/__pycache__/__main__.cpython-36.pyc deleted file mode 100644 index f7c9a1b..0000000 Binary files a/catfeeder-app/src/catfeeder/__pycache__/__main__.cpython-36.pyc and /dev/null differ diff --git a/catfeeder-app/src/catfeeder/__pycache__/app.cpython-36.pyc b/catfeeder-app/src/catfeeder/__pycache__/app.cpython-36.pyc deleted file mode 100644 index bdaa944..0000000 Binary files a/catfeeder-app/src/catfeeder/__pycache__/app.cpython-36.pyc and /dev/null differ diff --git a/catfeeder-app/src/catfeeder/app.py b/catfeeder-app/src/catfeeder/app.py index 331a0cb..e83f168 100644 --- a/catfeeder-app/src/catfeeder/app.py +++ b/catfeeder-app/src/catfeeder/app.py @@ -1,20 +1,24 @@ """ An app to feed the cats """ +import commonLibs import json import requests import toga import utils +import TLSAdapter from toga.style import Pack from toga.style.pack import COLUMN, ROW -BASE_URL = "http://localhost:6969" +BASE_URL = commonLibs.CommonLibs().GetFQDN() CREATE_TIME = BASE_URL + "/feedingTime" RECIEVE_A_TIME = BASE_URL + "/feedingTime" RECIEVE_ALL_TIMES = BASE_URL + "/feedingTimes" feeding_times = [] logger = utils.Applogger('cat-app') +cat_session = requests.Session() +cat_session.mount(BASE_URL, TLSAdapter.Tls12HttpAdapter()) class CatFeeder(toga.App): # TODO: Come up with better variable names @@ -113,7 +117,7 @@ def sendFeedingTime(self, widget): send_feeding_times.append(data) try: - req = requests.post( + req = cat_session.post( url = CREATE_TIME, data = str(json.dumps(send_feeding_times)) ) @@ -131,10 +135,10 @@ def sendFeedingTime(self, widget): ### def getFeedingTimes(self, widget): try: - req = requests.get(url = RECIEVE_ALL_TIMES) - if req == None: - logger.error("Uh oh! Received invalid feeding times: Feeding-Time(" + req + ")") - self.error_label.text = "Received invalid feeding times:", req + req = cat_session.get(url = RECIEVE_ALL_TIMES) + if req.json() is None: + logger.error("Uh oh! Received invalid feeding times: Feeding-Time(" + str(req) + ")") + self.error_label.text = "Received invalid feeding times:", str(req) return if req.status_code != 200: logger.error("Uh oh! Request unsuccessful: HTTP(" + str(req.status_code) + ")") diff --git a/catfeeder-machine/common.go b/catfeeder-machine/common.go index 22d070e..ec48569 100644 --- a/catfeeder-machine/common.go +++ b/catfeeder-machine/common.go @@ -8,6 +8,8 @@ import ( type CommonNames struct { Common_Names string `json:"log_filepath"` + FQDN string `json:"FQDN"` + PORT string `json:"PORT"` } var common_names CommonNames @@ -39,3 +41,21 @@ func InitCommon() error { func GetLogFilepath() string { return common_names.Common_Names } + +/*** + * @brief: Return the Fully Qualified Domain Name + * + * @return: See @brief + ***/ +func GetFQDN() string { + return common_names.FQDN +} + +/*** + * @brief: Return the port that the server is listening on + * + * @return: See @brief + ***/ +func GetPORT() string { + return common_names.PORT +} diff --git a/catfeeder-machine/rest-api.go b/catfeeder-machine/rest-api.go index 8d6e289..5c98527 100644 --- a/catfeeder-machine/rest-api.go +++ b/catfeeder-machine/rest-api.go @@ -40,8 +40,15 @@ func CreateNewFeedTime(w http.ResponseWriter, r *http.Request) { rest_log.Println("Received feeding times:" + FeedingTimeStr(ft)) } +/*** + * @brief: Handle to feed the cat. + ***/ +func CreateFeedNow(w http.ResponseWriter, r *http.Request) { + fmt.Println("Place holder to feed cat now, will update once TID003 is complete") +} + /** - * @brief: Handle for recieving a specific feeding time. + * @brief: Handle for returning a specific feeding time. **/ func ReturnSingleFeedingTime(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -59,7 +66,7 @@ func ReturnSingleFeedingTime(w http.ResponseWriter, r *http.Request) { } /** - * @brief: Handle for recieving all the feeding times. + * @brief: Handle for returning all the feeding times. **/ func ReturnAllFeedingTimes(w http.ResponseWriter, r *http.Request) { mut.Lock() @@ -69,12 +76,13 @@ func ReturnAllFeedingTimes(w http.ResponseWriter, r *http.Request) { } /** - * @brief: Handle to recieving info on REST API. + * @brief: Handle to returning info on REST API. **/ func HomePage(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Cat Feeding times") fmt.Fprintln(w, "-----------------------") fmt.Fprintln(w, "Create new feeding times with '/feedingTime'") + fmt.Fprintln(w, "Create request to feed cat now with '/feedNow'") fmt.Fprintln(w, "Return all feeding time with '/feedingTimes'") fmt.Fprintln(w, "Return single feeding time with '/feedingTime/'") } @@ -88,9 +96,13 @@ func HandleRequests() { myRouter := mux.NewRouter().StrictSlash(true) myRouter.HandleFunc("/", HomePage) myRouter.HandleFunc("/feedingTime", CreateNewFeedTime).Methods("POST") + myRouter.HandleFunc("/feedNow", CreateFeedNow).Methods("POST") myRouter.HandleFunc("/feedingTimes", ReturnAllFeedingTimes).Methods("GET") myRouter.HandleFunc("/feedingTime/{id}", ReturnSingleFeedingTime).Methods("GET") - rest_log.Println("Listening on 127.0.0.1:6969") - rest_log.Println(http.ListenAndServe(":6969", myRouter).Error()) + listenOn := "localhost:" + GetPORT() + rest_log.Println("Listening on " + listenOn) + + rest_log.Println("Connect to " + GetFQDN() + " for more information") + rest_log.Println(http.ListenAndServe(listenOn, myRouter).Error()) } diff --git a/common/common-names.json b/common/common-names.json index d4bbc81..cd897ed 100644 --- a/common/common-names.json +++ b/common/common-names.json @@ -1,3 +1,5 @@ { - "log_filepath":"/tmp/cat-feeder.log" + "log_filepath":"/tmp/cat-feeder.log", + "FQDN":"https://canescent-saola-6329.dataplicity.io/", + "PORT":"80" } diff --git a/task.md b/task.md new file mode 100644 index 0000000..0f3fbb0 --- /dev/null +++ b/task.md @@ -0,0 +1,35 @@ +# All +- [ ] loera - 007: Create a logger for both the machine and app + * [ ] It should rollover any existing logs and then create a new log +- [ ] 008: Update the configuration to install what is needed and where executables/logs should be stored in +- [ ] loera - 011: Create a method sharing common variables between the app and machine + * In TID007, both of them use the file name "/tmp/cat-feeder.log", this should be shared instead of written in both of them + +# Machine +- [x] loera - 001: Monitor the time + * [x] Only feed cat specified time, then keep feeder closed for the rest of the time + * [x] Make sure to lock/unlock `[]feeder_times` since it's used in both monitoring and REST API thread +- [ ] loera - 002: Create REST API + * [x] POST new feeding times + * [x] GET all/single feeding times + * [ ] POST to feed cat +- [ ] 003: Get them servo motors working + * [ ] Get the motor to open and close + * [ ] Add it to the monitoring +- [ ] 005: Communication from machine and app + * Over WiFi? + * Bluetooth? + * Host app on a webserver and let the user navigate to webpage? +- [ ] 009: Update `/feedNow` REST API to use the servo motors + +# App +- [ ] 004: Continue development: + * [ ] Delete time in table + * [ ] Edit time in table + * [ ] Check if time is valid + * [ ] Allow 24 hour and 12 hour (AM/PM) + > Currently, only has 24 hour +- [ ] 006: Add button to feed + * Send command to feed cat + * Tell Christian to finish up the REST API to start this +- [ ] 010: Display last time fed on a label