Jelajahi Sumber

PIU updates

* socat bridge, ichthyic (i2c to serial bridge) and
  piufared on startup (rc.local)
* text size fiddling for '/0:' message in index.html
* versioning and other information and cli options for
  piumsgd
* fixing ip address for javascript ui
clementinecomputing 4 tahun lalu
induk
melakukan
ae66208e95

+ 4 - 2
busunit-PIU/html/index.html

@@ -49,7 +49,7 @@
       </div>
     </div>
 
-    <div id='ui_test'>X</div>
+    <div id='ui_test'></div>
 
     <!-- ************************* -->
     <!-- ******            ******* -->
@@ -87,11 +87,13 @@
           <div class='pure-g row'>
             <div class='pure-u-1-12 col'> </div>
             <div class='pure-u-5-6 col'>
-              <div class='bdisp' style='color:#999a99;' id='ui_message_status' >SEE DRIVER....</div>
+              <div class='bdisp' style='color:#999a99; font-size:8vh;' id='ui_message_status' >00/00 00:00 AM READY</div>
             </div>
             <div class='pure-u-1-12 col'></div>
           </div>
 
+          <br>
+
           <div class='pure-g row'>
             <div class='pure-u-1-12 col'> </div>
             <div class='pure-u-5-6 col'>

+ 4 - 6
busunit-PIU/html/js/piu_ui.js

@@ -18,10 +18,8 @@
  *
  */
 
-console.log("...");
-
-var _ADDRESS = "localhost";
-//var _ADDRESS = "127.0.0.1";
+//var _ADDRESS = "localhost";
+var _ADDRESS = "127.0.0.1";
 //var _ADDRESS = "0.0.0.0";
 var _URL = "http://" + _ADDRESS;
 //var _PORT = 60535;
@@ -39,8 +37,8 @@ var _fqADDRESS = _ADDRESS + ":" + _PORT;
 // Use 127.0.0.1 instead
 //
 //var _wsADDRESS = "localhost" + ":" + _WS_PORT;
-//var _wsADDRESS = _ADDRESS +  ":" + _WS_PORT + "/ws";
-var _wsADDRESS = "192.168.0.26" +  ":" + _WS_PORT + "/ws";
+var _wsADDRESS = _ADDRESS +  ":" + _WS_PORT + "/ws";
+//var _wsADDRESS = "192.168.0.26" +  ":" + _WS_PORT + "/ws";
 
 var BG_COLOR = "#f7f7f7";
 var TEXT_COLOR = "#444444";

+ 103 - 69
busunit-PIU/piumsgd/piumsgd.c

@@ -36,48 +36,39 @@
 
 #include "mongoose.h"
 
-struct mg_mgr g_mgr;
-
-//char s_web_directory[] = ".";
-char s_web_directory[] = "/home/bus/config/html";
+#define PIUMSGD_VERSION "0.1.0"
+#define PIUMSGD_DEFAULT_WEBDIR "/home/bus/config/html"
+#define PIUMSGD_DEFAULT_ADDR "0.0.0.0"
+#define PIUMSGD_DEFAULT_PORT 8001
+
+static struct option long_opt[] = {
+  {"webdir", required_argument, 0, 0},
+  {"host", required_argument, 0, 0},
+  {"port", required_argument, 0, 0},
+  {"help", no_argument, 0, 0},
+  {"verbose", no_argument, 0, 0},
+  {"version", no_argument, 0, 0},
+  {0, 0, 0, 0}
+};
+
+int g_verbose = 0;
+
+//char s_web_directory[] = "/home/bus/config/html";
+char *s_web_directory = NULL;
 
