Last active
June 5, 2018 06:33
-
-
Save penguinpowernz/26a2563027256a2da1edc40aabea2a2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/ashwanthkumar/slack-go-webhook" | |
"github.com/autogrow/go-jelly/ig" | |
) | |
var webhookURL = "https://hooks.slack.com/services/00000000/11111111/XXXXXXXXXXXXXXXXXXXXXX" | |
func main() { | |
user := "me" | |
pass := "secret" | |
idoseSN := "ASLID17083242" | |
cl, err := ig.NewClient(user, pass) | |
if err != nil { | |
panic(err) | |
} | |
doser, err := cl.IntelliDose(idoseSN) | |
if err != nil { | |
panic(err) | |
} | |
phDosing := false | |
ecDosing := false | |
for { | |
time.Sleep(time.Second * 5) | |
doser.GetState() | |
status, ok := doser.FunctionStatuses()["ph_dosing"] | |
if ok && status.Active { | |
if !phDosing { | |
phDosing = true | |
} | |
} else { | |
phDosing = false | |
notifySlack(doser, "ph") | |
} | |
status, ok = doser.FunctionStatuses()["nutrient_dosing"] | |
if ok && status.Active { | |
if !ecDosing { | |
ecDosing = true | |
} | |
} else { | |
ecDosing = false | |
go notifySlack(doser, "nutrient") | |
} | |
} | |
} | |
func notifySlack(doser *ig.IntelliDose, t string) { | |
msg := "I've just done a " + t + " dose" | |
doser.GetMetrics() | |
switch t { | |
case "nutrient": | |
msg += fmt.Sprintf(" and the Nutrient level is now at %0.2f", doser.Metrics.Ec) | |
case "ph": | |
msg += fmt.Sprintf(" and the pH level is now at %0.2f", doser.Metrics.PH) | |
} | |
p := slack.Payload{ | |
Text: msg, | |
Username: "IntelliDose-" + doser.GetID(), | |
Channel: "#general", | |
IconEmoji: ":robot_face:", | |
} | |
err := slack.Send(webhookURL, "", p) | |
if len(err) > 0 { | |
log.Printf("error sending to slack: %s\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment