diff --git a/README.md b/README.md index 819a045..7ff4c33 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/VERSION b/VERSION index 60453e6..6a2b0ac 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.0.0 \ No newline at end of file +v1.0.1 \ No newline at end of file diff --git a/cmd/sensd/sensd.go b/cmd/sensd/sensd.go index 5ca9607..7b97533 100644 --- a/cmd/sensd/sensd.go +++ b/cmd/sensd/sensd.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "log" "os" "os/signal" "strings" @@ -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) @@ -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) @@ -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 @@ -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 } @@ -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) + } +} diff --git a/models/box.go b/models/box.go index 48ef57d..c0d7eae 100644 --- a/models/box.go +++ b/models/box.go @@ -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 @@ -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") @@ -167,10 +166,16 @@ 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() @@ -178,8 +183,9 @@ func (box *Box) Start() { serr := box.SensDCmd.Start() if serr != nil { log.Println("Couldn't start sensD process!", serr) + } else { + go box.ReadSensDPipe() } - go box.ReadSensDPipe() } } @@ -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 != "" { @@ -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) } @@ -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) } @@ -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 } @@ -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) + } +}