Exemplo n.º 1
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized(): 
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         with SBDB.session_scope() as session :
             start_dt=self.body.get(BaseCommand.PN_STARTDT,"1999-01-01 00:00:00")
             end_dt=self.body.get(BaseCommand.PN_ENDDT,"2059-01-01 00:00:00")
             alarm_types=self.body.get(BaseCommand.PN_ALARMTYPES,None)
             device_ids=self.body.get(BaseCommand.PN_DEVICEIDS,None)
             apartment_ids=self.body.get(BaseCommand.PN_APARTMENTIDS,None)
             page=self.body[BaseCommand.PN_PAGE]
             
             respond=self.GetResp()
             try:
                 theQuery=session.query(SBDB_ORM.Alarm,SBDB_ORM.ApartmentDeviceKey.name).join(SBDB_ORM.Event).\
                 join(SBDB_ORM.DeviceKeyCode,SBDB_ORM.DeviceKeyCode.id==SBDB_ORM.Event.device_key_code_id).\
                 outerjoin(SBDB_ORM.ApartmentDeviceKey,and_(SBDB_ORM.ApartmentDeviceKey.device_key_code_id==SBDB_ORM.DeviceKeyCode.id,SBDB_ORM.ApartmentDeviceKey.apartment_device_id==SBDB_ORM.Alarm.apartment_device_id)).\
                 join(SBDB_ORM.ApartmentDevice,SBDB_ORM.ApartmentDeviceKey.apartment_device_id==SBDB_ORM.ApartmentDevice.id).\
                 join(SBDB_ORM.Apartment,SBDB_ORM.Apartment.id==SBDB_ORM.ApartmentDevice.apartment_id).filter(and_(SBDB_ORM.Event.dt.between(start_dt,end_dt),SBDB_ORM.Apartment.account_id==self.protocol.account_id))
                 if apartment_ids:
                     theQuery=theQuery.filter(SBDB_ORM.ApartmentDevice.apartment_id.in_(apartment_ids))
                 if device_ids:
                     theQuery=theQuery.filter(SBDB_ORM.DeviceKeyCode.device_id.in_(device_ids))
                 if alarm_types:
                     theQuery=theQuery.join(SBDB_ORM.Device).join(SBDB_ORM.DeviceModel).join(SBDB_ORM.DeviceType).filter(SBDB_ORM.DeviceType.name.in_(alarm_types))
                 
                 theQuery=theQuery.order_by(SBDB_ORM.Alarm.id.desc()).offset(CQueryAlarm.PAGELIMIT*page).limit(CQueryAlarm.PAGELIMIT)
                 listAlarms=[]
                 for row in theQuery:
                     alarm=row.Alarm
                     dev_key_name=row.name
                     if dev_key_name is None:    dev_key_name="N/A"
                     alarm_item={}
                     alarm_item[BaseCommand.PN_ID]=alarm.id
                     alarm_item[BaseCommand.PN_APARTMENTID]=alarm.apartment_device.apartment_id
                     alarm_item[BaseCommand.PN_DEVICETYPENAME]=alarm.event.device_key_code.device.device_model.device_type.name
                     alarm_item[BaseCommand.PN_DEVICEID]=alarm.event.device_key_code.device_id
                     alarm_item[BaseCommand.PN_DT]=alarm.event.dt.strftime("%Y-%m-%d %H:%M:%S")  
                     alarm_item[BaseCommand.PN_DEVCODE]=alarm.event.device_key_code.device.uni_code
                     alarm_item[BaseCommand.PN_DEVNAME]=dev_key_name
                     alarm_item[BaseCommand.PN_DEVMODEL]=alarm.event.device_key_code.device.device_model.name
                     listAlarms.append(alarm_item)
                 respond.body[BaseCommand.PN_ALARMS]=listAlarms
                     
             except SQLAlchemyError,e:
                 respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                 logging.error("transport %d:%s",id(self.protocol.transport),e)
                 session.rollback()
         respond.Send()
