Skip to content
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
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)/
17 changes: 17 additions & 0 deletions catfeeder-app/commonLibs.py
Original file line number Diff line number Diff line change
@@ -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']
4 changes: 2 additions & 2 deletions catfeeder-app/src/catfeeder/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
5 changes: 4 additions & 1 deletion catfeeder-app/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import commonLibs
import logging

common = commonLibs.CommonLibs()

###
# @brief: Create new logging object with the identity of `name`
#
Expand All @@ -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'
)
Expand Down
41 changes: 41 additions & 0 deletions catfeeder-machine/common.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 7 additions & 5 deletions catfeeder-machine/golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
23 changes: 21 additions & 2 deletions catfeeder-machine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down
3 changes: 3 additions & 0 deletions common/common-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"log_filepath":"/tmp/cat-feeder.log"
}