optimizecode

This commit is contained in:
Quynh Nguyen 2019-11-06 14:40:45 +07:00
parent 42bc76cef4
commit 7443722000

View File

@ -156,7 +156,7 @@ func main() {
fmt.Println(door) fmt.Println(door)
mqtt_begin() mqtt_begin()
//SetConnectionLostHandler(connLostHandler) //SetConnectionLostHandler(connLostHandler)
mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler) //mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
// Open and map memory to access gpio, check for errors // Open and map memory to access gpio, check for errors
if err := rpio.Open(); err != nil { if err := rpio.Open(); err != nil {
@ -196,14 +196,25 @@ func mqtt_begin() {
fmt.Println(CMS_HOST_BI) fmt.Println(CMS_HOST_BI)
fmt.Println(CMS_ACCESS_TOKEN_BI) fmt.Println(CMS_ACCESS_TOKEN_BI)
fmt.Println(CMS_PASS_BI) fmt.Println(CMS_PASS_BI)
opts_cms_bi := mqtt.NewClientOptions() opts_cms_bi := mqtt.NewClientOptions()
opts_cms_bi.AddBroker(CMS_HOST_BI) opts_cms_bi.AddBroker(CMS_HOST_BI)
opts_cms_bi.SetUsername(CMS_ACCESS_TOKEN_BI) opts_cms_bi.SetUsername(CMS_ACCESS_TOKEN_BI)
opts_cms_bi.SetPassword(CMS_PASS_BI) opts_cms_bi.SetPassword(CMS_PASS_BI)
opts_cms_bi.SetCleanSession(true) opts_cms_bi.SetCleanSession(true)
opts_cms_bi.OnConnect = func(c mqtt.Client) {
fmt.Printf("Client connected, subscribing to: " + CMS_TOPIC_IN)
c.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
}
opts_cms_bi.OnConnectionLost = (c mqtt.Client, err error) {
fmt.Println("MQTT Lost Connect\n")
fmt.Println(err)
}
mqtt_cms_bi = mqtt.NewClient(opts_cms_bi) mqtt_cms_bi = mqtt.NewClient(opts_cms_bi)
opts_cms_bi.SetConnectionLostHandler(MQTTLostConnectHandler) // opts_cms_bi.SetConnectionLostHandler(MQTTLostConnectHandler)
opts_cms_bi.SetOnConnectHandler(MQTTOnConnectHandler) // opts_cms_bi.SetOnConnectHandler(MQTTOnConnectHandler)
var reconect = false var reconect = false
for reconect == false { for reconect == false {
@ -213,38 +224,36 @@ func mqtt_begin() {
fmt.Printf("MQTT CMS Beetsoft Connected\n") fmt.Printf("MQTT CMS Beetsoft Connected\n")
} }
reconect = true reconect = true
} else {
//file.Write_log("MQTT CMS Beetsoft cant not Connected\n", path_log)
if Debug == 1 {
fmt.Printf("MQTT CMS Beetsoft cant not Connected\n")
fmt.Printf("Loi CMS Beetsoft : %v \n", token_1.Error())
fmt.Printf("-------------------\n")
}
reconect = false
} }
// else {
// //file.Write_log("MQTT CMS Beetsoft cant not Connected\n", path_log)
// if Debug == 1 {
// fmt.Printf("MQTT CMS Beetsoft cant not Connected\n")
// fmt.Printf("Loi CMS Beetsoft : %v \n", token_1.Error())
// fmt.Printf("-------------------\n")
// }
// reconect = false
// }
} }
opts_cms_bi.OnConnect = func(c mqtt.Client) {
fmt.Printf("Client connected, subscribing to: " + CMS_TOPIC_IN)
c.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
}
} }
func MQTTLostConnectHandler(c mqtt.Client, err error) { // func MQTTLostConnectHandler(c mqtt.Client, err error) {
// file.Write_log("MQTT CMS Beetsoft Lost Connect\n", box.Path_log_luncher) // // file.Write_log("MQTT CMS Beetsoft Lost Connect\n", box.Path_log_luncher)
fmt.Println("MQTT CMS Beetsoft Lost Connect\n") // fmt.Println("MQTT CMS Beetsoft Lost Connect\n")
fmt.Println(err) // fmt.Println(err)
} // }
func MQTTOnConnectHandler(client mqtt.Client) { // func MQTTOnConnectHandler(client mqtt.Client) {
// file.Write_log("Reconnect: MQTT_OnConnectHandler\n", box.Path_log_luncher) // // file.Write_log("Reconnect: MQTT_OnConnectHandler\n", box.Path_log_luncher)
fmt.Println("Reconnect: MQTT_OnConnectHandler\n") // fmt.Println("Reconnect: MQTT_OnConnectHandler\n")
mqtt_cms_bi.Unsubscribe(CMS_TOPIC_IN) // mqtt_cms_bi.Unsubscribe(CMS_TOPIC_IN)
time.Sleep(10) // time.Sleep(10)
mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler) // mqtt_cms_bi.Subscribe(CMS_TOPIC_IN, 0, mqtt_messageHandler)
fmt.Println("Recall function Subscribe MQTT\n") // fmt.Println("Recall function Subscribe MQTT\n")
// file.Write_log("Recall function Subscribe MQTT\n", box.Path_log_luncher) // // file.Write_log("Recall function Subscribe MQTT\n", box.Path_log_luncher)
} // }
/************************************************************************/ /************************************************************************/
var k int var k int