def testDelayedRequestCancel(self):
        self.stream_start(mode='component', plugins=['xep_0030', 'xep_0323'])

        myDevice = Device("Device22")
        myDevice._add_field(name="Temperature", typename="numeric", unit="°C")
        myDevice._set_momentary_timestamp("2013-03-07T16:24:30")
        myDevice._add_field_momentary_data("Temperature",
                                           "23.4",
                                           flags={"automaticReadout": "true"})

        self.xmpp['xep_0323'].register_node(nodeId="Device22",
                                            device=myDevice,
                                            commTimeout=0.5)

        dtnow = datetime.datetime.now()
        ts_2sec = datetime.timedelta(0, 2)
        dtnow_plus_2sec = dtnow + ts_2sec
        when_flag = dtnow_plus_2sec.replace(microsecond=0).isoformat()

        self.recv("""
            <iq type='get'
                from='[email protected]/amr'
                to='*****@*****.**'
                id='1'>
                <req xmlns='urn:xmpp:iot:sensordata' seqnr='1' momentary='true' when='"""
                  + when_flag + """'/>
            </iq>
        """)

        self.send("""
            <iq type='result' 
                from='*****@*****.**'
                to='[email protected]/amr'
                id='1'>
                <accepted xmlns='urn:xmpp:iot:sensordata' seqnr='1' queued='true' />
            </iq>
            """)

        self.recv("""
            <iq type='get'
                from='[email protected]/amr'
                to='*****@*****.**'
                id='1'>
                <cancel xmlns='urn:xmpp:iot:sensordata' seqnr='1' />
            </iq>
            """)

        self.send("""
            <iq type='result'
                from='*****@*****.**'
                to='[email protected]/amr'
                id='1'>
                <cancelled xmlns='urn:xmpp:iot:sensordata' seqnr='1' />
            </iq>
            """)

        # Test cancel of non-existing request
        self.recv("""
            <iq type='get'
                from='tester@localhost'
                to='*****@*****.**'
                id='1'>
                <cancel xmlns='urn:xmpp:iot:sensordata' seqnr='1' />
            </iq>
            """)

        self.send("""
            <iq type='error'
                from='*****@*****.**'
                to='tester@localhost'
                id='1'>
                <rejected xmlns='urn:xmpp:iot:sensordata' seqnr='1'>
                    <error>Cancel request received, no matching request is active.</error>
                </rejected>
            </iq>
            """)

        time.sleep(2)

        # Ensure we don't get anything after cancellation
        self.send(None)
    def testRequestFieldFromTo(self):

        self.stream_start(mode='component', plugins=['xep_0030', 'xep_0323'])

        myDevice = Device("Device44")
        myDevice._add_field(name='Voltage', typename="numeric", unit="V")
        myDevice._add_field_timestamp_data(name="Voltage",
                                           value="230.1",
                                           timestamp="2000-01-01T00:01:02",
                                           flags={"invoiced": "true"})
        myDevice._add_field_timestamp_data(name="Voltage",
                                           value="230.2",
                                           timestamp="2000-02-01T00:01:02",
                                           flags={"invoiced": "true"})
        myDevice._add_field_timestamp_data(name="Voltage",
                                           value="230.3",
                                           timestamp="2000-03-01T00:01:02",
                                           flags={"invoiced": "true"})

        self.xmpp['xep_0323'].register_node('Device44',
                                            myDevice,
                                            commTimeout=0.5)

        print("."),

        self.recv("""
            <iq type='get'
                from='[email protected]/amr'
                to='*****@*****.**'
                id='6'>
                <req xmlns='urn:xmpp:iot:sensordata' seqnr='6' from='2000-01-01T00:01:03' to='2000-02-02T00:00:01'>
                    <field name='Voltage'/>
                </req>
            </iq>
        """)

        self.send("""
            <iq type='result' 
                from='*****@*****.**'
                to='[email protected]/amr'
                id='6'>
                <accepted xmlns='urn:xmpp:iot:sensordata' seqnr='6'/>
            </iq>
            """)

        self.send("""
            <message from='*****@*****.**'
                     to='[email protected]/amr'>
                <fields xmlns='urn:xmpp:iot:sensordata' seqnr='6'>
                    <node nodeId='Device44'>
                        <timestamp value='2000-02-01T00:01:02'>
                            <numeric name='Voltage' invoiced='true' value='230.2' unit='V'/> 
                        </timestamp>
                    </node>
                </fields>
            </message>            
            """)

        self.send("""
            <message from='*****@*****.**'
                     to='[email protected]/amr'>
                <fields xmlns='urn:xmpp:iot:sensordata' seqnr='6' done='true'>
                </fields>
            </message>            
            """)