Пример #1
0
def pubtest(delay=0):
    sleep(delay)
    m = Message()
    m.msg = {"data": [["0", 1]], "timestamp": timestamp}
    r.publish("data", m.as_json())
    print m.as_json()

    return
Пример #2
0
    def read_serial_data(self):

        Msg = Message(self.signature)
        bytes_in_waiting = self.serial.inWaiting()                
        if bytes_in_waiting:
            new_data = self.serial.read(bytes_in_waiting)
            self.buffer = self.buffer + new_data
        else:
            sleep(0.1)

        crlf_index = self.buffer.find('\r\n')

        if crlf_index > -1:
            self.last_read_line = self.buffer[0:crlf_index]            
            temp = self.re_data.findall(self.last_read_line)
            self.log.debug('read self.last_read_line: ' + self.last_read_line)

            if len(temp):
                final_data = dict()
                timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
                final_data['timestamp'] = timestamp
                final_data['raw']       = self.last_read_line
                try:
                    final_data.update({'cmd_number' : sjson.loads(temp[0][0])})
                    final_data.update(sjson.loads(temp[0][1]))
                    self.log.debug('.....updated final_data')

                except Exception as E:
                    final_data.update({'cmd_number' : -1})
                    error_msg = {'timestamp' : timestamp, 'from': self.signature, 'source' : 'ComPort', 'function' : 'def run() - inner', 'error' : E.message}
                    Msg.msg = error_msg
                    self.log.error(Msg.msg)

                Msg.msg = final_data
                self.log.debug("final_data={}".format(final_data))
                self.redis.publish(self.redis_pub_channel, Msg.as_jsno())
                #self.log.debug('.....publish to :' + self.redis_pub_channel)
                self.redis.set(self.redis_read_key,Msg.as_jsno())                
                #self.log.debug('.....empty buffer')
                self.buffer = self.buffer[crlf_index+2:]
            else:
                self.buffer = ''
                self.send('Z')
                self.log.debug('.....reseting command number')
                pass
Пример #3
0
def StarDigitempSubmit(channel, host='0.0.0.0', submit_to='192.168.1.10'):
    """
    :param channel:   - publish channel
    :param host:      - redis host
    :param submit_to: - server running sensoredweb
    :return:          - no ret value

    data_set = [[0, '1030B8D2010800BC', 20.1875],
     [1, '1002BDD2010800ED', 22.375],
     [2, '10F237C0010800D6', 23.5625],
     [3, '10EAB6D201080015', 22.9375],
     [4, '109A3FD30108003A', 23.9375],
     [5, '10F3F1D201080060', 22.875],
     [6, '101BBFD2010800A3', 22.75],
     [7, '109729C001080020', 24.875]]
    """

    print"StartIqrSubmit(%s, %s, %s)" % (channel, host, submit_to)
    D = dt.Digitemp()
    R = Redis()
    Q = Queue(connection=R)
    threshold = 0.5;
    try:
        not_done = True
        while not_done:
            timestamp = datetime.datetime.now()
            data_set     = D.GetData()
            print data_set
            M = Message()
            M.msg = {'data' : data_set}
            R.publish(channel, M.as_jsno())

            last_enqueue = Q.enqueue(submit, data_set,\
                                    timestamp=timestamp,\
                                    submit_to=submit_to,\
                                    threshold=threshold)
            #not_done = False

    except KeyboardInterrupt:
        pass
    print "Exiting " + __name__
