package main

import (
	"fmt"
	"net"
	"os"
	"time"

	"ems.agt/lib/log"
)

const (
	//经过测试，linux下，延时需要大于100ms
	TIME_DELAY_AFTER_WRITE = 500 //500ms
)

func main() {
	log.InitLogger(yamlConfig.Logger.File, yamlConfig.Logger.Duration, "omc:tt", GetLogLevel())
	fmt.Printf("OMC tt version: %s\n", version)
	log.Infof("========================= OMC tt startup =========================")
	log.Infof("OMC tt version: %s %s %s", version, buildTime, goVer)

	conn, err := net.Dial("tcp", "192.168.4.233:4100")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
		return
	}
	var buf [8192]byte
	var n int

	n, err = conn.Write([]byte("admin\nadmin\n"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
		return
	}
	time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)

	n, err = conn.Read(buf[0:])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
		return
	}
	log.Debug(string(buf[0:n]))

	n, err = conn.Write([]byte("date\n"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
		return
	}
	time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)

	n, err = conn.Read(buf[0:])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
		return
	}
	log.Debug(string(buf[0 : n-5]))

	/*
		n, err = conn.Write([]byte("\n"))
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
			return
		}
		time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)

		n, err = conn.Read(buf[0:])
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
			return
		}
		log.Debug(string(buf[0:n]))

		log.Debug(string(buf[0:n]))
	*/
	/*	n, err = conn.Write([]byte("\n"))
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
			return
		}
		n, err = conn.Read(buf[0:])
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
			return
		}
	*/
}
