Ejemplo n.º 1
0
    def loop(self):
        """
        The main segement of the run method
        :return:
        """
        # set up priority queue of all the boids to be interpreted
        # time_elapsed = 0.0
        # TODO time_step should be smallest time division of beat when beat is on
        boid_heap = []

        self.setup_priority_queue(boid_heap, self.time_elapsed)

        # TODO add all notes to a list to ensure concurrency (as much as poss)
        while not self.done:

            time_this_loop = timenow()

            while boid_heap[0][0] <= self.time_elapsed:
                self.parse_priority_queue(boid_heap, self.time_elapsed)

            time_this_loop = timenow() - time_this_loop
            to_sleep = max(0, self.time_step - time_this_loop)
            if to_sleep == 0:
                print("OOPS: missed a beat (must be really lagging)")

            sleep(to_sleep)
            self.time_elapsed += self.time_step
Ejemplo n.º 2
0
    def run_att(self):
        """ For use with attractor mode 2, which is old and bad """
        # TODO experimental timings
        buffer_time = 0.05
        while not self.done:
            started = timenow()
            buffer = list()
            while (timenow() - started) < buffer_time:
                msg = self.midiin.get_message()
                if msg:
                    buffer.append(msg)
                sleep(0.002)

            chord_time = buffer_time
            if len(buffer) >= 1:
                if buffer[0]:
                    chord_time = buffer[0][1]
                for msg in buffer:
                    message, delta_time = msg
                    # TODO for now only listen to NOTE_ON & OFF
                    if 128 <= message[0] < 160:
                        self.wallclock += delta_time
                        # print("[%s] @%0.6f %r" % (self.port_name, self.wallclock, message))

                        # publish the message to every interpreter
                        for interp in self.interpreters:
                            interp.backwards_interpret(message, chord_time)

            sleep(0.01)  # TODO need this?
Ejemplo n.º 3
0
    def loop(self):
        """ Play any old thing within the MIDI range """
        # TODO make something even more random to make me look better
        time_step = 0.1
        priority_queue = []
        chance = 0.5  # chance to play a note at any given time_step

        while not self.done:

            time_this_loop = timenow()

            # random chance to add a new event
            if random.random() < chance:
                note = random.randrange(0, 127)
                length = random.random() * 2
                volume = random.randrange(0, 127)
                # print(note)
                self.send_midi([self.note_on, note, volume], duration=length)
                heappush(priority_queue,
                         (self.time_elapsed + length, (note, length)))

            while priority_queue and priority_queue[0][0] <= self.time_elapsed:
                note, length = heappop(priority_queue)[1]
                self.send_midi([self.note_off, note], duration=length)

            time_this_loop = timenow() - time_this_loop
            to_sleep = max(0, time_step - time_this_loop)
            if to_sleep == 0:
                print("OOPS: missed a beat (must be really lagging)")

            sleep(to_sleep)
            self.time_elapsed += time_step
Ejemplo n.º 4
0
 def lidTest(self, avgTimeToComplete):
     currentTime = time.timenow()
     if stepper.start.pos_zero():
         currentTime = round(currentTime - time.timenow())
         if currentTime > avgTimeTComplete:
             print("lid endstop is problem")
         else:
             print("lid endstop is working")
         return True
     else:
         return False
Ejemplo n.º 5
0
    def run(self):
        self.done = False
        self.callcount = 0
        self.activate_drumkit(self.pattern.kit)
        cc = CONTROL_CHANGE | self.channel
        self.midiout.send_message([cc, CHANNEL_VOLUME, self.volume & 0x7F])

        # give MIDI instrument some time to activate drumkit
        sleep(0.3)
        self.started = timenow()

        while not self.done:
            self.worker()
            self.callcount += 1
            # Compensate for drift:
            # calculate the time when the worker should be called again.
            nexttime = self.started + self.callcount * self.interval
            timetowait = max(0, nexttime - timenow())
            if timetowait:
                sleep(timetowait)
            else:
                print("Oops!")

        self.midiout.send_message([cc, ALL_SOUND_OFF, 0])
