1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import logging
- import time
- import simplejson as json
- from apps.web.core.accounting import Accounting
- from apps.web.device.timescale import FluentedEngine
- from script.eventer.handlers import Handler
- logger = logging.getLogger(__name__)
- class OfflineCoinsHandler(Handler):
- def parse(self):
- return json.loads(bytes.decode(self.payload))
- def do(self):
- rv = self.parse()
- if self.dev.devNo != rv.get('IMEI', None):
- logger.warning('invalid msg for not same devNo({} != {})'.format(self.dev.devNo, rv.get('IMEI')))
- return
- if self.cmd != str(rv.get('cmd', '')):
- logger.warning('invalid msg for not same cmd({} != {})'.format(self.cmd, rv.get('cmd', None)))
- return
- if 'timestamp' in rv:
- ts = rv['timestamp']
- elif 'ts' in rv:
- ts = rv['ts']
- else:
- ts = int(time.time())
- if 'coin' in rv:
- coins = int(rv['coin'])
- elif 'coins' in rv:
- coins = int(rv['coins'])
- else:
- coins = 0
- if 'today_coins' in rv:
- Accounting.syncOfflineCoin(self.dev,
- datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d'),
- int(rv['today_coins']))
- if coins > 0:
- FluentedEngine().in_put_coins_udp(devNo = self.dev.devNo,
- ts = ts,
- coins = coins,
- mode = 'pulse')
- else:
- Accounting.recordOfflineCoin(self.dev, ts, coins, mode = 'pulse')
|