Ejemplo n.º 1
0
 def _post_settings(writer, request):
     global last_request_time
     last_request_time = time.time()
     try:
         new_settings = ujson.loads(request.get('body'))
     except Exception as e:
         error.add_error(ujson.dumps({
             'time': time.time(),
             'error': str(e)
         }))
         return userv.json(writer,
                           {"message": "Request had no valid json body."},
                           status=406)
     updated_settings = settings.save_settings(settings.get_settings(),
                                               new_settings)
     return userv.json(writer, updated_settings)
Ejemplo n.º 2
0
def run_several_latt(error_info, n_runs_total, init_state):
    '''
    '''

    #if output_folder[-1] != '/':  output_folder += '/'
    #chp_loc = './chp_extended'
    #CHP_IO_files = False

    n_fails = 0

    for n_run in xrange(n_runs_total):
        # create the supra-circuit and insert gates
        CNOT_circuits = qfun.create_latt_surg_CNOT(Is_after2q, initial_I, anc_parallel,
                                                   EC_ctrl_targ, FT)
        for supra_gate in CNOT_circuits.gates:
            if supra_gate.gate_name == 'Logical_I' or supra_gate.gate_name == 'MeasureX':
                error.add_error(supra_gate.circuit_list[0], error_info)
            elif supra_gate.gate_name[:8] == 'Measure2' or supra_gate.gate_name[:5] == 'Joint':
                for in_gate in supra_gate.circuit_list[0].gates:
                    if in_gate.gate_name[:7] == 'Partial':
                        error.add_error(in_gate.circuit_list[0], error_info)
                    elif in_gate.gate_name[:2] == 'EC':
                        for in_gate2 in in_gate.circuit_list[0].gates:
                            error.add_error(in_gate2.circuit_list[0], error_info)
        
        #brow.from_circuit(CNOT_circuits, True)
        
        # run the faulty circuit
        init_state_copy = init_state[0][:], init_state[1][:]
        fail = run_latt_surg_circ(init_state_copy, CNOT_circuits)
        if fail:  n_fails += 1

    return n_fails
Ejemplo n.º 3
0
    request_url = loaded_settings.get("request_url", None)

    # load batch data
    if _batch_file in os.listdir():
        _batch_file_ptr = open(_batch_file, "r")
        batches = ujson.loads(_batch_file_ptr.read())
        _batch_file_ptr.close()
    else:
        batches = list()

    # with every start we gather sensor data at least once
    sensor_data = sensor.sensor_data()
    sensor_data.update(loaded_settings.get('added_infos_to_sensor_data', {}))
    batches.append(sensor_data)

    # save batch data
    # and only save max reads batches
    _batch_file_ptr = open(_batch_file, "w")
    _batch_file_ptr.write(ujson.dumps(batches[-reads_without_send:]))
    _batch_file_ptr.close()

    # if we have enough batches and a request_url
    # hard reset starts webserver
    if (len(batches) >= reads_without_send and request_url is not None) or machine.reset_cause() == machine.HARD_RESET:

except Exception as e:
    print(e)
    error.add_error(ujson.dumps({'time': time.time(), 'error': "boot: " + str(e)}))
finally:
    gc.collect()
Ejemplo n.º 4
0
    request_url = loaded_settings.get("request_url", None)
    if wlan.sta_if.isconnected() and request_url is not None:

        _batch_file_ptr = open(_batch_file, "r")
        try:
            batches = ujson.loads(_batch_file_ptr.read())
            for batch in batches:
                # send plant_monitor data
                response = urequests.post(
                    request_url,
                    json=batch
                )

                if int(response.status_code) >= 300:
                    error.add_error(
                        ujson.dumps({'time': time.time(), 'status': response.status_code, 'body': response.body})
                    )

        except Exception as e:
            error.add_error(ujson.dumps({'time': time.time(), 'error': "Sending batches: " + str(e)}))
        finally:
            _batch_file_ptr.close()
            os.remove(_batch_file)

    # Only start server if there is a network
    if wlan.sta_if.isconnected() or wlan.ap_if.isconnected():
        if machine.reset_cause() == machine.HARD_RESET:
            print("Hard reset its config time")
            restful_online_time = loaded_settings.get('awake_time_for_config', 300)
            keep_alive_time = restful_online_time
Ejemplo n.º 5
0
_power = machine.Pin(13, machine.Pin.OUT)
_power.value(1)


def power_on():
    _power.value(1)


def power_off():
    _power.value(0)


try:
    _adc = machine.ADC(0)
except Exception:
    error.add_error("ADC is not defined")

try:
    _dht = dht.DHT22(machine.Pin(12))
except Exception:
    error.add_error("Dht22 is not running.")

try:
    _scl = machine.Pin(14)
    _sda = machine.Pin(2)
    _i2c = machine.I2C(scl=_scl, sda=_sda)
except Exception:
    error.add_error("Lightsensor is not running.")

_config_file = "sensor_config.json"
history_sensor_data_file = "sensor_data_log.json"