def json(self): return { 'version': self.version, 'time': self.time.isoformat(), 'timestamp': str(seconds_to_timestamp(self.time.timestamp())), 'product_name': 'Joulescope JS110', 'vendor_name': 'Jetperch LLC', 'subtype_name': 'Calibration', 'product': f'{self.product_id}.{self.vendor_id}.{self.subtype_id}', 'serial_number': self.serial_number, 'voltage': { 'offset': _np_list(self.voltage_offset), 'gain': _np_list(self.voltage_gain), }, 'current': { 'offset': _np_list(self.current_offset), 'gain': _np_list(self.current_gain), }, }
def save(self, private_key=None): """Save calibration to bytes. :param private_key: The private key used to sign the calibration. None (default) does not sign the calibration. The Joulescope software will display warnings if the calibration is not signed using a valid key. :return: The calibration as bytes. """ if isinstance(self.time, str): self.time = dateutil.parser.parse(self.time) fh = io.BytesIO() dfw = datafile.DataFileWriter(fh) if private_key is not None: dfw.signature_start(private_key) dfw.append_header( timestamp=seconds_to_timestamp(self.time.timestamp()), version=_version_str_to_u32(self.version), product_id=self.product_id, vendor_id=self.vendor_id, subtype_id=self.subtype_id, hardware_compatibility=0, serial_number=binascii.unhexlify(self.serial_number), ) self.current_offset = np.concatenate( (self.current_offset[:7], [0.0, np.nan])) self.current_gain = np.concatenate( (self.current_gain[:7], [0.0, np.nan])) if len(self.current_offset) != 9 or len(self.current_gain) != 9: raise ValueError('Invalid length for current') if len(self.voltage_offset) != 2 or len(self.voltage_gain) != 2: raise ValueError('Invalid length for voltage') cal = self.json() dfw.append(datafile.TAG_DATA_JSON, json.dumps(cal, allow_nan=True).encode('utf-8')) if private_key is not None: dfw.signature_end() dfw.finalize() return bytes(fh.getbuffer())
def test_conversion(self): now = time.timestamp_now() s = time.timestamp_to_seconds(now) t = time.seconds_to_timestamp(s) self.assertEqual(now, t)
def test_seconds_to_timestamp(self): self.assertEqual(0, time.seconds_to_timestamp(time.EPOCH))