def setAdapterConfig(self, port_start, num_things): ip = get_ip() data = { 'config': { 'pollInterval': 30, 'urls': ['http://{}:{}/'.format(ip, port) for port in range(port_start, port_start + num_things)], }, } self.put('/addons/thing-url-adapter/config', data=data)
def start(self): """Start listening for incoming connections.""" self.service_info = ServiceInfo( '_webthing._tcp.local.', '{}._webthing._tcp.local.'.format(self.name), address=socket.inet_aton(get_ip()), port=self.port, properties={ 'path': '/', }, server='{}.local.'.format(socket.gethostname())) self.zeroconf = Zeroconf() self.zeroconf.register_service(self.service_info) self.server.listen(self.port) tornado.ioloop.IOLoop.current().start()
def __init__(self, things, port=8086, hostname=None): """ Initialize the WebThingServer.\n :param things: SingleThing or MultipleThings managed by this server. :param port: port to listen on (defaults to 80) :param hostname: Optional host name, i.e. mything.com """ self.things = things self.name = things.get_name() self.port = port self.hostname = hostname self.ip = get_ip() # Resource tree creation self.app = aiocoap.resource.Site() self.app.add_resource(('authz-info', ), AuthzHandler()) self.app.add_resource(( '.well-known', 'edhoc', ), EdhocHandler()) # NOTE: As a workaround (' ',) is used instead of ('',). Usage of ('',) results in 404. coap_uri = (' ', ) self.app.add_resource(coap_uri, ThingsHandler(self.things, coap_uri)) if isinstance(things, MultipleThings): for idx, thing in enumerate(self.things.get_things()): tdx = (str(idx), ) coap_uri = tdx self.app.add_resource(coap_uri, ThingHandler(self.things, coap_uri)) self.make_handlers(tdx, thing) else: tdx = () self.make_handlers(tdx, self.things.get_thing(0))
def __init__(self, port, propertyclass=myProperty, msgq=None): self.port = port #self.hostname = '%s.local' % socket.gethostname() self.hostname = get_ip() self.tid = 'http---%s-%s' % (self.hostname, self.port) self.thing = myThing( 'urn:dev:ops:my-testthing-%s' % port, 'a testThing on port %s' % port, ['testThing'], 'A native webthing for testing' ) self.msgq = msgq self.thing.add_property( propertyclass(self.thing, 'on', Value(True), metadata={ '@type': 'OnOffProperty', 'title': 'On/Off', 'type': 'boolean', 'description': 'A boolean property of the thing', }, msgq=msgq, msgprefix=self.tid)) self.thing.add_property( propertyclass(self.thing, 'idx', Value(0), metadata={ '@type': 'IndexProperty', 'title': 'Index', 'type': 'integer', 'description': 'A numerical index of the thing', 'minimum': 0, 'maximum': 10000, 'unit': 'bar', }, msgq=msgq, msgprefix=self.tid))
def __init__(self, things, port=80, hostname=None, ssl_options=None): """ Initialize the WebThingServer. things -- things managed by this server -- should be of type SingleThing or MultipleThings port -- port to listen on (defaults to 80) hostname -- Optional host name, i.e. mything.com ssl_options -- dict of SSL options to pass to the tornado server """ self.things = things self.name = things.get_name() self.port = port self.hostname = hostname self.ip = get_ip() system_hostname = socket.gethostname() self.hosts = [ '127.0.0.1', '127.0.0.1:{}'.format(self.port), 'localhost', 'localhost:{}'.format(self.port), self.ip, '{}:{}'.format(self.ip, self.port), '{}.local'.format(system_hostname), '{}.local:{}'.format(system_hostname, self.port), ] if self.hostname is not None: self.hosts.extend([ self.hostname, '{}:{}'.format(self.hostname, self.port), ]) if isinstance(self.things, MultipleThings): for idx, thing in enumerate(self.things.get_things()): thing.set_href_prefix('/{}'.format(idx)) handlers = [ ( r'/?', ThingsHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/?', ThingHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/properties/?', PropertiesHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/properties/' + r'(?P<property_name>[^/]+)/?', PropertyHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/actions/?', ActionsHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/actions/(?P<action_name>[^/]+)/?', ActionHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/actions/' + r'(?P<action_name>[^/]+)/(?P<action_id>[^/]+)/?', ActionIDHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/events/?', EventsHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/(?P<thing_id>\d+)/events/(?P<event_name>[^/]+)/?', EventHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/authz-info', AuthzHandler, ), ( r'/.well-known/edhoc', EdhocHandler, ), ] else: # If SingleThing handlers = [ ( r'/?', ThingHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/properties/?', PropertiesHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/properties/(?P<property_name>[^/]+)/?', PropertyHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/actions/?', ActionsHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/actions/(?P<action_name>[^/]+)/?', ActionHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/actions/(?P<action_name>[^/]+)/(?P<action_id>[^/]+)/?', ActionIDHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/events/?', EventsHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/events/(?P<event_name>[^/]+)/?', EventHandler, dict(things=self.things, hosts=self.hosts), ), ( r'/authz-info/?', AuthzHandler, ), ( r'/.well-known/edhoc/?', EdhocHandler, ), ] self.app = tornado.web.Application(handlers) self.app.is_tls = ssl_options is not None self.server = tornado.httpserver.HTTPServer(self.app, ssl_options=ssl_options)
from queue import Queue import json import re import time import tornado.httpclient import tornado.websocket import websocket from webthing.utils import get_ip _TIME_REGEX = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}$' _PROTO = 'http' _BASE_URL = '{}:8888'.format(get_ip()) _PATH_PREFIX = '' _AUTHORIZATION_HEADER = None def http_request(method, path, data=None): url = _PROTO + '://' + _BASE_URL + _PATH_PREFIX + path client = tornado.httpclient.HTTPClient() headers = { 'Accept': 'application/json', } if _AUTHORIZATION_HEADER is not None: headers['Authorization'] = _AUTHORIZATION_HEADER if data is None: request = tornado.httpclient.HTTPRequest( url,