コード例 #1
0
ファイル: app.py プロジェクト: v222008/iot-platform
 def setup_modules(self):
     # Base config
     self.config = SimpleConfig()
     self.config.add_param('configured', False)
     self.config.add_param(
         'hostname',
         'neopixel_{:s}'.format(platform.utils.mac_last_digits()))
     # Setup remote logging
     self.rlogging = RemoteLogging(self.config)
     # MQTT
     self.mqtt = tinymqtt.MQTTClient('neopixelctrl-{:s}'.format(
         platform.utils.mac_last_digits()),
                                     config=self.config)
     # DNS
     self.dns = tinydns.Server(ttl=10)
     # Modules
     self.ambi = AmbientLightAnalogSensor(self.config, self.mqtt,
                                          machine.ADC(0))
     self.setupbtn = SetupButton(self.config, machine.Pin(setup_btn_pin))
     self.status = StatusLed(self.config, machine.Pin(status_led_pin))
     # WebServer
     self.web = tinyweb.webserver(max_concurrency=1, debug=True)
     # LED strip handler (+ associated web routes)
     self.neo = NeopixelStrip(machine.Pin(neopixel_pin), self.config,
                              self.web, self.mqtt, self.loop)
コード例 #2
0
 def testUnknownIgnore(self):
     # Re-create server with empty list
     self.dns = tinydns.Server({}, ignore_unknown=True)
     # Prepare mock socket to d ya.com query
     msock = MockSocket(ya_com_rq)
     self.dns.sock = msock
     # Emulate that receiving of packet
     itr = self.dns.__handler()
     self.assertIsInstance(next(itr), IORead)
     self.assertIsInstance(next(itr), IORead)
     # Check result - should be not response sent
     self.assertEqual(msock.addr, None)
     self.assertEqual(msock.pack, None)
コード例 #3
0
 def testUnknown(self):
     # Re-create server with empty list
     self.dns = tinydns.Server({})
     # Prepare mock socket to d ya.com query
     msock = MockSocket(ya_com_rq)
     self.dns.sock = msock
     # Emulate that receiving of packet
     itr = self.dns.__handler()
     self.assertIsInstance(next(itr), IORead)
     self.assertIsInstance(next(itr), IORead)
     # Check result
     self.assertEqual(msock.addr, 100)
     self.assertEqual(msock.pack, ya_com_rp2)
コード例 #4
0
 def testAddDomain(self):
     # Re-create DNS class with empty list
     self.dns = tinydns.Server(ttl=33)
     self.dns.add_domain('google.com', '1.1.1.1')
     # Prepare mock socket to send google.com query
     msock = MockSocket(google_rq)
     self.dns.sock = msock
     # Emulate that receiving of packet
     itr = self.dns.__handler()
     self.assertIsInstance(next(itr), IORead)
     self.assertIsInstance(next(itr), IORead)
     # Check result - response should be "sent"
     self.assertEqual(msock.addr, 100)
     self.assertEqual(msock.pack, mk_dns_response(google_rp, '1.1.1.1', 33))
コード例 #5
0
ファイル: network.py プロジェクト: xsteadfastx/henri
def create_dns():
    dns = tinydns.Server(domains={"henri.processor": "192.168.95.1"})
    dns.run(host="192.168.95.1", port=53)
コード例 #6
0
 def setUp(self):
     self.dns = tinydns.Server(domains={
         'ya.com': '192.168.5.1',
         'google.com': '1.1.1.1'
     },
                               ttl=33)
コード例 #7
0
def main():
    # Some ports requires to allocate extra mem for exceptions
    if hasattr(micropython, 'alloc_emergency_exception_buf'):
        micropython.alloc_emergency_exception_buf(100)

    loop = asyncio.get_event_loop()
    logging.basicConfig(level=logging.DEBUG)

    # Base config
    config = SimpleConfig()
    config.add_param('configured', False)
    wsetup = WifiSetup(config)

    # MQTT
    mqtt = tinymqtt.MQTTClient('LEDcontroller-{}'.format(
        platform.utils.mac_last_digits()), config=config)

    # DNS
    dns = tinydns.Server(ttl=10)

    # WebServer
    web = tinyweb.webserver()

    # Enable REST API for config & wifi
    web.add_resource(config, '/config')
    web.add_resource(wsetup, '/wifi')

    # Create LED strip handler
    WhiteLedStrip(machine.Pin(green_pin), config, web, mqtt, loop)

    # Peripheral modules
    setupbtn = SetupButton(config, None)

    # Other web routes
    @web.route('/')
    async def index(req, resp):
        if config.configured:
            await resp.redirect('/dashboard')
        else:
            await resp.redirect('/setup')

    @web.route('/dashboard')
    async def page_dashboard(req, resp):
        await resp.send_file('dashboard_all.html.gz',
                             content_encoding='gzip',
                             content_type='text/html')

    @web.route('/setup')
    async def page_setup(req, resp):
        await resp.send_file('setup_all.html.gz',
                             content_encoding='gzip',
                             content_type='text/html')

    # Setup AP parameters
    ap_if = network.WLAN(network.AP_IF)
    essid = b'LedCtrl-%s' % platform.utils.mac_last_digits()
    ap_if.active(True)
    ap_if.config(essid=essid, authmode=network.AUTH_WPA_WPA2_PSK, password=b'ledledled')
    ap_if.ifconfig(('192.168.168.1', '255.255.255.0', '192.168.168.1', '192.168.168.1'))
    ap_if.active(False)
    # Captive portal
    platform.utils.captiveportal.enable(web, dns, '192.168.168.1')

    # Load configuration
    try:
        config.load()
    except Exception as e:
        log.warning('Config load failed: {}'.format(e))
        pass

    # Main loop
    try:
        wport = 80
        dport = 53
        if platform.utils.is_emulator():
            wport = 8080
            dport = 5335
        # Start services
        dns.run(host='0.0.0.0', port=dport, loop=loop)
        web.run(host='0.0.0.0', port=wport, loop_forever=False, loop=loop)
        mqtt.run(loop)
        setupbtn.run(loop)

        # Run main loop
        loop.run_forever()
    except KeyboardInterrupt as e:
        if platform.utils.is_emulator():
            for s in [web, dns, mqtt]:
                s.shutdown()
            loop.run_until_complete(shutdown_wait())
        else:
            raise
    except Exception as e:
        log.exc(e, "Unhandled exception")