Exemplo n.º 2
0
 def Run(self):
     with self.protocol.lockCmd:
         if self.Authorized():
             CBaseCommand.Run(self)
             self.SendResp()
             if self.protocol.role == BaseCommand.PV_ROLE_HUMAN:
                 threads.deferToThread(SBDB.UpdateActiveTime,
                                       self.protocol.role,
                                       self.protocol.client_id,
                                       id(self.protocol.transport))
             elif self.protocol.role == BaseCommand.PV_ROLE_RELAYER:
                 threads.deferToThread(SBDB.UpdateActiveTime,
                                       self.protocol.role,
                                       self.protocol.relayer_id,
                                       id(self.protocol.transport))
         else:
             self.SendUnauthorizedResp()
Exemplo n.º 3
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized():
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         apartment_id = self.body[BaseCommand.PN_APARTMENTID]
         sb_code = self.body[BaseCommand.PN_SB_CODE]
         sb_name = self.body[BaseCommand.PN_NAME]
         #account_id=self.protocol.account_id
         respond = self.GetResp()
         with SBDB.session_scope() as session:
             if session.query(SBDB_ORM.Relayer).join(
                     SBDB_ORM.Apartment_Relayer).filter(
                         and_(
                             SBDB_ORM.Relayer.uni_code == sb_code,
                             SBDB_ORM.Apartment_Relayer.apartment_id ==
                             apartment_id)).first() is not None:
                 respond.SetErrorCode(BaseCommand.CS_DEVICEEXIST)
             try:
                 relayer_id = SBDB.GetRelayerIdForcely(sb_code)
                 apartment = SBDB.IncreaseVersion(session, apartment_id)
                 apartment_relayer = SBDB_ORM.Apartment_Relayer()
                 apartment_relayer.apartment_id = apartment_id
                 apartment_relayer.relayer_id = relayer_id
                 apartment_relayer.name = sb_name
                 '''
                 relayer=SBDB_ORM.Relayer()
                 relayer.apartment_id=apartment_id
                 relayer.name=sb_name
                 relayer.uni_code=sb_code            
                 session.add(relayer)
                 '''
                 session.add(apartment_relayer)
                 respond.body[BaseCommand.PN_VERSION] = apartment.version
                 session.commit()
                 #                 if session.query(SBDB_ORM.Device).join(SBDB_ORM.Relayer).filter(SBDB_ORM.Relayer.id==relayer.id).first():
                 #                     respond.body[BaseCommand.PN_UPDATEDATA]=True
                 respond.body[BaseCommand.PN_ID] = relayer_id
             except SQLAlchemyError, e:
                 respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                 logging.error("transport %d:%s",
                               id(self.protocol.transport), e)
                 session.rollback()
         respond.Send()
Exemplo n.º 4
0
    def Run(self):
        if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL:
            sb_protocol = None
            with self.protocol.factory.lockDict:
                sb_protocol = self.protocol.factory.dictRelayer.get(
                    self.internalMessage.destId)

            if sb_protocol is None:
                self.Finish(False)
                return

            with sb_protocol.lock_dictWaitResp:
                sb_protocol.dictWaitResp[self.command_seq] = self
            self.Execute()
            return

        with self.protocol.lockCmd:
            if self.protocol.role == BaseCommand.PV_ROLE_HUMAN:
                if not self.Authorized():
                    self.SendUnauthorizedResp()
                    return
                CBaseCommand.Run(self)

                #check whether contain parameter relayer_id
                relayer_id = self.body.get(BaseCommand.PN_RELAYERID, None)
                if relayer_id is None:
                    respond = self.GetResp()
                    respond.SetErrorCode(BaseCommand.CS_PARAMLACK)
                    respond.Send()
                    return

                with self.protocol.lock_dictWaitResp:
                    self.protocol.dictWaitResp[self.command_seq] = self

                interMessage = InternalMessage.CInternalMessage()
                interMessage.SetParam(InternalMessage.TTYPE_GATEWAY,
                                      relayer_id, 0,
                                      InternalMessage.OPER_REQUEST, "",
                                      InternalMessage.TTYPE_HUMAN,
                                      self.protocol.client_id,
                                      id(self.protocol.transport))

                self.Send(interMessage)
                #threading.Timer(Config.timeout_relayer_control,self.timeout).start()
                reactor.callLater(Config.timeout_relayer_control, self.timeout)
