diff --git a/Makefile b/Makefile index f0f8b9c..37e237b 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,9 @@ +CATFEEDER = /usr/local/share/.cat-feeder +CURDIR = $(shell pwd) + configure: + mkdir -p $(CATFEEDER) go get github.com/gorilla/mux + +install: + cp $(CURDIR)/common/common-names.json $(CATFEEDER)/ diff --git a/catfeeder-app/commonLibs.py b/catfeeder-app/commonLibs.py new file mode 100644 index 0000000..a6c15c4 --- /dev/null +++ b/catfeeder-app/commonLibs.py @@ -0,0 +1,17 @@ +import json + +class CommonLibs(): + ### + # @brief: Parse the common-names.json file + ### + def __init__(self): + self.common_names = {} + + with open('/usr/local/share/.cat-feeder/common-names.json') as f: + self.common_names = json.load(f) + + ### + # @brief: Return the path for the logfile + ### + def GetLogFilepath(self): + return self.common_names['log_filepath'] diff --git a/catfeeder-app/src/catfeeder/app.py b/catfeeder-app/src/catfeeder/app.py index 6f52271..331a0cb 100644 --- a/catfeeder-app/src/catfeeder/app.py +++ b/catfeeder-app/src/catfeeder/app.py @@ -124,7 +124,7 @@ def sendFeedingTime(self, widget): resp = req.text logger.info("Sent feeding time:" + str(resp)) - self.error_label.text = "" + self.error_label.text = "Feeding times sent" ### # @brief: Get any existing feeding time payload @@ -155,7 +155,7 @@ def getFeedingTimes(self, widget): self.time_table.data.insert(i, time) logger.info("Received feeding times:" + str(feeding_times)) self.send_butt.enabled = True - self.error_label.text = "" + self.error_label.text = "Received feeding times" def main(): return CatFeeder() diff --git a/catfeeder-app/utils.py b/catfeeder-app/utils.py index ff9a8e1..53c8fc7 100644 --- a/catfeeder-app/utils.py +++ b/catfeeder-app/utils.py @@ -1,5 +1,8 @@ +import commonLibs import logging +common = commonLibs.CommonLibs() + ### # @brief: Create new logging object with the identity of `name` # @@ -9,7 +12,7 @@ ### def Applogger(name): logging.basicConfig(level=logging.INFO, - filename='/tmp/cat-feeder.log', + filename=common.GetLogFilepath(), format='%(asctime)s %(name)s - %(message)s', datefmt='%Y/%m/%d %H:%M:%S' ) diff --git a/catfeeder-machine/common.go b/catfeeder-machine/common.go new file mode 100644 index 0000000..22d070e --- /dev/null +++ b/catfeeder-machine/common.go @@ -0,0 +1,41 @@ +package main + +import ( + "io/ioutil" + "encoding/json" + "os" +) + +type CommonNames struct { + Common_Names string `json:"log_filepath"` +} + +var common_names CommonNames + +/*** + * @brief: Read the common-names.json file. This holds values + * that both the app and machine use + * + * @return: nil on success, else error + ***/ +func InitCommon() error { + common_names_os, err := os.Open("/usr/local/share/.cat-feeder/common-names.json") + if err != nil { + return err + } + defer common_names_os.Close() + + common_names_io, _ := ioutil.ReadAll(common_names_os) + json.Unmarshal(common_names_io, &common_names) + + return err +} + +/*** + * @brief: Return the location of the log file + * + * @return: See @brief + ***/ +func GetLogFilepath() string { + return common_names.Common_Names +} diff --git a/catfeeder-machine/golog.go b/catfeeder-machine/golog.go index 1955312..e7c7d7a 100644 --- a/catfeeder-machine/golog.go +++ b/catfeeder-machine/golog.go @@ -13,7 +13,7 @@ type Golog struct { Prefix string } -const LogFilename string = "/tmp/cat-feeder.log" +var log_fpath string = "" /** * @brief: Checks to see if the log file is present from a previous @@ -22,11 +22,13 @@ const LogFilename string = "/tmp/cat-feeder.log" * @return: nil on success, else error **/ func InitGolog() error { - finfo, err := os.Stat(LogFilename) + log_fpath = GetLogFilepath() + + finfo, err := os.Stat(log_fpath) if err == nil { - path := strings.Split(LogFilename, finfo.Name())[0] + path := strings.Split(log_fpath, finfo.Name())[0] err = os.Rename( - LogFilename, + log_fpath, path + time.Now().Format("20060102T150405") + "-" + finfo.Name(), ) if err != nil { @@ -46,7 +48,7 @@ func InitGolog() error { * @return: New logger, nil if error **/ func OpenGolog(prefix string) *Golog { - fpath, err := os.OpenFile(LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) + fpath, err := os.OpenFile(log_fpath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) if err != nil { return nil diff --git a/catfeeder-machine/main.go b/catfeeder-machine/main.go index 3f84eeb..901c024 100644 --- a/catfeeder-machine/main.go +++ b/catfeeder-machine/main.go @@ -12,6 +12,7 @@ type FeedingTimes struct { Minute int `json:"minute"` } +var ok = true var has_been_fed bool = false var feeding_times []FeedingTimes var mut sync.Mutex @@ -37,13 +38,31 @@ func TimeToFeedCat() bool { return false } +/*** + * @brief: Initialize features in cat-feeder machine + * - Common + * - Golog + ***/ func init() { - if err := InitGolog(); err != nil { - fmt.Println("Failed to initialize log rollover:", err) + err := InitCommon() + if err != nil { + fmt.Println("Failed to initialize Common:", err) + ok = false + } + + err = InitGolog() + if (err != nil) && ok { + fmt.Println("Failed to initialize Golog:", err) + ok = false } } func main() { + if !ok { + fmt.Println("Error in initialization") + return + } + /* Open log file */ cat_log := OpenGolog("cat-manager") diff --git a/common/common-names.json b/common/common-names.json new file mode 100644 index 0000000..d4bbc81 --- /dev/null +++ b/common/common-names.json @@ -0,0 +1,3 @@ +{ + "log_filepath":"/tmp/cat-feeder.log" +}