From 3f0d51c3778827e2ecb904dbeb494979459cf495 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Sat, 14 Nov 2020 01:09:36 +0100 Subject: [PATCH] Removed obsolete files --- .gitignore | 3 +- HandlerHelpers.go | 80 ------------------- docs.md | 0 somewhat_working_dht22.go.inactive | 121 ----------------------------- 4 files changed, 2 insertions(+), 202 deletions(-) delete mode 100644 HandlerHelpers.go create mode 100644 docs.md delete mode 100644 somewhat_working_dht22.go.inactive diff --git a/.gitignore b/.gitignore index 2d2b47d..a6a29e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -node_modules \ No newline at end of file +/node_modules +climate-server diff --git a/HandlerHelpers.go b/HandlerHelpers.go deleted file mode 100644 index e7ad6f0..0000000 --- a/HandlerHelpers.go +++ /dev/null @@ -1,80 +0,0 @@ -package main - -import ( - "fmt" - "net/http" -) - -type HandlerFuncWithError = func (http.ResponseWriter, *http.Request) error - -type HttpMethod string - -const( - HttpGet HttpMethod = "GET" - HttpPost HttpMethod = "POST" -) - -type RequestContext struct { - ResponseWriter http.ResponseWriter - Request *http.Request - MethodType HttpMethod - Pattern string -} - -type Router struct { - patterns map[string] *PatternRouter -} - -type PatternRouter struct { - Get HandlerFuncWithError - Post HandlerFuncWithError -} - -func (p *Router) MethodFuncForPattern(methodType HttpMethod, pattern string) (HandlerFuncWithError, error) { - var methodFunc HandlerFuncWithError - switch methodType { - case HttpGet: - methodFunc = p.patterns[pattern].Get - case HttpPost: - methodFunc = p.patterns[pattern].Post - } - if methodFunc == nil { - return nil, fmt.Errorf("method %s not defined on pattern %s", methodType, pattern) - } - return methodFunc, nil -} - -func (p *Router) setGet(pattern string, cb HandlerFuncWithError) { - if _, ok := p.patterns[pattern]; ok { - p.patterns[pattern].Get = cb - } else { - p.patterns[pattern] = &PatternRouter{cb, nil} - http.HandleFunc(pattern, getPatternHandler(pattern)) - } -} - -func (p *Router) setPost(pattern string, cb HandlerFuncWithError) { - if _, ok := p.patterns[pattern]; ok { - p.patterns[pattern].Post = cb - } else { - p.patterns[pattern] = &PatternRouter{nil, cb} - http.HandleFunc(pattern, getPatternHandler(pattern)) - } -} - -var MainRouter = Router{make(map[string] *PatternRouter)} - -func getPatternHandler(pattern string) http.HandlerFunc { - return func (w http.ResponseWriter, r *http.Request) { - cb, err := MainRouter.MethodFuncForPattern(HttpMethod(r.Method), pattern) - if err == nil { - err = cb(w, r) - } - if err != nil { - internalErrorOnErr(err, w, r) - } - } -} - - - diff --git a/docs.md b/docs.md new file mode 100644 index 0000000..e69de29 diff --git a/somewhat_working_dht22.go.inactive b/somewhat_working_dht22.go.inactive deleted file mode 100644 index 1f8cc53..0000000 --- a/somewhat_working_dht22.go.inactive +++ /dev/null @@ -1,121 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "github.com/warthog618/gpio" - "math" - "strconv" - "time" -) - -const Timeout = math.MaxInt32 -const TimeoutErrInitial string = "timed out waiting for sensor's initial %s reading" - -var oneSecondInCycles = (func() int { - result := cyclesInAMillisecond() - for i := 0; i < 100; i++ { - result += cyclesInAMillisecond() - } - return result / 100 -})() - -func cyclesInAMillisecond() int { - count := 0 - start := time.Now().UnixNano() - for { - count++ - if time.Now().UnixNano() - start >= 1000000 { - break - } - } - return count -} - -func main() { - err := gpio.Open() - if err != nil { - panic(err) - } - defer gpio.Close() - - pin := gpio.NewPin(gpio.GPIO4) - dhtData, err := readDataFromDHT(pin) - if err != nil { - fmt.Println(fmt.Sprintf("Bad read! Reason: %s!", err.Error())) - } else { - fmt.Println( - strconv.FormatInt(int64(dhtData[0]), 2), - strconv.FormatInt(int64(dhtData[1]), 2), - strconv.FormatInt(int64(dhtData[2]), 2), - strconv.FormatInt(int64(dhtData[3]), 2), - strconv.FormatInt(int64(dhtData[4]), 2)) - fmt.Println(convertToRhAndTemp(dhtData)) - } -} - -func sendStartSignal(pin *gpio.Pin) { - pin.PullUp() - time.Sleep(time.Millisecond) - pin.Output() - pin.Low() - time.Sleep(1100 * time.Microsecond) - pin.PullUp() - pin.Input() - time.Sleep(55 * time.Microsecond) -} - -func readDataFromDHT(pin *gpio.Pin) ([5]byte, error) { - cycles := [80]int{} - sendStartSignal(pin) - if cyclesForReading(pin, gpio.Low) == Timeout { - return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "low")) - } - if cyclesForReading(pin, gpio.High) == Timeout { - return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "high")) - } - for i := 0; i < 80; i += 2 { - cycles[i] = cyclesForReading(pin, gpio.Low) - cycles[i + 1] = cyclesForReading(pin, gpio.High) - } - receivedInput, err := storeCycleCountsAsBinarySequence(&cycles) - if err != nil { - return [5]byte{}, err - } - return receivedInput, nil -} - -func cyclesForReading(pin *gpio.Pin, level gpio.Level) int { - count := 0 - for pin.Read() == level { - count++ - if count >= oneSecondInCycles { - return Timeout - } - } - return count -} - -func storeCycleCountsAsBinarySequence(cycles *[80]int) ([5]byte, error) { - data := [5]byte{} - for i := 0; i < 40; i++ { - lowCycles := cycles[2 * i] - highCycles := cycles[2 * i + 1] - fmt.Println(lowCycles, highCycles) - if (lowCycles == Timeout) || (highCycles == Timeout) { - //return [5]byte{}, errors.New("timed out waiting for sensor pulse") - } - data[i / 8] <<= 1 - if highCycles > lowCycles { - data[i / 8] |= 1 - } - } - return data, nil -} - -func convertToRhAndTemp(data [5]byte) (float32, float32) { - rh := float32(int(data[0]) << 8) + float32(data[1]) - temp := float32(int(data[2]) << 8) + float32(data[3]) - return rh/10, temp/10 -} -