-/*
-void ws_send(struct mg_mgr *mgr, char *msg) {
-  struct mg_connection *conn;
-  for (conn = mg_next(mgr, NULL); conn; conn = mg_next(mgr, conn)) {
-    if (!is_websocket(conn)) { continue; }
-    mg_send_websocket_frame(conn, WEBSOCKET_OP_TEXT, msg, strlen(msg));
-  }
-}
-*/
+struct mg_mgr g_mgr;
 
 void _ws_send(struct mg_mgr *mgr, char *msg, size_t msg_n) {
   int i;
   struct mg_connection *con;
 
-  printf("  ... ");
-  for (i=0; i<msg_n; i++) { printf("%c", msg[i]); }
-  printf("\n");
-
   for (con = mgr->conns; con; con = con->next) {
-    printf("  ...con %i %i\n", con->is_websocket, con->is_client);
-    //if (con->is_websocket && con->is_client) {
     if (con->is_websocket && con->is_accepted) {
-      printf("    !!\n");
-
       mg_ws_send(con, msg, msg_n, WEBSOCKET_OP_TEXT);
     }
   }
 }
 
-/*
-void _ws_send_str(struct mg_connection *mgc, char *msg) {
-  size_t msg_n;
-  msg_n = strlen(msg);
-  _ws_send(mgc, msg, msg_n);
-}
-*/
-
 
 static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
   int i;
