|
|
|
@ -1,7 +1,14 @@
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
|
|
|
"crypto/rsa"
|
|
|
|
|
|
|
|
"crypto/x509"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
@ -10,7 +17,9 @@ import (
|
|
|
|
_ "go_mqtt/models"
|
|
|
|
_ "go_mqtt/models"
|
|
|
|
"go_mqtt/mydb"
|
|
|
|
"go_mqtt/mydb"
|
|
|
|
_ "go_mqtt/mydb"
|
|
|
|
_ "go_mqtt/mydb"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@ -177,6 +186,207 @@ func dbTest() {
|
|
|
|
stmt.Exec(1)
|
|
|
|
stmt.Exec(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GenRsaKey generates an PKCS#1 RSA keypair of the given bit size in PEM format.
|
|
|
|
|
|
|
|
func GenRsaKey(bits int) (prvkey, pubkey []byte, err error) {
|
|
|
|
|
|
|
|
// Generates private key.
|
|
|
|
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
|
|
|
|
|
|
block := &pem.Block{
|
|
|
|
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
|
|
|
|
Bytes: derStream,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
prvkey = pem.EncodeToMemory(block)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generates public key from private key.
|
|
|
|
|
|
|
|
publicKey := &privateKey.PublicKey
|
|
|
|
|
|
|
|
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
block = &pem.Block{
|
|
|
|
|
|
|
|
Type: "RSA PUBLIC KEY",
|
|
|
|
|
|
|
|
Bytes: derPkix,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey = pem.EncodeToMemory(block)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func parsePrivateKey(privateKeyFile string) (*rsa.PrivateKey, error) {
|
|
|
|
|
|
|
|
// 读取私钥文件
|
|
|
|
|
|
|
|
keyData, err := ioutil.ReadFile(privateKeyFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析PEM块
|
|
|
|
|
|
|
|
block, _ := pem.Decode(keyData)
|
|
|
|
|
|
|
|
if block == nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("private key error not block in file")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析RSA私钥
|
|
|
|
|
|
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return privateKey, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//生成RSA私钥和公钥,保存到文件中
|
|
|
|
|
|
|
|
func GenerateRSAKey(bits int) {
|
|
|
|
|
|
|
|
//GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥
|
|
|
|
|
|
|
|
//Reader是一个全局、共享的密码用强随机数生成器
|
|
|
|
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//保存私钥
|
|
|
|
|
|
|
|
//通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串
|
|
|
|
|
|
|
|
// X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
|
|
|
|
|
|
|
|
X509PrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//使用pem格式对x509输出的内容进行编码
|
|
|
|
|
|
|
|
//创建文件保存私钥
|
|
|
|
|
|
|
|
privateFile, err := os.Create("private.pem")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//构建一个pem.Block结构体对象
|
|
|
|
|
|
|
|
privateBlock := pem.Block{Type: "PRIVATE KEY", Bytes: X509PrivateKey}
|
|
|
|
|
|
|
|
//将数据保存到文件
|
|
|
|
|
|
|
|
pem.Encode(privateFile, &privateBlock)
|
|
|
|
|
|
|
|
//保存公钥
|
|
|
|
|
|
|
|
//获取公钥的数据
|
|
|
|
|
|
|
|
publicKey := privateKey.PublicKey
|
|
|
|
|
|
|
|
//X509对公钥编码
|
|
|
|
|
|
|
|
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//pem格式编码
|
|
|
|
|
|
|
|
//创建用于保存公钥的文件
|
|
|
|
|
|
|
|
publicFile, err := os.Create("public.pem")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建一个pem.Block结构体对象
|
|
|
|
|
|
|
|
publicBlock := pem.Block{Type: "Public Key", Bytes: X509PublicKey}
|
|
|
|
|
|
|
|
//保存到文件
|
|
|
|
|
|
|
|
pem.Encode(publicFile, &publicBlock)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RSA_Decrypts RSA解密支持分段解密
|
|
|
|
|
|
|
|
func RSA_Decrypts(cipherText []byte, path string) []byte {
|
|
|
|
|
|
|
|
//打开文件
|
|
|
|
|
|
|
|
var bytesDecrypt []byte
|
|
|
|
|
|
|
|
//file, err := os.Open(path)
|
|
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
|
|
// fmt.Println(err)
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
////获取文件内容
|
|
|
|
|
|
|
|
//info, _ := file.Stat()
|
|
|
|
|
|
|
|
//buf := make([]byte, info.Size())
|
|
|
|
|
|
|
|
//file.Read(buf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 读取私钥文件
|
|
|
|
|
|
|
|
keyData, err := ioutil.ReadFile(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//pem解码
|
|
|
|
|
|
|
|
block, _ := pem.Decode(keyData)
|
|
|
|
|
|
|
|
//X509解码
|
|
|
|
|
|
|
|
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
p := privateKey.(*rsa.PrivateKey)
|
|
|
|
|
|
|
|
keySize := p.Size()
|
|
|
|
|
|
|
|
srcSize := len(cipherText)
|
|
|
|
|
|
|
|
log.Println("密钥长度", keySize, "密文长度", srcSize)
|
|
|
|
|
|
|
|
var offSet = 0
|
|
|
|
|
|
|
|
var buffer = bytes.Buffer{}
|
|
|
|
|
|
|
|
for offSet < srcSize {
|
|
|
|
|
|
|
|
endIndex := offSet + keySize
|
|
|
|
|
|
|
|
if endIndex > srcSize {
|
|
|
|
|
|
|
|
endIndex = srcSize
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesOnce, err := rsa.DecryptPKCS1v15(rand.Reader, p, cipherText[offSet:endIndex])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Write(bytesOnce)
|
|
|
|
|
|
|
|
offSet = endIndex
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesDecrypt = buffer.Bytes()
|
|
|
|
|
|
|
|
return bytesDecrypt
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RsaEncryptBlock 公钥加密-分段
|
|
|
|
|
|
|
|
func RsaEncryptBlock(src []byte, path string) (bytesEncrypt []byte, err error) {
|
|
|
|
|
|
|
|
//打开文件
|
|
|
|
|
|
|
|
//file, err := os.Open(path)
|
|
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
////读取文件的内容
|
|
|
|
|
|
|
|
//info, _ := file.Stat()
|
|
|
|
|
|
|
|
//buf := make([]byte, info.Size())
|
|
|
|
|
|
|
|
//file.Read(buf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 读取公钥文件
|
|
|
|
|
|
|
|
keyData, err := ioutil.ReadFile(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil,err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//pem解码
|
|
|
|
|
|
|
|
block, _ := pem.Decode(keyData)
|
|
|
|
|
|
|
|
//x509解码
|
|
|
|
|
|
|
|
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//类型断言
|
|
|
|
|
|
|
|
publicKey := publicKeyInterface.(*rsa.PublicKey)
|
|
|
|
|
|
|
|
keySize, srcSize := publicKey.Size(), len(src)
|
|
|
|
|
|
|
|
log.Println("密钥长度", keySize, "明文长度", srcSize)
|
|
|
|
|
|
|
|
offSet, once := 0, keySize-11
|
|
|
|
|
|
|
|
buffer := bytes.Buffer{}
|
|
|
|
|
|
|
|
for offSet < srcSize {
|
|
|
|
|
|
|
|
endIndex := offSet + once
|
|
|
|
|
|
|
|
if endIndex > srcSize {
|
|
|
|
|
|
|
|
endIndex = srcSize
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 加密一部分
|
|
|
|
|
|
|
|
bytesOnce, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src[offSet:endIndex])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Write(bytesOnce)
|
|
|
|
|
|
|
|
offSet = endIndex
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesEncrypt = buffer.Bytes()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func timing(client mqtt.Client) {
|
|
|
|
func timing(client mqtt.Client) {
|
|
|
|
//定时器,10秒钟执行一次
|
|
|
|
//定时器,10秒钟执行一次
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
@ -193,6 +403,27 @@ func timing(client mqtt.Client) {
|
|
|
|
// queryOneDataByMySql(db)
|
|
|
|
// queryOneDataByMySql(db)
|
|
|
|
// //mydb.Close()
|
|
|
|
// //mydb.Close()
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GenerateRSAKey(2048)
|
|
|
|
|
|
|
|
//publicPath := "public.pem"
|
|
|
|
|
|
|
|
//privatePath := "private.pem"
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//var a = []byte("jack")
|
|
|
|
|
|
|
|
//encrptTxt, err := RsaEncryptBlock(a, publicPath)
|
|
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
|
|
// fmt.Println(err.Error())
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//fmt.Println("加密后的字符串:")
|
|
|
|
|
|
|
|
//encodeString := base64.StdEncoding.EncodeToString(encrptTxt)
|
|
|
|
|
|
|
|
//fmt.Println(encodeString)
|
|
|
|
|
|
|
|
//fmt.Println("-------------------")
|
|
|
|
|
|
|
|
//decodeByte, err := base64.StdEncoding.DecodeString(encodeString)
|
|
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
|
|
// fmt.Println(err.Error())
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//decrptCode := RSA_Decrypts(decodeByte, privatePath)
|
|
|
|
|
|
|
|
//fmt.Println("解密后的字符串:")
|
|
|
|
|
|
|
|
//fmt.Println(string(decrptCode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -203,38 +434,81 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
|
|
|
|
message := fmt.Sprintf("%s", msg.Payload())
|
|
|
|
message := fmt.Sprintf("%s", msg.Payload())
|
|
|
|
topic := fmt.Sprintf("%s", msg.Topic())
|
|
|
|
topic := fmt.Sprintf("%s", msg.Topic())
|
|
|
|
fmt.Println(message)
|
|
|
|
fmt.Println(message)
|
|
|
|
result := strings.Split(message, " ")
|
|
|
|
fmt.Println(topic)
|
|
|
|
fmt.Println(result[0])
|
|
|
|
|
|
|
|
fmt.Println(result[1])
|
|
|
|
|
|
|
|
|
|
|
|
privatePath := "privateKey.pem"
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
temperature := new(models.Temperature)
|
|
|
|
if topic == "app_push" {
|
|
|
|
temperature.CreateDate = now
|
|
|
|
fmt.Println("珠海电厂APP收到mqtt消息")
|
|
|
|
temperature.DataDate = now.Format("2006-01-02")
|
|
|
|
decodeByte, err := base64.StdEncoding.DecodeString(message)
|
|
|
|
temperature.DataHour = now.Format("2006-01-02 15")
|
|
|
|
if err != nil {
|
|
|
|
temperature.DataMinute = now.Format("2006-01-02 15:04")
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
temperature.Humidity = result[0]
|
|
|
|
}
|
|
|
|
temperature.Temperature = result[1]
|
|
|
|
decrptCode := RSA_Decrypts(decodeByte, privatePath)
|
|
|
|
temperature.Topic = topic
|
|
|
|
fmt.Println("解密后的字符串:")
|
|
|
|
|
|
|
|
fmt.Println(string(decrptCode))
|
|
|
|
if topic == "WifiSHT/7C87CE9CA4E6/SHT20" {
|
|
|
|
fmt.Println("-----user--------")
|
|
|
|
temperature.LocationDesc = "广东省珠海市高新区唐家湾镇东岸村水风三街28号501"
|
|
|
|
var user models.User
|
|
|
|
}
|
|
|
|
json.Unmarshal(decrptCode, &user)
|
|
|
|
if topic == "WifiSHT/7C87CE9F5CBF/SHT20" {
|
|
|
|
fmt.Println(user)
|
|
|
|
temperature.LocationDesc = "广东省珠海市金湾区三灶镇百川路1号1栋1单元1508房"
|
|
|
|
//models.SaveUser(&user)
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic == "WifiSHT/4CEBD686B6AA/SHT20" {
|
|
|
|
if models.GetUser(&user) {
|
|
|
|
temperature.LocationDesc = "广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号"
|
|
|
|
models.UpdateUser(&user)
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
fmt.Println("测温传感器地点:", temperature.LocationDesc)
|
|
|
|
models.SaveUser(&user)
|
|
|
|
|
|
|
|
}
|
|
|
|
//db, err := getMySqlDB()
|
|
|
|
|
|
|
|
//if err == nil {
|
|
|
|
}else if topic == "app_push_dyw" {
|
|
|
|
// insertDataByMySql(db,temperature)
|
|
|
|
fmt.Println("大亚湾电厂APP收到mqtt消息")
|
|
|
|
// //mydb.Close()
|
|
|
|
decodeByte, err := base64.StdEncoding.DecodeString(message)
|
|
|
|
//}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
decrptCode := RSA_Decrypts(decodeByte, privatePath)
|
|
|
|
|
|
|
|
fmt.Println("解密后的字符串:")
|
|
|
|
|
|
|
|
fmt.Println(string(decrptCode))
|
|
|
|
|
|
|
|
fmt.Println("-----user--------")
|
|
|
|
|
|
|
|
//var user models.User
|
|
|
|
|
|
|
|
//json.Unmarshal(decrptCode, &user)
|
|
|
|
|
|
|
|
//fmt.Println(user)
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//if models.GetUser(&user) {
|
|
|
|
|
|
|
|
// models.UpdateUser(&user)
|
|
|
|
|
|
|
|
//}else{
|
|
|
|
|
|
|
|
// models.SaveUser(&user)
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
result := strings.Split(message, " ")
|
|
|
|
|
|
|
|
fmt.Println(result[0])
|
|
|
|
|
|
|
|
fmt.Println(result[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
temperature := new(models.Temperature)
|
|
|
|
|
|
|
|
temperature.CreateDate = now
|
|
|
|
|
|
|
|
temperature.DataDate = now.Format("2006-01-02")
|
|
|
|
|
|
|
|
temperature.DataHour = now.Format("2006-01-02 15")
|
|
|
|
|
|
|
|
temperature.DataMinute = now.Format("2006-01-02 15:04")
|
|
|
|
|
|
|
|
temperature.Humidity = result[0]
|
|
|
|
|
|
|
|
temperature.Temperature = result[1]
|
|
|
|
|
|
|
|
temperature.Topic = topic
|
|
|
|
|
|
|
|
if topic == "WifiSHT/7C87CE9CA4E6/SHT20" {
|
|
|
|
|
|
|
|
temperature.LocationDesc = "广东省珠海市高新区唐家湾镇东岸村水风三街28号501"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic == "WifiSHT/7C87CE9F5CBF/SHT20" {
|
|
|
|
|
|
|
|
temperature.LocationDesc = "广东省珠海市金湾区三灶镇百川路1号1栋1单元1508房"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic == "WifiSHT/4CEBD686B6AA/SHT20" {
|
|
|
|
|
|
|
|
temperature.LocationDesc = "广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("测温传感器地点:", temperature.LocationDesc)
|
|
|
|
|
|
|
|
models.SaveTemperature(temperature)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.SaveTemperature(temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//uuid := uuid.New()
|
|
|
|
//uuid := uuid.New()
|
|
|
|
//models.SaveProduct()
|
|
|
|
//models.SaveProduct()
|
|
|
|
@ -267,6 +541,8 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
|
|
|
|
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
|
|
|
|
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
|
|
|
|
fmt.Println("Connected")
|
|
|
|
fmt.Println("Connected")
|
|
|
|
subTemperature(client)
|
|
|
|
subTemperature(client)
|
|
|
|
|
|
|
|
subAppPush(client)
|
|
|
|
|
|
|
|
subAppPushDyw(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
|
|
|
|
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
|
|
|
|
@ -287,6 +563,20 @@ func subTemperature(client mqtt.Client) {
|
|
|
|
fmt.Printf("Subscribed to topic: %s", topic)
|
|
|
|
fmt.Printf("Subscribed to topic: %s", topic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func subAppPush(client mqtt.Client) {
|
|
|
|
|
|
|
|
topic := "app_push"
|
|
|
|
|
|
|
|
token := client.Subscribe(topic, 2, nil)
|
|
|
|
|
|
|
|
token.Wait()
|
|
|
|
|
|
|
|
fmt.Printf("Subscribed to topic: %s", topic)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func subAppPushDyw(client mqtt.Client) {
|
|
|
|
|
|
|
|
topic := "app_push_dyw"
|
|
|
|
|
|
|
|
token := client.Subscribe(topic, 2, nil)
|
|
|
|
|
|
|
|
token.Wait()
|
|
|
|
|
|
|
|
fmt.Printf("Subscribed to topic: %s", topic)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func publish(client mqtt.Client) {
|
|
|
|
func publish(client mqtt.Client) {
|
|
|
|
num := 999999000000000000
|
|
|
|
num := 999999000000000000
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
@ -324,6 +614,8 @@ func main() {
|
|
|
|
|
|
|
|
|
|
|
|
//sub(client)
|
|
|
|
//sub(client)
|
|
|
|
subTemperature(client)
|
|
|
|
subTemperature(client)
|
|
|
|
|
|
|
|
subAppPush(client)
|
|
|
|
|
|
|
|
subAppPushDyw(client)
|
|
|
|
publish(client)
|
|
|
|
publish(client)
|
|
|
|
|
|
|
|
|
|
|
|
//client.Disconnect(1000)
|
|
|
|
//client.Disconnect(1000)
|
|
|
|
|