123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/device/model"
- "iot_manager_service/util"
- "time"
- )
- // 中间件管理服务
- var GatewayService = new(gatewayService)
- type gatewayService struct{}
- func (s *gatewayService) Get(id int) (*model.GatewayDetail, *util.Errors) {
- // 创建查询实例
- device := &dao.Gateway{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- detail := &model.GatewayDetail{Gateway: *device}
- relation, e := GatewayRelationService.Get(id)
- if e == nil {
- detail.CountLampPole = relation.Total
- } else {
- fmt.Printf("GatewayRelationService.Get err = %v", err)
- }
- //todo 获取网关状态
- // vo.setEndLineTime(time);
- // vo.setNetworkState(online);
- // vo.setRunState(online);
- return detail, nil
- }
- func (s *gatewayService) CreateOrUpdate(userId int64, tenantId int, req *model.GatewayDetail) *util.Errors {
- device := req.Gateway
- device.TenantId = tenantId
- device.UpdateUser = userId
- device.UpdateTime = time.Now()
- if device.LampPoleId != 0 {
- lampPole, err := LampPoleService.GetOne(device.LampPoleId)
- if err == nil {
- device.LampLat = lampPole.PoleLat
- device.LampLng = lampPole.PoleLng
- device.LampPoleName = lampPole.PoleName
- device.LampPoleSn = lampPole.PoleSN
- device.LampPoleLocation = lampPole.InstallLocation
- } else {
- fmt.Printf("LampPoleService.GetOne err = %v \n", err)
- }
- }
- if device.ID == 0 {
- device.CreateTime = time.Now()
- device.CreateUser = userId
- if device.IsExistedBySN() {
- fmt.Printf("Create IsExistedBySN \n")
- return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
- }
- fmt.Printf("device = %+v \n", device)
- if err := device.Create(); err != nil {
- fmt.Printf("Create err = %s \n", err.Error())
- return util.FailResponse(err.Error(), nil)
- }
- return util.SuccessResponse(util.Succeeded, nil)
- }
- if err := device.Update(); err != nil {
- fmt.Printf("Update err = %s \n", err.Error())
- return util.FailResponse(err.Error(), nil)
- }
- //todo operation record
- return util.SuccessResponse(util.Succeeded, nil)
- }
- func (s *gatewayService) List(searchValue string, current, size int) ([]model.GatewayDetail, *util.Errors) {
- var details []model.GatewayDetail
- device := dao.Gateway{}
- if searchValue != "" {
- device.GatewaySN = searchValue
- }
- offset := (current - 1) * size
- limit := size
- devices, err := device.GetDevices(offset, limit)
- for _, d := range devices {
- detail := model.GatewayDetail{Gateway: d}
- relation, _ := GatewayRelationService.Get(d.ID)
- if relation != nil {
- detail.CountLampPole = relation.Total
- }
- details = append(details, detail)
- }
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return details, nil
- }
- func (s *gatewayService) Remove(userId int64, id int) *util.Errors {
- // 创建查询实例
- device := &dao.Gateway{
- ID: id,
- IsDeleted: 1,
- UpdateUser: userId,
- UpdateTime: time.Now(),
- }
- if relation, _ := GatewayRelationService.Get(id); relation != nil && relation.Total > 0 {
- return util.OperationInvalidResponse(model.GatewayHasRelation, nil)
- }
- //todo operation record
- err := device.Delete()
- if err != nil {
- return util.FailResponse(err.Error(), nil)
- }
- return nil
- }
- func (s *gatewayService) GetList(tenantId int) ([]*dao.Gateway, *util.Errors) {
- var devices []*dao.Gateway
- err := util.Redis.Get(getGatewayListRedisKey(tenantId)).Scan(&devices)
- if err == nil {
- return devices, nil
- }
- device := &dao.Gateway{
- TenantId: tenantId,
- IsDeleted: 0,
- }
- devices, err = device.GetAllDevices()
- for _, device := range devices {
- device.GatewayName = device.GatewayName + "(" + device.GatewaySN + ")"
- }
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- _ = util.Redis.Set(getGatewayListRedisKey(tenantId), devices, 0).Err()
- return devices, nil
- }
- func getGatewayListRedisKey(tenantId int) string {
- return fmt.Sprintf(model.GatewayList, tenantId)
- }
- func (s *gatewayService) GetRelevanceDetail(id int) (*model.GatewayDetail, *util.Errors) {
- // 创建查询实例
- device := &dao.Gateway{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- //todo get gateway ipBroadcast lightcontroller... list
- return &model.GatewayDetail{
- Gateway: *device,
- AlarmTerminalList: nil,
- CameraList: nil,
- CaptureUnitList: nil,
- InfoBoardList: nil,
- IpBroadcastList: nil,
- LightControlList: nil,
- OptoSensorList: nil,
- ZigbeeList: nil,
- }, nil
- }
- func (s *gatewayService) GetOne(id int) (*dao.Gateway, *util.Errors) {
- // 创建查询实例
- device := &dao.Gateway{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return device, nil
- }
|