From 04cbaf04c3c037867b164d189575a575892413da Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Tue, 31 Jan 2017 22:35:38 +0000
Subject: [PATCH] Add some model checks.

---
 cmd/gqgmc/main.go       |  5 ++++-
 devices/geiger/gqgmc.go | 47 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/cmd/gqgmc/main.go b/cmd/gqgmc/main.go
index 3f0f700..8261436 100644
--- a/cmd/gqgmc/main.go
+++ b/cmd/gqgmc/main.go
@@ -9,8 +9,9 @@ package main
 
 import (
 	"fmt"
-	"gitlab.com/lyda/gqgmc/devices/geiger"
 	"time"
+
+	"gitlab.com/lyda/gqgmc/devices/geiger"
 )
 
 func main() {
@@ -32,6 +33,8 @@ func main() {
 		return
 	}
 
+	gc.SetTime(time.Now())
+
 	t, err = gc.GetTime()
 	if err != nil {
 		fmt.Printf("Failed: '%s'\n", err)
diff --git a/devices/geiger/gqgmc.go b/devices/geiger/gqgmc.go
index 4fc3b13..95cedb2 100644
--- a/devices/geiger/gqgmc.go
+++ b/devices/geiger/gqgmc.go
@@ -203,9 +203,19 @@ func (gc *GQGMCCounter) SetConfiguration() {
 
 // SetTime sets the time
 func (gc *GQGMCCounter) SetTime(t time.Time) {
-	//command: <SETDATETIME[YYMMDDHHMMSS]>>
-	//Firmware supported: GMC-280, GMC-300 Re.3.00 or later
+	if !gc.supportedModels([]string{"GMC-280", "GMC-300"}) {
+		return
+	}
+	if gc.versionLT("Re 2.23") {
+		return
+	}
+	if gc.versionLT("Re 3.30") {
+		gc.setTimeParts(t)
+	}
+	gc.setTimeAll(t)
+}
 
+func (gc *GQGMCCounter) setTimeParts(t time.Time) {
 	cmd := make([]byte, 13)
 	var timeCmds = []struct {
 		cmd  string
@@ -223,11 +233,27 @@ func (gc *GQGMCCounter) SetTime(t time.Time) {
 		copy(cmd[:], c.cmd)
 		cmd[10] = uint8(c.unit)
 		copy(cmd[11:], ">>")
-		gc.port.Write(cmd)
-		gc.readCmd(1)
+		fmt.Printf("%s: %+v\n", c.cmd, cmd)
+		//gc.port.Write(cmd)
+		//gc.readCmd(1)
 	}
 }
 
+func (gc *GQGMCCounter) setTimeAll(t time.Time) {
+	cmd := make([]byte, 13)
+	copy(cmd[:], "<SETDATETIME")
+	cmd[12] = uint8(t.Year())
+	cmd[13] = uint8(int(t.Month()))
+	cmd[14] = uint8(t.Day())
+	cmd[15] = uint8(t.Hour())
+	cmd[16] = uint8(t.Minute())
+	cmd[17] = uint8(t.Second())
+	copy(cmd[18:], ">>")
+	fmt.Printf("setTimeAll: %+v\n", cmd)
+	//gc.port.Write(cmd)
+	//gc.readCmd(1)
+}
+
 // GetTime gets the time
 func (gc *GQGMCCounter) GetTime() (time.Time, error) {
 	b, err := gc.communicate(cmdGetTime, 7)
@@ -284,6 +310,19 @@ func (gc *GQGMCCounter) Reboot() {
 	return
 }
 
+func (gc *GQGMCCounter) supportedModels(models []string) bool {
+	for _, model := range models {
+		if model == gc.model {
+			return true
+		}
+	}
+	return false
+}
+
+func (gc *GQGMCCounter) versionLT(version string) bool {
+	return gc.version < version
+}
+
 func (gc *GQGMCCounter) communicate(cmd string, length uint32) ([]byte, error) {
 	gc.Clear()
 	if len(cmd) > 0 {
-- 
GitLab