123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package main
- import (
- "flag"
- "fmt"
- gmqtt "github.com/eclipse/paho.mqtt.golang"
- "github.com/jaryhe/gopkgs/mqtt"
- "net"
- "os"
- "lift-monitor/parser"
- "lift-monitor/pb"
- "lift-monitor/tcp_packet"
- "lift-monitor/timetask"
- "lift-monitor/udp_packet"
- )
- var (
- configFile = flag.String("config", "conf/common.yaml", "config file location")
- version = flag.Bool("version", false, "print the version")
- GitCommit = "library-import"
- Version = "library-import"
- )
- func showVersion() {
- fmt.Println("Version: ", Version)
- fmt.Println("GitCommit:", GitCommit)
- }
- var LimitChan = make(chan bool, 100)
- var HeartLimitChan = make(chan bool, 100)
- func prepare(filename string) {
- // 加载配置
- err := parser.LoadConfig(filename)
- if err != nil {
- fmt.Printf("get conf failed, err: %+v\n\n", err)
- os.Exit(1)
- }
- // 注册处理函数
- // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
- //parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.RedisHandler)
- parser.Register(parser.LoggerHandler, parser.RedisHandler, parser.MysqlHandler, parser.InfluxdbHandler, parser.MqttHandler)
- // 执行注册的处理函数
- parser.Handle()
- // 建立rpc客户端
- conns := pb.SetupClients()
- for _, conn := range conns {
- defer conn.Close()
- }
- timetask.Init()
- }
- func runDeviceUdp(ip string, port int, ch chan bool) {
- service := fmt.Sprintf("%s:%d", ip, port)
- udpAddr, err := net.ResolveUDPAddr("udp", service)
- if err != nil {
- fmt.Println("resolve udp addr err:", err)
- os.Exit(1)
- }
- listener, err := net.ListenUDP("udp", udpAddr)
- if err != nil {
- fmt.Println("listen tcp err:", err)
- os.Exit(2)
- }
- defer listener.Close()
- fmt.Printf("listen:%v\n", listener.LocalAddr().String())
- for {
- ch <-true
- go udp_packet.HandleSocketClient(listener, ch)
- }
- }
- func runDeviceTcp(ip string, port int) {
- service := fmt.Sprintf("%s:%d", ip, port)
- tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
- if err != nil {
- fmt.Println("resolve tcp addr err:", err)
- os.Exit(1)
- }
- listener, err := net.ListenTCP("tcp", tcpAddr)
- if err != nil {
- fmt.Println("listen tcp err:", err)
- os.Exit(2)
- }
- for {
- fmt.Printf("listen:%v\n", listener.Addr().String())
- conn, err := listener.Accept()
- if err != nil {
- continue
- }
- go tcp_packet.HandleSocketClient(conn)
- }
- }
- func subCallbackFunc(client gmqtt.Client, msg gmqtt.Message) {
- fmt.Printf("Subscribe: Topic is [%s]; msg is [%v]\n", msg.Topic(), msg.Payload())
- if msg.Topic() != parser.Conf.Mqtt.Topic {
- return
- }
- go udp_packet.MqttHandle(msg.Payload())
- }
- func mqttSubscribe() error {
- fmt.Printf("xxxtopic:%s,%v\n", parser.Conf.Mqtt.Topic, mqtt.MqttCli)
- return mqtt.Subscribe(mqtt.MqttCli, parser.Conf.Mqtt.Topic, subCallbackFunc)
- }
- func main() {
- flag.Parse()
- if *version {
- showVersion()
- os.Exit(1)
- }
- prepare(*configFile)
- //mqttSubscribe()
- //runDeviceUdp(parser.Conf.LiftMonitor.ServiceIp, parser.Conf.LiftMonitor.ServicePort, LimitChan)
- //runDeviceUdp(parser.Conf.LiftMonitor.ServiceIp, parser.Conf.LiftMonitor.HeartPort, HeartLimitChan)
- runDeviceTcp(parser.Conf.LiftMonitor.ServiceIp, parser.Conf.LiftMonitor.ServicePort)
- return
- }
|