def decodeClockBytes(data, isStatusResponse=False): if isinstance(data, str): data = utils.str_2_bytes(data) if len(data) != 6: raise Exception("clock data should be 6 bytes long") secs = data[0] mins = data[1] hours = data[2] * 2 hours += mins / 60 mins = mins % 60 yearday = data[3] yearday += (data[4] & 0x80) << 1 daymask = data[4] & 0x7F timeStr = "%(yearday)d %(hours)d %(mins)d %(secs)d" % locals() theTime = datetime.datetime.strptime(timeStr, "%j %H %M %S") housecode = VALUE_TO_HOUSECODE_MAP[data[5] >> 4] timerPurge = None clearBatteryTimer = None clearMonitoredStatus = None if not isStatusResponse: timerPurge = (data[5] & 4) != 0 clearBatteryTimer = (data[5] & 2) != 0 clearMonitoredStatus = (data[5] & 1) != 0 return (housecode, theTime, daymask, timerPurge, clearBatteryTimer, clearMonitoredStatus)
def put_item(item_json): """ 消费者爬取结果的MD5散列值存放,用于去重,暂未使用,直接用了MongoDB的upsert特性 :param item_json: 爬取结果json dump结果 """ md5_val = md5(str_2_bytes(item_json)).hexdigest() return DB.redis.sadd(RedisKey.RESULT_SET, md5_val) == 1
def put_hosts(hosts): """ 把生产者扫描到的IP地址存放到Redis的未爬取Key中 :param hosts: 扫描到的可连通IP """ pipe = DB.redis.pipeline() for host in hosts: pipe.rpush(RedisKey.UNFEACH_IP, str_2_bytes(host)) pipe.execute()
def decodeDawnDusk(data): if isinstance(data, str): data = utils.str_2_bytes(data) expectedLength = 3 if len(data) != expectedLength: raise Exception("dawn/dusk data should be %(expectedLength)d bytes long" % locals()) stopHour = 2 * (data[0] >> 4) startHour = 2 * (data[0] & 0xF) stopMins = data[1] & 0x7F startMins = data[2] & 0x7F return (datetime.time(startHour + (startMins/60), startMins % 60), datetime.time(stopHour + (stopMins/60), stopMins % 60))
def decodeStatusResponse(data): if isinstance(data, str): data = utils.str_2_bytes(data) expectedLength = 14 if len(data) != expectedLength: raise Exception("status data should be %(expectedLength)d bytes long" % locals()) batteryTimer = data[0] * 0x100 + data[1] clock = decodeClockBytes(data[2:8], True) houseCode = clock[0] deviceTime = clock[1] daymask = clock[2] firmwareRevision = data[7] & 0xFF monitoredDevices = data[8] * 0x100 + data[9] monitoredDevicesOnMask = data[10] * 0x100 + data[11] monitoredDevicesDims = data[12] * 0x100 + data[13] return (houseCode, deviceTime, daymask, firmwareRevision, monitoredDevices, monitoredDevicesOnMask, monitoredDevicesDims)
def put_range(start, end, length): """ 把生成的未扫描IP地址批量推到Redis :param start: 开始IP地址前三段 :param end: 结束IP地址前三段 :param length: 生成IP地址个数 """ start_str = '0.' + ip_2_str(start[:3]) end_str = '0.' + ip_2_str(end[:3]) if start_str == end_str: range_strs = [start_str] else: range_strs = UnScanDAO.ip_range(start_str, end_str, length) if len(range_strs) == 0: return ip3_range_list = [str(ip_str).lstrip('0.') for ip_str in range_strs] pipe = DB.redis.pipeline() for ip3_str in ip3_range_list: pipe.rpush(RedisKey.UNSCAN_IP, str_2_bytes(ip3_str)) pipe.execute()