Exemplo n.º 5
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized():
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         with SBDB.session_scope() as session:
             apartment_id = self.body[BaseCommand.PN_APARTMENTID]
             sb_id = self.body[BaseCommand.PN_RELAYERID]
             sb_code = self.body.get(BaseCommand.PN_SB_CODE, None)
             name = self.body.get(BaseCommand.PN_NAME, None)
             respond = self.GetResp()
             if sb_id is None:
                 respond.SetErrorCode(BaseCommand.CS_PARAMLACK)
             else:
                 try:
                     apartment = None
                     if sb_code is None:
                         apartment = SBDB.IncreaseVersion(
                             session, apartment_id)
                     else:
                         apartment = SBDB.IncreaseVersions(
                             session, 0, apartment_id)
                     relayer, apartment_relayer = session.query(
                         SBDB_ORM.Relayer, SBDB_ORM.Apartment_Relayer).join(
                             SBDB_ORM.Apartment_Relayer).filter(
                                 and_(
                                     SBDB_ORM.Relayer.id == sb_id,
                                     SBDB_ORM.Apartment_Relayer.apartment_id
                                     == apartment_id)).first()
                     if name is not None:
                         apartment_relayer.name = name
                     if sb_code is not None:
                         relayer.uni_code = sb_code
                     respond.body[
                         BaseCommand.PN_VERSION] = apartment.version
                     session.commit()
                 except SQLAlchemyError as e:
                     respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                     logging.error("transport %d:%s",
                                   id(self.protocol.transport), e)
                     session.rollback()
         respond.Send()
Exemplo n.º 6
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized():
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         with SBDB.session_scope() as session:
             arm_state = self.body[BaseCommand.PN_ARMSTATE]
             apartment_id = self.body.get(BaseCommand.PN_APARTMENTID)
             respond = self.GetResp()
             try:
                 apartment = session.query(SBDB_ORM.Apartment).filter(
                     SBDB_ORM.Apartment.id == apartment_id).first()
                 apartment.arm_state = arm_state
                 apartment.dt_arm = datetime.datetime.now()
                 session.commit()
                 self.PushToClients(session, apartment.dt_arm)
             except SQLAlchemyError, e:
                 respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                 logging.error("transport %d:%s",
                               id(self.protocol.transport), e)
                 session.rollback()
         respond.Send()
Exemplo n.º 7
0
    def Run(self):
        with self.protocol.lockCmd:
            CBaseCommand.Run(self)
            with SBDB.session_scope() as session:
                respond = self.GetResp()
                email = self.body.get(BaseCommand.PN_EMAIL, None)
                account_id, = session.query(SBDB_ORM.Account.id).filter(
                    SBDB_ORM.Account.email == email).first()
                if email is None:
                    respond.SetErrorCode(BaseCommand.CS_NOTFOUNDEMAIL)
                elif account_id is None:
                    respond.SetErrorCode(BaseCommand.CS_NOTFOUNDEMAIL)

    #             elif 'dt_restore_require' in dir(self.protocol) and (datetime.datetime.now()-self.protocol.dt_restore_require).seconds<Config.second_restore_require:
    #                 respond.SetErrorCode(BaseCommand.CS_TRYLATER)
                else:
                    try:
                        content = open(
                            os.path.join(Config.dir_local_static,
                                         "restore_confirm.html"), 'r').read()
                        url = Util.GenRestoreURL(account_id)
                        content = content.replace("{{url_restore}}", url)
                        #emaillib.SendEmail("Honeywell Smart Home: reset password confirm", content, [self.protocol.account.email])
                        threading.Thread(
                            target=emaillib.SendEmail,
                            args=(
                                "Honeywell Smart Home: reset password confirm",
                                content, [email]),
                            daemon=True).start()
                        self.protocol.dt_restore_require = datetime.datetime.now(
                        )
                    except SQLAlchemyError as e:
                        respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                        logging.error("transport %d:%s",
                                      id(self.protocol.transport), e)
                        session.rollback()
            respond.Send()
