// add home normal member func (this *MemberManager) AddMember(domain string, hid, uid int64, member string) error { common.CheckParam(this.store != nil) if len(member) <= 0 { log.Warningf("check member name failed:domain[%s], member[%s], uid[%d], hid[%d]", domain, member, uid, hid) return common.ErrInvalidName } // step 1. get home info homeManager := NewHomeManager(this.store) home, err := homeManager.Get(domain, hid) if err != nil { log.Warningf("get home failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return err } else if home == nil { log.Warningf("home not exist:domain[%s], hid[%d]", domain, hid) return common.ErrEntryNotExist } else if home.GetStatus() != ACTIVE { log.Warningf("check home status failed:domain[%s], hid[%d]", domain, hid) return common.ErrInvalidStatus } else if home.GetCreateUid() == uid { // do not return error log.Warningf("the user is creator:domain[%s], hid[%d], uid[%d]", domain, hid, home.createUid) return nil } // step 2. add member, if exist return error err = this.insertMemberInfo(domain, member, hid, uid) if err != nil { log.Warningf("insert one member to home faileddomain[%s], hid[%d], uid[%d]", domain, hid, uid) return err } return nil }
// modify member info all column can be modified func (this *MemberManager) modifyMemberInfo(domain string, hid, uid int64, key string, value interface{}) error { common.CheckParam(len(key) != 0) SQL := fmt.Sprintf("UPDATE %s_home_members SET %s = ? WHERE uid = ? AND hid = ?", domain, key) stmt, err := this.store.db.Prepare(SQL) if err != nil { log.Errorf("prepare query failed:err[%v]", err) return err } defer stmt.Close() result, err := stmt.Exec(value, uid, hid) if err != nil { log.Errorf("execute update failed:sql[%s], err[%v]", SQL, err) return err } affect, err := result.RowsAffected() if err != nil { log.Warningf("get affected rows failed:err[%v]", err) return err } if affect < 1 { log.Warningf("check affected rows failed:domain[%s], hid[%d], uid[%d], row[%d]", domain, hid, uid, affect) return common.ErrEntryNotExist } else if affect > 1 { log.Errorf("check affected rows failed:domain[%s], hid[%d], uid[%d], row[%d]", domain, hid, uid, affect) return common.ErrUnknown } return nil }
// modify device info all column can be modified func (this *DeviceManager) modifyDeviceInfo(checkStatus bool, domain string, did int64, key string, value interface{}) error { common.CheckParam(len(key) != 0) var SQL string if checkStatus { SQL = fmt.Sprintf("UPDATE %s_device_info SET %s = ? WHERE did = ? AND status = %d", domain, key, ACTIVE) } else { SQL = fmt.Sprintf("UPDATE %s_device_info SET %s = ? WHERE did = ?", domain, key) } stmt, err := this.store.db.Prepare(SQL) if err != nil { log.Errorf("prepare query failed:err[%v]", err) return err } defer stmt.Close() result, err := stmt.Exec(value, did) if err != nil { log.Errorf("execute update failed:domain[%s], key[%s], value[%v], err[%v]", domain, key, value, err) return err } affect, err := result.RowsAffected() if err != nil { log.Warningf("get affected rows failed:err[%v]", err) return err } if affect != 1 { log.Warningf("update device info failed:domain[%s], key[%s], value[%v], err[%v]", domain, key, value, err) return common.ErrEntryNotExist } return nil }
// add home owner func (this *MemberManager) AddOwner(domain, owner string, hid, uid int64) error { common.CheckParam(this.store != nil) if len(owner) <= 0 { log.Warningf("check owner name failed:domain[%s], owner[%s], uid[%d], hid[%d]", domain, owner, uid, hid) return common.ErrInvalidName } return this.addOwnerMember(domain, owner, hid, uid) }
// get all homeids belong to this member, if no hid return empty list func (this *MemberManager) GetAllHomeIds(domain string, uid int64) ([]int64, error) { common.CheckParam(this.store != nil) list, err := this.getMemberAllHomes(domain, uid) if err != nil { log.Warningf("get member all home ids failed:domain[%s], uid[%d], err[%v]", domain, uid, err) return nil, err } return list, nil }
// get all members of the home, if (hid) not exist, // return empty list, not nil + nil func (this *MemberManager) GetAllMembers(domain string, hid int64) ([]Member, error) { common.CheckParam(this.store != nil) // if home not exist, return empty list list, err := this.getAllMembers(domain, hid) if err != nil { log.Warningf("get one home all members failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return nil, err } return list, nil }
// delete the home and delete all the members func (this *MemberManager) DeleteAllMembers(domain string, hid int64) error { common.CheckParam(this.store != nil) err := this.deleteAllMembers(domain, hid) if err != nil { log.Warningf("delete home all members failed:domain[%s], hid[%d], err[%s]", domain, hid, err) return err } return nil }
//////////////////////////////////////////////////////////////////////////////////// // public interface ///////////////////////////////////////////////////////////////////////////////////// // if find the record return home + nil, else if no record return nil + nil func (this *HomeManager) Get(domain string, hid int64) (*Home, error) { common.CheckParam(this.store != nil) var home Home err := this.getHome(domain, hid, &home) if err != nil { if err == common.ErrEntryNotExist { return nil, nil } else { log.Warningf("get home failed:domain[%s], hid[%d]", domain, hid) return nil, err } } return &home, nil }
//////////////////////////////////////////////////////////////////////////////////// // public interface //////////////////////////////////////////////////////////////////////////////////// // get member for check if the user has privelige, if not exist return nil + nil func (this *MemberManager) Get(domain string, hid, uid int64) (*Member, error) { common.CheckParam(this.store != nil) var member Member err := this.getMemberInfo(domain, hid, uid, &member) if err != nil { if err == common.ErrEntryNotExist { return nil, nil } else { log.Warningf("get member info failed:domain[%d], hid[%d], uid[%d]", domain, hid, uid) return nil, err } } return &member, nil }
// change the slave or master device, the old must valid func (this *BindingManager) ChangeBinding(did int64, domain, subDomain, deviceId string) error { common.CheckParam(this.proxy != nil && this.warehouse != nil) // step 1. check the old did must be register succ var device DeviceInfo deviceManager := NewDeviceManager(this.store) err := deviceManager.getDeviceInfo(domain, did, &device) if err != nil { log.Warningf("get master device failed:domain[%s], did[%d], err[%v]", domain, did, err) return err } // step 2 check the old did must be binging ok var exist bool err = this.proxy.IsBindingExist(domain, did, &exist) if err != nil { log.Warningf("check binding exist failed:domain[%s], did[%d], err[%v]", domain, did, err) return err } else if !exist { log.Warningf("the old did not binding:domain[%s], did[%d]", domain, did) return common.ErrNotYetBinded } // step 3. check the new device basic info is valid according the old device type err = this.checkDeviceInfo(domain, subDomain, deviceId, device.IsMasterDevice()) if err != nil { log.Warningf("check device info failed:domain[%s], device[%s:%s], master[%d], err[%v]", domain, subDomain, deviceId, did, err) return err } // step 4. check the new device is not binding by others _, err = this.proxy.GetBindingInfo(domain, subDomain, deviceId) if err != nil { if err != common.ErrEntryNotExist { log.Warningf("get device binding info failed:domain[%s], device[%s:%s], master[%d], err[%v]", domain, subDomain, deviceId, did, err) return err } } else { log.Warningf("check the device is already binded:domain[%s], device[%s:%s], did[%d]", domain, subDomain, deviceId, did) return common.ErrAlreadyBinded } // step 5. change the mapping relation err = this.proxy.ChangeDeviceBinding(did, domain, subDomain, deviceId) if err != nil { log.Warningf("do change the device mapping binding info failed:domain[%s], device[%s:%s], err[%v]", domain, subDomain, deviceId, err) return err } return nil }
func (this *HomeManager) ModifyName(domain string, hid int64, name string) error { common.CheckParam(this.store != nil) home, err := this.Get(domain, hid) if err != nil { log.Warningf("get home failed:domain[%s], hid[%d], err[%v]", domain, home, err) return err } else if home == nil { log.Warningf("home not exist:domain[%s], hid[%d]", domain, hid) return common.ErrEntryNotExist } else if home.GetStatus() != ACTIVE { log.Warningf("home is not active:domain[%s], hid[%d]", domain, hid) return common.ErrInvalidStatus } return this.modifyHome(domain, hid, "name", name) }
// create a new home func (this *HomeManager) Create(domain string, uid int64, name string) error { common.CheckParam(this.store != nil) if len(name) <= 0 { log.Warningf("check the home name failed:uid[%d], name[%s]", uid, name) return common.ErrInvalidParam } hid, err := this.insertHome(domain, uid, name) if err != nil { log.Warningf("insert home failed:domain[%s], createUid[%d], name[%s]", domain, uid, name) return err } // insert member as creator type to the home_members member := NewMemberManager(this.store) err = member.AddOwner(domain, "owner", hid, uid) if err != nil { log.Errorf("add owner to the home failed:domain[%s], createUid[%d], name[%s]", domain, uid, name) return err } return nil }
// if no home, return empty list not nil func (this *HomeManager) GetAllHome(domain string, uid int64) ([]Home, error) { common.CheckParam(this.store != nil) member := NewMemberManager(this.store) homeIds, err := member.GetAllHomeIds(domain, uid) if err != nil { log.Warningf("get all home id list failed:domain[%s], uid[%d], err[%v]", domain, uid, err) return nil, err } list := make([]Home, 0) for _, hid := range homeIds { home, err := this.Get(domain, hid) if err != nil { log.Warningf("get home info failed:domain[%s], uid[%d], hid[%d]", domain, uid, hid) return nil, err } else if home == nil { log.Warningf("check home not exist:domain[%s], uid[%d], hid[%d]", domain, uid, hid) } else { list = append(list, *home) } } return list, nil }
// del a member from a home func (this *MemberManager) Delete(domain string, hid, uid int64) error { common.CheckParam(this.store != nil) homeManager := NewHomeManager(this.store) // step 1. check home info home, err := homeManager.Get(domain, hid) if err != nil { log.Warningf("get home failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return err } else if home == nil { log.Warningf("home not exist:domain[%s], hid[%d]", domain, hid) return common.ErrEntryNotExist } else if home.GetStatus() != ACTIVE { log.Warningf("check home status failed:domain[%s], hid[%d]", domain, hid) return common.ErrInvalidStatus } // step 2. delete member, if not exist return succ err = this.deleteOneMember(domain, hid, uid) if err != nil { log.Warningf("delete one member of home failed:domain[%s], hid[%d], err[%s]", domain, hid, err) return err } return nil }
// binding one device to one home, if masterDid < 0 it's master device, otherwise it's slave device func (this *BindingManager) Binding(domain, subDomain, deviceId, deviceName string, hid, masterDid int64) error { common.CheckParam(this.proxy != nil && this.warehouse != nil) // step 1. check the device basic info is valid err := this.checkDeviceInfo(domain, subDomain, deviceId, masterDid < 0) if err != nil { log.Warningf("check the binding device failed:err[%v]", err) return err } // step 2. check the master device is binding ok if masterDid > 0 { _, err = this.proxy.GetBindingByDid(domain, masterDid) if err != nil { log.Warningf("check the master device not active:domain[%s], did[%d], err[%v]", domain, masterDid, err) return err } } // step 3. build mapping device ids, if already exist return succ for rebinding... err = this.proxy.BindingDevice(domain, subDomain, deviceId, deviceName, hid, masterDid) if err != nil { log.Warningf("binding device failed:domain[%s], device[%s:%s], master[%d]", domain, subDomain, deviceId, masterDid) return err } return nil }
// TODO do not really delete the home info from the storage func (this *HomeManager) Delete(domain string, hid int64) error { common.CheckParam(this.store != nil) // step 1.delete all the devices not in a transaction device := NewDeviceManager(this.store) err := device.DeleteAllDevices(domain, hid) if err != nil { log.Warningf("delete the home devices failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return err } // step 2. delete all the members not in a transaction member := NewMemberManager(this.store) err = member.DeleteAllMembers(domain, hid) if err != nil { log.Errorf("delete the home all members failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return err } // step 3. delete the home info from err = this.deleteHome(domain, hid) if err != nil { log.Warningf("delete home info failed:domain[%s], hid[%d], err[%v]", domain, hid, err) return err } return nil }
func (this *HomeManager) Enable(domain string, hid int64) error { common.CheckParam(this.store != nil) return this.modifyHome(domain, hid, "status", ACTIVE) }
// if not exist return nil + nil func (this *BindingManager) GetBindingInfo(domain, subDomain, deviceId string) (*BindingInfo, error) { common.CheckParam(this.proxy != nil && this.warehouse != nil) return this.proxy.GetBindingInfo(domain, subDomain, deviceId) }