def dev_alias(dev_filter, pid, cur_account, alias): """ 设置别名 :param dev_filter: :param pid: :param cur_account: :param alias: :return: """ if not (isinstance(cur_account, str) and isinstance(alias, basestring)): return pid = rawid_tuple(pid, True) if not pid or len(alias) > 15 or ':' in alias: return alias = bs2utf8(alias) pid, typ, sn, io = pid #子帐号是否关注,主帐号拥有 is_sub_fo, primary_account = dev_filter.send_multi_cmd(*combine_redis_cmds( is_dev_subaccounted(pid, cur_account), test_primary_bound(pid))) if not primary_account: return _, _, primary_account = primary_account.split(':') if not (primary_account == cur_account or is_sub_fo): return dev_filter.send_cmd(*set_alias(pid, cur_account, alias)) return True
def extract(code, aes_key, aes_iv, hmac_sha1_key='8d35ba51c374e0f9d263'): if not code or not isinstance(code, str) or 36 != len(code): return None code = decrypt(code, aes_key, aes_iv, 24, 3) if not code: return None ptu_id = code[:4] akey = code[4:8] offset = struct.unpack('>B', code[8])[0] if take_offset_chars( HMAC.new(hmac_sha1_key, ptu_id + akey, SHA).hexdigest(), offset, 9) != code[9:]: return None return rawid_tuple(b2a_hex(ptu_id), True), b2a_hex(akey)
def pack(pid, src_ts, reason, user_cmd, cmd_ord, cur_state, rec_ts, rec_len, rec_tot, rec_uid, redis_key): return msgpack.packb({ 'id': pid, 'r': { 'src_ts': src_ts, 'reason': reason, 'dev_id': rawid_tuple(pid)[2], 'user_cmd': user_cmd, 'cmd_ord': cmd_ord, 'cur_state': cur_state, 'rec_ts': rec_ts, 'rec_len': rec_len, 'rec_tot': rec_tot, 'rec_uid': rec_uid, 'redis_key': redis_key, } }, use_bin_type=True)
def sms_support(pid): """ 短信支持情况 :param pid: 设备id 0: 不能发短信 1: 非结构化短信 2: 结构化短信 """ _ = rawid_tuple(pid, True) if _ is None: return None pid, dev_type, dev_sn, io = _ _, sms, _ = parse_io_options(io) if not sms: return 0 if (dev_sn >> 22) & 0b1: #结构化短信 return 2 return 1
def gen_code(ptu_id, akey, aes_key, aes_iv, hmac_sha1_key='8d35ba51c374e0f9d263', aes_seg_size=24): """ 返回36字节QR码、移除io选项的新id,还有io选项 由于AES加密转换ascii的关系,明文长度为36/2 ptu_id(4) + akey(4) + offset(1) + hash(9) :param ptu_id: 8字节字符串 :param akey: 4字节16进制或者8字节ascii码,产线生成 :param hmac_sha1_key: hmac_sha1密钥 """ _ = rawid_tuple(ptu_id, with_io=True) if not _: return None _, typ, sn, io = _ if not akey or not isinstance(akey, str) or len(akey) not in (4, 8): return None if 8 == len(akey): akey = a2b_hex(akey) if not hmac_sha1_key or not isinstance(hmac_sha1_key, str) or 20 != len(hmac_sha1_key): return None ptu_id = struct.pack('>I', tuple_rawid(io, typ, sn)) assert 4 == len(ptu_id) and 4 == len(akey) digest = HMAC.new(hmac_sha1_key, ptu_id + akey, SHA).hexdigest() offset = random.randint(0, 39) return encrypt( '{0}{1}{2}{3}'.format(ptu_id, akey, struct.pack('>B', offset), take_offset_chars(digest, offset, 9)), aes_key, aes_iv, aes_seg_size), typ, sn, io
def pack_entry(rawid, user_cmd, token, gps_dict, wifi_list, bs_list, battery, svr_ts, loc_reason, dev_state = 0, steps = 0, cur_move_state = 0, push_pack=None, charge=0, fix_bs_loc=None): """ :param loc_reason: 定位原因 :param svr_ts: 服务器时间 :param charge: 是否充电 :param battery: 电池电量 :param bs_list: 基站列表对象 :param wifi_list: wifi列表对象 :param gps_dict: :param user_cmd: :param rawid: 设备id :param token: 触发设备定位序号,例如PT30短信序号 :param dev_state: 设备当前状态 :param steps: 总步数 :param cur_move_state:当前移动状态 :param push_pack: push打包体,字符串格式 :param fix_bs_loc: 需矫正的基站坐标,北纬,东经 :param """ if not (rawid and isinstance(rawid, str) and 8 == len(rawid)): raise ValueError('rawid invalid: %s' % rawid) if not isinstance(user_cmd, str): raise ValueError('user_cmd not str: %s' % type(user_cmd)) if not (token and isinstance(token, int)): raise ValueError('token not int: %s' % type(token)) if not isinstance(battery, int): raise ValueError('battery not int: %s' % type(battery)) if push_pack: if not isinstance(push_pack, str): raise ValueError('push_pack not str: %s' % type(push_pack)) if not isinstance(svr_ts, int): raise ValueError('svr_ts not int: %s' % svr_ts) if not isinstance(loc_reason, int): raise ValueError('loc_reason not int: %s' % loc_reason) rawid = rawid_tuple(rawid) if not rawid: return None wifi_list = wifi_list or () bs_list = bs_list or () if not isinstance(wifi_list, (list, tuple)): wifi_list = (wifi_list,) if not isinstance(bs_list, (list, tuple)): bs_list = (bs_list,) return msgpack.packb( { 'dev_id': rawid, 'user_cmd': user_cmd, 'push': push_pack, 'cmd_ord': token, 'bat': battery, 'charge': charge, 'reason': loc_reason, 'svr_ts': svr_ts, 'dev_state':dev_state, 'steps':steps, 'cur_move_state':cur_move_state, 'loc': { 'gps': gps_dict, 'wifi': wifi_list, 'base': bs_list, }, 'fix_bs_loc': fix_bs_loc }, use_bin_type=True)