Plugin.go 8.9 KB

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