Exemplo n.º 8
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized(): 
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         with SBDB.session_scope() as session :
             apartment_id=self.body[BaseCommand.PN_ID]
             apartment_name=self.body[BaseCommand.PN_NAME]
             respond=self.GetResp()
             if apartment_id is None or apartment_name is None:
                 respond.SetErrorCode(BaseCommand.CS_PARAMLACK)
             else:
                 try:
                     SBDB.IncreaseVersion(session, apartment_id)
                     apartment=session.query(SBDB_ORM.Apartment).filter(SBDB_ORM.Apartment.id==apartment_id).first()
                     apartment.name=apartment_name
                     respond.body[BaseCommand.PN_VERSION]=apartment.version
                     session.commit()
                 except SQLAlchemyError,e:
                     respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                     logging.error("transport %d:%s",id(self.protocol.transport),e)
                     session.rollback()
         respond.Send()
Exemplo n.º 9
0
    def Run(self):
        with self.protocol.lockCmd:
            if not self.Authorized():
                self.SendUnauthorizedResp()
                return
            CBaseCommand.Run(self)
            with SBDB.session_scope() as session:
                version = self.body[BaseCommand.PN_VERSION]
                apartment_id = self.body[BaseCommand.PN_APARTMENTID]
                respond = self.GetResp()
                try:
                    apartment = session.query(SBDB_ORM.Apartment).filter(
                        SBDB_ORM.Apartment.id == apartment_id).one()
                    respond.body[BaseCommand.PN_VERSION] = apartment.version
                    respond.body[BaseCommand.PN_SCENE_ID] = apartment.scene_id
                    respond.body[BaseCommand.PN_ARMSTATE] = apartment.arm_state
                    if version != apartment.version:
                        apartment_info = {}
                        apartment_info[BaseCommand.PN_ID] = apartment.id
                        apartment_info[BaseCommand.PN_NAME] = apartment.name
                        apartment_info[
                            BaseCommand.PN_VERSION] = apartment.version

                        bDeviceInserted = False
                        listDevice = []
                        for apartment_device in apartment.apartment_devices:
                            elementDevice = {}
                            elementDevice[
                                BaseCommand.PN_ID] = apartment_device.device.id
                            elementDevice[
                                BaseCommand.
                                PN_DEVTYPE] = apartment_device.device.device_model.device_type.name
                            elementDevice[
                                BaseCommand.
                                PN_DEVMODEL] = apartment_device.device.device_model.name
                            elementDevice[
                                BaseCommand.
                                PN_DEVCODE] = apartment_device.device.uni_code
                            elementDevice[
                                BaseCommand.PN_DEVNAME] = apartment_device.name
                            listDeviceKey = []
                            listDevice.append(elementDevice)

                        listRelayer = []
                        for apartment_relayer in apartment.apartment_relayers:
                            relayer = apartment_relayer.relayer
                            elementRelayer = {}
                            elementRelayer[BaseCommand.PN_ID] = relayer.id
                            elementRelayer[
                                BaseCommand.PN_SB_CODE] = relayer.uni_code
                            if not bDeviceInserted:
                                elementRelayer[
                                    BaseCommand.PN_DEVICES] = listDevice
                                bDeviceInserted = True
                            listRelayer.append(elementRelayer)
                        apartment_info[BaseCommand.PN_RELAYERS] = listRelayer

                        respond.body[
                            BaseCommand.PN_APARTMENTINFO] = apartment_info

                except SQLAlchemyError, e:
                    respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                    logging.error("transport %d:%s",
                                  id(self.protocol.transport), e)
                    session.rollback()
            respond.Send()
