Plugin.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package Plugin
  2. import (
  3. "Yunlot/Handle"
  4. "Yunlot/lib"
  5. "Yunlot/logs"
  6. "Yunlot/models/Device"
  7. "Yunlot/models/Product"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "os"
  12. "plugin"
  13. "strings"
  14. "time"
  15. )
  16. func init() {
  17. }
  18. // 设备状态 T_State:(0 验证 1 在线 2 离线 ) T_Reason:备注内容
  19. func PluginStateHandle(T_sn, T_password string, T_State int, T_Reason string) error {
  20. Device_r := Device.Device{T_sn: T_sn}
  21. if !Device_r.Read_Tidy() {
  22. return errors.New("T_sn 错误!")
  23. }
  24. if Device_r.T_password != T_password {
  25. return errors.New("T_password 错误!")
  26. }
  27. if T_State != 1 && T_State != 2 {
  28. return nil
  29. }
  30. Device_r.T_online = T_State
  31. // 同步参数
  32. Device_r.Update("T_online")
  33. var Device_online_r map[string]interface{}
  34. Device_online_r = make(map[string]interface{})
  35. Device_online_r["online"] = Device_r.T_online
  36. Device_online_r["msg"] = T_Reason
  37. Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
  38. Handle.AnalysisMap(&Device_r, map[string]interface{}{"online": Device_online_r}, "")
  39. // 转换
  40. return nil
  41. }
  42. // 设备->平台 T_topic:订阅号 T_data:数据 返回:200 成功 201 失败,描述
  43. func PluginPullHandle(T_sn, T_password, T_topic string, T_data []byte) error {
  44. Device_r := Device.Device{T_sn: T_sn}
  45. if !Device_r.Read_Tidy() {
  46. return errors.New("T_sn 错误!")
  47. }
  48. if Device_r.T_password != T_password {
  49. return errors.New("T_password 错误!")
  50. }
  51. if len(T_data) == 0 {
  52. return errors.New("T_data 错误!")
  53. }
  54. // 转换
  55. return Handle.PullHandle(&Device_r, T_topic, T_data)
  56. }
  57. // 平台->设备 T_topic:订阅号 T_data:数据
  58. func PluginPushHandle(ProductModeId int, T_topic string, T_data []byte) error {
  59. v, is := Product.ProductModeMap[ProductModeId]
  60. if !is {
  61. return errors.New("ProductModeId E!")
  62. }
  63. //查找标识符
  64. lookup, err := v.T_Plugin.Lookup("PushHandle")
  65. if err != nil {
  66. return errors.New("ProductModeId PushHandle E!")
  67. }
  68. // 推送
  69. return lookup.(func(T_topic string, T_data []byte) error)(T_topic, T_data)
  70. }
  71. // 修改配置信息与启停服务
  72. func PluginProductModeConfigPu(ProductModeMap_v *Product.ProductMode) error {
  73. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  74. // 导出配置map到文件
  75. filePath_json := strings.Replace(strings.Replace(ProductModeMap_v.T_file, ".SO", ".json", -1), ".so", ".json", -1)
  76. lib.MapToFile(ProductModeMap_v.T_Config_map, filePath_json)
  77. return nil
  78. }
  79. // 开启服务
  80. func PluginGoRun(ProductModeMap_v *Product.ProductMode) error {
  81. logs.Println("PluginGoRun:", ProductModeMap_v.Id)
  82. ProductModeMap_v.T_log = "--Stop--"
  83. //查找标识符
  84. GoRun_r, err := ProductModeMap_v.T_Plugin.Lookup("GoRun")
  85. if err != nil {
  86. logs.PrintlnError("", err)
  87. ProductModeMap_v.T_log += err.Error()
  88. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  89. return errors.New("GoRun E!")
  90. }
  91. // 转换配置文件
  92. var TConfig_map map[string]string
  93. TConfig_map = make(map[string]string)
  94. for k, v := range ProductModeMap_v.T_Config_map {
  95. TConfig_map[k] = v["T_value"]
  96. }
  97. // 运行插件
  98. ProductModeMap, err := GoRun_r.(func(TConfig_map map[string]string) (map[string]string, error))(TConfig_map)
  99. if err != nil {
  100. ProductModeMap_v.T_log += err.Error()
  101. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  102. return errors.New("GoRun E!")
  103. }
  104. if v, is := ProductModeMap["T_state"]; is {
  105. ProductModeMap_v.T_state = lib.To_int(v)
  106. }
  107. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  108. return nil
  109. }
  110. // 关闭服务
  111. func PluginGoStop(ProductModeMap_v *Product.ProductMode) error {
  112. logs.Println("PluginGoStop:", ProductModeMap_v.Id)
  113. // 关闭服务
  114. ProductModeMap_v.T_log = "--Stop--"
  115. //查找标识符
  116. GoStop_r, err := ProductModeMap_v.T_Plugin.Lookup("GoStop")
  117. if err != nil {
  118. ProductModeMap_v.T_log += err.Error()
  119. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  120. return errors.New("GoStop E!")
  121. }
  122. // 转换配置文件
  123. var TConfig_map map[string]string
  124. TConfig_map = make(map[string]string)
  125. for k, v := range ProductModeMap_v.T_Config_map {
  126. TConfig_map[k] = v["T_value"]
  127. }
  128. // 运行插件
  129. ProductModeMap, err := GoStop_r.(func() (map[string]string, error))()
  130. if err != nil {
  131. ProductModeMap_v.T_log += err.Error()
  132. logs.PrintlnError("", err)
  133. }
  134. if v, is := ProductModeMap["T_state"]; is {
  135. ProductModeMap_v.T_state = lib.To_int(v)
  136. }
  137. Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
  138. return nil
  139. }
  140. // 插件初始化
  141. func Plugininit(open *plugin.Plugin, file_r string) {
  142. // 协议基础信息
  143. ProductMode, err := open.Lookup("ProductMode")
  144. if err != nil {
  145. logs.PrintlnError("ProductMode", err)
  146. return
  147. }
  148. ProductModeMap := *ProductMode.(*map[string]string)
  149. // 参数配置模版
  150. ProductModeConfig, err := open.Lookup("ProductModeConfig")
  151. if err != nil {
  152. logs.PrintlnError("ProductModeConfig", err)
  153. return
  154. }
  155. ProductModeConfigMap := *ProductModeConfig.(*map[string]map[string]string)
  156. // 参数配置 保存文件
  157. filePath_json := strings.Replace(strings.Replace(file_r, ".SO", ".json", -1), ".so", ".json", -1)
  158. // 从文件中导入map数据
  159. for k, v := range lib.MapFromFile(filePath_json) {
  160. logs.Println("加载配置:", k, v)
  161. ProductModeConfigMap[k] = v
  162. }
  163. // 获取旧的
  164. Id_v, is := ProductModeMap["Id"]
  165. if !is {
  166. return
  167. }
  168. Id_int := lib.To_int(Id_v)
  169. t, is := Product.ProductModeMap[Id_int]
  170. if !is {
  171. t = &Product.ProductMode{}
  172. }
  173. if v, is := ProductModeMap["T_name"]; is {
  174. t.T_name = lib.To_string(v)
  175. }
  176. if v, is := ProductModeMap["T_connect"]; is {
  177. t.T_connect = lib.To_string(v)
  178. }
  179. if v, is := ProductModeMap["T_describe"]; is {
  180. t.T_describe = lib.To_string(v)
  181. }
  182. t.Id = Id_int
  183. t.T_file = file_r
  184. t.T_Plugin = open
  185. t.T_Config_map = ProductModeConfigMap
  186. //初始化插件
  187. GoInit_r, err := open.Lookup("GoInit")
  188. if err != nil {
  189. logs.PrintlnError("", err)
  190. return
  191. }
  192. // 初始化插件
  193. GoInit_r.(func(FunPluginStateHandle func(T_sn, T_password string, T_State int, T_Reason string) error, FunPluginPullHandle func(T_sn, T_password, T_topic string, T_data []byte) error))(PluginStateHandle, PluginPullHandle)
  194. if err != nil {
  195. logs.PrintlnError("", err)
  196. }
  197. fmt.Println("Id_int:", Id_int)
  198. Product.ProductModeMap[Id_int] = t // 更新参数
  199. PluginGoRun(t) // 开启服务
  200. // 导出配置map到文件
  201. lib.MapToFile(ProductModeConfigMap, filePath_json)
  202. }
  203. func PluginInit() {
  204. time.Sleep(time.Second * 2)
  205. dir, err := os.Getwd()
  206. if err != nil {
  207. logs.Println("PluginInit:", err)
  208. return
  209. }
  210. logs.Println("dir:", dir)
  211. logs.Println("---PluginInit Mode----")
  212. files, err := ioutil.ReadDir(dir + "/Plugin/Mode")
  213. if err == nil {
  214. for _, file := range files {
  215. if strings.HasSuffix(file.Name(), ".so") {
  216. file_r := dir + "/Plugin/Mode/" + file.Name()
  217. logs.Println("PluginInit Mode 导入:" + file_r)
  218. //打开加载插件,参数是插件的存储位置,可以是相对路径
  219. open, err := plugin.Open(file_r)
  220. if err != nil {
  221. logs.PrintlnError("插件加载失败!", err)
  222. continue
  223. }
  224. println("---")
  225. Plugininit(open, file_r) // 插件初始化
  226. }
  227. }
  228. }
  229. logs.Println("---PluginInit Resource----")
  230. files, err = ioutil.ReadDir(dir + "/Plugin/Resource")
  231. if err == nil {
  232. for _, file := range files {
  233. if strings.HasSuffix(file.Name(), ".so") {
  234. logs.Println("PluginInit Resource 导入:" + file.Name())
  235. //打开加载插件,参数是插件的存储位置,可以是相对路径
  236. file_r := dir + "/Plugin/Mode/" + file.Name()
  237. open, err := plugin.Open(file_r)
  238. if err != nil {
  239. panic(any(err))
  240. }
  241. Plugininit(open, file_r) // 插件初始化
  242. }
  243. }
  244. }
  245. }