コード例 #1
0
ファイル: contrib.py プロジェクト: doudz/pySensors
def pGateway(callback):
    print('Starting Gateway, press Ctrl+C to exit')
    gw = MySensor()
    gw.begin(callback,0,True,0)
    
    try:
        while True:
            gw.process()
            time.sleep(SLEEP)
    except KeyboardInterrupt:
        print("\n Program stopped \n")
コード例 #2
0
ファイル: contrib.py プロジェクト: doudz/pySensors
 def __init__(self, event_callback=None, persistence=False,
              persistence_file="mysensors.pickle", protocol_version="1.5"):
     threading.Thread.__init__(self)
     Gateway.__init__(self, event_callback, persistence, persistence_file,
                      protocol_version)
     self._stop_event = threading.Event()
     self.gw = MySensor()
     self.gw.begin(self._callback,0,True,0)
コード例 #3
0
ファイル: contrib.py プロジェクト: doudz/pySensors
def RepeaterNode():
    print('Starting RepeaterNode, press Ctrl+C to exit')
    gw = MySensor()
    # The third argument enables repeater mode.
    gw.begin(None, AUTO, True);
    #Send the sensor node sketch version information to the gateway
    gw.sendSketchInfo("Repeater Node", "1.0");
    try:
        while True:
            gw.process()
            time.sleep(SLEEP)
    except KeyboardInterrupt:
        print("\n Program stopped \n")
コード例 #4
0
ファイル: contrib.py プロジェクト: doudz/pySensors
class pyGateway(Gateway, threading.Thread):
    """ MySensors gateway """

    def __init__(self, event_callback=None, persistence=False,
                 persistence_file="mysensors.pickle", protocol_version="1.5"):
        threading.Thread.__init__(self)
        Gateway.__init__(self, event_callback, persistence, persistence_file,
                         protocol_version)
        self._stop_event = threading.Event()
        self.gw = MySensor()
        self.gw.begin(self._callback,0,True,0)
        
    def _callback(self,gw,message):
        data = u'{0.sender};{0.destination};{0.command};{0.ack};{0.type};{0.payload}'.format(message)
        print(type(data),data)
#         response = self.logic(data)
        response = self.logic(message)
        print('response',response)
        if response:
            print('response',response.encode())
            print(response.node_id,response.child_id,response.type,response.ack,response.sub_type,response.payload)
            msg = MyMessage(response.node_id,response.sub_type)
            print('type,command',response.type)
            print('subtype,type',response.sub_type)
            msg.sender = gw.nc.nodeId
            msg.destination = response.node_id
            msg.miSetRequestAck(response.ack)
            msg.miSetCommand(response.type)
            if response.sub_type in [4,8]:
                msg.set_byte(chr(response.payload))
            else:
                msg.set(response.payload)
            #msg.set_byte(chr(response.payload))
            print(msg.data)
            gw.sendRoute(msg)
        

    def stop(self):
        """ Stops the background thread. """
        self._stop_event.set()

    def run(self):
        """ Background thread that reads messages from the gateway. """
        while not self._stop_event.is_set():
            self.gw.process()
            time.sleep(SLEEP)
            
#             try:
#                 msg = line.decode('utf-8')
#                 response = self.logic(msg)
#             except ValueError:
#                 LOGGER.warning('Error decoding message from gateway, probably received bad byte.')
#                 continue
#             if response is not None:
#                 try:
#                     self.send(response.encode())
#                 except ValueError:
#                     LOGGER.exception('Invalid response')
#                     continue

    def send(self, message):
        """ Writes a Message to the gateway. """
        print('send',message)