sitesMonitorControllers.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package controllers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fwh-toolbox-server/database"
  7. "fwh-toolbox-server/ext/api/fwh_pro"
  8. "time"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. "go.mongodb.org/mongo-driver/mongo/options"
  12. )
  13. type StateConfig struct {
  14. Enable bool `bson:"enable" json:"enable"`
  15. }
  16. type SocConfig struct {
  17. Enable bool `bson:"enable" json:"enable"`
  18. }
  19. type APowerConfig struct {
  20. Enable bool `bson:"enable" json:"enable"`
  21. }
  22. type AlarmConfig struct {
  23. Enable bool `bson:"enable" json:"enable"`
  24. }
  25. type VersionConfig struct {
  26. Enable bool `bson:"enable" json:"enable"`
  27. }
  28. type TariffsConfig struct {
  29. Enable bool `bson:"enable" json:"enable"`
  30. }
  31. type OthersConfig struct {
  32. Enable bool `bson:"enable" json:"enable"`
  33. }
  34. type MonitorPageConfig struct {
  35. State StateConfig `bson:"state" json:"state"`
  36. Soc SocConfig `bson:"soc" json:"soc"`
  37. APower APowerConfig `bson:"aPower" json:"aPower"`
  38. Alarm AlarmConfig `bson:"alarm" json:"alarm"`
  39. Version VersionConfig `bson:"version" json:"version"`
  40. Tariffs TariffsConfig `bson:"tariffs" json:"tariffs"`
  41. Others OthersConfig `bson:"others" json:"others"`
  42. }
  43. type SitesMonitorConfig struct {
  44. Name string `bson:"name" json:"name"`
  45. Description string `bson:"description" json:"description"`
  46. SiteList []string `bson:"siteList" json:"siteList"`
  47. PageConfig *MonitorPageConfig `bson:"pageConfig,omitempty" json:"pageConfig,omitempty"`
  48. }
  49. type SiteInfoPara struct {
  50. SiteList []string `json:"siteList"`
  51. PageSize uint `json:"pageSize"`
  52. PageNum uint `json:"pageNum"`
  53. }
  54. type SiteInfo struct {
  55. Id string `json:"id"`
  56. Name *string `json:"name"`
  57. FhpSn string `json:"fhpSn"`
  58. ActiveStatus int `json:"activeStatus"`
  59. ActiveTime string `json:"activeTime"`
  60. Country string `json:"country"`
  61. Province string `json:"province"`
  62. City string `json:"city"`
  63. OnOffStatus int `json:"onOffStatus"`
  64. RealSysHdVersion string `json:"realSysHdVersion"`
  65. RunStatus int `json:"runStatus"`
  66. Version string `json:"version"`
  67. }
  68. type SiteInfoResponse struct {
  69. Code int `json:"code"`
  70. Msg string `json:"msg"`
  71. Result []SiteInfo `json:"result"`
  72. }
  73. type WorkModeInfo struct {
  74. Id uint `json:"id"`
  75. OldIndex uint `json:"oldIndex"`
  76. Name string `json:"name"`
  77. Soc float32 `json:"soc"`
  78. MaxSoc float32 `json:"maxSoc"`
  79. MinSoc float32 `json:"minSoc"`
  80. DischargeDepthSoc *float32 `json:"dischargeDepthSoc"`
  81. WorkMode uint `json:"workMode"`
  82. EnergyIncentivesType uint `json:"energyIncentivesType"`
  83. ElectricityType uint `json:"electricityType"`
  84. }
  85. type SiteModeInfo struct {
  86. CurrendId uint `json:"currendId"`
  87. List []WorkModeInfo `json:"list"`
  88. }
  89. type SiteModeInfoResponse struct {
  90. Code int `json:"code"`
  91. Msg string `json:"msg"`
  92. Result SiteModeInfo `json:"result"`
  93. Success bool `json:"success"`
  94. }
  95. type SitePowerFlow struct {
  96. Index uint `json:"index"` // 非当天为0,当天为实时数量
  97. DeviceTimeArray []string `json:"deviceTimeArray"` // 设备本地时间,格式 “2025-08-18 00:00:00”
  98. PowerFhpArray []float32 `json:"powerFhpArray"`
  99. PowerGenArray []float32 `json:"powerGenArray"`
  100. PowerLoadArray []float32 `json:"powerLoadArray"`
  101. PowerSunArray []float32 `json:"powerSunArray"`
  102. PowerUtiArray []float32 `json:"powerUtiArray"`
  103. SocArray []float32 `json:"socArray"`
  104. RunStatusArray []uint `json:"runStatusArray"` // 运行状态,0:待机;1:充电;2:放电;3:故障
  105. KwhTotalArray []float32 `json:"kwhTotalArray"` // 总可用电量,应该是根据SOC推算出来的。
  106. }
  107. type SitePowerFlowResponse struct {
  108. Code int `json:"code"`
  109. Msg string `json:"msg"`
  110. Result SitePowerFlow `json:"result"`
  111. }
  112. // 从数据库获取站点监控配置列表
  113. func GetSiteMonitorConfig() ([]SitesMonitorConfig, error) {
  114. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  115. defer cancel()
  116. collection := database.GetCollection("SiteMonitorConfig")
  117. cursor, err := collection.Find(ctx, bson.M{}) // 空条件,查全部
  118. if err != nil {
  119. return nil, err
  120. }
  121. defer cursor.Close(ctx)
  122. var configList []SitesMonitorConfig
  123. for cursor.Next(ctx) {
  124. var cfg SitesMonitorConfig
  125. if err := cursor.Decode(&cfg); err != nil {
  126. return nil, err
  127. }
  128. configList = append(configList, cfg)
  129. }
  130. return configList, nil
  131. }
  132. // 从数据库获取监控名列表
  133. func GetSiteMonitorNameList() ([]string, error) {
  134. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  135. defer cancel()
  136. collection := database.GetCollection("SiteMonitorConfig")
  137. cursor, err := collection.Find(
  138. ctx,
  139. bson.M{}, // 空条件,查全部
  140. options.Find().SetProjection(bson.M{
  141. "name": 1, // 只要 name 字段
  142. "_id": 0, // 不要 _id 字段(默认会返回)
  143. }),
  144. )
  145. if err != nil {
  146. return nil, err
  147. }
  148. defer cursor.Close(ctx)
  149. var nameList []string
  150. for cursor.Next(ctx) {
  151. var result struct {
  152. Name string `bson:"name"`
  153. }
  154. if err := cursor.Decode(&result); err != nil {
  155. return nil, err
  156. }
  157. nameList = append(nameList, result.Name)
  158. }
  159. return nameList, nil
  160. }
  161. // 从数据库获取一条站点监控配置
  162. func GetOneSiteMonitorConfig(name string) ([]SitesMonitorConfig, error) {
  163. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  164. defer cancel()
  165. collection := database.GetCollection("SiteMonitorConfig")
  166. var cfg SitesMonitorConfig
  167. err := collection.FindOne(ctx, bson.M{"name": name}).Decode(&cfg)
  168. if err == mongo.ErrNoDocuments {
  169. return []SitesMonitorConfig{}, nil
  170. } else if err != nil {
  171. return nil, err
  172. }
  173. var configList []SitesMonitorConfig
  174. configList = append(configList, cfg)
  175. return configList, nil
  176. }
  177. // 插入一条站点监控配置到数据库
  178. func InsertSiteMonitorConfig(config SitesMonitorConfig) error {
  179. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  180. defer cancel()
  181. collection := database.GetCollection("SiteMonitorConfig")
  182. // _, err := collection.InsertOne(ctx, config)
  183. filter := bson.M{"name": config.Name} // 按用户名匹配
  184. update := bson.M{
  185. "$set": config, // 如果存在就更新所有字段
  186. }
  187. opts := options.Update().SetUpsert(true)
  188. _, err := collection.UpdateOne(ctx, filter, update, opts)
  189. if err != nil {
  190. return err
  191. }
  192. return nil
  193. }
  194. // 更新一条站点监控配置到数据库
  195. func UpdateSiteMonitorConfig(name string, config SitesMonitorConfig) error {
  196. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  197. defer cancel()
  198. collection := database.GetCollection("SiteMonitorConfig")
  199. // _, err := collection.InsertOne(ctx, config)
  200. filter := bson.M{"name": name} // 按用户名匹配
  201. update := bson.M{
  202. "$set": config, // 如果存在就更新所有字段
  203. }
  204. opts := options.Update().SetUpsert(true)
  205. _, err := collection.UpdateOne(ctx, filter, update, opts)
  206. if err != nil {
  207. return err
  208. }
  209. return nil
  210. }
  211. // 删除一条站点监控配置
  212. func DeleteSiteMonitorConfig(name string) error {
  213. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  214. defer cancel()
  215. filter := bson.M{"name": name} // 删除条件
  216. collection := database.GetCollection("SiteMonitorConfig")
  217. _, err := collection.DeleteOne(ctx, filter)
  218. if err != nil {
  219. return err
  220. }
  221. return nil
  222. }
  223. // 更新一条站点监控页面配置到数据库
  224. func UpdateSiteMonitorPageConfig(name string, config MonitorPageConfig) error {
  225. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  226. defer cancel()
  227. collection := database.GetCollection("SiteMonitorConfig")
  228. // _, err := collection.InsertOne(ctx, config)
  229. filter := bson.M{"name": name} // 按用户名匹配
  230. update := bson.M{
  231. "$set": bson.M{
  232. "pageConfig": config, // 更新页面配置字段
  233. },
  234. }
  235. opts := options.Update().SetUpsert(true)
  236. _, err := collection.UpdateOne(ctx, filter, update, opts)
  237. if err != nil {
  238. return err
  239. }
  240. return nil
  241. }
  242. // 从fwh_pro获取站点信息表
  243. func QuerySiteInfoList(para SiteInfoPara) (SiteInfoResponse, error) {
  244. resByte, err := fwh_pro.QuerySiteInfoList(para.SiteList, para.PageSize, para.PageNum)
  245. if err != nil {
  246. return SiteInfoResponse{}, err
  247. }
  248. var siteInfoRes SiteInfoResponse
  249. // 反序列化(unmarshal)
  250. jsonErr := json.Unmarshal(resByte, &siteInfoRes)
  251. if jsonErr != nil {
  252. return SiteInfoResponse{}, jsonErr
  253. }
  254. if siteInfoRes.Code == 401 {
  255. // TODO 尝试重新登录
  256. return SiteInfoResponse{}, errors.New("certification expired")
  257. }
  258. return siteInfoRes, nil
  259. }
  260. // 从fwh_pro获取站点工作模式信息
  261. func QuerySiteModeInfo(site string) (SiteModeInfoResponse, error) {
  262. resByte, err := fwh_pro.QuerySiteModeInfo(site)
  263. if err != nil {
  264. return SiteModeInfoResponse{}, err
  265. }
  266. var siteModeInfoRes SiteModeInfoResponse
  267. // 反序列化(unmarshal)
  268. jsonErr := json.Unmarshal(resByte, &siteModeInfoRes)
  269. if jsonErr != nil {
  270. return SiteModeInfoResponse{}, jsonErr
  271. }
  272. if siteModeInfoRes.Code == 401 {
  273. // TODO 尝试重新登录
  274. return SiteModeInfoResponse{}, errors.New("certification expired")
  275. }
  276. return siteModeInfoRes, nil
  277. }
  278. // 从fwh_pro获取站点功率流
  279. func QuerySitePowerFlow(site string, day time.Time) (SitePowerFlowResponse, error) {
  280. resByte, err := fwh_pro.QuerySitePowerFlow(site, day)
  281. if err != nil {
  282. return SitePowerFlowResponse{}, err
  283. }
  284. var sitePowerFlowRes SitePowerFlowResponse
  285. // 反序列化(unmarshal)
  286. jsonErr := json.Unmarshal(resByte, &sitePowerFlowRes)
  287. if jsonErr != nil {
  288. return SitePowerFlowResponse{}, jsonErr
  289. }
  290. if sitePowerFlowRes.Code == 401 {
  291. // TODO 尝试重新登录
  292. return SitePowerFlowResponse{}, errors.New("certification expired")
  293. }
  294. return sitePowerFlowRes, nil
  295. }