Exemplo n.º 10
0
    def Run(self):
        with self.protocol.lockCmd:
            CBaseCommand.Run(self)
            if 'role' in dir(self.protocol):
                self.protocol.releaseFromDict()
            role = self.body[BaseCommand.PN_TERMINALTYPE]
            resp = self.GetResp()
            resp.body[BaseCommand.PN_RESULT] = BaseCommand.PV_E_OK
            if cmp(role, BaseCommand.PV_ROLE_RELAYER) == 0:
                sb_code = self.body[BaseCommand.PN_SB_CODE]
                self.protocol.relayer_id = SBDB.GetRelayerIdForcely(sb_code)
                self.protocol.role = role
                with self.protocol.factory.lockDict:
                    self.protocol.factory.dictRelayer[
                        self.protocol.relayer_id] = self.protocol
                #logging.info("transport %d: relayer %s login pass",id(self.protocol.transport),sb_code)
                threads.deferToThread(SBDB.UpdateAuthTimeRelayer,
                                      self.protocol.relayer_id)
            elif cmp(role, BaseCommand.PV_ROLE_HUMAN) == 0:
                #self.protocol.account=SBDB.GetAccount(self.body[BaseCommand.PN_USERNAME], self.body[BaseCommand.PN_PASSWORD])
                with SBDB.session_scope() as session:
                    account = SBDB.GetAccount(
                        session, self.body[BaseCommand.PN_USERNAME])
                    if account is not None and not Util.check_password(
                            self.body[BaseCommand.PN_PASSWORD],
                            account.password):
                        account = None
                    if account is None:
                        resp.body[
                            BaseCommand.PN_RESULT] = BaseCommand.PV_E_USERPASS
                        resp.body[BaseCommand.
                                  PN_ERRORSTRING] = "user/password mismatch"
                        resp.SetErrorCode(BaseCommand.CS_LOGINFAIL)
                    else:
                        self.protocol.account_id = account.id
                        self.protocol.role = role
                        self.protocol.client_id = -1
                        self.protocol.rcv_alarm = self.body.get(
                            BaseCommand.PN_RCVALARM, "False")
                        listApartment = []
                        for apartment in account.apartments:
                            elementApartment = {}
                            elementApartment[BaseCommand.PN_ID] = apartment.id
                            listApartment.append(elementApartment)
                        resp.body[BaseCommand.PN_APARTMENTS] = listApartment
                        dictAccount = self.protocol.factory.dictAccounts
                        for relayerId in SBDB.GetRelayerIDsByAccountId(
                                self.protocol.account_id):
                            with self.protocol.factory.lockDict:
                                if dictAccount.has_key(relayerId):
                                    dictAccount[relayerId].append(
                                        self.protocol)
                                else:
                                    dictAccount[relayerId] = [
                                        self.protocol,
                                    ]

                        #set client information
                        os = self.body.get(BaseCommand.PN_OS,
                                           BaseCommand.PV_OS_IOS)
                        token = self.body.get(BaseCommand.PN_TOKEN)
                        last_token = self.body.get(BaseCommand.PN_LASTTOKEN)
                        balance = self.body.get(BaseCommand.PN_BALANCE)
                        #terminal_code=self.body.get(BaseCommand.PN_TERMINALCODE,datetime.datetime.now().strftime("%Y%m%d%H%M%S")+str(random.uniform(0,4000)))
                        if token == '' or token is None:
                            terminal_code = self.body.get(
                                BaseCommand.PN_TERMINALCODE,
                                datetime.datetime.now().strftime(
                                    "%Y%m%d%H%M%S") +
                                str(random.uniform(0, 4000)))
                        else:
                            terminal_code = self.body.get(
                                BaseCommand.PN_TERMINALCODE, token)

                        try:
                            ###for temply use
                            if token is not None:
                                session.query(SBDB_ORM.Client).filter(
                                    and_(
                                        SBDB_ORM.Client.account_id !=
                                        self.protocol.account_id,
                                        SBDB_ORM.Client.device_token ==
                                        token.strip())).delete()
                            ###-------------
                            if last_token is not None and token <> last_token:
                                #session.query(SBDB_ORM.Client).filter(and_(SBDB_ORM.Client.account_id==self.protocol.account_id,SBDB_ORM.Client.device_token==last_token.strip())).delete()
                                session.query(SBDB_ORM.Client).filter(
                                    SBDB_ORM.Client.device_token ==
                                    last_token.strip()).delete()

                            client = session.query(SBDB_ORM.Client).filter(
                                SBDB_ORM.Client.terminal_code ==
                                terminal_code.strip()).first()
                            if client is None:
                                if token is not None:
                                    session.query(SBDB_ORM.Client).filter(
                                        SBDB_ORM.Client.device_token ==
                                        token.strip()).delete()
                                client = SBDB_ORM.Client()
                                client.device_token = token
                                client.enable_alarm = True
                                client.os = os
                                session.add(client)
                            client.account_id = self.protocol.account_id
                            client.terminal_code = terminal_code
                            client.dt_auth = client.dt_active = datetime.datetime.now(
                            )
                            client.server_id = InternalMessage.MyServerID
                            session.commit()
                            self.protocol.client_id = client.id
                            #logging.info("transport %d: user %s login pass ",id(self.protocol.transport),self.body[BaseCommand.PN_USERNAME])
                            if balance is None: balance = 'n'
                            threads.deferToThread(SBDB.UpdateAuthTimeHuman,
                                                  client.id, balance,
                                                  id(self.protocol.transport))
                        except SQLAlchemyError, e:
                            resp.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                            logging.error("transport %d:%s",
                                          id(self.protocol.transport), e)
                            session.rollback()

            else:
