Adrien Carteron 2 лет назад
Сommit
cde3b7e556
9 измененных файлов с 237 добавлено и 0 удалено
  1. 16 0
      Makefile
  2. 21 0
      cmd/homectrl/main.go
  3. 10 0
      go.mod
  4. 12 0
      go.sum
  5. 70 0
      internal/connectors/mqtt/mqtt.go
  6. 44 0
      internal/devices/builder.go
  7. 10 0
      internal/devices/interaction.go
  8. 30 0
      internal/devices/light.go
  9. 24 0
      standard_def.mk

+ 16 - 0
Makefile

@@ -0,0 +1,16 @@
+include ./standard_def.mk
+
+all: createdirectories $(BUILD)
+
+$(BUILD):
+	go build -o ./build/homectrl ./cmd/homectrl
+
+
+init:
+	go mod init "$(BUILD)"
+
+createproject:
+	@mkdir -p $(DIR_CMD) $(DIR_INTERNAL) $(DIR_PKG) $(DIR_CONFIG) $(DIR_TEST) $(DIR_DOC)
+
+createdirectories:
+	@mkdir -p $(DIR_BUILD)

+ 21 - 0
cmd/homectrl/main.go

@@ -0,0 +1,21 @@
+package main
+
+import (
+	"fmt"
+
+	"homectrl/internal/connectors/mqtt"
+	interaction "homectrl/internal/devices"
+)
+
+func main() {
+	l := interaction.New(1, "loc")
+	fmt.Println(l.String())
+
+	var broker = "192.168.8.50"
+	var port = 1883
+
+	c := mqtt.New(broker, port)
+	c.SubTopic("zigbee2mqtt/bridge/devices")
+	interaction.Builder2(`[{"friendly_name":"lolilol"}]`)
+
+}

+ 10 - 0
go.mod

@@ -0,0 +1,10 @@
+module homectrl
+
+go 1.18
+
+require (
+	github.com/eclipse/paho.mqtt.golang v1.4.2 // indirect
+	github.com/gorilla/websocket v1.4.2 // indirect
+	golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
+	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
+)

+ 12 - 0
go.sum

@@ -0,0 +1,12 @@
+github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
+github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
+github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
+github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
+golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
+golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
+golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

+ 70 - 0
internal/connectors/mqtt/mqtt.go

@@ -0,0 +1,70 @@
+package mqtt
+
+import (
+	"flag"
+	"fmt"
+
+	"container/list"
+	devices "homectrl/internal/devices"
+
+	mqtt_cli "github.com/eclipse/paho.mqtt.golang"
+)
+
+type mqtt struct {
+	client mqtt_cli.Client
+	topics list.List
+}
+
+var messagePubHandler mqtt_cli.MessageHandler = func(client mqtt_cli.Client, msg mqtt_cli.Message) {
+	//fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
+	devices.Builder(msg.Payload())
+}
+
+var connectHandler mqtt_cli.OnConnectHandler = func(client mqtt_cli.Client) {
+	fmt.Println("Connected")
+}
+
+var connectLostHandler mqtt_cli.ConnectionLostHandler = func(client mqtt_cli.Client, err error) {
+	fmt.Printf("Connect lost: %v", err)
+}
+
+func sub(client mqtt_cli.Client, topic string) {
+	token := client.Subscribe(topic, 1, nil)
+	token.Wait()
+	fmt.Printf("Subscribed to topic: %s", topic)
+	receiveCount := 0
+	choke := make(chan [2]string)
+	num := flag.Int("num", 1, "The number of messages to publish or subscribe (default 1)")
+	for receiveCount < *num {
+		incoming := <-choke
+		fmt.Printf("RECEIVED TOPIC: %s MESSAGE: %s\n", incoming[0], incoming[1])
+		//devices.Builder2(incoming[1])
+		receiveCount++
+	}
+}
+
+func connect(broker string, port int) mqtt_cli.Client {
+	opts := mqtt_cli.NewClientOptions()
+	opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port))
+	opts.SetClientID("go_mqtt_client")
+	opts.SetUsername("emqx")
+	opts.SetPassword("public")
+	opts.SetDefaultPublishHandler(messagePubHandler)
+	opts.OnConnect = connectHandler
+	opts.OnConnectionLost = connectLostHandler
+	client := mqtt_cli.NewClient(opts)
+	if token := client.Connect(); token.Wait() && token.Error() != nil {
+		panic(token.Error())
+	}
+	return client
+}
+
+func New(broker string, port int) mqtt {
+	m := mqtt{client: connect(broker, port)}
+	return m
+}
+
+func (m mqtt) SubTopic(topic string) {
+	m.topics.PushBack(topic)
+	sub(m.client, topic)
+}

