Permalink
Browse files

[wip] 雑にいろいろな機能追加

- CC3200SensorTagからのデータ取得(ボタンだけ)対応
- SensorTagのボタン押下に応じてコマンド発行するI/F追加
  (動けばいいレベルなのでそのうち直す...)
- configその他のリファクタリング(というか必要に迫られた雑な修正)
- 暫定的にv1.0.3に更新
  • Loading branch information...
moguriso committed Nov 6, 2017
1 parent 8a920fe commit ca6b7ef006af59c00b190d44182b41ec9972259d
Showing with 320 additions and 66 deletions.
  1. +1 −1 Makefile
  2. +5 −0 config.json
  3. +1 −1 deb/conf
  4. +9 −1 src/main/main.go
  5. +62 −0 src/module/config/config.go
  6. +2 −2 src/module/httpcmd/cmd.go
  7. +15 −15 src/module/httpcmd/router.go
  8. +177 −0 src/module/sensortag/button.go
  9. +6 −26 src/module/webcam/webcam.go
  10. +42 −20 vendor/manifest
View
@@ -1,6 +1,6 @@
BUILD := release
VERSION := v1.0.2
VERSION := v1.0.3
REVISION := $(shell git rev-parse --short HEAD)
SRCS := $(shell find ./src -type f -name '*.go')
View
@@ -0,0 +1,5 @@
{
"camera_index": 0,
"sensor_url":"http://localhost/param_sensortag_poll.html",
"sensor_read_interval": 1000.0
}
View
@@ -1,5 +1,5 @@
Package: ir-cmd-recv-http%%DBG%%
Maintainer: Yuya Adachi <moguriso@tizen.moe>
Architecture: %%ARCH%%
Version: 1.0.2
Version: 1.0.3
Description: tentative
View
@@ -2,7 +2,9 @@ package main
import (
"flag"
"module/config"
"module/httpcmd"
"module/sensortag"
"module/webcam"
"os"
)
@@ -16,7 +18,13 @@ func main() {
if _, err := os.Stat(*configFile); err != nil {
*configFile = "config.json"
}
conf, _ := config.NewParams()
conf.LoadParam(*configFile)
webcam.SetCameraIndex(conf.CameraIndex)
sd, _ := sensortag.NewData(conf.SensorUrl, conf.SensorReadInterval)
go sd.ReadButtonThread()
webcam.LoadParam(*configFile)
httpcmd.Listen(":8089")
}
@@ -0,0 +1,62 @@
package config
import (
"encoding/json"
"io/ioutil"
"log"
)
type Param struct {
CameraIndex int
SensorUrl string
SensorReadInterval float64
}
func NewParams() (*Param, error) {
p := &Param{
CameraIndex: 0.0,
}
return p, nil
}
func (p *Param) LoadParam(fileName string) error {
var config map[string]interface{}
var ftmp float64
isSet := false
data, err := ioutil.ReadFile(fileName)
if err != nil {
log.Println(fileName, " read error ", err)
goto exit_func
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Println("json Unmarshal error ", err)
goto exit_func
}
err = nil
ftmp, isSet = config["camera_index"].(float64)
if !isSet {
p.CameraIndex = 0
} else {
p.CameraIndex = int(ftmp)
}
log.Println("camera index: ", p.CameraIndex)
p.SensorUrl, isSet = config["sensor_url"].(string)
if !isSet {
p.SensorUrl = "http://localhost" /* tentative ... */
}
log.Println("sensor target URL: ", p.SensorUrl)
p.SensorReadInterval, isSet = config["sensor_read_interval"].(float64)
if !isSet {
p.SensorReadInterval = 1000.0 /* default: 1sec */
}
log.Println("sensor read interval: ", p.SensorReadInterval)
exit_func:
return err
}
@@ -12,7 +12,7 @@ func initCmd() (cmd, arg string) {
return cm, ar
}
func runCommand(cmd, arg string) {
func RunCommand(cmd, arg string) {
log.Println("call cmd = ", cmd)
if arg == "" {
log.Fatal("arg error")
@@ -26,7 +26,7 @@ func runCommand(cmd, arg string) {
}
}
func readSeq(filename string) string {
func ReadSeq(filename string) string {
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
@@ -66,15 +66,15 @@ func Light(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
switch code {
case "on":
fmt.Fprintf(w, "Light show: on")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/light_all.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/light_all.txt"))
case "off":
fmt.Fprintf(w, "Light show: off")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/light_off.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/light_off.txt"))
case "fav":
fmt.Fprintf(w, "Light show: fav")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/light_fav.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/light_fav.txt"))
}
runCommand(cmd, arg)
RunCommand(cmd, arg)
}
func AirIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -87,21 +87,21 @@ func Air(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
switch code {
case "jositu":
fmt.Fprintf(w, "Air show: on")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/air_jositu.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/air_jositu.txt"))
case "reibo":
fmt.Fprintf(w, "Air show: off")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/air_reibo.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/air_reibo.txt"))
case "off":
fmt.Fprintf(w, "Air show: fav")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/air_off.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/air_off.txt"))
case "timer-on":
fmt.Fprintf(w, "Air show: timer on")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/air_timer_on.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/air_timer_on.txt"))
case "timer-off":
fmt.Fprintf(w, "Air show: timer off")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/air_timer_off.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/air_timer_off.txt"))
}
runCommand(cmd, arg)
RunCommand(cmd, arg)
}
func SenpuIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -114,17 +114,17 @@ func Senpu(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
switch code {
case "on":
fmt.Fprintf(w, "Senpu show: on")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/senpuuki_on-off.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/senpuuki_on-off.txt"))
case "off":
fmt.Fprintf(w, "Senpu show: off")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/senpuuki_on-off.txt"))
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/senpuuki_on-off.txt"))
case "timer":
fmt.Fprintf(w, "Senpu show: timer")
arg = fmt.Sprintf("-d %s", readSeq("./cmd/senpuuki_timer.txt"))
runCommand(cmd, arg)
arg = fmt.Sprintf("-d %s", ReadSeq("./cmd/senpuuki_timer.txt"))
RunCommand(cmd, arg)
time.Sleep(500 * time.Millisecond)
}
runCommand(cmd, arg)
RunCommand(cmd, arg)
}
func WebCamIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -0,0 +1,177 @@
package sensortag
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"module/httpcmd"
"github.com/PuerkitoBio/goquery"
)
type Data struct {
endpoint string
read_interval float64
leftDown chan bool
rightDown chan bool
}
func (sd *Data) LeftDownEvent() <-chan bool {
return sd.leftDown
}
func (sd *Data) RightDownEvent() <-chan bool {
return sd.rightDown
}
func (sd *Data) setLeftDown() {
sd.leftDown <- true
}
func (sd *Data) setRightDown() {
sd.rightDown <- true
}
func light(mode string) {
cmd := "./cmd/remocon"
arg := ""
switch mode {
case "on":
arg = fmt.Sprintf("-d %s", httpcmd.ReadSeq("./cmd/light_all.txt"))
case "off":
arg = fmt.Sprintf("-d %s", httpcmd.ReadSeq("./cmd/light_off.txt"))
case "fav":
arg = fmt.Sprintf("-d %s", httpcmd.ReadSeq("./cmd/light_fav.txt"))
}
if arg != "" {
httpcmd.RunCommand(cmd, arg)
} else {
log.Println("light: argument error")
}
}
func (sd *Data) getPage(url string) {
doc, _ := goquery.NewDocument(url)
doc.Find("p").Each(func(_ int, s *goquery.Selection) {
id, _ := s.Attr("id")
if id == "key" {
text := s.Text()
switch text {
case "1":
log.Println("left button pushed")
light("fav")
case "2":
log.Println("right button pushed")
light("off")
case "3":
log.Println("both button pushed")
light("on")
}
}
})
}
//func (sd *Data) getPage(url string) {
// doc, _ := goquery.NewDocument(url)
// doc.Find("p").Each(func(_ int, s *goquery.Selection) {
// id, _ := s.Attr("id")
// log.Println(id)
// text := s.Text()
// log.Println(text)
// })
// //doc.Find("a").Each(func(_ int, s *goquery.Selection) {
// // url, _ := s.Attr("href")
// // fmt.Println(url)
// //})
//}
//type Result struct {
// Url string
//}
//func (sd *Data) parseItem(r io.Reader) []Result {
// results := []Result{}
// doc, err := html.Parse(r)
// if err != nil {
// log.Println(err)
// }
// log.Println("a!")
//
// //var result Result
// var f func(*html.Node)
// f = func(n *html.Node) {
// log.Println("b! ", n)
// // n.Typeでノードの型をチェックできる、ElementNodeでHTMLタグのNode。
// // n.Dataでノートの値をチェックする、aタグをチェックしている
// if n.Type == html.ElementNode && n.Data == "p" {
// // n.Attrで属性を一覧する
// // ここでもう少し頑張るとparseできる
// for _, p := range n.Attr {
// ///if p.Key == "href" {
// //if p.Id == "href" {
// // result.Url = a.Val
// // results = append(results, result)
// //}
// log.Println(p)
// }
// log.Println(n)
// }
// for c := n.FirstChild; c != nil; c = c.NextSibling {
// f(c)
// }
// }
// f(doc)
// return results
//}
func (sd *Data) getSensorData() (string, error) {
ret := ""
resp, err := http.Get(sd.endpoint)
defer resp.Body.Close()
if err == nil {
byteArray, read_err := ioutil.ReadAll(resp.Body)
if read_err == nil {
//sd.parseItem(resp.Body)
sd.getPage(sd.endpoint)
ret = string(byteArray)
err = nil
} else {
log.Println("getSensorData: GET error ", read_err)
err = read_err
}
} else {
log.Println(err)
}
return ret, err
}
func (sd *Data) ReadButtonThread() {
t := time.NewTicker(time.Duration(sd.read_interval) * time.Millisecond)
defer t.Stop()
for {
select {
case <-t.C:
s, err := sd.getSensorData()
if err == nil {
log.Println("ReadButtonThread: ", s)
} else {
log.Println("ReadButtonThread: read error occurred... perhaps.")
}
}
}
}
func NewData(url string, interval float64) (*Data, error) {
sd := &Data{
endpoint: url,
read_interval: interval,
leftDown: make(chan bool),
rightDown: make(chan bool),
}
return sd, nil
}
Oops, something went wrong.

0 comments on commit ca6b7ef

Please sign in to comment.