Exemplo n.º 11
0
    def Run(self):
        with self.protocol.lockCmd:
            if not self.Authorized():
                self.SendUnauthorizedResp()
                return
            CBaseCommand.Run(self)

            apartment_id = self.body[BaseCommand.PN_APARTMENTID]
            relayer_id = self.body.get(BaseCommand.PN_RELAYERID)
            dev_model = self.body[BaseCommand.PN_DEVMODEL]
            dev_code = self.body[BaseCommand.PN_DEVCODE]
            dev_keys = self.body.get(BaseCommand.PN_DEVKEYS)
            respond = self.GetResp()
            with SBDB.session_scope() as session:
                model = SBDB.GetDeviceModelByName(session, dev_model)
                try:
                    if relayer_id is None:
                        relayer_id, = session.query(SBDB_ORM.Relayer.id).join(
                            SBDB_ORM.Apartment_Relayer).filter(
                                SBDB_ORM.Apartment_Relayer.apartment_id ==
                                apartment_id).order_by(
                                    SBDB_ORM.Relayer.id).first()
                    else:
                        relayer_id = int(relayer_id)
                    if model is None:
                        respond.SetErrorCode(BaseCommand.CS_DEVICEMODEL)
                    elif relayer_id is None:
                        respond.SetErrorCode(BaseCommand.CS_NORELAYER)
                    elif session.query(SBDB_ORM.ApartmentDevice).join(
                            SBDB_ORM.Device).filter(
                                and_(
                                    SBDB_ORM.Device.uni_code == dev_code,
                                    SBDB_ORM.ApartmentDevice.apartment_id ==
                                    apartment_id)).first() is not None:
                        respond.SetErrorCode(BaseCommand.CS_DEVICEEXIST)
                    else:
                        apartment = SBDB.IncreaseVersions(
                            session, 0, apartment_id)
                        device = SBDB.GetDeviceForcely(session, dev_code,
                                                       dev_model)
                        if device is None:
                            device = SBDB_ORM.Device()
                            device.device_model_id = model.id
                            device.uni_code = dev_code

                            session.add(device)

                        else:
                            apartment_device = SBDB_ORM.ApartmentDevice()
                            apartment_device.apartment_id = apartment_id
                            apartment_device.name = model.device_type.name
                            apartment_device.relayer_id = relayer_id
                            apartment_device.device_id = device.id

                            respond.body[
                                BaseCommand.PN_VERSION] = apartment.version
                            session.add(apartment_device)
                            session.commit()
                            respond.body[BaseCommand.PN_DEVICEID] = device.id
                except SQLAlchemyError, e:
                    respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                    logging.error("transport %d:%s",
                                  id(self.protocol.transport), e)
                    session.rollback()
            respond.Send()
