123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package controllers
- import (
- "AIOTCOER/Plugin"
- "AIOTCOER/conf"
- "AIOTCOER/lib"
- "AIOTCOER/logs"
- "AIOTCOER/models/Product"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- "io"
- "net/http"
- "net/url"
- "os"
- "path"
- "plugin"
- "time"
- )
- type ProductModeController struct {
- beego.Controller
- }
- func (c *ProductModeController) ProductModeLists() {
- ProductProt_r, _ := Product.ProductModeLists()
- //
- //if c.GetString("AdminTokey") != conf.Config_AdminTokey {
- // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ok!", Data: "AdminTokey E!" }
- // c.ServeJSON()
- // return
- //}
- // 网关
- type ProductMode struct {
- Id int `json:"Id"`
- T_name string `json:"T_name"` // 协议名称 Mqtt
- }
- var maplist []ProductMode
- for _,v := range ProductProt_r{
- maplist = append(maplist, ProductMode{Id: v.Id, T_name: v.T_name} )
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: maplist }
- c.ServeJSON()
- return
- }
- func (c *ProductModeController) Updata() {
- if c.GetString("AdminTokey") != conf.Config_AdminTokey {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "AdminTokey E!"}
- c.ServeJSON()
- return
- }
- Id, _ := c.GetInt("Id", 0)
- if Id <= 0 {
- // 添加设备
- //ProductProt_r := Product.ProductMode{}
- //c.ParseForm(&ProductProt_r)
- //ProductProt_r.Id = 0
- //ProductProt_r.Add()
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ""}
- c.ServeJSON()
- return
- }
- ProductModeMap_r, is := Product.ProductModeMap[Id]
- if !is {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
- c.ServeJSON()
- return
- }
- T_Config_map := c.GetString("T_Config_map")
- if len(T_Config_map) > 0 {
- logs.Println("修改配置参数:", T_Config_map)
- var articleSlide map[string]map[string]string
- err := json.Unmarshal([]byte(T_Config_map), &articleSlide)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "Data err!"}
- c.ServeJSON()
- return
- }
- ProductModeMap_r.T_Config_map = articleSlide
- Product.ProductModeMap[Id] = ProductModeMap_r
- Plugin.PluginProductModeConfigPu(ProductModeMap_r)
- if ProductModeMap_r.T_state == 1 {
- Plugin.PluginGoStop(ProductModeMap_r)
- time.Sleep(time.Second * 2)
- Plugin.PluginGoRun(ProductModeMap_r)
- }
- }
- T_state, err := c.GetInt("T_state")
- if err == nil {
- logs.Println("T_state:", T_state)
- if T_state == 1 {
- time.Sleep(time.Second * 1)
- Plugin.PluginGoRun(ProductModeMap_r)
- } else {
- Plugin.PluginGoStop(ProductModeMap_r)
- }
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *ProductModeController) Import() {
- if c.GetString("AdminTokey") != conf.Config_AdminTokey {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "AdminTokey E!"}
- c.ServeJSON()
- return
- }
- // 验证 TOKEY
- if len(c.GetString("T_uuid")) == 0 {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
- c.ServeJSON()
- return
- }
- dir, err := os.Getwd()
- if err != nil {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Getwd E!"}
- c.ServeJSON()
- return
- }
- // 获取上传的zip文件
- zipFile, FileHeader, err := c.GetFile("so")
- if err == nil {
- defer zipFile.Close()
- file_r := dir + "/Plugin/Mode/" + FileHeader.Filename
- logs.Println("file_r:", file_r)
- // 检查文件是否存在
- if _, err := os.Stat(file_r); !os.IsNotExist(err) {
- logs.Println("os.Stat:", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "文件已存在!"}
- c.ServeJSON()
- return
- }
- // 创建一个临时文件来保存上传的zip文件
- tempFile, err := os.Create(file_r)
- if err != nil {
- logs.Println("Error creating temp file:", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "os.Create!"}
- c.ServeJSON()
- return
- }
- // 将上传的zip文件内容复制到临时文件中
- _, err = io.Copy(tempFile, zipFile)
- if err != nil {
- logs.Println("Error copying zip file to temp file:", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Copy!"}
- c.ServeJSON()
- return
- }
- tempFile.Close()
- time.Sleep(time.Second * 1)
- //
- logs.Println("PluginInit Mode 导入:" + file_r)
- //打开加载插件,参数是插件的存储位置,可以是相对路径
- open, err := plugin.Open(file_r)
- if err != nil {
- logs.PrintlnError("插件加载失败!", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "插件加载失败!"}
- c.ServeJSON()
- return
- }
- Plugin.Plugininit(open, file_r) // 插件初始化
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- fileUrl := c.GetString("urlso")
- if len(fileUrl) > 0 {
- // 下载文件的URL
- parsedURL, err := url.Parse(fileUrl)
- if err != nil {
- logs.Println("Error parsing URL:", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "urlso E!"}
- c.ServeJSON()
- return
- }
- fileName := path.Base(parsedURL.Path)
- logs.Println("远程文件的文件名:", fileName)
- file_r := dir + "/Plugin/Mode/" + fileName
- logs.Println("file_r:", file_r)
- // 检查文件是否存在
- if _, err := os.Stat(file_r); !os.IsNotExist(err) {
- logs.Println("os.Stat:", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "文件已存在!"}
- c.ServeJSON()
- return
- }
- // 创建输出文件
- output, err := os.Create(file_r)
- if err != nil {
- logs.Println("Error while creating", "-", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "os.Create E!"}
- c.ServeJSON()
- return
- }
- // 发送HTTP GET请求
- response, err := http.Get(fileUrl)
- if err != nil {
- logs.Println("Error while downloading", fileUrl, "-", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "http.Get E!"}
- c.ServeJSON()
- return
- }
- defer response.Body.Close()
- // 将响应体写入输出文件
- _, err = io.Copy(output, response.Body)
- if err != nil {
- logs.Println("Error while downloading", fileUrl, "-", err)
- return
- }
- output.Close()
- logs.Println("文件下载完成")
- time.Sleep(time.Second * 1)
- //
- logs.Println("PluginInit Mode 导入:" + file_r)
- //打开加载插件,参数是插件的存储位置,可以是相对路径
- open, err := plugin.Open(file_r)
- if err != nil {
- logs.PrintlnError("插件加载失败!", err)
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "插件加载失败!"}
- c.ServeJSON()
- return
- }
- Plugin.Plugininit(open, file_r) // 插件初始化
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- }
|