offline_coins.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import datetime
  4. import logging
  5. import time
  6. import simplejson as json
  7. from apps.web.core.accounting import Accounting
  8. from apps.web.device.timescale import FluentedEngine
  9. from script.eventer.handlers import Handler
  10. logger = logging.getLogger(__name__)
  11. class OfflineCoinsHandler(Handler):
  12. def parse(self):
  13. return json.loads(bytes.decode(self.payload))
  14. def do(self):
  15. rv = self.parse()
  16. if self.dev.devNo != rv.get('IMEI', None):
  17. logger.warning('invalid msg for not same devNo({} != {})'.format(self.dev.devNo, rv.get('IMEI')))
  18. return
  19. if self.cmd != str(rv.get('cmd', '')):
  20. logger.warning('invalid msg for not same cmd({} != {})'.format(self.cmd, rv.get('cmd', None)))
  21. return
  22. if 'timestamp' in rv:
  23. ts = rv['timestamp']
  24. elif 'ts' in rv:
  25. ts = rv['ts']
  26. else:
  27. ts = int(time.time())
  28. if 'coin' in rv:
  29. coins = int(rv['coin'])
  30. elif 'coins' in rv:
  31. coins = int(rv['coins'])
  32. else:
  33. coins = 0
  34. if 'today_coins' in rv:
  35. Accounting.syncOfflineCoin(self.dev,
  36. datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d'),
  37. int(rv['today_coins']))
  38. if coins > 0:
  39. FluentedEngine().in_put_coins_udp(devNo = self.dev.devNo,
  40. ts = ts,
  41. coins = coins,
  42. mode = 'pulse')
  43. else:
  44. Accounting.recordOfflineCoin(self.dev, ts, coins, mode = 'pulse')