def get_reading(self):
        """
        Request a reading from a sensor.
        The process is:
            send the request
            wait for response
            if a response is detected, store it
            else, if there is a timeout while waiting,
                continue with the previous stored value

        :rtype: int or None

        """
        time.sleep(self.SEND_DELAY)

        send_data = ''.join([self.addr, GET_READ, '        '])
        radio.write(send_data)

        timer = Timer(NODE_TIMEOUT)
        radio.startListening()
        while not radio.available(pipe, True) and timer():
            time.sleep(1000 / 1000000.0)
        recv_buffer = []
        radio.read(recv_buffer, radio.getDynamicPayloadSize())
        recv_data = ''.join(chr(i) for i in recv_buffer)
        radio.stopListening()

        dest = recv_data[:2]
        source = recv_data[2:4]
        #print 'Got packet: {}'.format(recv_data)
        if (source == self.addr) and (dest == GATEWAY_ADDR):
            self.current_read = int(recv_data[-4:])
            print clr(
                'sensor {}, received {}'.format(self.addr, self.current_read),
                'cyan')
    def get_reading(self):
        """
        Request a reading from a sensor.
        The process is:
            send the request
            wait for response
            if a response is detected, store it
            else, if there is a timeout while waiting,
                continue with the previous stored value

        :rtype: int or None

        """
        time.sleep(self.SEND_DELAY)

        send_data = ''.join([self.addr, GET_READ, '        '])
        radio.write(send_data)

        timer = Timer(NODE_TIMEOUT)
        radio.startListening()
        while not radio.available(pipe, True) and timer():
            time.sleep(1000/1000000.0)
        recv_buffer = []
        radio.read(recv_buffer, radio.getDynamicPayloadSize())
        recv_data = ''.join(chr(i) for i in recv_buffer)
        radio.stopListening()

        dest = recv_data[:2]
        source = recv_data[2:4]
        #print 'Got packet: {}'.format(recv_data)
        if (source == self.addr) and (dest == GATEWAY_ADDR):
            self.current_read = int(recv_data[-4:])
            print clr('sensor {}, received {}'.format(self.addr, self.current_read),
                        'cyan')
    def _execute(self, task):
        """
        A task is a list, formatted like this:
        [callable, [args]]

        The [args] list can have any length, and
        it doesn't matter, since we use the star (*)
        to unpack it.

        """
        routine = task[0]
        args = task[1]
        print clr('Executing task:{} with args:{}'.format(routine.__name__, args),
                    'red', False)
        #task[0](*task[1])
        routine(*args)
示例#4
0
    def _execute(self, task):
        """
        A task is a list, formatted like this:
        [callable, [args]]

        The [args] list can have any length, and
        it doesn't matter, since we use the star (*)
        to unpack it.

        """
        routine = task[0]
        args = task[1]
        print clr(
            'Executing task:{} with args:{}'.format(routine.__name__, args),
            'red', False)
        #task[0](*task[1])
        routine(*args)
示例#5
0
    def on_modified(self, event):
        """
        the json file has the following format:
        {'light_xy_state': 'on' or 'off' or 'auto',
         'light_xy_int'  : '0' or '10' .... or '100',
         .
         .
         'thresh'        : '0' to '800'
        }

        """
        global gui_data
        print clr('Received command from WebGUI', 'green')
        with open(GUI_CONF_PATH, 'r') as f:
            gui_new = json.loads(f.read())
        # update light instance and class attributes
        for key, val in gui_new.items():
            if key == 'thresh':
                Light.thresh = int(val)
            elif key[9:] == 'state':
                Light.mapping.get(key[6:8]).state = val
            elif key[9:] == 'int':
                Light.mapping.get(
                    key[6:8]).intens = int(val) if val is not None else 50
        # queue any dimming tasks, if necessary
        for light in Light.instances:
            state = ''.join(['light_', light.addr, '_state'])
            intens = ''.join(['light_', light.addr, '_int'])

            if gui_new.get(state) == 'auto':
                continue
            if gui_new.get(state) == 'off' and gui_new.get(
                    state) != gui_data.get(state):
                task = [light.dim_to_val, [0]]
                self.queue.put(task)
                continue
            if gui_new.get(state) != gui_data.get(state) or gui_new.get(
                    intens) != gui_data.get(intens):
                task = [light.dim_to_val, [light.intens]]
                self.queue.put(task)

        gui_data = gui_new
    def on_modified(self, event):
        """
        the json file has the following format:
        {'light_xy_state': 'on' or 'off' or 'auto',
         'light_xy_int'  : '0' or '10' .... or '100',
         .
         .
         'thresh'        : '0' to '800'
        }

        """
        global gui_data
        print clr('Received command from WebGUI', 'green')
        with open(GUI_CONF_PATH, 'r') as f:
            gui_new = json.loads(f.read())
        # update light instance and class attributes
        for key, val in gui_new.items():
            if key=='thresh':
                Light.thresh = int(val)
            elif key[9:]=='state':
                Light.mapping.get(key[6:8]).state = val
            elif key[9:]=='int':
                Light.mapping.get(key[6:8]).intens = int(val) if val is not None else 50
        # queue any dimming tasks, if necessary
        for light in Light.instances:
            state = ''.join(['light_', light.addr, '_state'])
            intens = ''.join(['light_', light.addr, '_int'])

            if gui_new.get(state) == 'auto':
                continue
            if gui_new.get(state) == 'off' and gui_new.get(state) != gui_data.get(state):
                task = [light.dim_to_val, [0]]
                self.queue.put(task)
                continue
            if gui_new.get(state) != gui_data.get(state) or gui_new.get(intens) != gui_data.get(intens):
                task = [light.dim_to_val, [light.intens]]
                self.queue.put(task)

        gui_data = gui_new
            for sct in DissipationSensor.instances:
                task = [_db_insert, [sct]]
                queue.put(task)
                task = [_mqtt_publish, [sct]]
                queue.put(task)
            timer = Timer(15)

        task = [request_readings, [LightSensor]]
        queue.put(task)

        task = [adjust_lights, []]
        queue.put(task)


        #OPERATION_CYCLE = 1 # debugging
        time.sleep(OPERATION_CYCLE)

except KeyboardInterrupt:
    print clr('Wait for all tasks to be completed...', 'purple')
    queue.join()
    prompt = raw_input('Close all lights? (y/[n]) ')
    if prompt == 'y':
        close_all_lights()

    cur.close()
    db.close()
    mqttc.disconnect()
    GPIOcleanup()
    print 'EXITING'
示例#8
0
            queue.put(task)

            for sct in DissipationSensor.instances:
                task = [_db_insert, [sct]]
                queue.put(task)
                task = [_mqtt_publish, [sct]]
                queue.put(task)
            timer = Timer(15)

        task = [request_readings, [LightSensor]]
        queue.put(task)

        task = [adjust_lights, []]
        queue.put(task)

        #OPERATION_CYCLE = 1 # debugging
        time.sleep(OPERATION_CYCLE)

except KeyboardInterrupt:
    print clr('Wait for all tasks to be completed...', 'purple')
    queue.join()
    prompt = raw_input('Close all lights? (y/[n]) ')
    if prompt == 'y':
        close_all_lights()

    cur.close()
    db.close()
    mqttc.disconnect()
    GPIOcleanup()
    print 'EXITING'