def post_photo(request): response = device_response() device = check_device(request) if device is None: response.write("UNKNOWN DEVICE") return response try: pin = request.REQUEST.get("PIN", "") pin = pin.split(".")[0].split("-") dt = pin[0] if len(pin) == 2: #Success Picture pin = pin[1] else: pin = "" d = request.raw_post_data if "CMD=uploadphoto" in d: d = d.split("CMD=uploadphoto")[1][1:] if "CMD=realupload" in d: d = d.split("CMD=realupload")[1][1:] save_model_file( Transaction, "%s/%s/%s" % (device.sn, dt[:4], dt[4:8]) + "/" + pin + "_" + dt[8:] + ".jpg", d, "picture") if request.REQUEST.has_key('PhotoStamp'): DevLog(SN=device, Cnt=1, OP=u"PICTURE", Object=pin, OpTime=datetime.datetime.now()).save() device.photo_stamp = request.REQUEST['PhotoStamp'] device.save() check_and_save_cache(device) except: errorLog(request) response.write("OK\n") return response
def cdata(request): ''' 设备向服务器的http:://xxx/cdata请求 ''' network_monitor(request) try: from mysite import authorize_fun language=request.REQUEST.get('language',None) #---获得语言参数 authorize_fun.check_push_device(language) #---连接设备数量控制 device = check_device(request, True) #---检测、验证当前发出请求的设备 if device is None: return unknown_device_response() else: if device.deviceType==POSDEVICE:#消费机 from mysite.pos.posdevview import pos_cdata ret = pos_cdata(request) return device_response_write(ret) elif device.deviceType in ['ACP', 'ACD']:#控制器or一体机: from mysite.iaccess.push_comm_center import acc_cdata ret = acc_cdata(request) return device_response_write(ret) if request.REQUEST.has_key('action'): return ok_response(device) elif request.method == 'GET': return cdata_get(request,device) elif request.method == 'POST': try: return cdata_post(request, device) # resp =cdata_post(request, device) # return device_response_write(resp) except Exception, e: return erro_response(e) else:
def post_photo(request): response = device_response() device = check_device(request) if device is None: response.write("UNKNOWN DEVICE") return response try: pin = request.REQUEST.get("PIN","") pin = pin.split(".")[0].split("-") dt = pin[0] if len(pin) == 2: #Success Picture pin = pin[1] else: pin = "" d = request.raw_post_data if "CMD=uploadphoto" in d: d = d.split("CMD=uploadphoto")[1][1:] if "CMD=realupload" in d: d = d.split("CMD=realupload")[1][1:] save_model_file(Transaction, "%s/%s/%s" % (device.sn, dt[:4], dt[4:8])+"/"+ pin+"_"+ dt[8:] + ".jpg", d, "picture") if request.REQUEST.has_key('PhotoStamp'): DevLog(SN=device, Cnt=1, OP=u"PICTURE", Object=pin, OpTime=datetime.datetime.now()).save() device.photo_stamp = request.REQUEST['PhotoStamp'] device.save() check_and_save_cache(device) except: errorLog(request) response.write("OK\n") return response
def device_http_request(request): initialization() device = check_device(request, True) #---检测、验证当前发出请求的设备 if device is None: response.write("UNKNOWN DEVICE") return response else: pass
def devpost(request): ''' http://xxx/devicecmd 设备返回设备命令执行结果的请求 ''' from commen_utils import parse_posts network_monitor(request) device = check_device(request) #---检测、验证当前发出请求的设备 if device is None: return unknown_device_response() else: if request.method == 'GET': pass elif request.method == 'POST': try: rd = request.raw_post_data if settings.ENCRYPT: #---解密 try: import lzo rawData = lzo.bufferDecrypt(rd, device.sn) except: rawData = rd else: rawData = rd try: data0 = rawData.decode("gb18030") #---解码 except: data0 = rawData rets = {} pdata = parse_posts(data0) #---解析,结果为字典的数组 for apost in pdata: #[CMD,ID,Return] id = int(apost["ID"]) # 命令ID ret = apost["Return"] #命令执行返回值 if apost["CMD"] == "INFO":#更新设备信息 parse_dev_info(device, apost['Content']) device.save() rets[id] = ret elif (apost["CMD"] == "GetFile" or apost["CMD"] == "Shell") and ret > 0: check_upload_file(request, apost) rets[id] = ret elif apost["CMD"] == "VERIFY SUM" and ret > 0: #考勤校对 check_att_sum(apost,device) rets[id] = ret elif apost["CMD"] == "PutFile": check_upgradefile(id,apost,device,ret) post_check_update(device,[ret]) #立即单独更新此处理的开始指针 #rets[id] = ret else:#默认情况下CMD为DATA rets[id] = ret if len(rets) > 0: update_cmds(device, rets) return device_response_write("OK") except: device_response_write("") else: return unknown_data_response(device)
def devpost(request): ''' 设备返回命令执行结果的请求 ''' response = device_response() resp = "" device = check_device(request) if device is None: response.write("UNKNOWN DEVICE") return response try: rd = request.raw_post_data if settings.ENCRYPT: #---解密 try: import lzo rawData = lzo.bufferDecrypt(rd, device.sn) except: rawData = rd else: rawData = rd try: data0 = rawData.decode("gb18030") #---解码 except: data0 = rawData rets = {} pdata = parse_posts(data0) #---解析,结果为字典的数组 for apost in pdata: id = int(apost["ID"]) #---【字段】命令ID ret = apost["Return"] #---【字段】命令执行结果 if apost["CMD"] == "INFO": #--- 【字段】命令类别 parse_dev_info(device, apost['Content']) rets[id] = ret elif apost['CMD'] == 'PutFile' and ret > 100 * 1024: #可能是固件升级命令 cmdobj = update_cmd(device, id, ret) if cmdobj: check_upgrade_fw(device, cmdobj, request) elif (apost["CMD"] == "GetFile" or apost["CMD"] == "Shell") and ret > 0: check_upload_file(request, apost) rets[id] = ret else: rets[id] = ret if len(rets) > 0: update_cmds(device, rets) resp += "OK" #check_and_save_cache(device) device.save() except: errorLog(request) response["Content-Length"] = len(resp) response.write(resp) return response
def devpost(request): ''' 设备返回命令执行结果的请求 ''' response = device_response() resp = "" device = check_device(request) if device is None: response.write("UNKNOWN DEVICE") return response try: rd = request.raw_post_data if settings.ENCRYPT: #---解密 try: import lzo rawData = lzo.bufferDecrypt(rd, device.sn) except: rawData = rd else: rawData = rd try: data0 = rawData.decode("gb18030") #---解码 except: data0 = rawData rets = {} pdata = parse_posts(data0) #---解析,结果为字典的数组 for apost in pdata: id = int(apost["ID"]) #---【字段】命令ID ret = apost["Return"] #---【字段】命令执行结果 if apost["CMD"] == "INFO":#--- 【字段】命令类别 parse_dev_info(device, apost['Content']) rets[id] = ret elif apost['CMD'] == 'PutFile' and ret > 100 * 1024: #可能是固件升级命令 cmdobj = update_cmd(device, id, ret) if cmdobj: check_upgrade_fw(device, cmdobj, request) elif (apost["CMD"] == "GetFile" or apost["CMD"] == "Shell") and ret > 0: check_upload_file(request, apost) rets[id] = ret else: rets[id] = ret if len(rets) > 0: update_cmds(device, rets) resp += "OK" #check_and_save_cache(device) device.save() except: errorLog(request) response["Content-Length"] = len(resp) response.write(resp) return response
def cdata(request): ''' 设备向服务器的http:://xxx/cdata请求 ''' from device_http.network import network_monitor network_monitor(request) encrypt = 1 response = device_response() try: resp = "" from mysite import authorize_fun language = request.REQUEST.get('language', None) #---获得语言参数 authorize_fun.check_push_device(language) #---连接设备数量控制 device = check_device(request, True) #---检测、验证当前发出请求的设备 if device is None: response.write("UNKNOWN DEVICE") return response else: if request.REQUEST.has_key('action'): resp += "OK\n" elif request.method == 'GET': #---设备GET请求 if request.REQUEST.has_key('PIN'): #---带人员PIN参数 resp += cdata_get_pin(request, device) else: #--- 设备push的版本及其他一些信息的返回 alg_ver = "1.0" #--- push 默认版本 if request.REQUEST.has_key( 'pushver'): #---没有"PIN"但有"pushver" push版本参数 alg_ver = request.REQUEST.get( 'pushver' ) #2010-8-25 device字段alg_ver用来区分新老固件 >=2.0为新固件,默认为旧固件 device.alg_ver = alg_ver device.save() resp += cdata_get_options(device) encrypt = 0 elif request.method == 'POST': #---设备POST请求 try: resp += cdata_post(request, device) except Exception, e: resp = u"ERROR: %s" % e errorLog(request) else:
def cdata(request): ''' 设备向服务器的http:://xxx/cdata请求 ''' from device_http.network import network_monitor network_monitor(request) encrypt = 1 response = device_response() try: resp = "" from mysite import authorize_fun language=request.REQUEST.get('language',None) #---获得语言参数 authorize_fun.check_push_device(language) #---连接设备数量控制 device = check_device(request, True) #---检测、验证当前发出请求的设备 if device is None: response.write("UNKNOWN DEVICE") return response else: if request.REQUEST.has_key('action'): resp += "OK\n" elif request.method == 'GET': #---设备GET请求 if request.REQUEST.has_key('PIN'): #---带人员PIN参数 resp+=cdata_get_pin(request, device) else: #--- 设备push的版本及其他一些信息的返回 alg_ver="1.0" #--- push 默认版本 if request.REQUEST.has_key('pushver'): #---没有"PIN"但有"pushver" push版本参数 alg_ver=request.REQUEST.get('pushver') #2010-8-25 device字段alg_ver用来区分新老固件 >=2.0为新固件,默认为旧固件 device.alg_ver=alg_ver device.save() resp+=cdata_get_options(device) encrypt = 0 elif request.method == 'POST': #---设备POST请求 try: resp+=cdata_post(request, device) except Exception, e: resp = u"ERROR: %s" % e errorLog(request) else:
def getreq(request): ''' http://xxx/getrequest 设备读取服务器上存储的命令 ''' network_monitor(request) try: resp = "" #---要直接发送的内容 device = check_device(request, True) #---检测、验证当前发出请求的设备 if device is None: return unknown_device_response() else: if request.method == 'GET': return getreq_get(request,device) elif request.method == 'POST': pass else: return unknown_data_response(device) # 自动升级固件功能 # ...... except Exception, e: import traceback;traceback.print_exc() resp = u"%s" % e return device_response_write(resp)
def getreq(request): ''' http://xxx/getrequest 设备读取服务器上存储的命令------------------------------------------------------------------ ''' from device_http.network import network_monitor network_monitor(request) response = device_response() try: resp = "" #---要直接发送的内容 device = check_device(request) #---从请求得到设备对象 不自动注册 if device is None: response.write("UNKNOWN DEVICE") return response #读取服务器上存储的命令的请求都带有设备INFO信息 更新设备信息 info = request.GET.get("INFO", "") #版本号,用户数,指纹数,记录数,设备自身IP地址 if info: sql = [] info = info.split(",") device.fw_version = info[0] #---主版本号 device.user_count = int(info[1]) #---注册用户数 device.fp_count = int(info[2]) #---注册指纹数 device.fp_count = int(info[2]) device.transaction_count = int(info[3]) #---考勤记录数 if len(info) > 4: device.ipaddress = info[4] #---考勤机IP地址 if device.alias == "auto_add": device.alias = info[4] #由于网关问题,使名称对应的IP地址与机器IP不同时的更正。 if len(info) > 5: device.Fpversion = info[5] #指纹算法版本 device.save() # 自动升级固件功能 if not hasattr(device, "is_updating_fw"): #该设备现在没有正升级固件 fw = fwVerStd(device.fw_version) if fw: #该设备具有固件版本号 up_version = device.get_std_fw_version() #用于升级的设备固件标准版本号 if up_version > fw: #该设备固件版本号较低 n = int(q_server.get_from_file("UPGRADE_FW_COUNT") or "0") if n < settings.MAX_UPDATE_COUNT: #没有超出许可同时升级固件的范围 #升级固件 errMsg = dev_update_firmware(device) if not errMsg: device.is_updating_fw = device.last_activity if errMsg: #升级命令错 appendFile((u"%s UPGRADE FW %s:%s" % (device.sn, fw, errMsg))) else: q_server.incr("UPGRADE_FW_COUNT") upsql = [] c = 0 const_sql = "update devcmds set CmdTransTime='%(tr)s',CmdReturn=%(cm)s where id=%(id)s" if db_select == 4: #postgresql 数据库 const_sql = '''update devcmds set "CmdTransTime"='%(tr)s',"CmdReturn"=%(cm)s where id=%(id)s''' elif db_select == 3: #oracle 数据库 const_sql = "update devcmds set CmdTransTime=to_date('%(tr)s','yyyy-mm-dd hh24:mi:ss'),CmdReturn=%(cm)s where id=%(id)s" maxRet = device.max_comm_count #---每次传送给设备的命令数 maxRetSize = device.max_comm_size * 1024 #---最大数据包长度(KB) get_sql = "select top " + str( maxRet) + " id,CmdContent,CmdReturn from devcmds " get_sql += " where SN_id=" + str( device.pk ) + " and (CmdTransTime is null or (CmdReturn <=-99996 and CmdReturn>-99999)) order by id " if db_select == 1: get_sql = "select id,CmdContent,CmdReturn from devcmds " get_sql += " where SN_id=" + str( device.pk ) + " and (CmdTransTime is null or (CmdReturn <=-99996 and CmdReturn>-99999)) order by id limit " + str( maxRet) elif db_select == 4: get_sql = '''select id,"CmdContent","CmdReturn" from devcmds ''' get_sql += ''' where "SN_id"=''' + str( device.pk ) + ''' and ("CmdTransTime" is null or ("CmdReturn" <=-99996 and "CmdReturn">-99999) ) order by id limit ''' + str( maxRet) elif db_select == 3: get_sql = "select id,CmdContent,CmdReturn from devcmds " get_sql += " where SN_id=" + str( device.pk ) + " and (CmdTransTime is null or CmdReturn <=-99996 and CmdReturn>-99999)) and ROWNUM <= " + str( maxRet) + " ORDER BY ROWNUM ASC " dev_cur = conn.cursor() dev_cur.execute(get_sql) devcmds = dev_cur.fetchall() connection._commit() for d in devcmds: #---循环要发送给设备的命令 cr = d[2] #---命令返回值 if cr: cr += -1 else: cr = -99996 if cr < -99999: continue if db_select == 3: CmdContent = d[1].read() else: CmdContent = d[1] if CmdContent.find("DATA UPDATE user") == 0 or CmdContent.find( "SMS ") == 0: #传送用户命令,需要解码成GB2312 cc = CmdContent try: cc = cc.encode("gb18030") except: try: cc = cc.decode("utf-8").encode("gb18030") except: errorLog(request) else: cc = str(CmdContent) nowcmd = str(cc) cc = std_cmd_convert(cc, device) #----ZK-ECO 标准命令到 PUSH-SDK 命令的转换 if cc: resp += "C:%d:%s\n" % (d[0], cc) #---格式: C:设备序列号:内容 \n c = c + 1 if db_select == 1: excsql("update devcmds set CmdTransTime= now() ,CmdReturn=" + str(cr) + " where id=" + str(d[0])) elif db_select == 3: excsql( const_sql % { "tr": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "cm": cr, "id": d[0] }) else: upsql.append( const_sql % { "tr": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "cm": cr, "id": d[0] }) if (c >= maxRet) or (len(resp) >= maxRetSize): break #达到了最大命令数或最大命令长度限制 if CmdContent in ["CHECK", "CLEAR DATA", "REBOOT", "RESTART"]: break #重新启动命令只能是最后一条指令 #增加查找到CHECK指令后,直接发送 if upsql: excsql(";".join(upsql)) if db_select == 1: excsql("update iclock set last_activity=now() where id=" + str(device.pk)) elif db_select == 3: excsql("update iclock set last_activity=to_date('" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "','yyyy-mm-dd hh24:mi:ss') where id=" + str(device.pk)) else: excsql("update iclock set last_activity='" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "' where id=" + str(device.pk)) if c == 0: #没有发送任何命令时,简单向设备返回 "OK" 即可 resp += "OK" except Exception, e: resp = u"%s" % e errorLog(request)
def getreq(request): ''' http://xxx/getrequest 设备读取服务器上存储的命令------------------------------------------------------------------ ''' from device_http.network import network_monitor network_monitor(request) response = device_response() try: resp = "" #---要直接发送的内容 device = check_device(request) #---从请求得到设备对象 不自动注册 if device is None: response.write("UNKNOWN DEVICE") return response #读取服务器上存储的命令的请求都带有设备INFO信息 更新设备信息 info = request.GET.get("INFO", "") #版本号,用户数,指纹数,记录数,设备自身IP地址 if info: sql=[] info = info.split(",") device.fw_version=info[0] #---主版本号 device.user_count=int(info[1]) #---注册用户数 device.fp_count=int(info[2]) #---注册指纹数 device.fp_count=int(info[2]) device.transaction_count=int(info[3]) #---考勤记录数 if len(info)>4: device.ipaddress=info[4] #---考勤机IP地址 if device.alias=="auto_add": device.alias=info[4]#由于网关问题,使名称对应的IP地址与机器IP不同时的更正。 if len(info)>5: device.Fpversion=info[5] #指纹算法版本 device.save() # 自动升级固件功能 if not hasattr(device, "is_updating_fw"): #该设备现在没有正升级固件 fw = fwVerStd(device.fw_version) if fw: #该设备具有固件版本号 up_version=device.get_std_fw_version() #用于升级的设备固件标准版本号 if up_version>fw: #该设备固件版本号较低 n=int(q_server.get_from_file("UPGRADE_FW_COUNT") or "0") if n < settings.MAX_UPDATE_COUNT: #没有超出许可同时升级固件的范围 #升级固件 errMsg = dev_update_firmware(device) if not errMsg: device.is_updating_fw=device.last_activity if errMsg: #升级命令错 appendFile((u"%s UPGRADE FW %s:%s" % (device.sn, fw, errMsg))) else: q_server.incr("UPGRADE_FW_COUNT") upsql=[] c=0 const_sql="update devcmds set CmdTransTime='%(tr)s',CmdReturn=%(cm)s where id=%(id)s" if db_select==4:#postgresql 数据库 const_sql ='''update devcmds set "CmdTransTime"='%(tr)s',"CmdReturn"=%(cm)s where id=%(id)s''' elif db_select==3:#oracle 数据库 const_sql="update devcmds set CmdTransTime=to_date('%(tr)s','yyyy-mm-dd hh24:mi:ss'),CmdReturn=%(cm)s where id=%(id)s" maxRet = device.max_comm_count #---每次传送给设备的命令数 maxRetSize = device.max_comm_size * 1024 #---最大数据包长度(KB) get_sql="select top "+ str(maxRet) +" id,CmdContent,CmdReturn from devcmds " get_sql+=" where SN_id="+str(device.pk)+" and (CmdTransTime is null or (CmdReturn <=-99996 and CmdReturn>-99999)) order by id " if db_select==1: get_sql="select id,CmdContent,CmdReturn from devcmds " get_sql+=" where SN_id="+str(device.pk)+" and (CmdTransTime is null or (CmdReturn <=-99996 and CmdReturn>-99999)) order by id limit "+str(maxRet) elif db_select==4: get_sql ='''select id,"CmdContent","CmdReturn" from devcmds ''' get_sql+=''' where "SN_id"='''+str(device.pk)+''' and ("CmdTransTime" is null or ("CmdReturn" <=-99996 and "CmdReturn">-99999) ) order by id limit '''+str(maxRet) elif db_select==3: get_sql="select id,CmdContent,CmdReturn from devcmds " get_sql+=" where SN_id="+str(device.pk)+" and (CmdTransTime is null or CmdReturn <=-99996 and CmdReturn>-99999)) and ROWNUM <= "+str(maxRet)+" ORDER BY ROWNUM ASC " dev_cur=conn.cursor() dev_cur.execute(get_sql) devcmds=dev_cur.fetchall() connection._commit() for d in devcmds: #---循环要发送给设备的命令 cr=d[2] #---命令返回值 if cr: cr+=-1 else: cr=-99996 if cr<-99999: continue if db_select==3: CmdContent=d[1].read() else: CmdContent=d[1] if CmdContent.find("DATA UPDATE user")==0 or CmdContent.find("SMS ")==0: #传送用户命令,需要解码成GB2312 cc=CmdContent try: cc=cc.encode("gb18030") except: try: cc=cc.decode("utf-8").encode("gb18030") except: errorLog(request) else: cc=str(CmdContent) nowcmd=str(cc) cc=std_cmd_convert(cc, device) #----ZK-ECO 标准命令到 PUSH-SDK 命令的转换 if cc: resp+="C:%d:%s\n"%(d[0],cc) #---格式: C:设备序列号:内容 \n c=c+1 if db_select==1: excsql("update devcmds set CmdTransTime= now() ,CmdReturn="+str(cr)+" where id="+str(d[0])) elif db_select==3: excsql(const_sql%{"tr":datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),"cm":cr,"id":d[0]}) else: upsql.append(const_sql%{"tr":datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),"cm":cr,"id":d[0]}) if (c>=maxRet) or (len(resp)>=maxRetSize): break; #达到了最大命令数或最大命令长度限制 if CmdContent in ["CHECK","CLEAR DATA","REBOOT", "RESTART"]: break; #重新启动命令只能是最后一条指令 #增加查找到CHECK指令后,直接发送 if upsql: excsql(";".join(upsql)) if db_select==1: excsql("update iclock set last_activity=now() where id="+str(device.pk)) elif db_select==3: excsql("update iclock set last_activity=to_date('"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"','yyyy-mm-dd hh24:mi:ss') where id="+str(device.pk)) else: excsql("update iclock set last_activity='"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"' where id="+str(device.pk)) if c == 0:#没有发送任何命令时,简单向设备返回 "OK" 即可 resp += "OK" except Exception, e: resp = u"%s" % e errorLog(request)