sitesMonitorControllers.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 SiteBaseInfoPara struct {
  50. SiteList []string `json:"siteList"`
  51. PageSize uint `json:"pageSize"`
  52. PageNum uint `json:"pageNum"`
  53. }
  54. type SiteBaseInfo 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 SiteBaseInfoResponse struct {
  69. Code int `json:"code"`
  70. Message string `json:"message"`
  71. Result []SiteBaseInfo `json:"result"`
  72. Success bool `json:"success"`
  73. Total uint `json:"total"`
  74. }
  75. type SiteInfoPara struct {
  76. SN *string `json:"sn"`
  77. PageSize uint `json:"pageSize"`
  78. PageNum uint `json:"pageNum"`
  79. }
  80. type SiteInfo struct {
  81. Id string `json:"id"`
  82. Name *string `json:"name"`
  83. FhpSn string `json:"fhpSn"` // aPower SN 多设备用,分割
  84. ActiveStatus int `json:"activeStatus"` // 激活状态 0-未激活 1-激活
  85. ActiveTime string `json:"activeTime"` // 激活时间(北京时间)
  86. LocalActiveTime string `json:"localActiveTime"` // 激活时间(站点当地时间)
  87. Country string `json:"country"` // 国家
  88. Province string `json:"province"` // 州/省
  89. City string `json:"city"` // 城市
  90. RealSysHdVersion string `json:"realSysHdVersion"` // 系统版本信息
  91. Version string `json:"version"` // 设备固件版本
  92. InstallerName string `json:"installerName"` // 安装商名称
  93. Longitude string `json:"longitude"` // 经度
  94. Latitude string `json:"latitude"` // 纬度
  95. ICCID string `json:"iccId"`
  96. IMEI string `json:"imei"`
  97. FirmUpgradeTime string `json:"firmUpgradeTime"` // 固件最后更新时间 YYYY-MM-DD HH:mm:SS
  98. CreateTime string `json:"createTime"` // 设备首次登录时间 YYYY-MM-DD HH:mm:SS
  99. TimeZone float32 `json:"timeZone"` // 时区数值
  100. ZoneInfo string `json:"zoneInfo"` // 时区信息 America/Los_Angeles
  101. GenEn int `json:"genEn"` // 发电机是否使能 0-不使能 1-使能
  102. Remark *string `json:"remark"` // 备注信息
  103. }
  104. type SiteInfoResponse struct {
  105. Code int `json:"code"`
  106. Message string `json:"message"`
  107. Result []SiteInfo `json:"result"`
  108. Success bool `json:"success"`
  109. Total uint `json:"total"`
  110. }
  111. type WorkModeInfo struct {
  112. Id uint `json:"id"`
  113. OldIndex uint `json:"oldIndex"`
  114. Name string `json:"name"`
  115. Soc float32 `json:"soc"`
  116. MaxSoc float32 `json:"maxSoc"`
  117. MinSoc float32 `json:"minSoc"`
  118. DischargeDepthSoc *float32 `json:"dischargeDepthSoc"`
  119. WorkMode uint `json:"workMode"`
  120. EnergyIncentivesType uint `json:"energyIncentivesType"`
  121. ElectricityType uint `json:"electricityType"`
  122. }
  123. type SiteModeInfo struct {
  124. CurrendId uint `json:"currendId"`
  125. List []WorkModeInfo `json:"list"`
  126. }
  127. type SiteModeInfoResponse struct {
  128. Code int `json:"code"`
  129. Msg string `json:"msg"`
  130. Result SiteModeInfo `json:"result"`
  131. Success bool `json:"success"`
  132. }
  133. type SitePowerFlow struct {
  134. Index uint `json:"index"` // 非当天为0,当天为实时数量
  135. DeviceTimeArray []string `json:"deviceTimeArray"` // 设备本地时间,格式 “2025-08-18 00:00:00”
  136. PowerFhpArray []float32 `json:"powerFhpArray"`
  137. PowerGenArray []float32 `json:"powerGenArray"`
  138. PowerLoadArray []float32 `json:"powerLoadArray"`
  139. PowerSunArray []float32 `json:"powerSunArray"`
  140. PowerUtiArray []float32 `json:"powerUtiArray"`
  141. SocArray []float32 `json:"socArray"`
  142. RunStatusArray []uint `json:"runStatusArray"` // 运行状态,0:待机;1:充电;2:放电;3:故障
  143. KwhTotalArray []float32 `json:"kwhTotalArray"` // 总可用电量,应该是根据SOC推算出来的。
  144. }
  145. type SitePowerFlowResponse struct {
  146. Code int `json:"code"`
  147. Msg string `json:"msg"`
  148. Result SitePowerFlow `json:"result"`
  149. }
  150. type SiteAlarmList struct {
  151. Id uint `json:"id"`
  152. GatewayId string `json:"gatewayId"`
  153. AlarmEqSn string `json:"alarmEqSn"`
  154. AlarmCode string `json:"alarmCode"`
  155. CommonAlarmCode string `json:"commonAlarmCode"`
  156. Level int `json:"level"`
  157. Time string `json:"time"`
  158. LogName string `json:"logName"`
  159. CommonLogName string `json:"commonLogName"`
  160. LogNameEn string `json:"logNameEn"`
  161. CommonLogNameEn string `json:"commonLogNameEn"`
  162. }
  163. type SiteAlarmListResponse struct {
  164. Code int `json:"code"`
  165. Message string `json:"message"`
  166. Result []SiteAlarmList `json:"result"`
  167. Success bool `json:"success"`
  168. Total uint `json:"total"`
  169. }
  170. // 从数据库获取站点监控配置列表
  171. func GetSiteMonitorConfig() ([]SitesMonitorConfig, error) {
  172. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  173. defer cancel()
  174. collection := database.GetCollection("SiteMonitorConfig")
  175. cursor, err := collection.Find(ctx, bson.M{}) // 空条件,查全部
  176. if err != nil {
  177. return nil, err
  178. }
  179. defer cursor.Close(ctx)
  180. var configList []SitesMonitorConfig
  181. for cursor.Next(ctx) {
  182. var cfg SitesMonitorConfig
  183. if err := cursor.Decode(&cfg); err != nil {
  184. return nil, err
  185. }
  186. configList = append(configList, cfg)
  187. }
  188. return configList, nil
  189. }
  190. // 从数据库获取监控名列表
  191. func GetSiteMonitorNameList() ([]string, error) {
  192. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  193. defer cancel()
  194. collection := database.GetCollection("SiteMonitorConfig")
  195. cursor, err := collection.Find(
  196. ctx,
  197. bson.M{}, // 空条件,查全部
  198. options.Find().SetProjection(bson.M{
  199. "name": 1, // 只要 name 字段
  200. "_id": 0, // 不要 _id 字段(默认会返回)
  201. }),
  202. )
  203. if err != nil {
  204. return nil, err
  205. }
  206. defer cursor.Close(ctx)
  207. var nameList []string
  208. for cursor.Next(ctx) {
  209. var result struct {
  210. Name string `bson:"name"`
  211. }
  212. if err := cursor.Decode(&result); err != nil {
  213. return nil, err
  214. }
  215. nameList = append(nameList, result.Name)
  216. }
  217. return nameList, nil
  218. }
  219. // 从数据库获取一条站点监控配置
  220. func GetOneSiteMonitorConfig(name string) ([]SitesMonitorConfig, error) {
  221. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  222. defer cancel()
  223. collection := database.GetCollection("SiteMonitorConfig")
  224. var cfg SitesMonitorConfig
  225. err := collection.FindOne(ctx, bson.M{"name": name}).Decode(&cfg)
  226. if err == mongo.ErrNoDocuments {
  227. return []SitesMonitorConfig{}, nil
  228. } else if err != nil {
  229. return nil, err
  230. }
  231. var configList []SitesMonitorConfig
  232. configList = append(configList, cfg)
  233. return configList, nil
  234. }
  235. // 插入一条站点监控配置到数据库
  236. func InsertSiteMonitorConfig(config SitesMonitorConfig) error {
  237. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  238. defer cancel()
  239. collection := database.GetCollection("SiteMonitorConfig")
  240. // _, err := collection.InsertOne(ctx, config)
  241. filter := bson.M{"name": config.Name} // 按用户名匹配
  242. update := bson.M{
  243. "$set": config, // 如果存在就更新所有字段
  244. }
  245. opts := options.Update().SetUpsert(true)
  246. _, err := collection.UpdateOne(ctx, filter, update, opts)
  247. if err != nil {
  248. return err
  249. }
  250. return nil
  251. }
  252. // 更新一条站点监控配置到数据库
  253. func UpdateSiteMonitorConfig(name string, config SitesMonitorConfig) error {
  254. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  255. defer cancel()
  256. collection := database.GetCollection("SiteMonitorConfig")
  257. // _, err := collection.InsertOne(ctx, config)
  258. filter := bson.M{"name": name} // 按用户名匹配
  259. update := bson.M{
  260. "$set": config, // 如果存在就更新所有字段
  261. }
  262. opts := options.Update().SetUpsert(true)
  263. _, err := collection.UpdateOne(ctx, filter, update, opts)
  264. if err != nil {
  265. return err
  266. }
  267. return nil
  268. }
  269. // 删除一条站点监控配置
  270. func DeleteSiteMonitorConfig(name string) error {
  271. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  272. defer cancel()
  273. filter := bson.M{"name": name} // 删除条件
  274. collection := database.GetCollection("SiteMonitorConfig")
  275. _, err := collection.DeleteOne(ctx, filter)
  276. if err != nil {
  277. return err
  278. }
  279. return nil
  280. }
  281. // 更新一条站点监控页面配置到数据库
  282. func UpdateSiteMonitorPageConfig(name string, config MonitorPageConfig) error {
  283. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  284. defer cancel()
  285. collection := database.GetCollection("SiteMonitorConfig")
  286. // _, err := collection.InsertOne(ctx, config)
  287. filter := bson.M{"name": name} // 按用户名匹配
  288. update := bson.M{
  289. "$set": bson.M{
  290. "pageConfig": config, // 更新页面配置字段
  291. },
  292. }
  293. opts := options.Update().SetUpsert(true)
  294. _, err := collection.UpdateOne(ctx, filter, update, opts)
  295. if err != nil {
  296. return err
  297. }
  298. return nil
  299. }
  300. // 从fwh_pro获取站点基本信息表
  301. func QuerySiteBaseInfoList(para SiteBaseInfoPara) (SiteBaseInfoResponse, error) {
  302. resByte, err := fwh_pro.QuerySiteBaseInfoList(para.SiteList, para.PageSize, para.PageNum)
  303. if err != nil {
  304. return SiteBaseInfoResponse{}, err
  305. }
  306. var siteInfoRes SiteBaseInfoResponse
  307. // 反序列化(unmarshal)
  308. jsonErr := json.Unmarshal(resByte, &siteInfoRes)
  309. if jsonErr != nil {
  310. return SiteBaseInfoResponse{}, jsonErr
  311. }
  312. if siteInfoRes.Code == 401 {
  313. // TODO 尝试重新登录
  314. return SiteBaseInfoResponse{}, errors.New("certification expired")
  315. }
  316. return siteInfoRes, nil
  317. }
  318. // 从fwh_pro获取站点基本信息表
  319. func QuerySiteInfoList(para SiteInfoPara) (SiteInfoResponse, error) {
  320. resByte, err := fwh_pro.QuerySiteInfoList(para.SN, para.PageSize, para.PageNum)
  321. if err != nil {
  322. return SiteInfoResponse{}, err
  323. }
  324. var siteInfoRes SiteInfoResponse
  325. // 反序列化(unmarshal)
  326. jsonErr := json.Unmarshal(resByte, &siteInfoRes)
  327. if jsonErr != nil {
  328. return SiteInfoResponse{}, jsonErr
  329. }
  330. if siteInfoRes.Code == 401 {
  331. // TODO 尝试重新登录
  332. return SiteInfoResponse{}, errors.New("certification expired")
  333. }
  334. return siteInfoRes, nil
  335. }
  336. // 从fwh_pro获取站点工作模式信息
  337. func QuerySiteModeInfo(site string) (SiteModeInfoResponse, error) {
  338. resByte, err := fwh_pro.QuerySiteModeInfo(site)
  339. if err != nil {
  340. return SiteModeInfoResponse{}, err
  341. }
  342. var siteModeInfoRes SiteModeInfoResponse
  343. // 反序列化(unmarshal)
  344. jsonErr := json.Unmarshal(resByte, &siteModeInfoRes)
  345. if jsonErr != nil {
  346. return SiteModeInfoResponse{}, jsonErr
  347. }
  348. if siteModeInfoRes.Code == 401 {
  349. // TODO 尝试重新登录
  350. return SiteModeInfoResponse{}, errors.New("certification expired")
  351. }
  352. return siteModeInfoRes, nil
  353. }
  354. // 从fwh_pro获取站点功率流
  355. func QuerySitePowerFlow(site string, day time.Time) (SitePowerFlowResponse, error) {
  356. resByte, err := fwh_pro.QuerySitePowerFlow(site, day)
  357. if err != nil {
  358. return SitePowerFlowResponse{}, err
  359. }
  360. var sitePowerFlowRes SitePowerFlowResponse
  361. // 反序列化(unmarshal)
  362. jsonErr := json.Unmarshal(resByte, &sitePowerFlowRes)
  363. if jsonErr != nil {
  364. return SitePowerFlowResponse{}, jsonErr
  365. }
  366. if sitePowerFlowRes.Code == 401 {
  367. // TODO 尝试重新登录
  368. return SitePowerFlowResponse{}, errors.New("certification expired")
  369. }
  370. return sitePowerFlowRes, nil
  371. }
  372. // 从fwh_pro获取站点告警列表
  373. func QuerySiteAlarmList(site string) (SiteAlarmListResponse, error) {
  374. resByte, err := fwh_pro.QuerySiteAlarmList(site)
  375. if err != nil {
  376. return SiteAlarmListResponse{}, err
  377. }
  378. var siteAlarmListRes SiteAlarmListResponse
  379. // 反序列化(unmarshal)
  380. jsonErr := json.Unmarshal(resByte, &siteAlarmListRes)
  381. if jsonErr != nil {
  382. return SiteAlarmListResponse{}, jsonErr
  383. }
  384. if siteAlarmListRes.Code == 401 {
  385. // TODO 尝试重新登录
  386. return SiteAlarmListResponse{}, errors.New("certification expired")
  387. }
  388. return siteAlarmListRes, nil
  389. }