Пример #4
0
    def cmd_via_redis_subscriber(self):
        self.Log.debug("cmd_via_redis_subscriber()")
        self.pubsub.subscribe(self.channel)

        while self._redis_subscriber_alive:
            try:
                for item in self.pubsub.listen():
                    self.Log.debug(item)
                    if item["data"] == "unsubscribe":
                        self.pubsub.unsubscribe()
                        self.Log.info("unsubscribed and finished")
                        break
                    else:
                        cmd = item["data"]
                        if isinstance(cmd, str):
                            self.Log.debug("cmd_via_redis_subscriber() cmd = {}".format(cmd))

                            try:
                                cmd_obj = deserialize(cmd)
                                if isinstance(cmd_obj, dict):
                                    self.Log.debug(cmd_obj)
                                    cmd_str = cmd_obj["cmd"]
                                    if cmd_str == "GetStatusOfAllDevices":
                                        res = self.GetStatusOfAllDevices()
                                        final_data = dict()
                                        timestamp = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
                                        final_data["timestamp"] = timestamp
                                        final_data["raw"] = res
                                        final_data["data"] = res
                                        M = Message("InsteonPLM")
                                        M.msg = final_data
                                        self.redis.publish("data", M.as_json())
                                    if cmd_str == "GetLightLevel":
                                        addr_str = cmd_obj["addr"]
                                        res = self.GetLightLevel(addr_str)
                                    if cmd_str == "SetLightLevel":
                                        addr_str = cmd_obj["addr"]
                                        val = cmd_obj["val"]
                                        res = self.SetLightLevel(addr_str, val)
                                    if cmd_str == "SetSwOn":
                                        addr_str = cmd_obj["addr"]
                                        res = self.SetSwOn(addr_str)
                                    if cmd_str == "SetSwOff":
                                        addr_str = cmd_obj["addr"]
                                        res = self.SetSwOff(addr_str)

                                else:
                                    addr = str2hex(cmd_obj[0])
                                    insteon_cmd = cmd_obj[1]
                                    val = cmd_obj[2]
                                    res = self.send_sd_cmd(addr, insteon_cmd, val)
                                    self.Log.debug(res)
                                    self.redis.publish(self.channel + "_res", serialize(res))

                            except Exception as E:
                                error_msg = {
                                    "source": "serinsteon:cmd_via_redis_subscriber",
                                    "function": "def run(self):",
                                    "error": E.message,
                                }
                                self.Log.error(error_msg)

                        else:
                            self.Log.debug(cmd)
            except Exception as E:
                error_msg = {"source": "InsteonSub", "function": "def run(self):", "error": E.message}
                self.Log.error(error_msg)
        self.Log.debug("end of cmd_via_redis_subscriber()")
Пример #5
0
    def reader(self):
        '''
        Run is the function that runs in the new thread and is called by        
        '''
        
        try:
            self.log.debug('Starting the listner thread')
            Msg = Message(self.signature)

            while self.alive and self._reader_alive:
                bytes_in_waiting = self.serial.inWaiting()
                
                if bytes_in_waiting:
                    new_data = self.serial.read(bytes_in_waiting)
                    self.buffer = self.buffer + new_data
                else:
                    sleep(0.1)


                crlf_index = self.buffer.find('\r\n')

                if crlf_index > -1:
                    line = self.buffer[0:crlf_index]
                    temp = self.re_data.findall(line)
                    self.log.debug('read line: ' + line)

                    if len(temp):
                        final_data = dict()
                        timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
                        final_data['timestamp'] = timestamp
                        final_data['raw']       = line
                        try:
                            final_data.update({'cmd_number' : sjson.loads(temp[0][0])})
                            final_data.update(sjson.loads(temp[0][1]))
                            self.log.debug('.....updated final_data')

                        except Exception as E:
                            final_data.update({'cmd_number' : -1})
                            error_msg = {'timestamp' : timestamp, 'from': self.signature, 'source' : 'ComPort', 'function' : 'def run() - inner', 'error' : E.message}
                            Msg.msg = error_msg
                            self.log.error(Msg.msg)

                        Msg.msg = final_data
                        self.log.debug("final_data={}".format(final_data))
                        self.redis.publish(self.redis_pub_channel, Msg.as_jsno())
                        #self.log.debug('.....publish to :' + self.redis_pub_channel)
                        self.redis.set(self.redis_read_key,Msg.as_jsno())

                        self.buffer = self.buffer[crlf_index+2:]
                        #self.log.debug('.....empty buffer')
                    else:
                        #self.buffer = ''
                        #self.send('Z')
                        #self.log.debug('.....reseting command number')
                        pass

        except Exception as E:
            error_msg = {'source' : 'ComPort', 'function' : 'def run() - outter', 'error' : E.message}
            self.log.error("Exception occured, within the run function: %s" % E.message)
        
        self.log.debug('Exiting run() function')