+ 44 - 0
internal/devices/builder.go

@@ -0,0 +1,44 @@
+package interaction
+
+import (
+	"encoding/json"
+	"fmt"
+	"strings"
+)
+
+type device struct {
+	Friendly_name string
+}
+
+func to_devices(raw_device string) {
+	s2 := strings.Split(raw_device, "_")
+	fmt.Println(s2)
+}
+
+func Builder(data []byte) {
+	var arr []device
+	err := json.Unmarshal(data, &arr)
+	fmt.Printf("lolb")
+	if err != nil {
+		panic(err)
+	}
+	for _, s := range arr {
+		fmt.Println(s.Friendly_name)
+		to_devices(s.Friendly_name)
+	}
+}
+
+func Builder2(data string) {
+	var arr []device
+	err := json.Unmarshal([]byte(data), &arr)
+	fmt.Println("lols")
+	if err != nil {
+		panic(err)
+	}
+	fmt.Println(len(arr))
+	fmt.Println(arr[0])
+	for i, s := range arr {
+		fmt.Println(i)
+		fmt.Println(s.Friendly_name)
+	}
+}

+ 10 - 0
internal/devices/interaction.go

@@ -0,0 +1,10 @@
+package interaction
+
+type Interaction interface {
+	Location() string
+	LocationId() int
+	Id() int
+	Type() string
+	String() string
+	Purpose() string
+}

+ 30 - 0
internal/devices/light.go

@@ -0,0 +1,30 @@
+package interaction
+
+import "strconv"
+
+type Light struct {
+	id       int
+	location string
+	on       bool
+}
+
+func (l Light) Location() string {
+	return l.location
+}
+
+func (l Light) Id() int {
+	return l.id
+}
+
+func (Light) Type() string {
+	return "Light"
+}
+
+func (l Light) String() string {
+	return l.Type() + " " + strconv.Itoa(l.Id()) + " " + l.Location()
+}
+
+func New(id int, location string) Light {
+	l := Light{id, location, false}
+	return l
+}

+ 24 - 0
standard_def.mk

@@ -0,0 +1,24 @@
+## Directories
+ROOT=$(shell pwd)
+
+BUILD=homectrl
+
+DIR_CMD=cmd				### Main applications for this project.
+DIR_INTERNAL=internal			### Private application and library code.
+DIR_PKG=pkg				### Library code that's ok to use by external applications
+DIR_VENDOR=vendor			### Application dependencies, created by go mod vendor,  
+DIR_API=api				### OpenAPI/Swagger specs, JSON schema files, protocol definition files.
+DIR_WEB=web				### Web application specific components
+DIR_CONFIGS=configs			### Configuration file templates or default configs.
+DIR_INIT=init				### System init (systemd, upstart, sysv) and process manager/supervisor (runit, supervisord) configs.
+DIR_SCRIPTS=scripts			### Scripts to perform various build, install, analysis, etc operations.
+DIR_BUILD=build				### 
+DIR_DEPLOYMENTS=deployments		### 
+DIR_TEST=test				### 
+DIR_DOC=docs				### 
+DIR_TOOLS=tools				### 
+DIR_EXAMPLES=examples			### 
+DIG_GITHOOKS=githooks			### 
+DIR_ASSETS=assets			### 
+DIR_WEBSITE=website			### 
+