change chat to irc

This commit is contained in:
2018-11-23 17:15:56 +01:00
parent 62186af303
commit f7ba31c2d2
8 changed files with 78 additions and 37 deletions

11
.project Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>illucat</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

6
data/ircConfig.json Normal file
View File

@@ -0,0 +1,6 @@
{
"ircServer": "chat.freenode.net",
"ircPort": 6665,
"ircNickname": "redcat",
"ircUser": "redcat"
}

View File

@@ -1,7 +1,7 @@
{ {
"pin": 4, "pin": 4,
"length": 8, "length": 8,
"brightness": 100, "brightness": 127,
"updateInterval": 100, "updateInterval": 100,
"defaultColor": 100 "defaultColor": 100
} }

View File

@@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>ESP Kit</title> <title>IlluCat</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="/favicon.png"> <link rel="icon" type="image/png" href="/favicon.png">
@@ -73,8 +73,10 @@
<div class="Form" data-fileName="/config.json" data-name="configForm" data-from="/config.json" data-endpoint="/config"></div> <div class="Form" data-fileName="/config.json" data-name="configForm" data-from="/config.json" data-endpoint="/config"></div>
<h2>NeoPixel</h2> <h2>NeoPixel</h2>
<div class="Form" data-fileName="/pixelConfig.json" data-name="configForm" data-from="/pixelConfig.json" data-endpoint="/config"></div> <div class="Form" data-fileName="/pixelConfig.json" data-name="configForm" data-from="/pixelConfig.json" data-endpoint="/config"></div>
<h2>MQTT</h2> <!-- <h2>MQTT</h2>
<div class="Form" data-fileName="/mqttConfig.json" data-name="configForm" data-from="/mqttConfig.json" data-endpoint="/config"></div> <div class="Form" data-fileName="/mqttConfig.json" data-name="configForm" data-from="/mqttConfig.json" data-endpoint="/config"></div> -->
<h2>IRC</h2>
<div class="Form" data-fileName="/ircConfig.json" data-name="configForm" data-from="/ircConfig.json" data-endpoint="/config"></div>
</div> </div>
</div> </div>
<div class="settings container collapsible"> <div class="settings container collapsible">

View File