Ejemplo n.º 6
0
    def run(self):
        self.done = False
        self.callcount = 0
        self.activate_drumkit(self.pattern.kit)
        cc = CONTROL_CHANGE | self.channel
        self.midiout.send_message([cc, CHANNEL_VOLUME, self.volume & 0x7F])

        # give MIDI instrument some time to activate drumkit
        sleep(0.3)
        self.started = timenow()

        while not self.done:
            self.worker()
            self.callcount += 1
            # Compensate for drift:
            # calculate the time when the worker should be called again.
            nexttime = self.started + self.callcount * self.interval
            timetowait = max(0, nexttime - timenow())
            if timetowait:
                sleep(timetowait)
            else:
                print("Oops!")

        self.midiout.send_message([cc, ALL_SOUND_OFF, 0])
Ejemplo n.º 7
0
    def __init__(self, interpreters, port):
        super(InStream, self).__init__()
        self.interpreters = interpreters
        self.port = port
        self.wallclock = timenow()

        try:
            self.midiin = MidiIn().open_port(port)
            print("Listening for midi on input port {}".format(port))
        except Exception:
            print("WARNING: failed to open MIDI-in port {0}".format(port))

        self.done = False
        self.run = self.run_bound if Parameters.SP.ATTRACTOR_MODE == 1 else self.run_att
        # print(self.run.__name__)
        self.start()
Ejemplo n.º 8
0
    def update(self, fd):
        now = timenow()

        to_remove = []
        for sf in self.scheduled_funcs:
            if now > sf.when:
                for func in sf.funcs:
                    func()
                if sf.delay is not None:
                    sf.when += sf.delay
                else:
                    to_remove.append(sf)

        for sf in to_remove:
            self.scheduled_funcs.remove(sf)

        select([fd], [], [fd], self.select_timeout)
Ejemplo n.º 9
0
def checklockacct():
    #Check if user should be unlocked and unlock them
    logkpr('checklockacct called')
    #Find *.lock in VAR['TIMEKPRDIR']
    s = VAR['TIMEKPRDIR'] + '/*.lock'
    l = glob(s)
    for f in l:
        #Get username from filename - os.path.split
        u = splitpath(f)[1].replace('.lock', '')
        lastmodified = getmtime(f) #Get last modified time from username.lock file
        #Get time when lock should be lifted
        dtlock = float(lastmodified + getlocklasts())
        dtnow = float(timenow())
        #If time now is great than or equal to the time when lock should be lifted
        if dtnow >= dtlock:
            logkpr('checklockacct: %s should be unlocked, unlocking..' % u)
            unlockuser(u)
            logkpr('checklockacct: removing %s.lock file..' % u)
            remove(f)
Ejemplo n.º 10
0
def checklockacct():
    #Check if user should be unlocked and unlock them
    logkpr('checklockacct called')
    #Find *.lock in VAR['TIMEKPRDIR']
    s = VAR['TIMEKPRDIR'] + '/*.lock'
    l = glob(s)
    for f in l:
        #Get username from filename - os.path.split
        u = splitpath(f)[1].replace('.lock', '')
        lastmodified = getmtime(f) #Get last modified time from username.lock file
        #Get time when lock should be lifted
        dtlock = float(lastmodified + getlocklasts())
        dtnow = float(timenow())
        #If time now is great than or equal to the time when lock should be lifted
        if dtnow >= dtlock:
            logkpr('checklockacct: %s should be unlocked, unlocking..' % u)
            unlockuser(u)
            logkpr('checklockacct: removing %s.lock file..' % u)
            remove(f)
Ejemplo n.º 11
0
    def pack_request(self, data=None):
        """
		Create an ICMP Echo Request package with default data (like ping)
		or allow custom data to be passed to it.
		"""
        self.type = self.REQUEST_TYPE.to_bytes(Sizes.ICMP_TYPE,
                                               byteorder=byteorder)
        self.code = self.REQUEST_CODE.to_bytes(Sizes.ICMP_CODE,
                                               byteorder=byteorder)
        self.checksum = self.REQUEST_CHECKSUM.to_bytes(Sizes.ICMP_CHECKSUM,
                                                       byteorder='big')
        new_identifier = int((id(data) * random()) % 65535)
        self.id = new_identifier.to_bytes(Sizes.ICMP_IDENTIFIER,
                                          byteorder=byteorder)
        self.seq = self.REQUEST_SEQUENCE.to_bytes(Sizes.ICMP_SEQUENCE,
                                                  byteorder='big')
        if not data:
            curTime = int(timenow())
            timestamp = curTime.to_bytes(Sizes.ICMP_TIMESTAMP,
                                         byteorder=byteorder)
            self.optional_timestamp = timestamp
            # unknown field not listed in rfc or man de ping
            self.optional_unknown = bytes.fromhex('9dc7060000000000')
            # ping always sends the following data
            self.data = bytes.fromhex(
                '101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'
            )
        else:
            assert isinstance(data, bytes)
            self.data = data

        # calculate new checksum of icmp request packet
        raw_packet = self.toBytes()
        newchecksum = self.calc_checksum(raw_packet)
        self.checksum = newchecksum.to_bytes(Sizes.ICMP_CHECKSUM,
                                             byteorder='big')
