From f7ba31c2d2b87e2c578a4eb21f34a9c0bb088c04 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Fri, 23 Nov 2018 17:15:56 +0100 Subject: [PATCH] change chat to irc --- .project | 11 ++++++++ data/ircConfig.json | 6 +++++ data/pixelConfig.json | 2 +- data/www/index.html | 8 +++--- data/www/script.js | 40 +++++++++++++++++++++++++----- platformio.ini | 4 +-- src/var/illuchat/illuchat_config.h | 10 ++++---- src/var/illuchat/main.cpp | 34 +++++++++++-------------- 8 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 .project create mode 100644 data/ircConfig.json diff --git a/.project b/.project new file mode 100644 index 0000000..1a19211 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + illucat + + + + + + + + diff --git a/data/ircConfig.json b/data/ircConfig.json new file mode 100644 index 0000000..4390db3 --- /dev/null +++ b/data/ircConfig.json @@ -0,0 +1,6 @@ +{ + "ircServer": "chat.freenode.net", + "ircPort": 6665, + "ircNickname": "redcat", + "ircUser": "redcat" +} \ No newline at end of file diff --git a/data/pixelConfig.json b/data/pixelConfig.json index d45d7b9..8adce06 100644 --- a/data/pixelConfig.json +++ b/data/pixelConfig.json @@ -1,7 +1,7 @@ { "pin": 4, "length": 8, - "brightness": 100, + "brightness": 127, "updateInterval": 100, "defaultColor": 100 } \ No newline at end of file diff --git a/data/www/index.html b/data/www/index.html index 34223a0..a42d1a2 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -2,7 +2,7 @@ - ESP Kit + IlluCat @@ -73,8 +73,10 @@

NeoPixel

-

MQTT

-
+ +

IRC

