Esempio n. 1
0
func TestCoolpy5(t *testing.T) {
	Addr := "127.0.0.1:6380"
	svcpwd := "icoolpy.com"
	//初始化用户账号服务1
	Account.Connect(Addr, svcpwd)
	//hub库2
	Hubs.Connect(Addr, svcpwd)
	//node库3
	Nodes.Connect(Addr, svcpwd)
	//控制器库4
	Controller.Connect(Addr, svcpwd)
	//数据结点value库5
	Values.Connect(Addr, svcpwd)
	//数据结点gps库6
	Gpss.Connect(Addr, svcpwd)
	//数据结点gen库7
	Gens.Connect(Addr, svcpwd)
	//数据结点img库8
	Photos.Connect(Addr, svcpwd)

	acs, err := Account.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range acs {
		fmt.Println("users", v)
	}

	hubs, err := Hubs.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range hubs {
		fmt.Println("hubs", v)
	}

	nodes, err := Nodes.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range nodes {
		fmt.Println("nodes", v)
	}

	Control, err := Controller.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range Control {
		fmt.Println("Control", v)
	}

	vals, err := Values.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range vals {
		fmt.Println("vals", v)
	}

	gpss, err := Gpss.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range gpss {
		fmt.Println("gpss", v)
	}

	gens, err := Gens.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range gens {
		fmt.Println("gens", v)
	}

	photos, err := Photos.All()
	if err != nil {
		t.Error(err)
	}
	for _, v := range photos {
		fmt.Println("photos", v)
	}

	//req, _ := http.NewRequest("GET", "http://localhost:8080/api/hub/5/node/18/photo/content", nil)
	//req.Header.Add("U-ApiKey","83f68172-62a7-412d-96b6-5db3f15c3983")
	//req.Header.Add("Range", "bytes=1023-")
	//var client http.Client
	//resp, _ := client.Do(req)
	//body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Println(len(body))
}
Esempio n. 2
0
func main() {
	fmt.Println("Coolpy Version:", CoSystem.CpVersion)
	var (
		port int
		mport int
		wport int
	)
	flag.IntVar(&port, "a", 6543, "web api port munber")
	flag.IntVar(&mport, "m", 1883, "mqtt port munber")
	flag.IntVar(&wport, "w", 8000, "www website port munber")
	flag.Parse()
	//初始化数据库服务
	redServer, err := Redico.Run()
	if err != nil {
		panic(err)
	}
	defer redServer.Close()
	svcpwd := "icoolpy.com"
	redServer.RequireAuth(svcpwd)
	//初始化用户账号服务1
	Account.Connect(redServer.Addr(), svcpwd)
	//自动检测创建超级账号
	Account.CreateAdmin()
	//自动id库0
	Incr.Connect(redServer.Addr(), svcpwd)
	//hub库2
	Hubs.Connect(redServer.Addr(), svcpwd)
	//node库3
	Nodes.Connect(redServer.Addr(), svcpwd)
	//控制器库4
	Controller.Connect(redServer.Addr(), svcpwd)
	//数据结点value库5
	Values.Connect(redServer.Addr(), svcpwd)
	//数据结点gps库6
	Gpss.Connect(redServer.Addr(), svcpwd)
	//数据结点gen库7
	Gens.Connect(redServer.Addr(), svcpwd)
	//数据结点img库8
	Photos.Connect(redServer.Addr(), svcpwd)

	//host mqtt service
	go func() {
		msvc := &Mtsvc.MqttSvc{}
		msvc.Host(mport)
	}()
	fmt.Println("Coolpy mqtt on port", strconv.Itoa(mport))

	router := httprouter.New()
	//用户管理api
	router.POST("/api/user", Basicauth.Auth(Account.UserPost))
	router.GET("/api/user/:uid", Basicauth.Auth(Account.UserGet))
	router.PUT("/api/user/:uid", Basicauth.Auth(Account.UserPut))
	router.DELETE("/api/user/:uid", Basicauth.Auth(Account.UserDel))
	router.GET("/api/um/all", Basicauth.Auth(Account.UserAll))
	router.GET("/api/um/apikey", Basicauth.Auth(Account.UserApiKey))
	router.GET("/api/um/newkey", Basicauth.Auth(Account.UserNewApiKey))
	//hubs管理api
	router.POST("/api/hubs", Basicauth.Auth(Hubs.HubPost))
	router.GET("/api/hubs", Basicauth.Auth(Hubs.HubsGet))
	router.GET("/api/hub/:hid", Basicauth.Auth(Hubs.HubGet))
	router.PUT("/api/hub/:hid", Basicauth.Auth(Hubs.HubPut))
	router.DELETE("/api/hub/:hid", Basicauth.Auth(Hubs.HubDel))
	//nodes管理api
	router.POST("/api/hub/:hid/nodes", Basicauth.Auth(Nodes.NodePost))
	router.GET("/api/hub/:hid/nodes", Basicauth.Auth(Nodes.NodesGet))
	router.GET("/api/hub/:hid/node/:nid", Basicauth.Auth(Nodes.NodeGet))
	router.PUT("/api/hub/:hid/node/:nid", Basicauth.Auth(Nodes.NodePut))
	router.DELETE("/api/hub/:hid/node/:nid", Basicauth.Auth(Nodes.NodeDel))
	//datapoints管理api
	router.POST("/api/hub/:hid/node/:nid/datapoints", DataPoints.DPPost)//传感器提交单个数据结点
	router.GET("/api/hub/:hid/node/:nid/datapoint", DataPoints.DPGet)//所有控制器及传感器取最新值
	router.PUT("/api/hub/:hid/node/:nid/datapoint", DataPoints.DPPut)//控制器更新值
	router.GET("/api/hub/:hid/node/:nid/datapoint/:key", DataPoints.DPGetByKey)//传感器取得key对应值
	router.PUT("/api/hub/:hid/node/:nid/datapoint/:key", DataPoints.DPPutByKey)//传感器更新key对应值
	router.DELETE("/api/hub/:hid/node/:nid/datapoint/:key", DataPoints.DPDelByKey)//传感器删除key对应值
	router.GET("/api/hub/:hid/node/:nid/json", DataPoints.DPGetRange)//传感器取得历史数据
	//图像管理api
	router.POST("/api/hub/:hid/node/:nid/photos", Photos.PhotoPost)//上传图片png,jpg,gif
	router.GET("/api/hub/:hid/node/:nid/photo/content", Photos.PhotoGet)
	router.GET("/api/hub/:hid/node/:nid/photo/content/:key", Photos.PhotoGetByKey)
	router.DELETE("/api/hub/:hid/node/:nid/photo/content/:key", Photos.PhotoDelByKey)
	//系统api
	router.GET("/api/sys/version", CoSystem.VersionGet)
	go func() {
		ln, err := net.Listen("tcp", ":" + strconv.Itoa(port))
		if err != nil {
			fmt.Println("Can't listen:", err)
		}
		err = http.Serve(ln, Cors.CORS(router))
		if err != nil {
			log.Fatal(err)
		}
	}()
	fmt.Println("Coolpy http on port", strconv.Itoa(port))
	if port != 6543 {
		//当api端口号被启动参数修改时即自动更新www相关连接参数
		f, err := ioutil.ReadFile("www/scripts-app/setting.js")
		if err != nil {
			fmt.Println(err)
		}
		nstring := strings.Replace(string(f), "6543", strconv.Itoa(port), -1)
		err = ioutil.WriteFile("www/scripts-app/setting.js", []byte(nstring), 0644)
		if err != nil{
			fmt.Println(err)
		}
	}

	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("www"))))
	go func() {
		err := http.ListenAndServe(":" + strconv.Itoa(wport), nil)
		if err != nil {
			log.Fatal(err)
		}
	}()
	fmt.Println("Coolpy www on port", strconv.Itoa(wport))
	fmt.Println("Power By ICOOLPY.COM")

	signalChan := make(chan os.Signal, 1)
	cleanupDone := make(chan bool)
	signal.Notify(signalChan, os.Interrupt)
	go func() {
		for _ = range signalChan {
			close(Deller.DelControl)
			close(Deller.DelControls)
			close(Deller.DelHub)
			close(Deller.DelHubs)
			close(Deller.DelNode)
			close(Deller.DelNodes)
			close(Deller.DelValues)
			close(Deller.DelGpss)
			close(Deller.DelGens)
			close(Deller.DelPhotos)
			cleanupDone <- true
		}
	}()
	<-cleanupDone
	fmt.Println("\nStoped Coolpy5...\n")
}