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 json | ||||
| 
 | ||||
| import pytz | ||||
| 
 | ||||
| from dsmr_parser import obis_name_mapping | ||||
| 
 | ||||
| 
 | ||||
| @ -154,16 +156,20 @@ class MBusObject(DSMRObject): | ||||
|             return self.values[1]['unit'] | ||||
| 
 | ||||
|     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 | ||||
| 
 | ||||
|     def to_json(self): | ||||
|         timestamp = self.datetime | ||||
|         if isinstance(self.datetime, datetime.datetime): | ||||
|             timestamp = self.datetime.astimezone().isoformat() | ||||
|             timestamp = self.datetime.astimezone().astimezone(pytz.utc).isoformat() | ||||
|         value = self.value | ||||
|         if isinstance(self.value, datetime.datetime): | ||||
|             value = self.value.astimezone().isoformat() | ||||
|             value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||
|         if isinstance(self.value, Decimal): | ||||
|             value = float(self.value) | ||||
|         output = { | ||||
| @ -194,20 +200,20 @@ class MBusObjectPeak(DSMRObject): | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         output = "{}\t[{}] at {} occurred {}"\ | ||||
|             .format(str(self.value), str(self.unit), str(self.datetime.astimezone().isoformat()), | ||||
|                     str(self.occurred.astimezone().isoformat())) | ||||
|             .format(str(self.value), str(self.unit), str(self.datetime.astimezone().astimezone(pytz.utc).isoformat()), | ||||
|                     str(self.occurred.astimezone().astimezone(pytz.utc).isoformat())) | ||||
|         return output | ||||
| 
 | ||||
|     def to_json(self): | ||||
|         timestamp = self.datetime | ||||
|         if isinstance(self.datetime, datetime.datetime): | ||||
|             timestamp = self.datetime.astimezone().isoformat() | ||||
|             timestamp = self.datetime.astimezone().astimezone(pytz.utc).isoformat() | ||||
|         timestamp_occurred = self.occurred | ||||
|         if isinstance(self.occurred, datetime.datetime): | ||||
|             timestamp_occurred = self.occurred.astimezone().isoformat() | ||||
|             timestamp_occurred = self.occurred.astimezone().astimezone(pytz.utc).isoformat() | ||||
|         value = self.value | ||||
|         if isinstance(self.value, datetime.datetime): | ||||
|             value = self.value.astimezone().isoformat() | ||||
|             value = self.value.astimezone().astimezone(pytz.utc).isoformat() | ||||
|         if isinstance(self.value, Decimal): | ||||
|             value = float(self.value) | ||||
|         output = { | ||||
| @ -232,14 +238,14 @@ class CosemObject(DSMRObject): | ||||
|     def __str__(self): | ||||
|         print_value = self.value | ||||
|         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)) | ||||
|         return output | ||||
| 
 | ||||
|     def to_json(self): | ||||
|         json_value = self.value | ||||
|         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): | ||||
|             json_value = float(self.value) | ||||
|         output = { | ||||
| @ -301,7 +307,7 @@ class ProfileGenericObject(DSMRObject): | ||||
|         for buffer_value in self.buffer: | ||||
|             timestamp = buffer_value.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 += "\t for: {} [{}]".format(buffer_value.value, buffer_value.unit) | ||||
|         return output | ||||
|  | ||||
| @ -46,7 +46,7 @@ class MbusDeviceTest(unittest.TestCase): | ||||
|                 'CHANNEL_ID': 1, | ||||
|                 'DEVICE_TYPE': {'value': 3, '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): | ||||
| @ -56,6 +56,6 @@ class MbusDeviceTest(unittest.TestCase): | ||||
|                 'CHANNEL_ID: 	 1\n' | ||||
|                 'DEVICE_TYPE: 	 3	[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, | ||||
|                                       'value': '4B384547303034303436333935353037'}, | ||||
|              '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', | ||||
|                                           'value': 0.107}, | ||||
|              'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE': {'unit': 'kW', 'value': 0.0}, | ||||
| @ -418,14 +418,14 @@ class TelegramTest(unittest.TestCase): | ||||
|                                'DEVICE_TYPE': {'unit': None, 'value': 3}, | ||||
|                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, | ||||
|                                                             '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', | ||||
|                                                             'value': 0.107}}, | ||||
|                               {'CHANNEL_ID': 2, | ||||
|                                'DEVICE_TYPE': {'unit': None, 'value': 3}, | ||||
|                                'EQUIPMENT_IDENTIFIER_GAS': {'unit': None, 'value': None}}], | ||||
|              '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': [], | ||||
|                                          'buffer_length': 0, | ||||
|                                          'buffer_type': '0-0:96.7.19'}, | ||||
| @ -447,7 +447,7 @@ class TelegramTest(unittest.TestCase): | ||||
|             str(telegram), | ||||
|             ( | ||||
|                 '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' | ||||
|                 'ELECTRICITY_USED_TARIFF_1: 	 4.426	[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_L3_NEGATIVE: 	 0.000	[kW]\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' | ||||
|                 '	DEVICE_TYPE: 	 3	[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' | ||||
|                 '	DEVICE_TYPE: 	 3	[None] \n' | ||||
|                 '	EQUIPMENT_IDENTIFIER_GAS: 	 None	[None] \n' | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user