Exemplo n.º 12
0
 def Run(self):
     with self.protocol.lockCmd:
         CBaseCommand.Run(self)
         user_name = self.body.get(BaseCommand.PN_USERNAME)
         if user_name is not None:
             user_name = user_name.strip()
         password = self.body[BaseCommand.PN_PASSWORD]
         email = self.body.get(BaseCommand.PN_EMAIL)
         if email is not None:
             email = email.strip()
         mobile_phone = self.body.get(BaseCommand.PN_MOBLEPHONE)
         if mobile_phone is not None:
             mobile_phone = mobile_phone.strip()
         respond = self.GetResp()
         with SBDB.session_scope() as session:
             if user_name is None and password is None and email is None:
                 respond.SetErrorCode(BaseCommand.CS_PARAMLACK)
             elif user_name is not None and (
                     session.query(SBDB_ORM.Account).filter(
                         or_(SBDB_ORM.Account.user_name == user_name,
                             SBDB_ORM.Account.email == user_name,
                             SBDB_ORM.Account.mobile_phone == user_name))
                     .first() is not None or len(user_name) < 2):
                 respond.SetErrorCode(BaseCommand.CS_USERNAME)
             elif email is not None and (
                     session.query(SBDB_ORM.Account).filter(
                         or_(SBDB_ORM.Account.user_name == email,
                             SBDB_ORM.Account.email == email,
                             SBDB_ORM.Account.mobile_phone == email))
                     .first() is not None or not Util.validateEmail(email)):
                 respond.SetErrorCode(BaseCommand.CS_EMAIL)
             elif mobile_phone is not None and (
                     session.query(SBDB_ORM.Account).filter(
                         or_(SBDB_ORM.Account.user_name == mobile_phone,
                             SBDB_ORM.Account.email == mobile_phone,
                             SBDB_ORM.Account.mobile_phone == mobile_phone))
                     .first() is not None
                     or not Util.validateMobilePhone(mobile_phone)):
                 respond.SetErrorCode(BaseCommand.CS_MOBILEPHONE)
             else:
                 try:
                     account = SBDB_ORM.Account()
                     account.language_id = 2
                     account.email = email
                     account.password = Util.hash_password(password)
                     account.user_name = user_name
                     account.mobile_phone = mobile_phone
                     account.version = 0
                     apartment = SBDB_ORM.Apartment()
                     apartment.arm_state = BaseCommand.PV_ARM_OFF
                     apartment.name = "Home"
                     apartment.scene_id = None
                     apartment.version = 0
                     account.apartments.append(apartment)
                     session.add(account)
                     session.commit()
                     respond.body[
                         BaseCommand.PN_VERSION] = apartment.version
                     respond.body[BaseCommand.PN_APARTMENTID] = apartment.id
                     respond.body[BaseCommand.PN_NAME] = apartment.name
                 except SQLAlchemyError as e:
                     respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                     logging.error("transport %d:%s",
                                   id(self.protocol.transport), e)
                     session.rollback()
         respond.Send()