Ejemplo n.º 12
0
    def schedule_repeated(self, func, delay):
        # TODO: check for sf's with same delay and attach func to it
        # instead of creating a new item

        sf = ScheduledFunc([func], timenow()+delay, delay)
        self.scheduled_funcs.append(sf)
Ejemplo n.º 13
0
    def run_bound(self):
        """ For use with attractor mode 1, and is not yet done """
        # gather <event_time> seconds of notes and analyse them for:
        #   - pitch min & range
        #   - time min & range
        #   - dynam min & range
        #   - (if possible, the duration of notes, from note off messages)
        event_time = 0.5  # TODO maybe this should adapt to the pace of the music?
        buffer_time = 0.05

        while not self.done:
            started = timenow()
            p_min, d_min, t_min = [float('inf')] * 3
            p_max, d_max, t_max = [0] * 3
            event_notes = list()

            # a single event
            while timenow() - started < event_time:
                b_started = timenow()
                delta_t = -1

                # buffer, so chords count as simultaneous notes
                while timenow() - b_started < buffer_time:
                    msg = self.midiin.get_message()
                    if msg:
                        message, d_t = msg
                        if delta_t == -1:
                            delta_t = d_t
                        event_notes.append((message, delta_t))
                    sleep(0.002)

            num_note_ons = 0

            # analyse event_notes for all the fun stuff
            for (message, time) in event_notes:

                if 144 <= message[0] < 160:
                    note_on, pitch, dynam = message
                    p_min = min(p_min, pitch)
                    p_max = max(p_max, pitch)
                    d_min = min(d_min, dynam)
                    d_max = max(d_max, dynam)
                    t_min = min(t_min, time)
                    t_max = max(t_max, time)
                    num_note_ons += 1
                    # TODO duration
                elif 128 < message[0] < 144:
                    # until we do duration, we don't care
                    pass

            # if there's anything to report
            if num_note_ons:
                p_rng, d_rng, t_rng = (p_max - p_min), (d_max -
                                                        d_min), (t_max - t_min)

                for interp in self.interpreters:
                    # print(p_min, p_rng, d_min, d_rng, t_min, t_rng)
                    interp.change_bounds(p_min, p_rng, d_min, d_rng, t_min,
                                         t_rng)
                    # pass

            sleep(0.5)
Ejemplo n.º 14
0
    from rivus.main.rivus import get_constants

# General input
profile_log = Series(name='runchess-profiler')  # minimal profiler
DX, DY = 250, 125  # Grid block sizes

if SOLVER:
    proj_name = 'chessboard'
    datenow = datetime.now().strftime('%y%m%dT%H%M')
    result_dir = os.path.join('result', '{}-{}'.format(proj_name, datenow))
    # Origin of grid creation
    lat, lon = [48.13512, 11.58198]
    base_directory = os.path.join('data', proj_name)

    # Spatial inputs from the gridder module
    creategrid = timenow()
    vertex, edge = create_square_grid(origo_latlon=(lat, lon),
                                      num_edge_x=2,
                                      dx=DX,
                                      dy=DY,
                                      noise_prop=0.1)
    profile_log['grid_creation'] = round(timenow() - creategrid, 2)

    extendgrid = timenow()
    extend_edge_data(edge)  # only residential, with 1000 kW init
    vert_init_commodities(vertex, ('Elec', 'Gas', 'Heat'),
                          [('Elec', 0, 100000), ('Gas', 0, 5000)])
    profile_log['grid_data'] = timenow() - extendgrid

    # Non spatial input
    data_spreadsheet = os.path.join(base_directory, 'data.xlsx')
