Beispiel #1
0
    def _send_request(self,
                      appliance: GeAppliance,
                      method: str,
                      uri: str,
                      key: Optional[str] = None,
                      value: Optional[str] = None,
                      message_id: Optional[str] = None):
        """
        Send a pseudo-http request to the appliance
        :param appliance: GeAppliance, the appliance to send the request to
        :param method: str, Usually "GET" or "POST"
        :param uri: str, the "endpoint" for the request, usually of the form "/UUID/erd/{erd_code}"
        :param key: The json key to set in a POST request.  Usually a four-character hex string with leading "0x".
        :param value: The value to set, usually encoded as a hex string without a leading "0x"
        :param message_id:
        """
        if method.lower() != 'post' and (key is not None or value is not None):
            raise RuntimeError('Values can only be set in a POST request')

        if message_id is None:
            message_id = self._client.new_id()
        message_body = self._format_request(message_id, uri, method, key,
                                            value)
        jid = slixmpp.JID(self._get_appliance_jid(appliance))
        self._send_raw_message(jid, message_body)
Beispiel #2
0
    async def _on_presence_unavailable(self, evt):
        """When appliance is no longer available, mark it as such."""
        jid = slixmpp.JID(evt['from']).bare

        if jid == self._client.boundjid.bare:
            return
        try:
            self._set_appliance_availability(self.appliances[jid], False)
        except KeyError:
            pass
Beispiel #3
0
    async def on_presence_unavailable(self, evt):
        """When appliance is no longer available, mark it as such."""
        jid = slixmpp.JID(evt['from']).bare

        if jid == self.boundjid.bare:
            return
        try:
            self.appliances[jid].set_unavailable()
            _LOGGER.debug(f'Appliance {jid} marked unavailable')
        except KeyError:
            pass
Beispiel #4
0
    async def _on_presence_available(self, evt: slixmpp.ElementBase):
        """Perform actions when notified of an available JID."""
        await asyncio.sleep(2)  # Wait 2 seconds to give it time to register
        jid = slixmpp.JID(evt['from']).bare

        if jid == self._client.boundjid.bare:
            return
        try:
            await self._set_appliance_availability(self.appliances[jid], True)
        except KeyError:
            await self._add_appliance(jid)
            self.appliances[jid].set_available()
Beispiel #5
0
    async def on_presence_available(self, evt: slixmpp.ElementBase):
        """Perform actions when notified of an available JID."""
        await asyncio.sleep(2)  # Wait 2 seconds to give it time to register
        jid = slixmpp.JID(evt['from']).bare

        if jid == self.boundjid.bare:
            return
        try:
            self.appliances[jid].set_available()
            _LOGGER.debug(f'Appliance {jid} marked available')
        except KeyError:
            await self.add_appliance(jid)
            self.appliances[jid].set_available()
Beispiel #6
0
async def handle(request):
    "Handle the HTTP request and block until the vcard is fetched"
    err_404 = web.Response(status=404, text='Not found')
    try:
        jid = slixmpp.JID(request.match_info.get('jid', ""))
    except slixmpp.InvalidJID:
        return err_404
    else:
        if not jid:
            return err_404
    try:
        vcard = await XMPP.fetch_vcard(jid_to=jid)
    except Exception:
        log.warning("Failed to fetch vcard for %s", jid, exc_info=True)
        return err_404

    reply = parse_vcard(vcard)
    return web.Response(**reply)
Beispiel #7
0
 async def on_message(self, event):
     """Handle incoming messages."""
     global set_timer
     msg = str(event)
     msg_from = slixmpp.JID(event['from']).bare
     try:
         message_data = self.extract_message_json(msg)
     except ValueError:
         _LOGGER.info(f"From: {msg_from}: Not a GE message")
         return
     try:
         appliance = self.appliances[msg_from]
         state_changes = appliance.update_erd_values(message_data)
         if state_changes:
             self.event(EVENT_APPLIANCE_STATE_CHANGE,
                        [appliance, state_changes])
     except KeyError:
         _LOGGER.warning(
             'Received json message from unregistered appliance')