Esempio n. 1
0
def register_device(addr):
    if (csmapi.ENDPOINT == None):
        detect_local_ec()

    global MAC
    if addr != None: MAC = addr

    global profile
    if profile['d_name'] == None:
        profile['d_name'] = str(int(random.uniform(
            1, 100))) + '.' + profile['dm_name']

    global timestamp
    for i in profile['df_list']:
        timestamp[i] = ''

    print('IoTtalk Server = {}'.format(csmapi.ENDPOINT))
    if csmapi.register(MAC, profile):
        print('This device has successfully registered.')
        print('Device name = ' + profile['d_name'])
        csmapi.push(MAC, '__Ctl_I__',
                    ['SET_DF_STATUS_RSP', {
                        'cmd_params': []
                    }])
        return True
    else:
        print('Registration failed.')
        return False
Esempio n. 2
0
def register_device(addr):
    if (csmapi.ENDPOINT == None):
        detect_local_ec()

    global MAC
    if addr != None: MAC=addr


    global profile
    if profile['d_name'] == None: profile['d_name']= str(int(random.uniform(1, 100)))+'.'+ profile['dm_name']

    global timestamp
    for i in profile['df_list']:
        timestamp[i]= ''



    print('IoTtalk Server = {}'.format(csmapi.ENDPOINT))
    if csmapi.register(MAC,profile):
        print ('This device has successfully registered.')
        print ('Device name = ' + profile['d_name'])
        csmapi.push(MAC,'__Ctl_I__',['SET_DF_STATUS_RSP',{'cmd_params':[]}])
        return True
    else:
        print ('Registration failed.')
        return False
Esempio n. 3
0
def ControlChannel():
    global state, SelectedDF, control_channel_timestamp
    NewSession = requests.Session()
    while True:
        #time.sleep(2)
        try:
            CH = csmapi.pull(MAC, '__Ctl_O__', NewSession)
            if CH != []:
                if control_channel_timestamp == CH[0][0]: continue
                control_channel_timestamp = CH[0][0]
                cmd = CH[0][1][0]
                if cmd == 'RESUME': state = 'RESUME'
                elif cmd == 'SUSPEND': stste = 'SUSPEND'
                elif cmd == 'SET_DF_STATUS':
                    csmapi.push(MAC, '__Ctl_I__', [
                        'SET_DF_STATUS_RSP', {
                            'cmd_params': CH[0][1][1]['cmd_params']
                        }
                    ], NewSession)
                    DF_STATUS = list(CH[0][1][1]['cmd_params'][0])
                    SelectedDF = []
                    index = 0
                    profile['df_list'] = csmapi.pull(
                        MAC, 'profile')['df_list']  #new
                    for STATUS in DF_STATUS:
                        if STATUS == '1':
                            SelectedDF.append(profile['df_list'][index])
                        index = index + 1
        except Exception as e:
            print('Control error:', e)
            if str(e).find('mac_addr not found:') != -1:
                print('Reg_addr is not found. Try to re-register...')
                device_registration_with_retry()
            else:
                print('ControlChannel failed due to unknow reasons.')
Esempio n. 4
0
def ControlChannel():
    global state, SelectedDF, control_channel_timestamp
    while True:
        time.sleep(2)
        try:
            CH = csmapi.pull(MAC, '__Ctl_O__')
            if CH != []:
                if control_channel_timestamp == CH[0][0]: continue
                control_channel_timestamp = CH[0][0]
                state = CH[0][1][0]
                if state == 'SET_DF_STATUS':
                    csmapi.push(MAC, '__Ctl_I__', [
                        'SET_DF_STATUS_RSP', {
                            'cmd_params': CH[0][1][1]['cmd_params']
                        }
                    ])
                    DF_STATUS = list(CH[0][1][1]['cmd_params'][0])
                    SelectedDF = []
                    index = 0
                    for STATUS in DF_STATUS:
                        if STATUS == '1':
                            SelectedDF.append(profile['df_list'][index])
                        index = index + 1
        except Exception as e:
            print('Control error', e)
 def ControlChannel(self):
     print('Device state:', self.state)
     NewSession = requests.Session()
     control_channel_timestamp = None
     while True:
         time.sleep(2)
         try:
             self.CH = csmapi.pull(self.MAC, '__Ctl_O__', NewSession)
             if self.CH != []:
                 if control_channel_timestamp == self.CH[0][0]: continue
                 control_channel_timestamp = self.CH[0][0]
                 cmd = self.CH[0][1][0]
                 if cmd == 'RESUME':
                     print('Device state: RESUME.')
                     self.state = 'RESUME'
                 elif cmd == 'SUSPEND':
                     print('Device state: SUSPEND.')
                     self.state = 'SUSPEND'
                 elif cmd == 'SET_DF_STATUS':
                     csmapi.push(self.MAC, '__Ctl_I__', [
                         'SET_DF_STATUS_RSP', {
                             'cmd_params': self.CH[0][1][1]['cmd_params']
                         }
                     ], NewSession)
                     DF_STATUS = list(self.CH[0][1][1]['cmd_params'][0])
                     self.SelectedDF = []
                     index = 0
                     self.profile['df_list'] = csmapi.pull(
                         self.MAC, 'profile')['df_list']  #new
                     for STATUS in DF_STATUS:
                         if STATUS == '1':
                             self.SelectedDF.append(
                                 self.profile['df_list'][index])
                         index = index + 1
         except Exception as e:
             print('Control error:', e)
             if str(e).find('mac_addr not found:') != -1:
                 print('Reg_addr is not found. Try to re-register...')
                 self.device_registration_with_retry()
             else:
                 print('ControlChannel failed due to unknow reasons.')
                 time.sleep(1)
def register_device(addr=MAC):
    if (csmapi.ENDPOINT == None):
        detect_local_ec()

    global MAC
    MAC = addr

    global timestamp
    for i in profile['df_list']:
        timestamp[i] = ''

    print('IoTtalk Server = {}'.format(csmapi.ENDPOINT))
    if csmapi.create(MAC, profile):
        print('This device has successfully registered.')
        csmapi.push(MAC, '__Ctl_I__',
                    ['SET_DF_STATUS_RSP', {
                        'cmd_params': []
                    }])
        return True
    else:
        print('Registration failed.')
        return False
Esempio n. 7
0
def push(idf_name, data):
    logging('push({})', idf_name)
    try:
        if idf_name == 'Control':
            idf_name = '__Ctl_I__'

        else:
            _df_is_odf[idf_name] = False
            if not _df_selected[idf_name]:
                return False

            if _suspended:
                return False

        return csmapi.push(_mac_addr, idf_name, data)
    except csmapi.CSMError as e:
        logging('push({}): CSMError({})', idf_name, e)

    return False
Esempio n. 8
0
def push(FEATURE_NAME, *data):
    return csmapi.push(MAC, FEATURE_NAME, list(data))
Esempio n. 9
0
def push(FEATURE_NAME, *data):
    if state == 'RESUME':
        return csmapi.push(MAC, FEATURE_NAME, list(data))
    else: return None
Esempio n. 10
0
def push(FEATURE_NAME, *data):
    return csmapi.push(MAC, FEATURE_NAME, list(data))