sitesAnalysisControllers.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. type SiteAlarmList struct {
  113. Id uint `json:"id"`
  114. GatewayId string `json:"gatewayId"`
  115. AlarmEqSn string `json:"alarmEqSn"`
  116. AlarmCode string `json:"alarmCode"`
  117. CommonAlarmCode string `json:"commonAlarmCode"`
  118. Level int `json:"level"`
  119. Time string `json:"time"`
  120. LogName string `json:"logName"`
  121. CommonLogName string `json:"commonLogName"`
  122. LogNameEn string `json:"logNameEn"`
  123. CommonLogNameEn string `json:"commonLogNameEn"`
  124. }
  125. type SiteAlarmListResponse struct {
  126. Code int `json:"code"`
  127. Message string `json:"message"`
  128. Result []SiteAlarmList `json:"result"`
  129. Success bool `json:"success"`
  130. Total uint `json:"total"`
  131. }
  132. type Progress string // 'pending' | 'running' | 'error' | 'complete'
  133. type SiteAnalysisInfo struct {
  134. Id uint64 `bson:"id" json:"id"`
  135. Sn string `bson:"sn" json:"sn"`
  136. Description string `bson:"description" json:"description"`
  137. Item []string `bson:"item" json:"item"`
  138. Progress Progress `bson:"progress" json:"progress"`
  139. }
  140. // 从数据库获取站点分析列表
  141. func GetSiteAnalysisList() ([]SiteAnalysisInfo, error) {
  142. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  143. defer cancel()
  144. collection := database.GetCollection("SiteMonitorConfig")
  145. cursor, err := collection.Find(ctx, bson.M{}) // 空条件,查全部
  146. if err != nil {
  147. return nil, err
  148. }
  149. defer cursor.Close(ctx)
  150. var configList []SitesMonitorConfig
  151. for cursor.Next(ctx) {
  152. var cfg SitesMonitorConfig
  153. if err := cursor.Decode(&cfg); err != nil {
  154. return nil, err
  155. }
  156. configList = append(configList, cfg)
  157. }
  158. return configList, nil
  159. }
  160. // 从数据库获取站点监控配置列表
  161. func GetSiteMonitorConfig() ([]SitesMonitorConfig, error) {
  162. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  163. defer cancel()
  164. collection := database.GetCollection("SiteMonitorConfig")
  165. cursor, err := collection.Find(ctx, bson.M{}) // 空条件,查全部
  166. if err != nil {
  167. return nil, err
  168. }
  169. defer cursor.Close(ctx)
  170. var configList []SitesMonitorConfig
  171. for cursor.Next(ctx) {
  172. var cfg SitesMonitorConfig
  173. if err := cursor.Decode(&cfg); err != nil {
  174. return nil, err
  175. }
  176. configList = append(configList, cfg)
  177. }
  178. return configList, nil
  179. }
  180. // 从数据库获取监控名列表
  181. func GetSiteMonitorNameList() ([]string, error) {
  182. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  183. defer cancel()
  184. collection := database.GetCollection("SiteMonitorConfig")
  185. cursor, err := collection.Find(
  186. ctx,
  187. bson.M{}, // 空条件,查全部
  188. options.Find().SetProjection(bson.M{
  189. "name": 1, // 只要 name 字段
  190. "_id": 0, // 不要 _id 字段(默认会返回)
  191. }),
  192. )
  193. if err != nil {
  194. return nil, err
  195. }
  196. defer cursor.Close(ctx)
  197. var nameList []string
  198. for cursor.Next(ctx) {
  199. var result struct {
  200. Name string `bson:"name"`
  201. }
  202. if err := cursor.Decode(&result); err != nil {
  203. return nil, err
  204. }
  205. nameList = append(nameList, result.Name)
  206. }
  207. return nameList, nil
  208. }
  209. // 从数据库获取一条站点监控配置
  210. func GetOneSiteMonitorConfig(name string) ([]SitesMonitorConfig, error) {
  211. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  212. defer cancel()
  213. collection := database.GetCollection("SiteMonitorConfig")
  214. var cfg SitesMonitorConfig
  215. err := collection.FindOne(ctx, bson.M{"name": name}).Decode(&cfg)
  216. if err == mongo.ErrNoDocuments {
  217. return []SitesMonitorConfig{}, nil
  218. } else if err != nil {
  219. return nil, err
  220. }
  221. var configList []SitesMonitorConfig
  222. configList = append(configList, cfg)
  223. return configList, nil
  224. }
  225. // 插入一条站点监控配置到数据库
  226. func InsertSiteMonitorConfig(config SitesMonitorConfig) error {
  227. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  228. defer cancel()
  229. collection := database.GetCollection("SiteMonitorConfig")
  230. // _, err := collection.InsertOne(ctx, config)
  231. filter := bson.M{"name": config.Name} // 按用户名匹配
  232. update := bson.M{
  233. "$set": config, // 如果存在就更新所有字段
  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. // 更新一条站点监控配置到数据库
  243. func UpdateSiteMonitorConfig(name string, config SitesMonitorConfig) error {
  244. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  245. defer cancel()
  246. collection := database.GetCollection("SiteMonitorConfig")
  247. // _, err := collection.InsertOne(ctx, config)
  248. filter := bson.M{"name": name} // 按用户名匹配
  249. update := bson.M{
  250. "$set": config, // 如果存在就更新所有字段
  251. }
  252. opts := options.Update().SetUpsert(true)
  253. _, err := collection.UpdateOne(ctx, filter, update, opts)
  254. if err != nil {
  255. return err
  256. }
  257. return nil
  258. }
  259. // 删除一条站点监控配置
  260. func DeleteSiteMonitorConfig(name string) error {
  261. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  262. defer cancel()
  263. filter := bson.M{"name": name} // 删除条件
  264. collection := database.GetCollection("SiteMonitorConfig")
  265. _, err := collection.DeleteOne(ctx, filter)
  266. if err != nil {
  267. return err
  268. }
  269. return nil
  270. }
  271. // 更新一条站点监控页面配置到数据库
  272. func UpdateSiteMonitorPageConfig(name string, config MonitorPageConfig) error {
  273. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  274. defer cancel()
  275. collection := database.GetCollection("SiteMonitorConfig")
  276. // _, err := collection.InsertOne(ctx, config)
  277. filter := bson.M{"name": name} // 按用户名匹配
  278. update := bson.M{
  279. "$set": bson.M{
  280. "pageConfig": config, // 更新页面配置字段
  281. },
  282. }
  283. opts := options.Update().SetUpsert(true)
  284. _, err := collection.UpdateOne(ctx, filter, update, opts)
  285. if err != nil {
  286. return err
  287. }
  288. return nil
  289. }
  290. // 从fwh_pro获取站点信息表
  291. func QuerySiteInfoList(para SiteInfoPara) (SiteInfoResponse, error) {
  292. resByte, err := fwh_pro.QuerySiteInfoList(para.SiteList, para.PageSize, para.PageNum)
  293. if err != nil {
  294. return SiteInfoResponse{}, err
  295. }
  296. var siteInfoRes SiteInfoResponse
  297. // 反序列化(unmarshal)
  298. jsonErr := json.Unmarshal(resByte, &siteInfoRes)
  299. if jsonErr != nil {
  300. return SiteInfoResponse{}, jsonErr
  301. }
  302. if siteInfoRes.Code == 401 {
  303. // TODO 尝试重新登录
  304. return SiteInfoResponse{}, errors.New("certification expired")
  305. }
  306. return siteInfoRes, nil
  307. }
  308. // 从fwh_pro获取站点工作模式信息
  309. func QuerySiteModeInfo(site string) (SiteModeInfoResponse, error) {
  310. resByte, err := fwh_pro.QuerySiteModeInfo(site)
  311. if err != nil {
  312. return SiteModeInfoResponse{}, err
  313. }
  314. var siteModeInfoRes SiteModeInfoResponse
  315. // 反序列化(unmarshal)
  316. jsonErr := json.Unmarshal(resByte, &siteModeInfoRes)
  317. if jsonErr != nil {
  318. return SiteModeInfoResponse{}, jsonErr
  319. }
  320. if siteModeInfoRes.Code == 401 {
  321. // TODO 尝试重新登录
  322. return SiteModeInfoResponse{}, errors.New("certification expired")
  323. }
  324. return siteModeInfoRes, nil
  325. }
  326. // 从fwh_pro获取站点功率流
  327. func QuerySitePowerFlow(site string, day time.Time) (SitePowerFlowResponse, error) {
  328. resByte, err := fwh_pro.QuerySitePowerFlow(site, day)
  329. if err != nil {
  330. return SitePowerFlowResponse{}, err
  331. }
  332. var sitePowerFlowRes SitePowerFlowResponse
  333. // 反序列化(unmarshal)
  334. jsonErr := json.Unmarshal(resByte, &sitePowerFlowRes)
  335. if jsonErr != nil {
  336. return SitePowerFlowResponse{}, jsonErr
  337. }
  338. if sitePowerFlowRes.Code == 401 {
  339. // TODO 尝试重新登录
  340. return SitePowerFlowResponse{}, errors.New("certification expired")
  341. }
  342. return sitePowerFlowRes, nil
  343. }
  344. // 从fwh_pro获取站点告警列表
  345. func QuerySiteAlarmList(site string) (SiteAlarmListResponse, error) {
  346. resByte, err := fwh_pro.QuerySiteAlarmList(site)
  347. if err != nil {
  348. return SiteAlarmListResponse{}, err
  349. }
  350. var siteAlarmListRes SiteAlarmListResponse
  351. // 反序列化(unmarshal)
  352. jsonErr := json.Unmarshal(resByte, &siteAlarmListRes)
  353. if jsonErr != nil {
  354. return SiteAlarmListResponse{}, jsonErr
  355. }
  356. if siteAlarmListRes.Code == 401 {
  357. // TODO 尝试重新登录
  358. return SiteAlarmListResponse{}, errors.New("certification expired")
  359. }
  360. return siteAlarmListRes, nil
  361. }