Exemple #1
0
def SendAndSave(sms_template, apartment, alarm_type, listPhone, device_name):

    global mutex
    with mutex:

        # message,result,error=SendAlarmBySMS(sms_template,apartment.name,alarm_type,listPhone,device_name)
        message, result, error = SendAlarmBySMS(
            sms_template, apartment.account.user_name, alarm_type, listPhone,
            device_name)

        # insert into database
        sms_head = SBDB_ORM.SmsSenderHead()
        sms_head.apartment_id = apartment.id
        sms_head.content = message
        sms_head.dt = datetime.datetime.now()
        for phone in listPhone:
            sms_list = SBDB_ORM.SmsSenderList()
            sms_list.mobile_phone = phone
            sms_list.result = error
            sms_head.sms_sender_lists.append(sms_list)

        with SBDB.session_scope() as session:
            session.add(sms_head)
            session.commit()
        return result
Exemple #2
0
 def Run(self):
     with self.protocol.lockCmd:
         if not self.Authorized():
             self.SendUnauthorizedResp()
             return
         CBaseCommand.Run(self)
         name = self.body[BaseCommand.PN_APARTMENTNAME]
         respond = self.GetResp()
         with SBDB.session_scope() as session:
             try:
                 #SBDB.IncreaseVersion(session, self.protocol.account)
                 apartment = SBDB_ORM.Apartment()
                 apartment.account_id = self.protocol.account_id
                 apartment.arm_state = BaseCommand.PV_ARM_OFF
                 apartment.name = name
                 apartment.scene_id = None
                 apartment.version = 0
                 session.add(apartment)
                 respond.body[BaseCommand.PN_VERSION] = apartment.version
                 session.commit()
                 respond.body[BaseCommand.PN_ID] = apartment.id
             except SQLAlchemyError as e:
                 respond.SetErrorCode(BaseCommand.CS_DBEXCEPTION)
                 logging.error("transport %d:%s",
                               id(self.protocol.transport), e)
                 session.rollback()
         respond.Send()
Exemple #3
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=password
                     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()
Exemple #4
0
def GetDefaultTemplate():
    with SBDB.session_scope() as session :
        defaultTemplate=session.query(SBDB_ORM.MessageTemplate).filter(and_(SBDB_ORM.MessageTemplate.language_id == None,SBDB_ORM.MessageTemplate.account_id == None,SBDB_ORM.MessageTemplate.sensor_model_id == None)).first()
        if defaultTemplate is None:
            defaultTemplate=SBDB_ORM.MessageTemplate()
            defaultTemplate.template="[apartment]的[device]于[time]发生[type]告警"
            session.add(defaultTemplate)
            session.commit()
    return defaultTemplate
Exemple #5
0
def GenRestoreURL(account_id):
    from DB import SBDB, SBDB_ORM
    uuid_restore = GenUUID()
    with SBDB.session_scope() as session:
        restore = SBDB_ORM.RestoreRequire()
        restore.account_id = account_id
        restore.dt = datetime.datetime.now()
        restore.finished = False
        restore.uuid = uuid_restore
        session.add(restore)
        session.commit()
    return "https://www.honhome.com/customer/reset_password/" + uuid_restore
 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()
Exemple #7
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:
Exemple #8
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()