def test_process(): data = read("tests/process.json") msg = dumps(loads(data)) orig = json.loads(data) out = json.loads(msg) assert orig == out schemata.validate_process(loads(data))
def test_message(): data = read("tests/message.json") msg = dumps(loads(data)) orig = json.loads(data) out = json.loads(msg) assert orig == out schemata.validate_message(loads(data))
def test_validation2(): data = read("tests/invalid.json") msg = loads(data, validate=False) errors = msg.problems() assert len(errors) > 0 msg = dumps(loads(data)) orig = json.loads(data) out = json.loads(msg) assert orig == out
def test_measurement(): data = read("tests/measurement.json") msg = dumps(loads(data)) orig = json.loads(data) out = json.loads(msg) assert orig == out print json.dumps(out, indent=2) schemata.validate_measurement(loads(data))
def process_inbound_message(msg, classifier): inbound_msg = util.loads(msg, validate=True) data = stats.load_from_ppmp_msg(inbound_msg) result = classifier.predict([data])[0] meas = Measurement(ts=inbound_msg.measurements[0].ts, result='NOK' if result == 'bad' else 'OK') meas.series.offsets.append(0) for dim, val in zip(stats.CHARACTERISTICS, data): meas.series.add_dimension(dim) meas.series[dim].append(val) outbound_msg = MeasurementPayload(device=inbound_msg.device, measurements=[meas]) return util.dumps(outbound_msg, indent=2)
def send(payload, endpoint=None): """ Converts the payload to json and sends it to the endpoint. If no endpoint given, it will be printed to stdout. """ plain_payload = util.dumps(payload, indent=2) if not endpoint: print_out(plain_payload) return try: r = requests.post(endpoint, data=plain_payload) r.raise_for_status() except requests.exceptions.ConnectionError as exc: print_err('Failed to connect') except requests.exceptions.HTTPError as exc: print_err('Error, server returned: %s' % exc.response.status_code) else: print_err('Successfully sent to endpoint:\n%s' % plain_payload)
def roundtrip(obj): clone = loads(dumps(obj)) assert obj == clone
def test_build_sample(): device = Device("a4927dad-58d4-4580-b460-79cefd56775b", operationalStatus="normal", swVersion="2.0.3.13", swBuildId="41535") part = Part('SINGLE', partTypeID="F00VH07328", partID="420003844", result="NOK", code="HUH289", toolId="32324-432143") tz_plus2 = dateutil.tz.tzoffset("+02:00", 2 * 60 * 60) ts = datetime(2002, 5, 30, 9, 30, 10, 123 * 1000, tz_plus2) process = Process(externalProcessId="b4927dad-58d4-4580-b460-79cefd56775b", ts=ts, result="NOK", shutoffPhase="phase 1", program=Program("1", "Programm 1", lastChangedDate=ts), name="Getriebedeckel verschrauben") payload = ProcessPayload( device=device, part=part, process=process) payload.process.shutoffValues.add_dimension("force") payload.process.shutoffValues.add_dimension("pressure") payload.process.shutoffValues.force.ts = ts payload.process.shutoffValues.force.value = 24 payload.process.shutoffValues.force.upperError = 26 payload.process.shutoffValues.force.lowerError = 22 payload.process.shutoffValues.pressure.value = 50 payload.process.shutoffValues.pressure.upperError = 52 payload.process.shutoffValues["pressure"].lowerError = 48 m = Measurement(ts=ts, phase="phasen name 2", name="500 Grad links drehen", result="NOK", code="0000 EE01") m.limits.add_dimension("temperature") m.limits.temperature.upperError = 4444 m.limits.temperature.lowerError = 44 m.limits.temperature.upperWarn = 2222 m.limits.temperature.lowerWarn = 46 m.specialValues.append( SpecialValue(name='turning point', value={"pressure": 24, "force": 50})) m.series.add_dimension("time") m.series.add_dimension("force") m.series.add_dimension("pressure") m.series.add_dimension("temperature") m.add_sample(time=0, force=26, pressure=52.4, temperature=45.4243) m.add_sample(time=23, force=23, pressure=46.32, temperature=46.42342) m.add_sample(time=24, force=24, pressure=44.2432, temperature=44.2432) payload.measurements.append(m) m = Measurement(ts=ts, phase="phasen name", result="OK") m.limits.add_dimension("force") m.limits.force.upperError = [27, 24, 25] m.limits.force.lowerError = [25, 22, 23] m.limits.add_dimension("pressure") m.limits.pressure.upperError = [54, 48, 46] m.limits.pressure.lowerError = [50, 44, 42] m.specialValues.append( SpecialValue(name='some special value', value={'force': 50, 'pressure': 24})) m.series.add_dimension("time") m.series.add_dimension("force") m.series.add_dimension("pressure") m.series.add_dimension("temperature") m.add_sample(time=30, force=26, pressure=52.4, temperature=45.4243) m.add_sample(time=36, force=23, pressure=46.32, temperature=46.42342) m.add_sample(time=42, force=24, pressure=44.2432, temperature=44.2432) payload.measurements.append(m) assert len(list(m.samples())) == 3 assert not payload.problems() schemata.validate_process(payload) payload.device._data["murx"] = 9 errors = payload.problems() assert len(errors) == 1 payload._data["content-spec"] = "wrong" errors = payload.problems() assert len(errors) == 2 dumps(payload, indent=4)
"Authorization": authHeader } def bg_cb(sess, resp): # parse the json storing the result on the response object resp.data = resp.json() print(resp) while True: lastMeasurement = datetime.datetime.utcnow() newMetrics = dict() for val in currentSeries: newMetrics[val.name] = r.randint(val.min, val.max) m.add_sample(unide.process.local_now(), **newMetrics) if len(m.series.offsets) >= measurementsPerMessage: print("Sending message") payload = MeasurementPayload(device=device, measurements=[m]) content = dumps(payload) result = session.post(monitoringEndpoint, data=content, background_callback=bg_cb, timeout=timeout) # Reset measurement object m = Measurement(unide.process.local_now(), dimensions=currentSeries) timeToSleep = ((lastMeasurement + timedelta(milliseconds=msBetweenMeasurements)) - datetime.datetime.utcnow()).total_seconds() if timeToSleep < 0: timeToSleep = 0 time.sleep(timeToSleep)