@@ -86,70 +77,40 @@ static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
 
   if (ev == MG_EV_HTTP_MSG) {
 
-    printf("bang\n"); fflush(stdout);
-
     hm = (struct mg_http_message *) ev_data;
 
-    //DEBUG
-    //ptr len
-    /*
-    printf("body:\n");
-    for (i=0; i<hm->body.len; i++) {
-      printf("%i (%c)\n", hm->body.ptr[i], hm->body.ptr[i]);
-    }
-    printf("message:\n");
-    for (i=0; i<hm->message.len; i++) {
-      printf("%c", hm->message.ptr[i], hm->message.ptr[i]);
-    }
-    printf("\n\n");
-    */
-
-
-
     if (mg_http_match_uri(hm, "/ws")) {
-
       // Upgrade to websocket. From now on, a connection is a full-duplex
       // Websocket connection, which will receive MG_EV_WS_MSG events.
       //
-      printf("ws request?\n"); fflush(stdout);
-
       mg_ws_upgrade(c, hm, NULL);
     }
-    else if (mg_http_match_uri(hm, "/")) {
 
-      printf("html request? (1)\n"); fflush(stdout);
+    else if (mg_http_match_uri(hm, "/")) {
       struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
       mg_http_serve_dir(c, hm, &opts);
     }
-    else if (mg_http_match_uri(hm, "/rest")) {
-
-      printf("other req?\n");
 
+    else if (mg_http_match_uri(hm, "/rest")) {
       // Serve REST response
       //
       mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
     }
-    else {
-
-      printf("cp x\n");
 
+    else {
       // Serve static files
       //
       struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
       mg_http_serve_dir(c, (mg_http_message *)ev_data, &opts);
     }
-  } else if (ev == MG_EV_WS_MSG) {
+  }
+
+  else if (ev == MG_EV_WS_MSG) {
 
     // Got websocket frame. Received data is wm->data. Echo it back!
     //
     struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
 
-    printf(">>> ");
-    for (int i=0; i<wm->data.len; i++) {
-      printf("%c", *(wm->data.ptr + i));
-    }
-    printf("\n"); fflush(stdout);
-
     n = strlen("relay ");
     if (wm->data.len < n) {
       n = wm->data.len;
@@ -164,17 +125,37 @@ static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
     mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
     mg_iobuf_delete(&c->recv, c->recv.len);
 
-    printf("broadcasting...\n");
     n = strlen("broadcast");
     _ws_send(&g_mgr, (char *)"broadcast", n);
 
   }
+
   (void) fn_data;
 }
 
-int main(int argc, char **argv) {
+void show_version(FILE *ofp) {
+  fprintf(ofp, "piumsgd version %s\n", PIUMSGD_VERSION);
+}
+
+void show_help(FILE *ofp) {
+
+  show_version(ofp);
+
+  fprintf(ofp, "\npiusmgd usage:\n\n");
+  fprintf(ofp, "    piumsgd [-h] [-v] [-p port] [-H host] [-D webdir] [-V]\n");
+  fprintf(ofp, "\n");
+  fprintf(ofp, "  -D        web directory (default %s)\n", PIUMSGD_DEFAULT_WEBDIR);
+  fprintf(ofp, "  -H        host (default %s)\n", PIUMSGD_DEFAULT_ADDR);
+  fprintf(ofp, "  -p        port (default %i)\n", PIUMSGD_DEFAULT_PORT);
+  fprintf(ofp, "  -V        verbose\n");
+  fprintf(ofp, "  -v        show version\n");
+  fprintf(ofp, "  -h        help (this screen)\n");
+  fprintf(ofp, "\n");
+}
 
-  //struct pollfd fds[32];
+
+int main(int argc, char **argv) {
+  int ch, opt_index;
   int nfd=0;
 
   int poll_return;
@@ -182,13 +163,66 @@ int main(int argc, char **argv) {
 
   time_t now;
 
+  int port = -1;
+  char *host = NULL, *webdir = NULL;
   struct mg_conneciton *nc;
 
+  char *listen_host = NULL;
+
+  while ((ch = getopt_long(argc, argv, "hvVH:D:p:", long_opt, &opt_index)) >= 0) {
+    switch (ch) {
+      case 0:
+        break;
+      case 'h':
+        show_help(stdout);
+        exit(0);
+        break;
+      case 'v':
+        show_version(stdout);
+        exit(0);
+        break;
+      case 'V':
+        g_verbose++;
+        break;
+      case 'p':
+        port = atoi(optarg);
+        break;
+      case 'H':
+        host = strdup(optarg);
+        break;
+      case 'D':
+        s_web_directory = strdup(optarg);
+        break;
+      default:
+        fprintf(stderr, "unknown option %c\n", ch);
+        show_help(stderr);
+        exit(-1);
+        break;
+    }
+  }
+
+  if(!s_web_directory) {
+    s_web_directory = strdup(PIUMSGD_DEFAULT_WEBDIR);
+  }
+
+  if (port < 0) {
+    port = PIUMSGD_DEFAULT_PORT;
+  }
+
+  if (!host) {
+    host = strdup(PIUMSGD_DEFAULT_ADDR);
+  }
+
+  listen_host = (char *)malloc(sizeof(char)*2*strlen(host));
+  sprintf(listen_host, "http://%s:%i", host, port);
+
   // setup mongoose web server
   //
   mg_mgr_init(&g_mgr);
+
   //mg_http_listen(&g_mgr, "http://127.0.0.1:8001", _cb, NULL);
-  mg_http_listen(&g_mgr, "http://0.0.0.0:8001", _cb, NULL);
+  //mg_http_listen(&g_mgr, "http://0.0.0.0:8001", _cb, NULL);
+  mg_http_listen(&g_mgr, listen_host, _cb, NULL);
   for (;;) {
     mg_mgr_poll(&g_mgr, 200);
   }

+ 8 - 0
busunit-PIU/rootfs/etc/rc.local

@@ -27,7 +27,15 @@ export LOGDIR=/home/bus/log
 /home/bus/bin/magstripe_manager > $LOGDIR/mag_manager.log &
 /home/bus/bin/qr_manager > $LOGDIR/qr_manager.log &
 motion -b -c /home/bus/.motion/motion.conf -p $LOGDIR/motion.pid -l $LOGDIR/motion.log 2>> $LOGDIR/init.log
+
 /home/bus/bin/piumsgd > $LOGDIR/piumsgd.log &
+/home/bus/bin/piufared > $LOGDIR/piufared.log &
+
+socat -d -d pty,raw,echo=0,link=/tmp/ttyPIUich pty,raw,echo=0,link=/dev/ttyPIU > $LOGDIR/socat.log &
+sleep 1
+/home/bus/bin/ichthyic-passthrough -i /tmp/ttyPIUich -I /dev/i2c-1 > $LOGDIR/ichthyic.log &
+
+
 
 ( sleep 5 && su -c "/usr/bin/startx /home/bus/popufare/busunit-PIU/scripts/piu_kiosk 2>&1 $LOGDIR/kiosk.log" )  &
 #( sleep 5 && su -c "/usr/bin/startx /home/bus/bin/piu_wrapp 2>&1 /tmp/piu_wrapp.log" )  &