Skip to content

Commit

Permalink
Incr. VERSION. Updated README. Added extra debug output to allow bett…
Browse files Browse the repository at this point in the history
…er debuging.
  • Loading branch information
guerillagrow committed Jul 18, 2018
1 parent 75fb877 commit db4b4a2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ Big thanks to the developers of gobot.io! They made it so muche easier for me. C
## 6.) TODOs

* Add tests
* Fix broken beego file based session store. Build custom store using gorilla/sessions package
* Maybe add ability for measure PH of water if we grow in hydro culture
See: http://www.sparkyswidgets.com/product/miniph/
http://wiki.seeedstudio.com/Grove-PH_Sensor/#usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.0
v1.0.1
18 changes: 18 additions & 0 deletions cmd/sensd/sensd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
Expand All @@ -26,6 +27,7 @@ var Config *jstorage.Storage = jstorage.NewStorage()
var mainQueue chan common.Response

var ARG_CONFIG_FILE *string
var ARG_Debug *bool

func main() {
sigs := make(chan os.Signal, 1)
Expand All @@ -41,6 +43,7 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

ARG_CONFIG_FILE = flag.String("c", "", "JSON config file")
ARG_Debug = flag.Bool("debug", false, "Debug mode")
flag.Parse()

mainQueue = make(chan common.Response)
Expand Down Expand Up @@ -91,12 +94,14 @@ func sensorWorkT(sensorName string, dhtType dht.SensorType) {
var res common.Response
sensorPin, cerr := Config.GetInt(fmt.Sprintf("devices/%s/gpio", strings.ToLower(sensorName)))
if cerr != nil {
DebugLogError(cerr)
time.Sleep(2 * time.Second)
continue
}
tmp, hum, _, err := dht.ReadDHTxxWithRetry(dhtType, int(sensorPin), false, 4)

if err != nil {
DebugLogError(err)
//log.Println(err)
time.Sleep(2 * time.Second)
continue
Expand Down Expand Up @@ -149,6 +154,7 @@ func sensorWorkS(sensorName string) {
var res common.Response
sensorPin, cerr := Config.GetInt(fmt.Sprintf("devices/%s/gpio", strings.ToLower(sensorName)))
if cerr != nil {
DebugLogError(cerr)
time.Sleep(2 * time.Second)
continue
}
Expand Down Expand Up @@ -188,3 +194,15 @@ func sensorWorkS(sensorName string) {
time.Sleep(time.Duration(t1Sleep) * time.Second)
}
}

func DebugLogError(e error) {
if *ARG_Debug {
log.Println("[DEBUG]", e)
}
}

func DebugLog(s string) {
if *ARG_Debug {
log.Println("[DEBUG]", s)
}
}
58 changes: 44 additions & 14 deletions models/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// !NOTE: maybe add flag pkg to parse -c as config file flag/arg

var DB *storm.DB
var GoBox *Box
var GoBox *Box = NewBox()
var BoxConfig *jstorage.Storage = jstorage.NewStorage()
var ARG_ConfigFile *string
var ARG_Debug *bool
Expand Down Expand Up @@ -64,7 +64,6 @@ func Init() {
InitDatabase()

devices := []gobot.Device{}
GoBox = NewBox()
GoBox.RPIAdaptor = raspi.NewAdaptor()
//GoBox.RelayL1 = gpio.NewLedDriver(GoBox.RPIAdaptor, "4")
rl1Status, _ := BoxConfig.GetBool("devices/relay_l1/status")
Expand Down Expand Up @@ -167,19 +166,26 @@ func (box *Box) Start() {
go box.Robot.Start()
var cerr error
sensdBin, _ := BoxConfig.GetString("sensd_bin")
box.mux.Lock()
box.SensDCmd = exec.Command(sensdBin, "-c", BoxConfig.File)
box.mux.Unlock()
if sensdBin != "" {
box.mux.Lock()
args := []string{}
if *ARG_Debug {
args = []string{"-debug", "-c", BoxConfig.File}
} else {
args = []string{"-c", BoxConfig.File}
}
box.SensDCmd = exec.Command(sensdBin, args...)
box.mux.Unlock()
box.SensDStdout, cerr = box.SensDCmd.StdoutPipe()
checkError(cerr)
box.SensDStdin, cerr = box.SensDCmd.StdinPipe()
checkError(cerr)
serr := box.SensDCmd.Start()
if serr != nil {
log.Println("Couldn't start sensD process!", serr)
} else {
go box.ReadSensDPipe()
}
go box.ReadSensDPipe()
}
}

Expand Down Expand Up @@ -213,9 +219,11 @@ func (box *Box) relayWork(relayName string, relayDevice *gpio.GroveRelayDriver)
if rlForce != 0 && (rlForce == 1 || rlForce == -1) {

if rlForce == 1 {
relayDevice.On()
DebugLog(fmt.Sprintf("Force switch relay %s on", relayName))
DebugLogError(relayDevice.On())
} else {
relayDevice.Off()
DebugLog(fmt.Sprintf("Force switch relay %s off", relayName))
DebugLogError(relayDevice.Off())
}

} else if rlCond != "" {
Expand All @@ -224,13 +232,16 @@ func (box *Box) relayWork(relayName string, relayDevice *gpio.GroveRelayDriver)
if berr != nil {
log.Println(berr)
SaveException("internal", fmt.Sprintf("relay/%s", relayName), berr)
DebugLogError(berr)
} else {
if condRes {
relayDevice.On()
DebugLog(fmt.Sprintf("Switch relay %s on", relayName))
DebugLogError(relayDevice.On())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
} else {
relayDevice.Off()
DebugLog(fmt.Sprintf("Switch relay %s off", relayName))
DebugLogError(relayDevice.Off())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
}
Expand All @@ -239,21 +250,25 @@ func (box *Box) relayWork(relayName string, relayDevice *gpio.GroveRelayDriver)
rlLastSwitch, _ := box.RelayCache.GetString(fmt.Sprintf("relay_%s/last_switch", relayName))
if tOn > tOff && (rlLastSwitch == "" || rlLastSwitch < tD) {
if t >= tOff && rlLastSwitch != "" && relayDevice.State() == true {
relayDevice.Off()
DebugLog(fmt.Sprintf("Switch relay %s off", relayName))
DebugLogError(relayDevice.Off())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
} else if t >= tOn && relayDevice.State() == false {
relayDevice.On()
DebugLog(fmt.Sprintf("Switch relay %s on", relayName))
DebugLogError(relayDevice.On())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
}
} else if tOn < tOff {
if t >= tOff || t < tOn {
relayDevice.Off()
DebugLog(fmt.Sprintf("Switch relay %s off", relayName))
DebugLogError(relayDevice.Off())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
} else if t >= tOn && relayDevice.State() == false {
relayDevice.On()
DebugLog(fmt.Sprintf("Switch relay %s on", relayName))
DebugLogError(relayDevice.On())
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch", relayName), tD)
box.RelayCache.Set(fmt.Sprintf("relay_%s/last_switch_time", relayName), t)
}
Expand All @@ -278,6 +293,9 @@ func (box *Box) ReadSensDPipe() {

if err != nil {
checkError(err)
if strings.HasPrefix(string(line), "[DEBUG]") && *ARG_Debug {
log.Println(string(line))
}
time.Sleep(500 * time.Millisecond)
continue
}
Expand Down Expand Up @@ -510,3 +528,15 @@ func checkError(e error) {
}
log.Println(e)
}

func DebugLogError(e error) {
if *ARG_Debug {
log.Println("[DEBUG]", e)
}
}

func DebugLog(s string) {
if *ARG_Debug {
log.Println("[DEBUG]", s)
}
}

0 comments on commit db4b4a2

Please sign in to comment.