Ejemplo n.º 15
0
def run_bunch(use_email=False):
    """Run a bunch of optimizations and analysis automated. """
    # Files Access | INITs
    proj_name = 'runbunch'
    base_directory = os.path.join('data', proj_name)
    data_spreadsheet = os.path.join(base_directory, 'data.xlsx')
    profile_log = Series(name='{}-profiler'.format(proj_name))

    # Email connection
    email_setup = {
        'sender': config['email']['s_user'],
        'send_pass': config['email']['s_pass'],
        'recipient': config['email']['r_user'],
        'smtp_addr': config['email']['smtp_addr'],
        'smtp_port': config['email']['smtp_port']
    }

    # DB connection
    _user = config['db']['user']
    _pass = config['db']['pass']
    _host = config['db']['host']
    _base = config['db']['base']
    engine_string = ('postgresql://{}:{}@{}/{}'
                     .format(_user, _pass, _host, _base))
    engine = create_engine(engine_string)

    # Input Data
    # ----------
    # Spatial
    street_lengths = arange(50, 300, 100)
    num_edge_xs = [5, ]
    # Non-spatial
    data = read_excel(data_spreadsheet)
    original_data = deepcopy(data)
    interesting_parameters = [
        {'df_name': 'commodity',
         'args': {'index': 'Heat',
                  'column': 'cost-inv-fix',
                  'lim_lo': 0.5, 'lim_up': 1.6, 'step': 0.5}},
        {'df_name': 'commodity',
         'args': {'index': 'Heat',
                  'column': 'cost-fix',
                  'lim_lo': 0.5, 'lim_up': 1.6, 'step': 0.5}}
        # {'df_name': 'commodity',
        #  'args': {'index': 'Elec',
        #           'column': 'cost-var',
        #           'step': 0.1}}
    ]
    # Model Creation
    solver = SolverFactory(config['solver'])
    solver = setup_solver(solver, log_to_console=False, guro_time_lim=14400)
    # Solve | Analyse | Store | Change | Repeat
    for dx in street_lengths:
        for len_x, len_y in [(dx, dx), (dx, dx / 2)]:
            run_summary = 'Run with x:{}, y:{}'.format(len_x, len_y)
            for num_edge_x in num_edge_xs:
                vdf, edf = create_square_grid(num_edge_x=num_edge_x, dx=len_x,
                                              dy=len_y)
                extend_edge_data(edf)
                dim_x = num_edge_x + 1
                dim_y = dim_x
                for _vdf in _source_variations(vdf, dim_x, dim_y):
                    for param in interesting_parameters:
                        para_name = param['args']['column']
                        print('{0}\n{3}x{3} grid\t'
                              'dx:{1}, dy:{2}, #e:{3}, src:-, par:{4}\n'
                              .format('=' * 10, len_x, len_y, num_edge_x, para_name))
                        counter = 1
                        for variant in parameter_range(data[param['df_name']],
                                                       **param['args']):
                            changed = (variant.loc[param['args']['index']]
                                       [param['args']['column']])
                            print('variant <{0}>:{1}'.format(counter, changed))
                            counter = counter + 1
                            # Use temporal local versions.
                            # As create_model is destructive. See Issue #31.
                            __vdf = deepcopy(_vdf)
                            __edf = deepcopy(edf)
                            __data = data.copy()
                            __data[param['df_name']] = variant
                            print('\tcreating model')
                            _p_model = timenow()
                            prob = create_model(__data, __vdf, __edf)
                            profile_log['model_creation'] = (
                                timenow() - _p_model)
                            _p_solve = timenow()
                            print('\tsolving...')
                            try:
                                results = solver.solve(prob, tee=True)
                            except Exception as solve_error:
                                print(solve_error)
                                if use_email:
                                    sub = run_summary + '[rivus][solve-error]'
                                    email_me(solve_error, subject=sub,
                                             **email_setup)
                            if (results.solver.status != SolverStatus.ok):
                                status = 'error'
                                outcome = 'error'
                            else:
                                status = 'run'
                                if (results.solver.termination_condition !=
                                        TerminationCondition.optimal):
                                    outcome = 'optimum_not_reached'
                                else:
                                    outcome = 'optimum'
                            profile_log['solve'] = (timenow() - _p_solve)
                            # Plot
                            _p_plot = timenow()
                            plotcomms = ['Gas', 'Heat', 'Elec']
                            try:
                                fig = fig3d(prob, plotcomms, linescale=8,
                                            use_hubs=True)
                            except Exception as plot_error:
                                print(plot_error)
                                if use_email:
                                    sub = run_summary + '[rivus][plot-error]'
                                    email_me(plot_error, subject=sub,
                                             **email_setup)
                            profile_log['3d_plot_prep'] = (timenow() - _p_plot)
                            # Graph
                            _p_graph = timenow()
                            try:
                                _, pmax, _, _ = get_constants(prob)
                                graphs = to_nx(_vdf, edf, pmax)
                                graph_results = minimal_graph_anal(graphs)
                            except Exception as graph_error:
                                print(graph_error)
                                if use_email:
                                    sub = run_summary + '[rivus][graph-error]'
                                    email_me(graph_error, subject=sub,
                                             **email_setup)
                            profile_log['all_graph_related'] = (
                                timenow() - _p_graph)
                            # Store
                            this_run = {
                                'comment': config['run_comment'],
                                'status': status,
                                'outcome': outcome,
                                'runner': 'lnksz',
                                'plot_dict': fig,
                                'profiler': profile_log}
                            try:
                                rdb.store(engine, prob, run_data=this_run,
                                          graph_results=graph_results)
                            except Exception as db_error:
                                print(db_error)
                                if use_email:
                                    sub = run_summary + '[rivus][db-error]'
                                    email_me(db_error, subject=sub,
                                             **email_setup)
                            del __vdf
                            del __edf
                            del __data
                            print('\tRun ended with: <{}>\n'.format(outcome))

                        data = original_data
                if use_email:
                    status_txt = ('Finished iteration with edge number {}\n'
                                  'did: [source-var, param-seek]\n'
                                  'from [street-length, dim-shift, source-var,'
                                  ' param-seek]'
                                  'dx:{}, dy:{}'
                                  .format(num_edge_x, len_x, len_y))
                    sub = run_summary + '[rivus][finish-a-src]'
                    email_me(status_txt, subject=sub, **email_setup)
        if use_email:
            status_txt = ('Finished iteration with street lengths {}-{}\n'
                          'did: [dim-shift, source-var, param-seek]\n'
                          'from [street-length, dim-shift, source-var,'
                          ' param-seek]'
                          .format(len_x, len_y))
            sub = run_summary + '[rivus][finish-a-len-combo]'
            email_me(status_txt, subject=sub, **email_setup)
    if use_email:
        status_txt = ('Finished run-bunch at {}\n'
                      'did: [street-length, dim-shift, source-var, param-seek]'
                      .format(datetime.now().strftime('%y%m%dT%H%M')))
        sub = run_summary + '[rivus][finish-run]'
        email_me(status_txt, subject=sub, **email_setup)
    print('End of runbunch.')
Ejemplo n.º 16
0
                    # Inserting roll
                    lottery.roll = final_roll
                    # Finding winner
                    winner_ticket = Ticket.get(Ticket.ticket == lottery.roll)

                    # Sending to winner and dev funds.
                    pot_amount = tickets_sold * Decimal("0.01")
                    pot_dev = pot_amount * Decimal(str(
                        config["dev_fee"]))  # Hurray for float precision! :D
                    pot_win = pot_amount - pot_dev

                    print(
                        f'Winner account is: {winner_ticket.account}. He gets {pot_win}'
                    )
                    # Make sure the win amount is sent
                    send_id = round(timenow())
                    while True:
                        try:
                            send_block = rpc.send(
                                wallet=config["wallet"],
                                source=config["account"],
                                destination=winner_ticket.account,
                                amount=int(pot_win * 10**30),
                                id=send_id)
                            if send_block:
                                print(send_block)
                                break
                        except Exception as er:
                            print(er)
                            pass
                    # sleep(0.01)
Ejemplo n.º 17
0
def pong(packet, client):
    client.latency = (timenow() - client.lastping) / 2
Ejemplo n.º 18
0
def ping(builder, client):
    client.lastping = timenow()
    return builder.get_packet()