ProductMode.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package controllers
  2. import (
  3. "AIOTCOER/Plugin"
  4. "AIOTCOER/conf"
  5. "AIOTCOER/lib"
  6. "AIOTCOER/logs"
  7. "AIOTCOER/models/Product"
  8. "encoding/json"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "path"
  15. "plugin"
  16. "time"
  17. )
  18. type ProductModeController struct {
  19. beego.Controller
  20. }
  21. func (c *ProductModeController) ProductModeLists() {
  22. ProductProt_r, _ := Product.ProductModeLists()
  23. //
  24. //if c.GetString("AdminTokey") != conf.Config_AdminTokey {
  25. // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ok!", Data: "AdminTokey E!" }
  26. // c.ServeJSON()
  27. // return
  28. //}
  29. // 网关
  30. type ProductMode struct {
  31. Id int `json:"Id"`
  32. T_name string `json:"T_name"` // 协议名称 Mqtt
  33. }
  34. var maplist []ProductMode
  35. for _,v := range ProductProt_r{
  36. maplist = append(maplist, ProductMode{Id: v.Id, T_name: v.T_name} )
  37. }
  38. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: maplist }
  39. c.ServeJSON()
  40. return
  41. }
  42. func (c *ProductModeController) Updata() {
  43. if c.GetString("AdminTokey") != conf.Config_AdminTokey {
  44. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "AdminTokey E!"}
  45. c.ServeJSON()
  46. return
  47. }
  48. Id, _ := c.GetInt("Id", 0)
  49. if Id <= 0 {
  50. // 添加设备
  51. //ProductProt_r := Product.ProductMode{}
  52. //c.ParseForm(&ProductProt_r)
  53. //ProductProt_r.Id = 0
  54. //ProductProt_r.Add()
  55. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ""}
  56. c.ServeJSON()
  57. return
  58. }
  59. ProductModeMap_r, is := Product.ProductModeMap[Id]
  60. if !is {
  61. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
  62. c.ServeJSON()
  63. return
  64. }
  65. T_Config_map := c.GetString("T_Config_map")
  66. if len(T_Config_map) > 0 {
  67. logs.Println("修改配置参数:", T_Config_map)
  68. var articleSlide map[string]map[string]string
  69. err := json.Unmarshal([]byte(T_Config_map), &articleSlide)
  70. if err != nil {
  71. c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "Data err!"}
  72. c.ServeJSON()
  73. return
  74. }
  75. ProductModeMap_r.T_Config_map = articleSlide
  76. Product.ProductModeMap[Id] = ProductModeMap_r
  77. Plugin.PluginProductModeConfigPu(ProductModeMap_r)
  78. if ProductModeMap_r.T_state == 1 {
  79. Plugin.PluginGoStop(ProductModeMap_r)
  80. time.Sleep(time.Second * 2)
  81. Plugin.PluginGoRun(ProductModeMap_r)
  82. }
  83. }
  84. T_state, err := c.GetInt("T_state")
  85. if err == nil {
  86. logs.Println("T_state:", T_state)
  87. if T_state == 1 {
  88. time.Sleep(time.Second * 1)
  89. Plugin.PluginGoRun(ProductModeMap_r)
  90. } else {
  91. Plugin.PluginGoStop(ProductModeMap_r)
  92. }
  93. }
  94. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  95. c.ServeJSON()
  96. return
  97. }
  98. func (c *ProductModeController) Import() {
  99. if c.GetString("AdminTokey") != conf.Config_AdminTokey {
  100. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "AdminTokey E!"}
  101. c.ServeJSON()
  102. return
  103. }
  104. // 验证 TOKEY
  105. if len(c.GetString("T_uuid")) == 0 {
  106. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. dir, err := os.Getwd()
  111. if err != nil {
  112. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Getwd E!"}
  113. c.ServeJSON()
  114. return
  115. }
  116. // 获取上传的zip文件
  117. zipFile, FileHeader, err := c.GetFile("so")
  118. if err == nil {
  119. defer zipFile.Close()
  120. file_r := dir + "/Plugin/Mode/" + FileHeader.Filename
  121. logs.Println("file_r:", file_r)
  122. // 检查文件是否存在
  123. if _, err := os.Stat(file_r); !os.IsNotExist(err) {
  124. logs.Println("os.Stat:", err)
  125. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "文件已存在!"}
  126. c.ServeJSON()
  127. return
  128. }
  129. // 创建一个临时文件来保存上传的zip文件
  130. tempFile, err := os.Create(file_r)
  131. if err != nil {
  132. logs.Println("Error creating temp file:", err)
  133. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "os.Create!"}
  134. c.ServeJSON()
  135. return
  136. }
  137. // 将上传的zip文件内容复制到临时文件中
  138. _, err = io.Copy(tempFile, zipFile)
  139. if err != nil {
  140. logs.Println("Error copying zip file to temp file:", err)
  141. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Copy!"}
  142. c.ServeJSON()
  143. return
  144. }
  145. tempFile.Close()
  146. time.Sleep(time.Second * 1)
  147. //
  148. logs.Println("PluginInit Mode 导入:" + file_r)
  149. //打开加载插件,参数是插件的存储位置,可以是相对路径
  150. open, err := plugin.Open(file_r)
  151. if err != nil {
  152. logs.PrintlnError("插件加载失败!", err)
  153. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "插件加载失败!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. Plugin.Plugininit(open, file_r) // 插件初始化
  158. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. fileUrl := c.GetString("urlso")
  163. if len(fileUrl) > 0 {
  164. // 下载文件的URL
  165. parsedURL, err := url.Parse(fileUrl)
  166. if err != nil {
  167. logs.Println("Error parsing URL:", err)
  168. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "urlso E!"}
  169. c.ServeJSON()
  170. return
  171. }
  172. fileName := path.Base(parsedURL.Path)
  173. logs.Println("远程文件的文件名:", fileName)
  174. file_r := dir + "/Plugin/Mode/" + fileName
  175. logs.Println("file_r:", file_r)
  176. // 检查文件是否存在
  177. if _, err := os.Stat(file_r); !os.IsNotExist(err) {
  178. logs.Println("os.Stat:", err)
  179. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "文件已存在!"}
  180. c.ServeJSON()
  181. return
  182. }
  183. // 创建输出文件
  184. output, err := os.Create(file_r)
  185. if err != nil {
  186. logs.Println("Error while creating", "-", err)
  187. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "os.Create E!"}
  188. c.ServeJSON()
  189. return
  190. }
  191. // 发送HTTP GET请求
  192. response, err := http.Get(fileUrl)
  193. if err != nil {
  194. logs.Println("Error while downloading", fileUrl, "-", err)
  195. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "http.Get E!"}
  196. c.ServeJSON()
  197. return
  198. }
  199. defer response.Body.Close()
  200. // 将响应体写入输出文件
  201. _, err = io.Copy(output, response.Body)
  202. if err != nil {
  203. logs.Println("Error while downloading", fileUrl, "-", err)
  204. return
  205. }
  206. output.Close()
  207. logs.Println("文件下载完成")
  208. time.Sleep(time.Second * 1)
  209. //
  210. logs.Println("PluginInit Mode 导入:" + file_r)
  211. //打开加载插件,参数是插件的存储位置,可以是相对路径
  212. open, err := plugin.Open(file_r)
  213. if err != nil {
  214. logs.PrintlnError("插件加载失败!", err)
  215. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "插件加载失败!"}
  216. c.ServeJSON()
  217. return
  218. }
  219. Plugin.Plugininit(open, file_r) // 插件初始化
  220. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  221. c.ServeJSON()
  222. return
  223. }
  224. }