huangyan vor 3 Wochen
Ursprung
Commit
e89d75e8c8

BIN
cmd/server/city_chips


+ 0 - 1
cmd/server/main.go

@@ -16,7 +16,6 @@ func main() {
 
 	logger.Info("server start", zap.String("host", "http://127.0.0.1:"+conf.GetString("http.port")))
 	// err := helper.ModBus(conf, logger)
-
 	app, cleanup, err := wire.NewWire(conf, logger)
 	if err != nil {
 		panic(err)

+ 1 - 1
cmd/server/wire/wire.go

@@ -9,6 +9,7 @@ import (
 	"city_chips/internal/server"
 	"city_chips/internal/service"
 	"city_chips/pkg/log"
+
 	"github.com/gin-gonic/gin"
 	"github.com/google/wire"
 	"github.com/spf13/viper"
@@ -18,7 +19,6 @@ var ServerSet = wire.NewSet(server.NewServerHTTP)
 
 var RepositorySet = wire.NewSet(
 	repository.NewDb,
-	repository.NewRedis,
 	repository.NewRepository,
 	repository.NewUserRepository,
 	repository.NewAccessControlRepository,

+ 2 - 3
cmd/server/wire/wire_gen.go

@@ -23,8 +23,7 @@ func NewWire(viperViper *viper.Viper, logger *log.Logger) (*gin.Engine, func(),
 	handlerHandler := handler.NewHandler(logger)
 	serviceService := service.NewService(logger)
 	db := repository.NewDb(viperViper, logger)
-	client := repository.NewRedis(viperViper)
-	repositoryRepository := repository.NewRepository(logger, db, client)
+	repositoryRepository := repository.NewRepository(logger, db)
 	accessControlRepository := repository.NewAccessControlRepository(repositoryRepository)
 	accessControlService := service.NewAccessControlService(serviceService, accessControlRepository)
 	accessControlHandler := handler.NewAccessControlHandler(handlerHandler, accessControlService, viperViper)
@@ -67,7 +66,7 @@ func NewWire(viperViper *viper.Viper, logger *log.Logger) (*gin.Engine, func(),
 
 var ServerSet = wire.NewSet(server.NewServerHTTP)
 
-var RepositorySet = wire.NewSet(repository.NewDb, repository.NewRedis, repository.NewRepository, repository.NewUserRepository, repository.NewAccessControlRepository, repository.NewHikvisionRepository, repository.NewConferenceRepository, repository.NewHomeRepository, repository.NewElevatorRepository, repository.NewBroadcastRepository, repository.NewPropertyRepository, repository.NewInformationRepository, repository.NewIlluminatingRepository, repository.NewEnergyRepository, repository.NewIntelligentBuildingControlRepository)
+var RepositorySet = wire.NewSet(repository.NewDb, repository.NewRepository, repository.NewUserRepository, repository.NewAccessControlRepository, repository.NewHikvisionRepository, repository.NewConferenceRepository, repository.NewHomeRepository, repository.NewElevatorRepository, repository.NewBroadcastRepository, repository.NewPropertyRepository, repository.NewInformationRepository, repository.NewIlluminatingRepository, repository.NewEnergyRepository, repository.NewIntelligentBuildingControlRepository)
 
 var ServiceSet = wire.NewSet(service.NewService, service.NewUserService, service.NewAccessControlService, service.NewHikvisionService, service.NewConferenceService, service.NewHomeService, service.NewElevatorService, service.NewBroadcastService, service.NewPropertyService, service.NewInformationService, service.NewIlluminatingService, service.NewEnergyService, service.NewIntelligentBuildingControlService)
 

+ 3 - 1
config/local.yml

@@ -9,7 +9,7 @@ security:
     key: 1234
 data:
   mysql:
-    user: root:123456@tcp(127.0.0.1:3306)/user?charset=utf8mb4&parseTime=True&loc=Local
+    user: root:bzd@123#321!@tcp(182.43.195.17:3306)/park_a?charset=utf8mb4&parseTime=True&loc=Local
   redis:
     addr: 127.0.0.1:6379
     password: ""
@@ -76,6 +76,8 @@ bacnet:
   port: 47808
 Modbus:
   url: "tcp://"
+#楼控系统
 obix:
+  baseUrl: "https://10.1.201.253/obix/config"
   username: "obix"
   password: "Obix123456"

+ 2 - 1
internal/handler/hikvision.go

@@ -14,6 +14,7 @@ import (
 
 	"github.com/gin-gonic/gin"
 	"github.com/spf13/viper"
+	"go.uber.org/zap"
 )
 
 type HikvisionHandler struct {
@@ -596,7 +597,7 @@ func (h *HikvisionHandler) RealTimeInspection(ctx *gin.Context) {
 	var eventLogs model.EventLogs
 	hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.eventLogs"), m, 15)
 	if err != nil {
-		h.logger.Error("入侵报警事件日志查询失败")
+		h.logger.Error("入侵报警事件日志查询失败", zap.Error(err))
 		resp.HandleError(ctx, 1203, "入侵报警事件日志查询失败", err)
 		return
 	}

+ 62 - 2
internal/handler/illuminating.go

@@ -4,11 +4,14 @@ import (
 	"city_chips/internal/model"
 	"city_chips/internal/service"
 	"city_chips/pkg/helper/resp"
+	"encoding/json"
 	"fmt"
-	"github.com/gin-gonic/gin"
-	"github.com/spf13/viper"
 	"math/rand"
+	"strconv"
 	"time"
+
+	"github.com/gin-gonic/gin"
+	"github.com/spf13/viper"
 )
 
 type IlluminatingHandler struct {
@@ -29,6 +32,7 @@ func NewIlluminatingHandler(
 	}
 }
 
+// 照明系统
 func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
 	m := make(map[string]any)
 	runAnaly := make(map[string]any)
@@ -67,3 +71,59 @@ func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
 	m["EventList"] = eventList                   //事件列表
 	resp.HandleSuccess(ctx, m)
 }
+
+// 获取灯光状态
+func (h *IlluminatingHandler) GetLightingstatus(ctx *gin.Context) {
+	// 设置响应头
+	ctx.Header("Content-Type", "text/event-stream")
+	ctx.Header("Cache-Control", "no-cache")
+	ctx.Header("Connection", "keep-alive")
+	// 监听客户端断开连接
+	conn := true
+	notify := ctx.Writer.CloseNotify()
+	type Response struct {
+		RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
+		Code      int32  `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
+		Msg       string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
+		Data      any    `json:"data"`
+	}
+	stationNo := ctx.Query("id")
+	if len(stationNo) == 0 {
+		resp.HandleError(ctx, 1201, "缺少必要参数", nil)
+		return
+	}
+	var response Response
+	for conn {
+		select {
+		case <-notify:
+			conn = false
+			fmt.Println("断开连接")
+			return
+		default:
+			// 模拟数据
+			data := make(map[string]any)
+			response.Code = 200
+			response.RequestId = strconv.Itoa(rand.Intn(1000))
+			response.Msg = "success"
+			data["name"] = "设备一"
+			data["state"] = rand.Intn(2) // 1开  0关  2故障
+			response.Data = data
+			res, _ := json.Marshal(&response)
+			fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			ctx.Writer.Flush()
+			time.Sleep(10 * time.Second)
+		}
+	}
+}
+
+// 修改灯光状态
+func (h *IlluminatingHandler) UpdataLightingStatus(ctx *gin.Context) {
+	id := ctx.PostForm("id")
+	state := ctx.PostForm("state")
+
+	if len(id) == 0 || len(state) == 0 {
+		resp.HandleError(ctx, 1201, "缺少必要参数", nil)
+		return
+	}
+	resp.HandleSuccess(ctx, id)
+}

+ 51 - 2
internal/handler/intelligentbuildingcontrol.go

@@ -4,10 +4,11 @@ import (
 	"city_chips/internal/model"
 	"city_chips/internal/service"
 	"city_chips/pkg/helper/resp"
-	"github.com/spf13/viper"
 	"math/rand"
 	"time"
 
+	"github.com/spf13/viper"
+
 	"github.com/gin-gonic/gin"
 )
 
@@ -29,6 +30,8 @@ func NewIntelligentBuildingControlHandler(
 	}
 }
 
+// 智能楼宇控制系统
+
 // 示例:创建一个报警记录
 func generateAlarm() model.AlarmList {
 	return model.AlarmList{
@@ -52,6 +55,52 @@ func (h *IntelligentBuildingControlHandler) GetIntelligentBuildingControl(ctx *g
 	m["RunState"] = rand.Intn(100)    //运行状态
 	m["FaultState"] = rand.Intn(1000) //故障状态
 	m["DeviceList"] = device          //设备列表
-
 	resp.HandleSuccess(ctx, m)
 }
+
+// 获取点位数据
+func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
+	pointName := ctx.PostForm("pointName")
+	deviceType := ctx.PostForm("deviceType")
+	building := ctx.PostForm("building")
+	floor := ctx.PostForm("floor")
+	section := ctx.PostForm("section")
+	device_name := ctx.PostForm("deviceName")
+	conds := make(map[string]any)
+	if pointName != "" {
+		conds["point_name"] = pointName
+	}
+	if deviceType != "" {
+		conds["device_type"] = deviceType
+	}
+	if building != "" {
+		conds["building"] = building
+	}
+	if floor != "" {
+		conds["floor"] = floor
+	}
+	if section != "" {
+		conds["section"] = section
+	}
+	if device_name != "" {
+		conds["device_name"] = device_name
+	}
+	baseUrl := h.conf.GetString("obix.baseUrl")
+	println("obix baseUrl:", baseUrl)
+	points, err := h.intelligentBuildingControlService.GetPoint(conds)
+	if err != nil {
+		resp.HandleError(ctx, 1201, "查询点位失败", nil)
+		return
+	}
+	resp.HandleSuccess(ctx, points)
+}
+
+// 获取点位类型
+func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
+	points, err := h.intelligentBuildingControlService.GetPointType()
+	if err != nil {
+		resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
+		return
+	}
+	resp.HandleSuccess(ctx, points)
+}

+ 16 - 0
internal/model/intelligentbuildingcontrol.go

@@ -9,3 +9,19 @@ type IntelligentBuildingControl struct {
 func (m *IntelligentBuildingControl) TableName() string {
 	return "intelligent_building_control"
 }
+
+// 点位表
+type Point struct {
+	Id         int64  `json:"id" gorm:"column:id;primaryKey;autoIncrement;comment:点位ID"`
+	FullPath   string `json:"full_path" gorm:"column:full_path;type:varchar(255);comment:点位全路径"`
+	DeviceType string `json:"device_type" gorm:"column:device_type;type:varchar(50);comment:设备类型"`
+	BuildingId string `json:"building_id" gorm:"column:building_id;type:varchar(50);comment:所属楼宇ID"`
+	Floor      string `json:"floor" gorm:"column:floor;type:varchar(50);comment:楼层"`
+	Section    string `json:"section" gorm:"column:section;type:varchar(50);comment:区域"`
+	DeviceName string `json:"device_name" gorm:"column:device_name;type:varchar(100);comment:设备名称"`
+	PointName  string `json:"point_name" gorm:"column:point_name;type:varchar(100);comment:点位名称"`
+}
+
+func (p *Point) TableName() string {
+	return "point"
+}

+ 24 - 0
internal/repository/intelligentbuildingcontrol.go

@@ -2,11 +2,14 @@ package repository
 
 import (
 	"city_chips/internal/model"
+	helper "city_chips/pkg/helper/dbutils"
 	"context"
 )
 
 type IntelligentBuildingControlRepository interface {
 	GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error)
+	GetPoint(conds map[string]any) (*[]model.Point, error)
+	GetPointType() ([]string, error)
 }
 
 func NewIntelligentBuildingControlRepository(
@@ -21,8 +24,29 @@ type intelligentBuildingControlRepository struct {
 	*Repository
 }
 
+// GetPointType implements IntelligentBuildingControlRepository.
+func (r *intelligentBuildingControlRepository) GetPointType() ([]string, error) {
+	var names []string
+	err := r.db.Model(&model.Point{}).Select("point_name").Group("point_name").Find(&names).Error
+	if err != nil {
+		return nil, err
+	}
+	return names, nil
+}
+
 func (r *intelligentBuildingControlRepository) GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error) {
 	var intelligentBuildingControl model.IntelligentBuildingControl
+	// 模拟从数据库获取数据
 
 	return &intelligentBuildingControl, nil
 }
+
+// 根据点位数据获取点位信息
+func (r *intelligentBuildingControlRepository) GetPoint(conds map[string]any) (*[]model.Point, error) {
+	var points []model.Point
+	points, err := helper.QueryByConditions[model.Point](r.db, conds)
+	if err != nil {
+		return &points, err
+	}
+	return &points, nil
+}

+ 23 - 24
internal/repository/repository.go

@@ -2,27 +2,25 @@ package repository
 
 import (
 	"city_chips/pkg/log"
-	"context"
-	"fmt"
-	"github.com/redis/go-redis/v9"
+	"runtime"
+	"time"
+
 	"github.com/spf13/viper"
 	"gorm.io/driver/mysql"
 	"gorm.io/gorm"
 	"gorm.io/gorm/logger"
-	"runtime"
-	"time"
 )
 
 type Repository struct {
-	db     *gorm.DB
-	rdb    *redis.Client
+	db *gorm.DB
+	// rdb    *redis.Client
 	logger *log.Logger
 }
 
-func NewRepository(logger *log.Logger, db *gorm.DB, rdb *redis.Client) *Repository {
+func NewRepository(logger *log.Logger, db *gorm.DB) *Repository {
 	return &Repository{
-		db:     db,
-		rdb:    rdb,
+		db: db,
+		// rdb:    rdb,
 		logger: logger,
 	}
 }
@@ -51,20 +49,21 @@ func NewDb(conf *viper.Viper, l *log.Logger) *gorm.DB {
 	}
 	return db
 }
-func NewRedis(conf *viper.Viper) *redis.Client {
-	rdb := redis.NewClient(&redis.Options{
-		Addr:     conf.GetString("data.redis.addr"),
-		Password: conf.GetString("data.redis.password"),
-		DB:       conf.GetInt("data.redis.db"),
-	})
 
-	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
-	defer cancel()
+// func NewRedis(conf *viper.Viper) *redis.Client {
+// 	rdb := redis.NewClient(&redis.Options{
+// 		Addr:     conf.GetString("data.redis.addr"),
+// 		Password: conf.GetString("data.redis.password"),
+// 		DB:       conf.GetInt("data.redis.db"),
+// 	})
 
-	_, err := rdb.Ping(ctx).Result()
-	if err != nil {
-		panic(fmt.Sprintf("redis error: %s", err.Error()))
-	}
+// 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+// 	defer cancel()
 
-	return rdb
-}
+// 	_, err := rdb.Ping(ctx).Result()
+// 	if err != nil {
+// 		panic(fmt.Sprintf("redis error: %s", err.Error()))
+// 	}
+
+// 	return rdb
+// }

+ 4 - 0
internal/server/http.go

@@ -92,6 +92,8 @@ func NewServerHTTP(
 	ill := r.Group("/illuminating")
 	{
 		ill.GET("/count", illuminating.GetIlluminating)
+		ill.GET("/getLightingstatus", illuminating.GetLightingstatus)
+		ill.POST("/updateLightingStatus", illuminating.UpdataLightingStatus)
 	}
 	//能源系统
 	ener := r.Group("/energy")
@@ -103,6 +105,8 @@ func NewServerHTTP(
 	inte := r.Group("/intell")
 	{
 		inte.GET("/count", intell.GetIntelligentBuildingControl)
+		inte.POST("/point", intell.GetPoint)
+		inte.GET("/pointType", intell.GetPointType)
 	}
 	return r
 }

+ 20 - 0
internal/service/intelligentbuildingcontrol.go

@@ -8,6 +8,8 @@ import (
 
 type IntelligentBuildingControlService interface {
 	GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error)
+	GetPoint(conds map[string]any) (*[]model.Point, error)
+	GetPointType() ([]string, error)
 }
 
 func NewIntelligentBuildingControlService(
@@ -25,6 +27,24 @@ type intelligentBuildingControlService struct {
 	intelligentBuildingControlRepository repository.IntelligentBuildingControlRepository
 }
 
+// GetPointType implements IntelligentBuildingControlService.
+func (s *intelligentBuildingControlService) GetPointType() ([]string, error) {
+	poions, err := s.intelligentBuildingControlRepository.GetPointType()
+	if err != nil {
+		return nil, err
+	}
+	return poions, nil
+}
+
+// GetPoint implements IntelligentBuildingControlService.
+func (s *intelligentBuildingControlService) GetPoint(conds map[string]any) (*[]model.Point, error) {
+	points, err := s.intelligentBuildingControlRepository.GetPoint(conds)
+	if err != nil {
+		return &[]model.Point{}, err
+	}
+	return points, err
+}
+
 func (s *intelligentBuildingControlService) GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error) {
 	return s.intelligentBuildingControlRepository.GetIntelligentBuildingControl(ctx, id)
 }

+ 10 - 0
pkg/helper/dbutils/dbutils.go

@@ -0,0 +1,10 @@
+package helper
+
+import "gorm.io/gorm"
+
+//通用多条查询
+func QueryByConditions[T any](db *gorm.DB, conditions map[string]interface{}) ([]T, error) {
+	var results []T
+	tx := db.Where(conditions).Find(&results)
+	return results, tx.Error
+}