主文件:picTime 定时启动任务
#!usr/bin/evn python
#coding=utf-8 #支持中文注释
from datetime import date, time, datetime, timedelta
import StrToImageBase64
import cmdPic
import redis
import Fileout
import time
def work():
strTimes=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
print(strTimes)
cmdPic.Excute(strTimes)
time.sleep(50)
Fileout.ReadPic("Sleep 50s")
r=redis.Redis(host='192.168.20.153',port=6379)
print(strTimes+'_130000000000_11')
ccstr=r.get(strTimes+'_130000000000_11')
Fileout.ReadPic("Read key"+strTimes+'_130000000000_11')
isNones=ccstr
if isNones==None:
Fileout.ReadPic("Pictrue KeyAndVlaue not Exist")
else:
if b'picData' in ccstr:
StrToImageBase64.str_to_image(ccstr, './image/'+strTimes+'.jpeg')
Fileout.ReadPic("Pictrue sussful")
elif b'order' in ccstr:
Fileout.ReadPic("Pictrue save fail")
else:
Fileout.ReadPic("fails")
def runTask(func, day=0, hour=0, min=0, second=0):
# Init time
now = datetime.now()
strnow = now.strftime('%Y-%m-%d %H:%M:%S')
#print("now:",strnow)
Fileout.ReadPic("now:"+strnow)
# First next run time
period = timedelta(days=day, hours=hour, minutes=min, seconds=second)
next_time = now + period
strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S')
#print("next run:",strnext_time)
Fileout.ReadPic("next run:"+strnext_time)
while True:
# Get system current time
iter_now = datetime.now()
iter_now_time = iter_now.strftime('%Y-%m-%d %H:%M:%S')
if str(iter_now_time) == str(strnext_time):
# Get every start work time
#print ("start work: %s" % iter_now_time)
Fileout.ReadPic("start work: %s" %iter_now_time)
# Call task func
func()
#print ("task done.")
Fileout.ReadPic("task done.")
# Get next iteration time
iter_time = iter_now + period
strnext_time = iter_time.strftime('%Y-%m-%d %H:%M:%S')
#print ("next_iter: %s" % strnext_time)
Fileout.ReadPic("next_iter: %s" %strnext_time)
# Continue next iteration
continuework()
runTask(work,min=2)
连接redis 执行命令 cmdPic.py
#!usr/bin/evn python
#coding=utf-8
import redis
import json
import base64
import time
import Fileout
def Excute(strTime):
#连接
r=redis.Redis(host='192.168.20.153',port=6379)
#写
Fileout.ReadPic(r)
data={"order":"11","camera":[{"id":"130000000000","position":["11","12"]}]};
sdata=json.dumps(data)
str=r.set('orders_001_'+strTime,sdata)
print(str)
Fileout.ReadPic('Set key'+'orders_001_'+strTime)
temp=r.get('orders_001_'+strTime)#decode()解决Python3输出带b
Fileout.ReadPic('Get keyValue'+'orders_001_'+strTime)
#将读取的字符串转换为图片 StrToImageBase64.py
#!usr/bin/evn python
#coding=utf-8
#Author DangKai
import base64
import json
#将数据转换为json在转换为相片
#class Image:
# def __init__(self,name,cstr)
##self.name=name
#self.cstr=cstr
#pass
def str_to_image(cstr,name):
print(cstr)
str_to_json=json.loads(cstr)
print(str_to_json)
data=str_to_json['picData']
#print(data)
picData=base64.b64decode(data)
picDatas=open(name,'wb')
picDatas.write(picData)
picDatas.close()
def image_to_str(ImageName):
imageFile=open(ImageName,"rb")
str_to_base64=base64.b64encode(imageFile.read())
data=str_to_base64.decode()
#print(data)
sdata={'picData':data}
#in_json=json.dumps(sdata)#转化为json存储
#return in_json
return sdata
Python 定时任务
点赞
收藏