sitesMonitorControllers.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // 从数据库获取站点监控配置列表
  133. func GetSiteMonitorConfig() ([]SitesMonitorConfig, error) {
  134. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  135. defer cancel()
  136. collection := database.GetCollection("SiteMonitorConfig")
  137. cursor, err := collection.Find(ctx, bson.M{}) // 空条件,查全部
  138. if err != nil {
  139. return nil, err
  140. }
  141. defer cursor.Close(ctx)
  142. var configList []SitesMonitorConfig
  143. for cursor.Next(ctx) {
  144. var cfg SitesMonitorConfig
  145. if err := cursor.Decode(&cfg); err != nil {
  146. return nil, err
  147. }
  148. configList = append(configList, cfg)
  149. }
  150. return configList, nil
  151. }
  152. // 从数据库获取监控名列表
  153. func GetSiteMonitorNameList() ([]string, error) {
  154. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  155. defer cancel()
  156. collection := database.GetCollection("SiteMonitorConfig")
  157. cursor, err := collection.Find(
  158. ctx,
  159. bson.M{}, // 空条件,查全部
  160. options.Find().SetProjection(bson.M{
  161. "name": 1, // 只要 name 字段
  162. "_id": 0, // 不要 _id 字段(默认会返回)
  163. }),
  164. )
  165. if err != nil {
  166. return nil, err
  167. }
  168. defer cursor.Close(ctx)
  169. var nameList []string
  170. for cursor.Next(ctx) {
  171. var result struct {
  172. Name string `bson:"name"`
  173. }
  174. if err := cursor.Decode(&result); err != nil {
  175. return nil, err
  176. }
  177. nameList = append(nameList, result.Name)
  178. }
  179. return nameList, nil
  180. }
  181. // 从数据库获取一条站点监控配置
  182. func GetOneSiteMonitorConfig(name string) ([]SitesMonitorConfig, error) {
  183. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  184. defer cancel()
  185. collection := database.GetCollection("SiteMonitorConfig")
  186. var cfg SitesMonitorConfig
  187. err := collection.FindOne(ctx, bson.M{"name": name}).Decode(&cfg)
  188. if err == mongo.ErrNoDocuments {
  189. return []SitesMonitorConfig{}, nil
  190. } else if err != nil {
  191. return nil, err
  192. }
  193. var configList []SitesMonitorConfig
  194. configList = append(configList, cfg)
  195. return configList, nil
  196. }
  197. // 插入一条站点监控配置到数据库
  198. func InsertSiteMonitorConfig(config SitesMonitorConfig) error {
  199. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  200. defer cancel()
  201. collection := database.GetCollection("SiteMonitorConfig")
  202. // _, err := collection.InsertOne(ctx, config)
  203. filter := bson.M{"name": config.Name} // 按用户名匹配
  204. update := bson.M{
  205. "$set": config, // 如果存在就更新所有字段
  206. }
  207. opts := options.Update().SetUpsert(true)
  208. _, err := collection.UpdateOne(ctx, filter, update, opts)
  209. if err != nil {
  210. return err
  211. }
  212. return nil
  213. }
  214. // 更新一条站点监控配置到数据库
  215. func UpdateSiteMonitorConfig(name string, config SitesMonitorConfig) error {
  216. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  217. defer cancel()
  218. collection := database.GetCollection("SiteMonitorConfig")
  219. // _, err := collection.InsertOne(ctx, config)
  220. filter := bson.M{"name": name} // 按用户名匹配
  221. update := bson.M{
  222. "$set": config, // 如果存在就更新所有字段
  223. }
  224. opts := options.Update().SetUpsert(true)
  225. _, err := collection.UpdateOne(ctx, filter, update, opts)
  226. if err != nil {
  227. return err
  228. }
  229. return nil
  230. }
  231. // 删除一条站点监控配置
  232. func DeleteSiteMonitorConfig(name string) error {
  233. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  234. defer cancel()
  235. filter := bson.M{"name": name} // 删除条件
  236. collection := database.GetCollection("SiteMonitorConfig")
  237. _, err := collection.DeleteOne(ctx, filter)
  238. if err != nil {
  239. return err
  240. }
  241. return nil
  242. }
  243. // 更新一条站点监控页面配置到数据库
  244. func UpdateSiteMonitorPageConfig(name string, config MonitorPageConfig) error {
  245. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  246. defer cancel()
  247. collection := database.GetCollection("SiteMonitorConfig")
  248. // _, err := collection.InsertOne(ctx, config)
  249. filter := bson.M{"name": name} // 按用户名匹配
  250. update := bson.M{
  251. "$set": bson.M{
  252. "pageConfig": config, // 更新页面配置字段
  253. },
  254. }
  255. opts := options.Update().SetUpsert(true)
  256. _, err := collection.UpdateOne(ctx, filter, update, opts)
  257. if err != nil {
  258. return err
  259. }
  260. return nil
  261. }
  262. // 从fwh_pro获取站点信息表
  263. func QuerySiteInfoList(para SiteInfoPara) (SiteInfoResponse, error) {
  264. resByte, err := fwh_pro.QuerySiteInfoList(para.SiteList, para.PageSize, para.PageNum)
  265. if err != nil {
  266. return SiteInfoResponse{}, err
  267. }
  268. var siteInfoRes SiteInfoResponse
  269. // 反序列化(unmarshal)
  270. jsonErr := json.Unmarshal(resByte, &siteInfoRes)
  271. if jsonErr != nil {
  272. return SiteInfoResponse{}, jsonErr
  273. }
  274. if siteInfoRes.Code == 401 {
  275. // TODO 尝试重新登录
  276. return SiteInfoResponse{}, errors.New("certification expired")
  277. }
  278. return siteInfoRes, nil
  279. }
  280. // 从fwh_pro获取站点工作模式信息
  281. func QuerySiteModeInfo(site string) (SiteModeInfoResponse, error) {
  282. resByte, err := fwh_pro.QuerySiteModeInfo(site)
  283. if err != nil {
  284. return SiteModeInfoResponse{}, err
  285. }
  286. var siteModeInfoRes SiteModeInfoResponse
  287. // 反序列化(unmarshal)
  288. jsonErr := json.Unmarshal(resByte, &siteModeInfoRes)
  289. if jsonErr != nil {
  290. return SiteModeInfoResponse{}, jsonErr
  291. }
  292. if siteModeInfoRes.Code == 401 {
  293. // TODO 尝试重新登录
  294. return SiteModeInfoResponse{}, errors.New("certification expired")
  295. }
  296. return siteModeInfoRes, nil
  297. }
  298. // 从fwh_pro获取站点功率流
  299. func QuerySitePowerFlow(site string, day time.Time) (SitePowerFlowResponse, error) {
  300. resByte, err := fwh_pro.QuerySitePowerFlow(site, day)
  301. if err != nil {
  302. return SitePowerFlowResponse{}, err
  303. }
  304. var sitePowerFlowRes SitePowerFlowResponse
  305. // 反序列化(unmarshal)
  306. jsonErr := json.Unmarshal(resByte, &sitePowerFlowRes)
  307. if jsonErr != nil {
  308. return SitePowerFlowResponse{}, jsonErr
  309. }
  310. if sitePowerFlowRes.Code == 401 {
  311. // TODO 尝试重新登录
  312. return SitePowerFlowResponse{}, errors.New("certification expired")
  313. }
  314. return sitePowerFlowRes, nil
  315. }
  316. // 从fwh_pro获取站点告警列表
  317. func QuerySiteAlarmList(site string) (SiteAlarmListResponse, error) {
  318. resByte, err := fwh_pro.QuerySiteAlarmList(site)
  319. if err != nil {
  320. return SiteAlarmListResponse{}, err
  321. }
  322. var siteAlarmListRes SiteAlarmListResponse
  323. // 反序列化(unmarshal)
  324. jsonErr := json.Unmarshal(resByte, &siteAlarmListRes)
  325. if jsonErr != nil {
  326. return SiteAlarmListResponse{}, jsonErr
  327. }
  328. if siteAlarmListRes.Code == 401 {
  329. // TODO 尝试重新登录
  330. return SiteAlarmListResponse{}, errors.New("certification expired")
  331. }
  332. return siteAlarmListRes, nil
  333. }