2
0

webpack.dev.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* eslint-disable */
  2. var path = require('path');
  3. const merge = require('webpack-merge');
  4. const common = require('./webpack.common.js');
  5. const bodyParser = require('body-parser')
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  7. const { config } = require('process');
  8. const HtmlWebPackPlugin = require('html-webpack-plugin');
  9. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  10. const { Command } = require('commander');
  11. let cmdLines= { };
  12. const data = {
  13. messages: require("../mock/messages.json"),
  14. messagequeue: require("../mock/messages.json"),
  15. commands: require("../mock/commands.json"),
  16. scan: require("../mock/scan.json"),
  17. ap: require("../mock/ap.json"),
  18. config: require("../mock/config.json"),
  19. statusdefinition: require("../mock/statusdefinition.json"),
  20. status: require("../mock/status.json")
  21. };
  22. const messagingTypes= {
  23. MESSAGING_INFO : 'MESSAGING_INFO',
  24. MESSAGING_WARNING : 'MESSAGING_WARNING',
  25. MESSAGING_ERROR : 'MESSAGING_ERROR'
  26. };
  27. const messagingClass= {
  28. MESSAGING_CLASS_OTA : 'MESSAGING_CLASS_OTA',
  29. MESSAGING_CLASS_SYSTEM : 'MESSAGING_CLASS_SYSTEM',
  30. MESSAGING_CLASS_STATS : 'MESSAGING_CLASS_STATS',
  31. MESSAGING_CLASS_CFGCMD: 'MESSAGING_CLASS_CFGCMD',
  32. MESSAGING_CLASS_BT: 'MESSAGING_CLASS_BT'
  33. } ;
  34. function requeueMessages(){
  35. data.messagequeue.push(...data.messages);
  36. console.log(`Re-queued ${data.messages.length} messages. Total queue length is: ${data.messagequeue.length}`);
  37. }
  38. function sendMessaging(cmdname,msgtype,msgClass,msg){
  39. let message_txt=`${cmdname}\n${msg}`;
  40. var d = new Date();
  41. var n = d.getMilliseconds();
  42. data.messagequeue.push({
  43. message: message_txt,
  44. type: msgtype,
  45. class: msgClass,
  46. sent_time: n,
  47. current_time: n});
  48. console.log(`Queued message ~${data.messagequeue.length} type ${msgtype}, class ${msgClass}: ${message_txt}`);
  49. }
  50. Array.prototype.filter = function(fun /*, thisp*/) {
  51. var len = this.length >>> 0;
  52. if (typeof fun != "function")
  53. throw new TypeError();
  54. var res = [];
  55. var thisp = arguments[1];
  56. for (var i = 0; i < len; i++) {
  57. if (i in this) {
  58. var val = this[i];
  59. if (fun.call(thisp, val, i, this))
  60. res.push(val);
  61. }
  62. }
  63. return res;
  64. };
  65. for(const cmdIdx in data.commands.commands){
  66. const cmd = data.commands.commands[cmdIdx];
  67. //console.log(`Creating command structure for ${cmd.name}`);
  68. cmdLines[cmd.name] = {
  69. cmd: new Command(),
  70. };
  71. cmdLines[cmd.name].cmd
  72. .storeOptionsAsProperties(false)
  73. .name(cmd.name)
  74. .exitOverride();
  75. for(const argIdx in cmd.argtable){
  76. const arg=cmd.argtable[argIdx];
  77. const optstr=((arg.shortopts?'-'+arg.shortopts:'')+
  78. (arg.shortopts && arg.longopts?', ':'')+
  79. (arg.longopts?'--'+arg.longopts:'') +
  80. (arg.hasvalue?`${(arg.shortopts || arg.longopts?' ':'')}<${arg.datatype.replace(/[<>]/gm,'')}>`:''));
  81. //console.log(` Option: ${optstr}, Glossary: ${arg.glossary}`);
  82. if(arg.mincount>0){
  83. cmdLines[cmd.name].cmd.requiredOption( optstr,arg.glossary);
  84. }
  85. else {
  86. cmdLines[cmd.name].cmd.option( optstr,arg.glossary);
  87. }
  88. }
  89. }
  90. const connectReturnCode = {
  91. UPDATE_CONNECTION_OK : 0,
  92. UPDATE_FAILED_ATTEMPT : 1,
  93. UPDATE_USER_DISCONNECT : 2,
  94. UPDATE_LOST_CONNECTION : 3,
  95. UPDATE_FAILED_ATTEMPT_AND_RESTORE : 4
  96. }
  97. module.exports = merge(common, {
  98. mode: 'development',
  99. devtool: 'inline-source-map',
  100. entry: {
  101. test: './src/test.ts'
  102. },
  103. devServer: {
  104. contentBase: path.join(__dirname, 'dist'),
  105. publicPath: '/',
  106. port: 9100,
  107. host: 'desktop-n8u8515',//your ip address
  108. disableHostCheck: true,
  109. overlay: true,
  110. before: function(app) {
  111. app.use(bodyParser.json()) // for parsing application/json
  112. app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
  113. app.get('/ap.json', function(req, res) { res.json( data.ap ); });
  114. app.get('/scan.json', function(req, res) { res.json( data.scan ); });
  115. app.get('/config.json', function(req, res) { res.json( data.config ); });
  116. app.get('/status.json', function(req, res) { res.json( data.status ); });
  117. app.get('/messages.json', function(req, res) {
  118. res.json( data.messagequeue ) ;
  119. data.messagequeue=[];
  120. });
  121. app.get('/statusdefinition.json', function(req, res) { res.json( data.statusdefinition ); });
  122. app.get('/commands.json', function(req, res) { res.json( data.commands ); });
  123. app.post('/commands.json', function(req, res) {
  124. console.log(req.body.command);
  125. try {
  126. const cmdName=req.body.command.split(" ")[0];
  127. const args=('node '+req.body.command).split(" ");
  128. let cmd=cmdLines[cmdName].cmd;
  129. if(cmd){
  130. cmd.parse(args);
  131. const msg=`Received Options: ${JSON.stringify(cmd.opts())}\n`;
  132. console.log('Options: ', cmd.opts());
  133. console.log('Remaining arguments: ', cmd.args);
  134. sendMessaging(cmdName,messagingTypes.MESSAGING_INFO,messagingClass.MESSAGING_CLASS_CFGCMD,msg);
  135. }
  136. } catch (error) {
  137. console.error(error);
  138. }
  139. res.json( { 'Result' : 'Success' } );
  140. });
  141. app.post('/config.json', function(req, res) {
  142. console.log(req.body);
  143. console.log(data.config);
  144. for (const property in req.body.config) {
  145. console.log(`${property}: ${req.body.config[property].value}`);
  146. if(data.config[property]=== undefined){
  147. console.log(`Added config value ${property} [${req.body.config[property].value}]`);
  148. data.config[property] = {value: req.body.config[property].value};
  149. }
  150. else if (data.config[property].value!=req.body.config[property].value){
  151. console.log(`Updated config value ${property}\nFrom: ${data.config[property].value}\nTo: ${req.body.config[property].value}]`);
  152. data.config[property].value=req.body.config[property].value;
  153. }
  154. }
  155. res.json( {} );
  156. });
  157. app.post('/status.json', function(req, res) {
  158. for (const property in req.body.status) {
  159. if(data.status[property]=== undefined){
  160. console.log(`Added status value ${property} [${req.body.status[property]}]`);
  161. data.status[property] = {value: req.body.status[property]};
  162. }
  163. else if (data.status[property]!==req.body.status[property]){
  164. console.log(`Updated status value ${property}\nFrom: ${data.status[property]}\nTo: ${req.body.status[property]}`);
  165. data.status[property]=req.body.status[property];
  166. }
  167. }
  168. res.json( {} );
  169. });
  170. app.post('/connect.json', function(req, res) {
  171. setTimeout(function(r){
  172. if(r.body.ssid.search('fail')>=0){
  173. if(data.status.ssid){
  174. // in this case, the same ssid will be reused - the ESP32 would restore its previous state on failure
  175. data.status.urc=connectReturnCode.UPDATE_FAILED_ATTEMPT_AND_RESTORE;
  176. }
  177. else {
  178. data.status.urc=connectReturnCode.UPDATE_FAILED_ATTEMPT;
  179. }
  180. }
  181. else {
  182. data.status.ssid=r.body.ssid;
  183. data.status.urc=connectReturnCode.UPDATE_CONNECTION_OK;
  184. }
  185. }, 1000, req);
  186. res.json( {} );
  187. });
  188. app.post('/reboot_ota.json', function(req, res) {
  189. data.status.recovery=0;
  190. requeueMessages();
  191. res.json( {} );
  192. });
  193. app.post('/reboot.json', function(req, res) {
  194. res.json( {} );
  195. requeueMessages();
  196. });
  197. app.post('/recovery.json', function(req, res) {
  198. data.status.recovery=1;
  199. requeueMessages();
  200. res.json( { } );
  201. });
  202. app.post('/flash.json', function(req, res) {
  203. if(data.status.recovery>0){
  204. res.json({});
  205. }
  206. else {
  207. res.status(404).end();
  208. }
  209. });
  210. app.delete('/connect.json', function(req, res) {
  211. data.status.ssid='';
  212. res.json( {} ); });
  213. app.get('/reboot', function(req, res) { res.json( {} ); });
  214. },
  215. },
  216. plugins: [
  217. new MiniCssExtractPlugin({
  218. filename: 'css/[name].css',
  219. chunkFilename: 'css/[id].css' ,
  220. }),
  221. new HtmlWebPackPlugin({
  222. title: 'SqueezeESP32-test',
  223. template: './src/test.ejs',
  224. filename: 'test',
  225. minify: false,
  226. excludeChunks: ['index'],
  227. }),
  228. new SpriteLoaderPlugin({plainSprite: true})
  229. ],
  230. });