issue-51-telegram convert all datetime objects to UTC
This commit is contained in:
		
							parent
							
								
									d930c1082a
								
							
						
					
					
						commit
						929435c5fb
					
				| @ -5,6 +5,8 @@ from operator import attrgetter | |||||||
| import datetime | import datetime | ||||||
| import json | import json | ||||||
| 
 | 
 | ||||||
|  | import pytz | ||||||
|  | 
 | ||||||
| from dsmr_parser import obis_name_mapping | from dsmr_parser import obis_name_mapping | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -154,16 +156,20 @@ class MBusObject(DSMRObject): | |||||||
|             return self.values[1]['unit'] |             return self.values[1]['unit'] | ||||||
| 
 | 
 | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         output = "{}\t[{}] at {}".format(str(self.value), str(self.unit), str(self.datetime.astimezone().isoformat())) |         output = "{}\t[{}] at {}".format( | ||||||
|  |             str(self.value), | ||||||
|  |             str(self.unit), | ||||||
|  |             str(self.datetime.astimezone().astimezone(pytz.utc).isoformat()) | ||||||
|  |         ) | ||||||
|         return output |         return output | ||||||
| 
 | 
 | ||||||
|     def to_json(self): |     def to_json(self): | ||||||
|         timestamp = self.datetime |         timestamp = self.datetime | ||||||
|         if isinstance(self.datetime, datetime.datetime): |         if isinstance(self.datetime, datetime.datetime): | ||||||
|             timestamp = self.datetime.astimezone().isoformat() |             timestamp = self.datetime.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         value = self.value |         value = self.value | ||||||
|         if isinstance(self.value, datetime.datetime): |         if isinstance(self.value, datetime.datetime): | ||||||
|             value = self.value.astimezone().isoformat() |             value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         if isinstance(self.value, Decimal): |         if isinstance(self.value, Decimal): | ||||||
|             value = float(self.value) |             value = float(self.value) | ||||||
|         output = { |         output = { | ||||||
| @ -194,20 +200,20 @@ class MBusObjectPeak(DSMRObject): | |||||||
| 
 | 
 | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         output = "{}\t[{}] at {} occurred {}"\ |         output = "{}\t[{}] at {} occurred {}"\ | ||||||
|             .format(str(self.value), str(self.unit), str(self.datetime.astimezone().isoformat()), |             .format(str(self.value), str(self.unit), str(self.datetime.astimezone().astimezone(pytz.utc).isoformat()), | ||||||
|                     str(self.occurred.astimezone().isoformat())) |                     str(self.occurred.astimezone().astimezone(pytz.utc).isoformat())) | ||||||
|         return output |         return output | ||||||
| 
 | 
 | ||||||
|     def to_json(self): |     def to_json(self): | ||||||
|         timestamp = self.datetime |         timestamp = self.datetime | ||||||
|         if isinstance(self.datetime, datetime.datetime): |         if isinstance(self.datetime, datetime.datetime): | ||||||
|             timestamp = self.datetime.astimezone().isoformat() |             timestamp = self.datetime.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         timestamp_occurred = self.occurred |         timestamp_occurred = self.occurred | ||||||
|         if isinstance(self.occurred, datetime.datetime): |         if isinstance(self.occurred, datetime.datetime): | ||||||
|             timestamp_occurred = self.occurred.astimezone().isoformat() |             timestamp_occurred = self.occurred.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         value = self.value |         value = self.value | ||||||
|         if isinstance(self.value, datetime.datetime): |         if isinstance(self.value, datetime.datetime): | ||||||
|             value = self.value.astimezone().isoformat() |             value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         if isinstance(self.value, Decimal): |         if isinstance(self.value, Decimal): | ||||||
|             value = float(self.value) |             value = float(self.value) | ||||||
|         output = { |         output = { | ||||||
| @ -232,14 +238,14 @@ class CosemObject(DSMRObject): | |||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         print_value = self.value |         print_value = self.value | ||||||
|         if isinstance(self.value, datetime.datetime): |         if isinstance(self.value, datetime.datetime): | ||||||
|             print_value = self.value.astimezone().isoformat() |             print_value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         output = "{}\t[{}]".format(str(print_value), str(self.unit)) |         output = "{}\t[{}]".format(str(print_value), str(self.unit)) | ||||||
|         return output |         return output | ||||||
| 
 | 
 | ||||||
|     def to_json(self): |     def to_json(self): | ||||||
|         json_value = self.value |         json_value = self.value | ||||||
|         if isinstance(self.value, datetime.datetime): |         if isinstance(self.value, datetime.datetime): | ||||||
|             json_value = self.value.astimezone().isoformat() |             json_value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||||
|         if isinstance(self.value, Decimal): |         if isinstance(self.value, Decimal): | ||||||
|             json_value = float(self.value) |             json_value = float(self.value) | ||||||
|         output = { |         output = { | ||||||
| @ -301,7 +307,7 @@ class ProfileGenericObject(DSMRObject): | |||||||
|         for buffer_value in self.buffer: |         for buffer_value in self.buffer: | ||||||
|             timestamp = buffer_value.datetime |             timestamp = buffer_value.datetime | ||||||
|             if isinstance(timestamp, datetime.datetime): |             if isinstance(timestamp, datetime.datetime): | ||||||
|                 timestamp = str(timestamp.astimezone().isoformat()) |                 timestamp = str(timestamp.astimezone().astimezone(pytz.utc).isoformat()) | ||||||
|             output += "\n\t event occured at: {}".format(timestamp) |             output += "\n\t event occured at: {}".format(timestamp) | ||||||
|             output += "\t for: {} [{}]".format(buffer_value.value, buffer_value.unit) |             output += "\t for: {} [{}]".format(buffer_value.value, buffer_value.unit) | ||||||
|         return output |         return output | ||||||
|  | |||||||
| @ -46,7 +46,7 @@ class MbusDeviceTest(unittest.TestCase): | |||||||
|                 'CHANNEL_ID': 1, |                 'CHANNEL_ID': 1, | ||||||
|                 'DEVICE_TYPE': {'value': 3, 'unit': None}, |                 'DEVICE_TYPE': {'value': 3, 'unit': None}, | ||||||
|                 'EQUIPMENT_IDENTIFIER_GAS': {'value': '4730303339303031393336393930363139', 'unit': None}, |                 'EQUIPMENT_IDENTIFIER_GAS': {'value': '4730303339303031393336393930363139', 'unit': None}, | ||||||
|                 'HOURLY_GAS_METER_READING': {'datetime': '2020-04-26T22:30:01+02:00', 'value': 246.138, 'unit': 'm3'}} |                 'HOURLY_GAS_METER_READING': {'datetime': '2020-04-26T20:30:01+00:00', 'value': 246.138, 'unit': 'm3'}} | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|     def test_str(self): |     def test_str(self): | ||||||
| @ -56,6 +56,6 @@ class MbusDeviceTest(unittest.TestCase): | |||||||
|                 'CHANNEL_ID: 	 1\n' |                 'CHANNEL_ID: 	 1\n' | ||||||
|                 'DEVICE_TYPE: 	 3	[None]\n' |                 'DEVICE_TYPE: 	 3	[None]\n' | ||||||
|                 'EQUIPMENT_IDENTIFIER_GAS: 	 4730303339303031393336393930363139	[None]\n' |                 'EQUIPMENT_IDENTIFIER_GAS: 	 4730303339303031393336393930363139	[None]\n' | ||||||
|                 'HOURLY_GAS_METER_READING: 	 246.138	[m3] at 2020-04-26T22:30:01+02:00\n' |                 'HOURLY_GAS_METER_READING: 	 246.138	[m3] at 2020-04-26T20:30:01+00:00\n' | ||||||
|             ) |             ) | ||||||
|         ) |         ) | ||||||
|  | |||||||
| @ -398,7 +398,7 @@ class TelegramTest(unittest.TestCase): | |||||||
|              'EQUIPMENT_IDENTIFIER': {'unit': None, |              'EQUIPMENT_IDENTIFIER': {'unit': None, | ||||||
|                                       'value': '4B384547303034303436333935353037'}, |                                       'value': '4B384547303034303436333935353037'}, | ||||||
|              'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, 'value': None}, |              'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, 'value': None}, | ||||||
|              'HOURLY_GAS_METER_READING': {'datetime': '2017-01-02T16:10:05+01:00', |              'HOURLY_GAS_METER_READING': {'datetime': '2017-01-02T15:10:05+00:00', | ||||||
|                                           'unit': 'm3', |                                           'unit': 'm3', | ||||||
|                                           'value': 0.107}, |                                           'value': 0.107}, | ||||||
|              'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE': {'unit': 'kW', 'value': 0.0}, |              'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE': {'unit': 'kW', 'value': 0.0}, | ||||||
| @ -418,14 +418,14 @@ class TelegramTest(unittest.TestCase): | |||||||
|                                'DEVICE_TYPE': {'unit': None, 'value': 3}, |                                'DEVICE_TYPE': {'unit': None, 'value': 3}, | ||||||
|                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, |                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, | ||||||
|                                                             'value': '3232323241424344313233343536373839'}, |                                                             'value': '3232323241424344313233343536373839'}, | ||||||
|                                'HOURLY_GAS_METER_READING': {'datetime': '2017-01-02T16:10:05+01:00', |                                'HOURLY_GAS_METER_READING': {'datetime': '2017-01-02T15:10:05+00:00', | ||||||
|                                                             'unit': 'm3', |                                                             'unit': 'm3', | ||||||
|                                                             'value': 0.107}}, |                                                             'value': 0.107}}, | ||||||
|                               {'CHANNEL_ID': 2, |                               {'CHANNEL_ID': 2, | ||||||
|                                'DEVICE_TYPE': {'unit': None, 'value': 3}, |                                'DEVICE_TYPE': {'unit': None, 'value': 3}, | ||||||
|                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, 'value': None}}], |                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, 'value': None}}], | ||||||
|              'P1_MESSAGE_HEADER': {'unit': None, 'value': '50'}, |              'P1_MESSAGE_HEADER': {'unit': None, 'value': '50'}, | ||||||
|              'P1_MESSAGE_TIMESTAMP': {'unit': None, 'value': '2017-01-02T19:20:02+01:00'}, |              'P1_MESSAGE_TIMESTAMP': {'unit': None, 'value': '2017-01-02T18:20:02+00:00'}, | ||||||
|              'POWER_EVENT_FAILURE_LOG': {'buffer': [], |              'POWER_EVENT_FAILURE_LOG': {'buffer': [], | ||||||
|                                          'buffer_length': 0, |                                          'buffer_length': 0, | ||||||
|                                          'buffer_type': '0-0:96.7.19'}, |                                          'buffer_type': '0-0:96.7.19'}, | ||||||
| @ -447,7 +447,7 @@ class TelegramTest(unittest.TestCase): | |||||||
|             str(telegram), |             str(telegram), | ||||||
|             ( |             ( | ||||||
|                 'P1_MESSAGE_HEADER: 	 50	[None]\n' |                 'P1_MESSAGE_HEADER: 	 50	[None]\n' | ||||||
|                 'P1_MESSAGE_TIMESTAMP: 	 2017-01-02T19:20:02+01:00	[None]\n' |                 'P1_MESSAGE_TIMESTAMP: 	 2017-01-02T18:20:02+00:00	[None]\n' | ||||||
|                 'EQUIPMENT_IDENTIFIER: 	 4B384547303034303436333935353037	[None]\n' |                 'EQUIPMENT_IDENTIFIER: 	 4B384547303034303436333935353037	[None]\n' | ||||||
|                 'ELECTRICITY_USED_TARIFF_1: 	 4.426	[kWh]\n' |                 'ELECTRICITY_USED_TARIFF_1: 	 4.426	[kWh]\n' | ||||||
|                 'ELECTRICITY_USED_TARIFF_2: 	 2.399	[kWh]\n' |                 'ELECTRICITY_USED_TARIFF_2: 	 2.399	[kWh]\n' | ||||||
| @ -481,11 +481,11 @@ class TelegramTest(unittest.TestCase): | |||||||
|                 'INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: 	 0.000	[kW]\n' |                 'INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: 	 0.000	[kW]\n' | ||||||
|                 'INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: 	 0.000	[kW]\n' |                 'INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: 	 0.000	[kW]\n' | ||||||
|                 'EQUIPMENT_IDENTIFIER_GAS: 	 None	[None]\n' |                 'EQUIPMENT_IDENTIFIER_GAS: 	 None	[None]\n' | ||||||
|                 'HOURLY_GAS_METER_READING: 	 0.107	[m3] at 2017-01-02T16:10:05+01:00\n' |                 'HOURLY_GAS_METER_READING: 	 0.107	[m3] at 2017-01-02T15:10:05+00:00\n' | ||||||
|                 'MBUS DEVICE (channel: 1)\n' |                 'MBUS DEVICE (channel: 1)\n' | ||||||
|                 '	DEVICE_TYPE: 	 3	[None] \n' |                 '	DEVICE_TYPE: 	 3	[None] \n' | ||||||
|                 '	EQUIPMENT_IDENTIFIER_GAS: 	 3232323241424344313233343536373839	[None] \n' |                 '	EQUIPMENT_IDENTIFIER_GAS: 	 3232323241424344313233343536373839	[None] \n' | ||||||
|                 '	HOURLY_GAS_METER_READING: 	 0.107	[m3] at 2017-01-02T16:10:05+01:00 \n' |                 '	HOURLY_GAS_METER_READING: 	 0.107	[m3] at 2017-01-02T15:10:05+00:00 \n' | ||||||
|                 'MBUS DEVICE (channel: 2)\n' |                 'MBUS DEVICE (channel: 2)\n' | ||||||
|                 '	DEVICE_TYPE: 	 3	[None] \n' |                 '	DEVICE_TYPE: 	 3	[None] \n' | ||||||
|                 '	EQUIPMENT_IDENTIFIER_GAS: 	 None	[None] \n' |                 '	EQUIPMENT_IDENTIFIER_GAS: 	 None	[None] \n' | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user