+
diff --git a/data/www/script.js b/data/www/script.js index 152482a..b2f0c68 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -12268,8 +12268,11 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default super(ctx, node, template || __WEBPACK_IMPORTED_MODULE_1__Chat_html___default.a); this.render(this.config); this.ctx.mediator.on(this.config.topic, this.onMessage.bind(this)); + this.ctx.mediator.on("chat/connected", this.connected.bind(this)); + //this.ctx.mediator.on("irc/configValid", this.configValid.bind(this)); this.node.delegate('input.msg', 'keypress', this.onInput.bind(this)); - this.node.delegate('button', 'click', this.send.bind(this)); + this.node.delegate('button.send', 'click', this.send.bind(this)); + this.node.delegate('button.join', 'click', this.join.bind(this)); } templates() { @@ -12279,17 +12282,43 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default ${user} ${msg} + `, + serverMessage: (msg) => ` +
  • + ${msg} +
  • ` }; } + configValid() { + this.node.text("Please configure first"); + } + + join(evt) { + evt.preventDefault(); + let message = JSON.stringify({ + topic: 'irc/join', + payload: this.sanitizeInput(this.node.find('.channel').val()) + }); + this.ctx.ws.send(message); + this.node.find('.controls').show(); + //this.node.find('button.join').hide(); + } + connected() { + this.node.find('.controls').show(); + this.node.find('button.connect').hide(); + } + onMessage(msg) { let payload = msg.payload; //.replace(/<.+?>/g, ''); //console.log('onMsg: ' + msg); let msgParts = payload.split(':'); let messages = this.node.find('.messages'); messages.append( - this.templates().message(msgParts[0], msgParts[1] ? msgParts[1] : '') + msgParts.length == 2 ? + this.templates().message(msgParts[0], this.sanitizeInput(msgParts[1])) + : this.templates().serverMessage(this.sanitizeInput(payload)) ); this.node.find('.message-container').animate({ scrollTop: messages[0].scrollHeight @@ -12300,12 +12329,11 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default } send(evt) { evt.preventDefault(); - let username = this.node.find('input.username'); let msg = this.node.find('input.msg'); - if (username.length > 0 && msg.length > 0) { + if (msg.length > 0) { let message = JSON.stringify({ topic: this.sanitizeInput(this.config.topic), - payload: this.sanitizeInput(username.val() + ':' + msg.val()) + payload: this.sanitizeInput(msg.val()) }); this.ctx.ws.send(message); msg.val(''); @@ -12325,7 +12353,7 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default /* 31 */ /***/ (function(module, exports) { -module.exports = "
    \n
      \n
    • \n \n \n
    • \n
    \n
    \n
      \n
      \n
        \n
      • \n \n \n
      • \n
      \n \n
      " +module.exports = "
      \n
        \n
      • \n \n \n \n \n \n
      • \n
      \n
      \n
        \n
        \n
          \n
        • \n \n \n
        • \n
        \n
        " /***/ }) /******/ ]); \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 5253e31..8860265 100644 --- a/platformio.ini +++ b/platformio.ini @@ -99,5 +99,5 @@ build_flags = -Wl,-Teagle.flash.4m1m.ld lib_deps = ${common.lib_deps} https://gitlab.com/wirelos/sprocket-network-wifi.git https://gitlab.com/wirelos/sprocket-plugin-web.git - https://gitlab.com/wirelos/sprocket-plugin-mqtt.git - PubSubClient \ No newline at end of file + https://gitlab.com/wirelos/sprocket-plugin-irc.git + ArduinoIRC diff --git a/src/var/illuchat/illuchat_config.h b/src/var/illuchat/illuchat_config.h index cad44b8..2acadb1 100644 --- a/src/var/illuchat/illuchat_config.h +++ b/src/var/illuchat/illuchat_config.h @@ -26,6 +26,7 @@ // config files #define PIXEL_CONFIG_FILE "/pixelConfig.json" #define MQTT_CONFIG_FILE "/mqttConfig.json" +#define IRC_CONFIG_FILE "/ircConfig.json" // NeoPixel #define LED_STRIP_PIN D2 @@ -36,11 +37,10 @@ #define COLOR_CONNECTED LED_STRIP_DEFAULT_COLOR #define COLOR_NOT_CONNECTED 255 -// mqtt config -#define MQTT_CLIENT_NAME "illucat" -#define MQTT_HOST "192.168.1.2" -#define MQTT_PORT 1883 -#define MQTT_ROOT_TOPIC "wirelos/illucat" +#define IRC_SERVER "chat.freenode.net" +#define IRC_PORT 6665 +#define IRC_NICKNAME "" +#define IRC_USER "" // OTA config #define OTA_PORT 8266 diff --git a/src/var/illuchat/main.cpp b/src/var/illuchat/main.cpp index ecf0dcb..ea8015b 100644 --- a/src/var/illuchat/main.cpp +++ b/src/var/illuchat/main.cpp @@ -7,8 +7,7 @@ #include #include #include -#include -#include +#include WiFiNet *network; Sprocket *sprocket; @@ -16,25 +15,23 @@ WebServerPlugin *webServerPlugin; WebConfigPlugin *webConfigPlugin; WebApiPlugin *webApiPlugin; PixelPlugin *pixelPlugin; -MqttPlugin *mqttPlugin; -OtaTcpPlugin *otaTcpPlugin; +IrcPlugin *ircPlugin; void setup() { + const char *chipName = String("Sprocket" + String(ESP.getChipId())).c_str(); sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); pixelPlugin = new PixelPlugin({LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL}); - mqttPlugin = new MqttPlugin({MQTT_CLIENT_NAME, MQTT_HOST, MQTT_PORT, MQTT_ROOT_TOPIC}); + ircPlugin = new IrcPlugin({IRC_SERVER, IRC_PORT, chipName, chipName}); webServerPlugin = new WebServerPlugin({WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_PORT}); webConfigPlugin = new WebConfigPlugin(webServerPlugin->server); webApiPlugin = new WebApiPlugin(webServerPlugin->server); - otaTcpPlugin = new OtaTcpPlugin({OTA_PORT, OTA_PASSWORD}); sprocket->addPlugin(pixelPlugin); sprocket->addPlugin(webServerPlugin); sprocket->addPlugin(webConfigPlugin); sprocket->addPlugin(webApiPlugin); - sprocket->addPlugin(mqttPlugin); - sprocket->addPlugin(otaTcpPlugin); + sprocket->addPlugin(ircPlugin); network = new WiFiNet( WIFI_MODE, @@ -47,29 +44,26 @@ void setup() network->connect(); webServerPlugin->server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); - webServerPlugin->server->serveStatic(MQTT_CONFIG_FILE, SPIFFS, "mqttConfig.json"); + webServerPlugin->server->serveStatic(IRC_CONFIG_FILE, SPIFFS, "ircConfig.json"); - const char *mqChatTopic = "wirelos/chat/log"; - const char *outChatTopic = "out/chat/log"; - - sprocket->subscribe("mqtt/connect", [mqChatTopic, outChatTopic](String msg) { - if (msg.length() > 0) + sprocket->subscribe("irc/connected", [](String msg) { + if (atoi(msg.c_str())) { - mqttPlugin->client->subscribe(mqChatTopic); - sprocket->subscribe(mqChatTopic, [](String msg) { + sprocket->subscribe("irc/log", [](String msg) { PRINT_MSG(Serial, "CHAT", String("incoming: " + msg).c_str()); webApiPlugin->ws->textAll(msg); }); - - // send message from WS to this topic - sprocket->subscribe(outChatTopic, [mqChatTopic](String msg) { + sprocket->subscribe("out/chat/log", [](String msg) { PRINT_MSG(Serial, "CHAT", String("outgoing: " + msg).c_str()); - mqttPlugin->client->publish(mqChatTopic, msg.c_str()); + sprocket->publish("irc/sendMessage", msg); + webApiPlugin->ws->textAll("You:"+msg); }); + sprocket->publish("chat/connected", ""); } }); sprocket->activate(); + sprocket->publish("irc/connect",""); } void loop()