record_pic.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:record_pic.py 处理摄像头的图片数据
  6. author:rabin
  7. key:pic/图片名_图片总数_当前序号/gateway/device
  8. """
  9. import time
  10. import json
  11. import uuid
  12. from datetime import *
  13. from demeter import *
  14. class Record_pic(object):
  15. pic = {}
  16. def handle(self, config, value):
  17. method = config['type'].split('_')
  18. name = method[0] + '.jpg'
  19. if name not in self.pic:
  20. self.pic[name] = {}
  21. total = int(method[1])
  22. index = int(method[2])
  23. self.pic[name][index] = value
  24. picLen = len(self.pic[name])
  25. if picLen >= total:
  26. self.create(name)
  27. def create(self, name):
  28. pic = sorted(self.pic[name].items(), key=lambda e:e[0], reverse=False)
  29. data = []
  30. day = str(date.today())
  31. day = day.split('-')
  32. file_name = str(uuid.uuid5(uuid.uuid1(), 'file'))
  33. file_path = day[0] + '/' + day[1] + '/' + day[2]
  34. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  35. for value in pic:
  36. data.append(value[1])
  37. with open(file_path, 'wb') as f:
  38. f.writelines(data)
  39. del self.pic[name]
  40. self.write(file_path)
  41. def write(self, file):
  42. return