@@ -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); super(ctx, node, template || __WEBPACK_IMPORTED_MODULE_1__Chat_html___default.a);
this.render(this.config); this.render(this.config);
this.ctx.mediator.on(this.config.topic, this.onMessage.bind(this)); 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('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() { templates() {
@@ -12279,17 +12282,43 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default
<span class="user-label">${user}</span> <span class="user-label">${user}</span>
<span class="message-text">${msg}</span> <span class="message-text">${msg}</span>
</li> </li>
`,
serverMessage: (msg) => `
<li>
<span class="message-text">${msg}</span>
</li>
` `
}; };
} }
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) { onMessage(msg) {
let payload = msg.payload; //.replace(/<.+?>/g, ''); let payload = msg.payload; //.replace(/<.+?>/g, '');
//console.log('onMsg: ' + msg); //console.log('onMsg: ' + msg);
let msgParts = payload.split(':'); let msgParts = payload.split(':');
let messages = this.node.find('.messages'); let messages = this.node.find('.messages');
messages.append( 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({ this.node.find('.message-container').animate({
scrollTop: messages[0].scrollHeight scrollTop: messages[0].scrollHeight
@@ -12300,12 +12329,11 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default
} }
send(evt) { send(evt) {
evt.preventDefault(); evt.preventDefault();
let username = this.node.find('input.username');
let msg = this.node.find('input.msg'); let msg = this.node.find('input.msg');
if (username.length > 0 && msg.length > 0) { if (msg.length > 0) {
let message = JSON.stringify({ let message = JSON.stringify({
topic: this.sanitizeInput(this.config.topic), topic: this.sanitizeInput(this.config.topic),
payload: this.sanitizeInput(username.val() + ':' + msg.val()) payload: this.sanitizeInput(msg.val())
}); });
this.ctx.ws.send(message); this.ctx.ws.send(message);
msg.val(''); msg.val('');
@@ -12325,7 +12353,7 @@ class Chat extends __WEBPACK_IMPORTED_MODULE_2__core_Component__["a" /* default
/* 31 */ /* 31 */
/***/ (function(module, exports) { /***/ (function(module, exports) {
module.exports = "<form>\n <ul>\n <li class=\"form-row\">\n <input type=\"text\" placeholder=\"Topic\" class=\"topic\" value=\"{{topic}}\" disabled>\n <input type=\"text\" placeholder=\"Username\" class=\"username\">\n </li>\n </ul>\n <div class=\"message-container\">\n <ul class=\"messages\"></ul>\n </div>\n <ul>\n <li class=\"form-row\">\n <input type=\"text\" placeholder=\"{{placeholder}}\" class=\"msg\">\n <button>Send</button>\n </li>\n </ul>\n </ul>\n</form>" module.exports = "<form>\n <ul>\n <li class=\"form-row\">\n <label for=\"channel\">Channel</label>\n <input type=\"text\" placeholder=\"Channel\" class=\"channel\" value=\"#illucat\">\n <button class=\"join\">Join</button>\n <!-- <input type=\"text\" placeholder=\"Topic\" class=\"topic\" value=\"{{topic}}\" disabled> -->\n <!-- <input type=\"text\" placeholder=\"Username\" class=\"username\"> -->\n </li>\n </ul>\n <div class=\"message-container\">\n <ul class=\"messages\"></ul>\n </div>\n <ul class=\"controls\" style=\"display:none\">\n <li class=\"form-row\">\n <input type=\"text\" placeholder=\"{{placeholder}}\" class=\"msg\">\n <button class=\"send\">Send</button>\n </li>\n </ul>\n</form>"
/***/ }) /***/ })
/******/ ]); /******/ ]);

View File

@@ -99,5 +99,5 @@ build_flags = -Wl,-Teagle.flash.4m1m.ld
lib_deps = ${common.lib_deps} lib_deps = ${common.lib_deps}
https://gitlab.com/wirelos/sprocket-network-wifi.git https://gitlab.com/wirelos/sprocket-network-wifi.git
https://gitlab.com/wirelos/sprocket-plugin-web.git https://gitlab.com/wirelos/sprocket-plugin-web.git
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git https://gitlab.com/wirelos/sprocket-plugin-irc.git
PubSubClient ArduinoIRC

View File

@@ -26,6 +26,7 @@
// config files // config files
#define PIXEL_CONFIG_FILE "/pixelConfig.json" #define PIXEL_CONFIG_FILE "/pixelConfig.json"
#define MQTT_CONFIG_FILE "/mqttConfig.json" #define MQTT_CONFIG_FILE "/mqttConfig.json"
#define IRC_CONFIG_FILE "/ircConfig.json"
// NeoPixel // NeoPixel
#define LED_STRIP_PIN D2 #define LED_STRIP_PIN D2
@@ -36,11 +37,10 @@
#define COLOR_CONNECTED LED_STRIP_DEFAULT_COLOR #define COLOR_CONNECTED LED_STRIP_DEFAULT_COLOR
#define COLOR_NOT_CONNECTED 255 #define COLOR_NOT_CONNECTED 255
// mqtt config #define IRC_SERVER "chat.freenode.net"
#define MQTT_CLIENT_NAME "illucat" #define IRC_PORT 6665
#define MQTT_HOST "192.168.1.2" #define IRC_NICKNAME ""
#define MQTT_PORT 1883 #define IRC_USER ""
#define MQTT_ROOT_TOPIC "wirelos/illucat"
// OTA config // OTA config
#define OTA_PORT 8266 #define OTA_PORT 8266

View File

@@ -7,8 +7,7 @@
#include <WebConfigPlugin.h> #include <WebConfigPlugin.h>
#include <WebApiPlugin.h> #include <WebApiPlugin.h>
#include <PixelPlugin.h> #include <PixelPlugin.h>
#include <MqttPlugin.h> #include <IrcPlugin.h>
#include <OtaTcpPlugin.cpp>
WiFiNet *network; WiFiNet *network;
Sprocket *sprocket; Sprocket *sprocket;
@@ -16,25 +15,23 @@ WebServerPlugin *webServerPlugin;
WebConfigPlugin *webConfigPlugin; WebConfigPlugin *webConfigPlugin;
WebApiPlugin *webApiPlugin; WebApiPlugin *webApiPlugin;
PixelPlugin *pixelPlugin; PixelPlugin *pixelPlugin;
MqttPlugin *mqttPlugin; IrcPlugin *ircPlugin;
OtaTcpPlugin *otaTcpPlugin;
void setup() void setup()
{ {
const char *chipName = String("Sprocket" + String(ESP.getChipId())).c_str();
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
pixelPlugin = new PixelPlugin({LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL}); 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}); webServerPlugin = new WebServerPlugin({WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_PORT});
webConfigPlugin = new WebConfigPlugin(webServerPlugin->server); webConfigPlugin = new WebConfigPlugin(webServerPlugin->server);
webApiPlugin = new WebApiPlugin(webServerPlugin->server); webApiPlugin = new WebApiPlugin(webServerPlugin->server);
otaTcpPlugin = new OtaTcpPlugin({OTA_PORT, OTA_PASSWORD});
sprocket->addPlugin(pixelPlugin); sprocket->addPlugin(pixelPlugin);
sprocket->addPlugin(webServerPlugin); sprocket->addPlugin(webServerPlugin);
sprocket->addPlugin(webConfigPlugin); sprocket->addPlugin(webConfigPlugin);
sprocket->addPlugin(webApiPlugin); sprocket->addPlugin(webApiPlugin);
sprocket->addPlugin(mqttPlugin); sprocket->addPlugin(ircPlugin);
sprocket->addPlugin(otaTcpPlugin);
network = new WiFiNet( network = new WiFiNet(
WIFI_MODE, WIFI_MODE,
@@ -47,29 +44,26 @@ void setup()
network->connect(); network->connect();
webServerPlugin->server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json"); 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"; sprocket->subscribe("irc/connected", [](String msg) {
const char *outChatTopic = "out/chat/log"; if (atoi(msg.c_str()))
sprocket->subscribe("mqtt/connect", [mqChatTopic, outChatTopic](String msg) {
if (msg.length() > 0)
{ {
mqttPlugin->client->subscribe(mqChatTopic); sprocket->subscribe("irc/log", [](String msg) {
sprocket->subscribe(mqChatTopic, [](String msg) {
PRINT_MSG(Serial, "CHAT", String("incoming: " + msg).c_str()); PRINT_MSG(Serial, "CHAT", String("incoming: " + msg).c_str());
webApiPlugin->ws->textAll(msg); webApiPlugin->ws->textAll(msg);
}); });
sprocket->subscribe("out/chat/log", [](String msg) {
// send message from WS to this topic
sprocket->subscribe(outChatTopic, [mqChatTopic](String msg) {
PRINT_MSG(Serial, "CHAT", String("outgoing: " + msg).c_str()); 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->activate();
sprocket->publish("irc/connect","");
} }
void loop() void loop()