11# -*- coding: utf-8 -*-
2+ from __future__ import print_function
23import requests
3- import datetime
4- import argparse
4+ from datetime import datetime , tzinfo
55from copy import deepcopy
66
77
@@ -18,14 +18,20 @@ def check_error(answer):
1818 :return:
1919 """
2020 if answer ['errorCode' ] != 0 :
21- raise Exception (u 'Error code: {0}, message: {1}' .format (answer ['errorCode' ], answer ['errorMessage' ]))
21+ raise Exception ('Error code: {0}, message: {1}' .format (answer ['errorCode' ], answer ['errorMessage' ]))
2222
2323
24+ COLD_WATER = 1
25+ HOT_WATER = 2
26+
2427counter_types = {
25- 1 : u 'ХВС' ,
26- 2 : u 'ГВС'
28+ COLD_WATER : 'ХВС' ,
29+ HOT_WATER : 'ГВС'
2730}
2831
32+ COLD_WATER_TITLE_RUS = counter_types [COLD_WATER ]
33+ HOT_WATER_TITLE_RUS = counter_types [HOT_WATER ]
34+
2935
3036class MosAPI (object ):
3137
@@ -110,7 +116,7 @@ def get_profile(self):
110116 """
111117 :return: JSON
112118 {
113- u'profile': {
119+ u'profile': {
114120 u'drive_license': None,
115121 u'firstname': u'x',
116122 u'middlename': u'x',
@@ -260,7 +266,7 @@ def send_watercounters(self, flat_id, counters_data):
260266 :param counters_data: array of
261267 [{
262268 'counter_id': u'123456', # ['counters'][0]['counterId']
263- 'period': datetime.datetime. now().strftime("%Y-%m-%d"),
269+ 'period': datetime.now().strftime("%Y-%m-%d"),
264270 'indication': 'xxx,xx'
265271 }, {
266272 ...
@@ -314,28 +320,166 @@ def logout(self):
314320 return response ['result' ]
315321
316322
317- def get_watercounters_id (water_type , response ):
323+ def get_profile_firstname (profile_json ):
324+ return profile_json ['firstname' ]
325+
326+
327+ def get_profile_middlename (profile_json ):
328+ return profile_json ['middlename' ]
329+
330+
331+ def get_profile_lastname (profile_json ):
332+ return profile_json ['lastname' ]
333+
334+
335+ def get_profile_birthdate (profile_json ):
336+ return profile_json ['birthdate' ]
337+
338+
339+ def get_profile_msisdn (profile_json ):
340+ return profile_json ['msisdn' ]
341+
342+
343+ def get_profile_email (profile_json ):
344+ return profile_json ['email' ]
345+
346+
347+ def get_flat_id (flat_json ):
348+ """
349+ :param flat_json answer for api.get_flats()
350+ :return:
351+ """
352+ return flat_json ['flat_id' ]
353+
354+
355+ def get_flat_name (flat_json ):
356+ """
357+ :param flat_json answer for api.get_flats()
358+ :return:
359+ """
360+ return flat_json ['name' ]
361+
362+
363+ def get_flat_address (flat_json ):
364+ """
365+ :param flat_json answer for api.get_flats()
366+ :return:
367+ """
368+ return flat_json ['address' ]
369+
370+
371+ def get_flat_number (flat_json ):
372+ """
373+ :param flat_json answer for api.get_flats()
374+ :return:
375+ """
376+ return flat_json ['flat_number' ]
377+
378+
379+ def get_flat_paycode (flat_json ):
380+ """
381+ :param flat_json answer for api.get_flats()
382+ :return:
318383 """
319- :param water_type: ГВС, ХВС
384+ return flat_json ['paycode' ]
385+
386+
387+ def get_watercounters_id (water_type_id , response ):
388+ """
389+ :param water_type_id: COLD_WATER, HOT_WATER
320390 :param response: get_watercounters() response
321391 :return: array of int or NULL
322392 """
323- counters = filter (lambda x : counter_types [ x ['type' ]] == water_type , response ['counters' ])
393+ counters = filter (lambda x : x ['type' ] == water_type_id , response ['counters' ])
324394 if counters :
325395 return list (c ['counterId' ] for c in counters )
326396
327397
398+ def get_watercounters_by_type (water_type_id , response ):
399+ """
400+ :param water_type_id: COLD_WATER, HOT_WATER
401+ :param response: get_watercounters() response
402+ :return: response JSON array:
403+ [{'counterId': 1437373,
404+ 'type': 1,
405+ 'num': '417944',
406+ 'checkup': '2023-09-25+03:00',
407+ 'indications':
408+ [{'period': '2018-08-31+03:00', 'indication': '21.38'},
409+ {'period': '2018-07-31+03:00', 'indication': '20.7'},
410+ {'period': '2018-06-30+03:00', 'indication': '19'}]
411+ },
412+ {...}]
413+ """
414+ return list (filter (lambda x : x ['type' ] == water_type_id , response ['counters' ]))
415+
416+
417+ def get_watercounter_by_id (id , response ):
418+ return list (filter (lambda x : x ['counterId' ] == id , response ['counters' ]))[0 ]
419+
420+
421+ def get_counter_by_num (num , response ):
422+ """
423+ :param num: Номер счетчика из приложения
424+ :param response: JSON
425+ {'counterId': 1437373,
426+ 'type': 1,
427+ 'num': '417944',
428+ 'checkup': '2023-09-25+03:00',
429+ 'indications':
430+ [{'period': '2018-08-31+03:00', 'indication': '21.38'},
431+ {'period': '2018-07-31+03:00', 'indication': '20.7'},
432+ {'period': '2018-06-30+03:00', 'indication': '19'}]
433+ }
434+ :return:
435+ """
436+ return list (filter (lambda x : x ['num' ] == num , response ['counters' ]))[0 ]
437+
438+
439+ def get_watermeter_last_value (counter ):
440+ """
441+ :param counter: counter JSON from get_counter_by_num
442+ :return: float
443+ """
444+ indications = counter ['indications' ]
445+ indications .sort (key = lambda x : x ['period' ]) # alphabetical sort data =)
446+ assert indications , 'Нет показаний'
447+ return float (indications [- 1 ]['indication' ])
448+
449+
450+ def get_watermeter_id (counter ):
451+ return counter ['counterId' ]
452+
453+
454+ def get_watermeter_checkup (counter ):
455+ """
456+ :param counter: counter JSON from get_counter_by_num
457+ 'checkup': '2023-09-25+03:00'
458+ :return: datetime с временной зоной
459+ """
460+ checkup = counter ['checkup' ]
461+ d = datetime .strptime (checkup , '%Y-%m-%d%z' ) #https://docs.python.org/3/library/datetime.html#datetime.timezone
462+ return d # Не будем приводить к UTC d.replace(tzinfo=None)
463+
464+
328465def get_watercounter_value (counterId , response ):
329466 """
330467 :param counterId: ['counters'][x]['counterId']
331468 :param response: get_watercounters() response
332469 :return: float
333470 """
334- for c in response ['counters' ]:
335- if c ['counterId' ] == counterId :
336- if 'indications' in c :
337- indications = c ['indications' ]
338- indications .sort (key = lambda x : x ['period' ]) # alphabetical sort data =)
339- assert indications
340- return float (indications [- 1 ]['indication' ])
471+ c = get_watercounter_by_id (counterId , response )
472+ return get_watermeter_last_value (c )
473+
341474
475+ def watermeter_new_value_json (counterId , value ):
476+ """
477+ :param counterId: id счетчика. не номер из приложения!
478+ :param value: значение float
479+ :return: dict
480+ """
481+ return {
482+ 'counter_id' : int (counterId ),
483+ 'period' : datetime .now ().strftime ("%Y-%m-%d" ),
484+ 'indication' : '{:.2f}' .format (value ).replace ('.' , ',' )
485+ }
0 commit comments