123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- package Plugin
- import (
- "AIOTCOER/Handle"
- "AIOTCOER/lib"
- "AIOTCOER/logs"
- "AIOTCOER/models/Device"
- "AIOTCOER/models/Product"
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "plugin"
- "strings"
- "time"
- )
- func init() {
- }
- // 设备状态 T_State:(0 验证 1 在线 2 离线 3 添加 ) T_Reason:备注内容
- func PluginStateHandle(T_sn, T_password string, T_State int, T_Reason string) error {
- switch {
- case T_State == 0: // 0 验证
- Device_r := Device.Device{T_sn: T_sn}
- if !Device_r.Read_Tidy() {
- return errors.New("T_sn 错误!")
- }
- if Device_r.T_password != T_password {
- return errors.New("T_password 错误!")
- }
- break
- case T_State == 1 || T_State == 2: // 1 在线 2 离线
- Device_r := Device.Device{T_sn: T_sn}
- if !Device_r.Read_Tidy() {
- return errors.New("T_sn 错误!")
- }
- if Device_r.T_password != T_password {
- return errors.New("T_password 错误!")
- }
- Device_r.T_online = T_State
- // 同步参数
- Device_r.Update("T_online")
- var Device_online_r map[string]interface{}
- Device_online_r = make(map[string]interface{})
- Device_online_r["online"] = Device_r.T_online
- Device_online_r["msg"] = T_Reason
- Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
- Handle.AnalysisMap(fmt.Sprintf("Msid:%d", uint8(time.Now().UnixNano())), &Device_r, map[string]interface{}{"online": Device_online_r}, "")
- // 数据 Websocket 实时推送
- Device_online_r["T_sn"] = Device_r.T_sn
- go lib.WebsocketSubscribeSendAll(Device_r.T_sn, Device_online_r)
- break
- case T_State == 3: // 3 验证添加 (deviceList[0]["deviceSn"], deviceList[0]["deviceId"], 3, T_ProductID)
- ProductTyper := Product.ProductType{T_ProductID: T_Reason}
- if !ProductTyper.Read() {
- return errors.New("产品ID 错误!")
- }
- Device_r := Device.Device{T_sn: T_sn}
- if Device_r.Read_Tidy() {
- return errors.New("T_sn 重复!")
- }
- CreateDevice_r := Device.Device{T_ProductID: ProductTyper.T_ProductID, T_sn: T_sn, T_password: T_password}
- // 单个
- if !CreateDevice_r.CreateSn(1) {
- return errors.New("CreateSn 失败!")
- }
- break
- }
- // 转换
- return nil
- }
- // 设备->平台 T_topic:订阅号 T_data:数据 返回:200 成功 201 失败,描述
- func PluginPullHandle(T_sn, T_password, T_topic string, T_data []byte) error {
- Device_r := Device.Device{T_sn: T_sn}
- if !Device_r.Read_Tidy() {
- return errors.New("T_sn 错误!")
- }
- if Device_r.T_password != T_password {
- return errors.New("T_password 错误!")
- }
- if len(T_data) == 0 {
- return errors.New("T_data 错误!")
- }
- // 转换
- return Handle.PullHandle(&Device_r, T_topic, T_data)
- }
- // 平台->设备 T_topic:订阅号 T_data:数据
- func PluginPushHandle(ProductModeId int, T_topic string, T_data []byte) error {
- v, is := Product.ProductModeMap[ProductModeId]
- if !is {
- return errors.New("ProductModeId E!")
- }
- //查找标识符
- lookup, err := v.T_Plugin.Lookup("PushHandle")
- if err != nil {
- return errors.New("ProductModeId PushHandle E!")
- }
- // 推送
- return lookup.(func(T_topic string, T_data []byte) error)(T_topic, T_data)
- }
- // 修改配置信息与启停服务
- func PluginProductModeConfigPu(ProductModeMap_v *Product.ProductMode) error {
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- // 导出配置map到文件
- filePath_json := strings.Replace(strings.Replace(ProductModeMap_v.T_file, ".SO", ".json", -1), ".so", ".json", -1)
- lib.MapToFile(ProductModeMap_v.T_Config_map, filePath_json)
- return nil
- }
- // 开启服务
- func PluginGoRun(ProductModeMap_v *Product.ProductMode) error {
- logs.Println("PluginGoRun:", ProductModeMap_v.Id)
- ProductModeMap_v.T_log = "--Start--" + "\n"
- //查找标识符
- GoRun_r, err := ProductModeMap_v.T_Plugin.Lookup("GoRun")
- if err != nil {
- logs.PrintlnError("T_Plugin.Lookup:", err)
- ProductModeMap_v.T_log += err.Error() + "\n"
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- return errors.New("GoRun E!")
- }
- // 转换配置文件
- var TConfig_map map[string]string
- TConfig_map = make(map[string]string)
- for k, v := range ProductModeMap_v.T_Config_map {
- TConfig_map[k] = v["T_value"]
- }
- // 运行插件
- ProductModeMap, err := GoRun_r.(func(TConfig_map map[string]string) (map[string]string, error))(TConfig_map)
- if err != nil {
- ProductModeMap_v.T_log += err.Error() + "\n"
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- logs.PrintlnError("GoRun E!:", err)
- return errors.New("GoRun E!")
- }
- if v, is := ProductModeMap["T_state"]; is {
- ProductModeMap_v.T_state = lib.To_int(v)
- }
- if v, is := ProductModeMap["T_connect"]; is {
- ProductModeMap_v.T_connect = lib.To_string(v)
- }
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- return nil
- }
- // 关闭服务
- func PluginGoStop(ProductModeMap_v *Product.ProductMode) error {
- logs.Println("PluginGoStop:", ProductModeMap_v.Id)
- // 关闭服务
- ProductModeMap_v.T_log = "--Stop--" + "\n"
- //查找标识符
- GoStop_r, err := ProductModeMap_v.T_Plugin.Lookup("GoStop")
- if err != nil {
- ProductModeMap_v.T_log += err.Error() + "\n"
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- return errors.New("GoStop E!")
- }
- // 转换配置文件
- var TConfig_map map[string]string
- TConfig_map = make(map[string]string)
- for k, v := range ProductModeMap_v.T_Config_map {
- TConfig_map[k] = v["T_value"]
- }
- // 运行插件
- ProductModeMap, err := GoStop_r.(func() (map[string]string, error))()
- if err != nil {
- ProductModeMap_v.T_log += err.Error() + "\n"
- logs.PrintlnError("GoStop_r.:", err)
- }
- if v, is := ProductModeMap["T_state"]; is {
- ProductModeMap_v.T_state = lib.To_int(v)
- }
- Product.ProductModeMap[ProductModeMap_v.Id] = ProductModeMap_v // 更新参数
- return nil
- }
- // 插件初始化
- func Plugininit(open *plugin.Plugin, file_r string) {
- // 协议基础信息
- ProductMode, err := open.Lookup("ProductMode")
- if err != nil {
- logs.PrintlnError("ProductMode", err)
- return
- }
- ProductModeMap := *ProductMode.(*map[string]string)
- // 参数配置模版
- ProductModeConfig, err := open.Lookup("ProductModeConfig")
- if err != nil {
- logs.PrintlnError("ProductModeConfig", err)
- return
- }
- ProductModeConfigMap := *ProductModeConfig.(*map[string]map[string]string)
- // 参数配置 保存文件
- filePath_json := strings.Replace(strings.Replace(file_r, ".SO", ".json", -1), ".so", ".json", -1)
- // 从文件中导入map数据
- for k, v := range lib.MapFromFile(filePath_json) {
- logs.Println("加载配置:", k, v)
- ProductModeConfigMap[k] = v
- }
- // 获取旧的
- Id_v, is := ProductModeMap["Id"]
- if !is {
- return
- }
- Id_int := lib.To_int(Id_v)
- t, is := Product.ProductModeMap[Id_int]
- if !is {
- t = &Product.ProductMode{}
- }
- if v, is := ProductModeMap["T_name"]; is {
- t.T_name = lib.To_string(v)
- }
- if v, is := ProductModeMap["T_connect"]; is {
- t.T_connect = lib.To_string(v)
- }
- if v, is := ProductModeMap["T_describe"]; is {
- t.T_describe = lib.To_string(v)
- }
- t.Id = Id_int
- t.T_file = file_r
- t.T_Plugin = open
- t.T_Config_map = ProductModeConfigMap
- //初始化插件
- GoInit_r, err := open.Lookup("GoInit")
- if err != nil {
- logs.PrintlnError("open.Lookup(\"GoInit\"):", err)
- return
- }
- // 初始化插件
- 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)
- if err != nil {
- logs.PrintlnError("GoInit_r.", err)
- }
- logs.Println("Id_int:", Id_int)
- Product.ProductModeMap[Id_int] = t // 更新参数
- PluginGoRun(t) // 开启服务
- // 导出配置map到文件
- lib.MapToFile(ProductModeConfigMap, filePath_json)
- }
- func PluginInit() {
- time.Sleep(time.Second * 2)
- dir, err := os.Getwd()
- if err != nil {
- logs.PrintlnError("PluginInit:", err)
- return
- }
- logs.Println("dir:", dir)
- logs.Println("---PluginInit Mode----")
- files, err := ioutil.ReadDir(dir + "/Plugin/Mode")
- if err == nil {
- for _, file := range files {
- if strings.HasSuffix(file.Name(), ".so") {
- file_r := dir + "/Plugin/Mode/" + file.Name()
- logs.Println("PluginInit Mode 导入:" + file_r)
- //打开加载插件,参数是插件的存储位置,可以是相对路径
- open, err := plugin.Open(file_r)
- if err != nil {
- logs.PrintlnError("插件加载失败!", err)
- continue
- }
- println("---")
- Plugininit(open, file_r) // 插件初始化
- }
- }
- }
- logs.Println("---PluginInit Resource----")
- files, err = ioutil.ReadDir(dir + "/Plugin/Resource")
- if err == nil {
- for _, file := range files {
- if strings.HasSuffix(file.Name(), ".so") {
- logs.Println("PluginInit Resource 导入:" + file.Name())
- //打开加载插件,参数是插件的存储位置,可以是相对路径
- //file_r := dir + "/Plugin/Mode/" + file.Name()
- //open, err := plugin.Open(file_r)
- //if err != nil {
- // panic(any(err))
- //}
- //Plugininit(open, file_r) // 插件初始化
- }
- }
- }
- }
|