Skip to content

Commit 0e2ebe8

Browse files
committed
とりあえずコミット
0 parents  commit 0e2ebe8

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### https://raw.github.com/github/gitignore/9e018473e80cf262d25841facbbd69e7f1a4ef41/Go.gitignore
2+
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# My env
17+
.env

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Smartagri4Alexa
2+
3+
This is alexa skill for [Smartagri](https://github.com/hatobus/Smartagri)
4+
5+
## Using
6+
7+
- AWS lambda
8+
- Amazon Alexa
9+
- Golang
10+
11+
12+

smartagri.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"os"
9+
"strings"
10+
11+
"github.com/davecgh/go-spew/spew"
12+
"github.com/joho/godotenv"
13+
)
14+
15+
type AgreData struct {
16+
No string `json:"no"`
17+
Date string `json:"date"`
18+
Time string `json:"time"`
19+
Temperature string `json:"temperature"`
20+
Humidity string `json:"humidity"`
21+
SoilHumidity string `json:"soil_humidity"`
22+
Co2Concentration string `json:"co2_concentration"`
23+
Wavelength string `json:"wavelength"`
24+
Illuminance string `json:"illuminance"`
25+
}
26+
27+
func getHouseinfoJSON(APIURL string) error {
28+
29+
res, err := http.Get(APIURL)
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
defer res.Body.Close()
35+
36+
body, err := ioutil.ReadAll(res.Body)
37+
if err != nil {
38+
log.Println(err)
39+
}
40+
41+
// 返ってきたjsonじゃないjsonを}でsplitする
42+
bodystring := strings.Split(string(body), "}")
43+
44+
// spew.Dump(bodystring)
45+
46+
var resdataNO1, resdataNO2, resdataNO3 []AgreData
47+
var tmpdata AgreData
48+
49+
// } で splitしたので消えているから } をくっつけてjson unmarshall
50+
for _, jsondata := range bodystring {
51+
jsondata = jsondata + "}"
52+
json.Unmarshal([]byte(jsondata), &tmpdata)
53+
54+
// 機械Noで分ける
55+
switch tmpdata.No {
56+
case "1":
57+
resdataNO1 = append(resdataNO1, tmpdata)
58+
case "2":
59+
resdataNO2 = append(resdataNO2, tmpdata)
60+
case "3":
61+
resdataNO3 = append(resdataNO3, tmpdata)
62+
default:
63+
log.Fatal("Invalid value")
64+
}
65+
66+
}
67+
68+
spew.Dump(resdataNO1)
69+
spew.Dump(resdataNO2)
70+
spew.Dump(resdataNO3)
71+
72+
return err
73+
}
74+
75+
func main() {
76+
77+
err := godotenv.Load()
78+
if err != nil {
79+
log.Fatal("Error loading .env file")
80+
}
81+
url := os.Getenv("APIURL")
82+
83+
getHouseinfoJSON(url)
84+
85+
}

0 commit comments

Comments
 (0)