Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_3_parse_id1(self):
d = log2data('6000013fa0053c00660b0000338baf1e0000010000000200080012ffffffffff')
db.add_id_to_db(35635, 7855) # код счетчика с паролем из сообщения
p = CounterParser(None)
p.handle_data(d)
self.assertEqual(p.header.bytes, 96)
self.assertEqual(p.header.version, 1)
self.assertEqual(p.header.wake, 1440)
self.assertEqual(p.header.period, 60)
self.assertEqual(p.header.voltage, 2918)
self.assertEqual(p.header.device_id, 35635)
self.assertEqual(p.header.device_pwd, 7855)
self.assertEqual(len(p.header.values), 4)
def device_menu(bot, update, id):
factor = db.get_factor(id)
v1, v2 = db.get_current_value(id)
imp1, imp2 = db.get_impulses(id)
pwd = db.get_pwd(id)
order = db.get_order(id, COLD_HOT)
if order == COLD_HOT:
order_button = InlineKeyboardButton(ARROWS + u' ' + BLUE + u'ХВС ' + RED + u'ГВС', callback_data=u'ГВС ХВС')
else:
order_button = InlineKeyboardButton(ARROWS + u' ' + RED + u'ГВС ' + BLUE + u'ХВС', callback_data=u'ХВС ГВС')
query = update.callback_query
if query:
day = db.get_send_day(query.message.chat_id, id)
else:
day = db.get_send_day(update.message.chat_id, id)
day_button = InlineKeyboardButton(u'Число: ' + unicode(day), callback_data=u'Число')
else:
updater.start_polling()
log.info('start polling')
for sig in (SIGINT, SIGTERM, SIGABRT):
signal(sig, updater.signal_handler)
updater.is_idle = True
#Tcp server
h = CounterParser(updater.bot)
server = TcpServer(args.shost, args.sport, h.handle_data)
server.loop()
log.info("correct exit")
db.close()
chat_list = db.get_chats(d.device_id)
#factor = db.get_factor(d.device_id)
#next_connect = db.get_next_connect(d.device_id)
now = datetime.utcnow()
prev = db.get_connect_time(d.device_id)
db.set_connect_time(d.device_id, now)
sleep_sec = (now - prev).total_seconds()
db.set_sleep_time_sec(d.device_id, sleep_sec)
if d.values:
imp1, imp2, timestamp = d.values[-1]
prev_imp1, prev_imp2 = db.get_impulses(d.device_id)
v1, v2 = db.get_current_value(d.device_id)
db.set_impulses(d.device_id, imp1, imp2)
db.set_voltage(d.device_id, d.voltage)
if not chat_list:
log.error("нет чатов для устройства #" + str(d.device_id))
for chat_id in chat_list:
# Проверим, что счетчик не перезапустился
if imp1 < prev_imp1: #либо ресет, либо 65535
bot.send_message(chat_id=chat_id, text=u"Переполнение счетчика ГВС. Введите тек.значение.")
db.set_start_value1(d.device_id, v1)
log.warning(u"Переполнение ГВС: было %d имп., стало %d. Перезаписана точка старта." % (prev_imp1, imp1))
if imp2 < prev_imp2:
bot.send_message(chat_id=chat_id, text=u"Переполнение счетчика ХВС. Проверьте тек.значение.")
db.set_start_value2(d.device_id, v2)
#next_connect = db.get_next_connect(d.device_id)
now = datetime.utcnow()
prev = db.get_connect_time(d.device_id)
db.set_connect_time(d.device_id, now)
sleep_sec = (now - prev).total_seconds()
db.set_sleep_time_sec(d.device_id, sleep_sec)
if d.values:
imp1, imp2, timestamp = d.values[-1]
prev_imp1, prev_imp2 = db.get_impulses(d.device_id)
v1, v2 = db.get_current_value(d.device_id)
db.set_impulses(d.device_id, imp1, imp2)
db.set_voltage(d.device_id, d.voltage)
if not chat_list:
log.error("нет чатов для устройства #" + str(d.device_id))
for chat_id in chat_list:
# Проверим, что счетчик не перезапустился
if imp1 < prev_imp1: #либо ресет, либо 65535
bot.send_message(chat_id=chat_id, text=u"Переполнение счетчика ГВС. Введите тек.значение.")
db.set_start_value1(d.device_id, v1)
log.warning(u"Переполнение ГВС: было %d имп., стало %d. Перезаписана точка старта." % (prev_imp1, imp1))
if imp2 < prev_imp2:
bot.send_message(chat_id=chat_id, text=u"Переполнение счетчика ХВС. Проверьте тек.значение.")
db.set_start_value2(d.device_id, v2)
log.warning(u"Переполнение ХВС: было %d имп., стало %d. Перезаписана точка старта." % (prev_imp2, imp2))
if 0 == db.get_send_day(chat_id, d.device_id):
def send_message(bot, device_id, chat_id, voltage):
try:
v1, v2 = db.get_current_value(device_id)
text = u'Счетчик №{0}, V={1:.2f}\n'.format(device_id, voltage/1000.0)
text += RED + u'{0:.3f} '.format(v1) + BLUE + u'{0:.3f}'.format(v2)
if bot:
bot.send_message(chat_id=chat_id, text=text, disable_notification=True, timeout=5000)
bot.send_message(chat_id=chat_id, text=db.sms_text(device_id), disable_notification=True, timeout=5000)
except Exception, err:
log.error('send_message id={0}, chat={1}: {2}'.format(device_id, chat_id, err))
v1, v2 = db.get_current_value(id)
imp1, imp2 = db.get_impulses(id)
pwd = db.get_pwd(id)
order = db.get_order(id, COLD_HOT)
if order == COLD_HOT:
order_button = InlineKeyboardButton(ARROWS + u' ' + BLUE + u'ХВС ' + RED + u'ГВС', callback_data=u'ГВС ХВС')
else:
order_button = InlineKeyboardButton(ARROWS + u' ' + RED + u'ГВС ' + BLUE + u'ХВС', callback_data=u'ХВС ГВС')
query = update.callback_query
if query:
day = db.get_send_day(query.message.chat_id, id)
else:
day = db.get_send_day(update.message.chat_id, id)
day_button = InlineKeyboardButton(u'Число: ' + unicode(day), callback_data=u'Число')
keyboard = [
[InlineKeyboardButton(u'%dимп/л' % factor, callback_data=u'Множитель'),
InlineKeyboardButton(RED + u'{0:.3f} '.format(v1) + BLUE + u'{0:.3f}'.format(v2), callback_data=u'Значение')],
[InlineKeyboardButton(u'Текст СМС', callback_data=u'Текст СМС'), order_button, day_button],
[InlineKeyboardButton(u'Удалить', callback_data=u'Удалить'), InlineKeyboardButton(u'Выход', callback_data=u'Выход')]
]
if query:
bot.editMessageText(
message_id=query.message.message_id,
chat_id=query.message.chat_id,
text=u'Счетчик #%s (%s) _ [%d, %d]' % (id, pwd, imp1, imp2),
inline_message_id=query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard))
def getDataFromRCMED( params, settings, options ):
"""
This function takes in the params, settings, and options dictionaries and will return an rcmedData dictionary.
return:
rcmedData = {"lats": 1-d numpy array of latitudes,
"lons": 1-d numpy array of longitudes,
"levels": 1-d numpy array of height/pressure levels (surface based data will have length == 1),
"times": list of python datetime objects,
"data": masked numpy arrays of data values}
"""
rcmedData = {}
obsLats, obsLons, obsLevs, obsTimes, obsData = db.extractData(params['obsDatasetId'],
params['obsParamId'],
params['latMin'],
params['latMax'],
params['lonMin'],
params['lonMax'],
params['startTime'],
params['endTime'],
settings['cacheDir'],
options['timeRegrid'])
rcmedData['lats'] = obsLats
rcmedData['lons'] = obsLons
rcmedData['levels'] = obsLevs
rcmedData['times'] = obsTimes
rcmedData['data'] = obsData
return rcmedData
def getDataFromRCMED( params, settings, options ):
"""
This function takes in the params, settings, and options dictionaries and will return an rcmedData dictionary.
return:
rcmedData = {"lats": 1-d numpy array of latitudes,
"lons": 1-d numpy array of longitudes,
"levels": 1-d numpy array of height/pressure levels (surface based data will have length == 1),
"times": list of python datetime objects,
"data": masked numpy arrays of data values}
"""
rcmedData = {}
obsLats, obsLons, obsLevs, obsTimes, obsData = db.extractData(params['obsDatasetId'],
params['obsParamId'],
params['latMin'],
params['latMax'],
params['lonMin'],
params['lonMax'],
params['startTime'],
params['endTime'],
settings['cacheDir'],
options['timeRegrid'])
rcmedData['lats'] = obsLats
rcmedData['lons'] = obsLons
rcmedData['levels'] = obsLevs
rcmedData['times'] = obsTimes
rcmedData['data'] = obsData
return rcmedData