Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Permalink
Browse files

avr: power on when phone has an active DHCP lease

  • Loading branch information
stapelberg committed Feb 1, 2020
1 parent 687c7af commit 5e7a2d35e2501b5fe70f30a52b390ff8142d8a2b
Showing with 41 additions and 1 deletion.
  1. +36 −0 avr-x1100w/dhcp.go
  2. +5 −1 avr-x1100w/main.go
@@ -0,0 +1,36 @@
package main

import (
"context"
"log"
"net/http"
"time"
)

func pollDHCP1() error {
ctx, canc := context.WithTimeout(context.Background(), 5*time.Second)
defer canc()
req, err := http.NewRequest("GET", "http://dhcp4d.router7/lease/Galaxy-S10e", nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
active := resp.Header.Get("X-Lease-Active") == "true"
stateMu.Lock()
state.galaxyActive.Set(active)
lastContact["galaxy"] = time.Now()
stateMu.Unlock()
stateChanged.Broadcast()
return nil
}

func pollDHCP() {
for range time.Tick(5 * time.Second) {
if err := pollDHCP1(); err != nil {
log.Printf("[dhcp] poll failed: %v", err)
}
}
}
@@ -33,6 +33,7 @@ type State struct {
avrPowered timestamped.Bool
roombaCanClean timestamped.Bool
roombaCleaning bool
galaxyActive timestamped.Bool
//difmxChannel int
timestamp time.Time
}
@@ -54,7 +55,6 @@ var (
func stateMachine(current State) State {
var next State

next.avrPowered.Set(current.beastPowered || current.midnaUnlocked.Value())
// next.difmxChannel = 0 // midna
// if current.beastPowered {
// next.difmxChannel = 1 // beast
@@ -70,6 +70,9 @@ func stateMachine(current State) State {
roombaCanClean = false
}
next.roombaCanClean.Set(roombaCanClean)
next.avrPowered.Set(current.beastPowered ||
current.midnaUnlocked.Value() ||
(hour > 8 && current.galaxyActive.Value()))
return next
}

@@ -110,6 +113,7 @@ func main() {
go scheduleRoomba()
//go pollDifmx()
go pollAVR()
go pollDHCP()

// Wait a little bit to give the various goroutines time to do their initial polls.
time.Sleep(10 * time.Second)

0 comments on commit 5e7a2d3

Please sign in to comment.
You can’t perform that action at this time.