package service import ( "fmt" "iot_manager_service/app/dao" "iot_manager_service/app/model" "iot_manager_service/app/utils" "strconv" "strings" "time" ) // 中间件管理服务 var LightControlService = new(lightControlService) type lightControlService struct{} func (s *lightControlService) Get(id int) (*dao.LightControl, *utils.Errors) { // 创建查询实例 device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } //todo runstate 需要使用各个设备的状态来处理 return device, nil } func (s *lightControlService) GetRelevanceDetail(id int) (*model.LightControlDetail, *utils.Errors) { // 创建查询实例 device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } //todo get gateway ipBroadcast lightcontroller... list return &model.LightControlDetail{ LightControl: *device, AlarmTerminalList: nil, CameraList: nil, CaptureUnitList: nil, GatewayList: nil, InfoBoardList: nil, IpBroadcastList: nil, LightControlList: nil, SensorList: nil, ZigbeeList: nil, }, nil } func (s *lightControlService) CreateOrUpdate(req *dao.LightControl) *utils.Errors { device := req if device.TenantId == "" { device.TenantId = "000000" // todo: 使用登录态 } device.UpdateUser = "TODO" // todo: 使用登录态 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 device.GroupId = lampPole.GroupId } else { fmt.Printf("LampPoleService.GetOne err = %v \n", err) } } if device.ControlType == model.ControlType_ZigBee { if device.ZigbeeId == 0 { return utils.ParamsInvalidResponse(model.ZigbeeSelect, nil) } if device.IsOnDemand == 0 { if device.ControlNO == "" { return utils.ParamsInvalidResponse(model.ControlNONull, nil) } if !checkControlNoIsCompliance(device.ControlNO) { return utils.ParamsInvalidResponse(model.ControlNOInvalid, nil) } } //todo zigbee // Zigbee zigbee = zigbeeService.getById(lightControl.getZigbeeId()); // lightControl.setZigbeeName(zigbee.getName()); // lightControl.setZigbeeSn(zigbee.getSn()); // lightControl.setChanelNum(zigbee.getChanelNum()); // lightControl.setNetworkNum(zigbee.getNetworkNum()); } if device.ID == 0 { device.CreateTime = time.Now() device.CreateUser = "TODO" // todo: 使用登录态 if device.IsExistedBySN() { fmt.Printf("Create IsExistedBySN \n") return utils.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 utils.FailResponse(err.Error(), nil) } return utils.SuccessResponse(utils.Succeeded, nil) } if err := device.Update(); err != nil { fmt.Printf("Update err = %s \n", err.Error()) return utils.FailResponse(err.Error(), nil) } //todo operation record return utils.SuccessResponse(utils.Succeeded, nil) } func (s *lightControlService) List(searchValue, controlType, zigbeeId string, current, size int) ([]dao.LightControl, *utils.Errors) { device := dao.LightControl{} if searchValue != "" { device.SN = searchValue } if controlType != "" { cType, err := strconv.Atoi(controlType) if err == nil { device.ControlType = cType } } if zigbeeId != "" { zId, err := strconv.Atoi(zigbeeId) if err == nil { device.ZigbeeId = zId } } offset := (current - 1) * size limit := size devices, err := device.GetDevices(offset, limit) if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return devices, nil } func (s *lightControlService) Remove(id int) *utils.Errors { // 创建查询实例 device := &dao.LightControl{ ID: id, IsDeleted: 1, UpdateUser: "TODO", // todo 使用登录态 UpdateTime: time.Now(), } //todo // service.lampPoleService.CountRelation() //todo operation record err := device.Delete() if err != nil { return utils.FailResponse(err.Error(), nil) } return nil } func (s *lightControlService) GetList() ([]*dao.LightControl, *utils.Errors) { device := &dao.LightControl{ TenantId: "000000", // todo 使用登录态 IsDeleted: 0, } devices, err := device.GetAllDevices() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return devices, nil } func (s *lightControlService) Enable(id, status int) *utils.Errors { // 创建查询实例 device := &dao.LightControl{ ID: id, IsEnable: status, } err := device.UpdateEnable() if err != nil { return utils.FailResponse(err.Error(), nil) } return nil } func (s *lightControlService) GetOne(id int) (*dao.LightControl, *utils.Errors) { device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return device, nil } //检查灯控编号是否合规1-1——255-255 func checkControlNoIsCompliance(controlNo string) bool { arr := strings.Split(controlNo, "-") if len(arr) != 2 { return false } one, err := strconv.Atoi(arr[0]) if err != nil || one < 1 || one > 255 { return false } two, err := strconv.Atoi(arr[1]) if err != nil || two < 1 || two > 255 { return false } return true }