فهرست منبع

Merge branch 'release' of https://tree.clementinecomputing.com/clementinecomputing/popufare into release

clementinecomputing 5 سال پیش
والد
کامیت
afbedc2f27
3فایلهای تغییر یافته به همراه275 افزوده شده و 0 حذف شده
  1. 3 0
      busunit/scripts/crontab.root
  2. 143 0
      busunit/scripts/format_debug_msg.py
  3. 129 0
      busunit/scripts/popufare_monitor

+ 3 - 0
busunit/scripts/crontab.root

@@ -0,0 +1,3 @@
+# min hour day-month month day-week cmd
+* * * * * /home/bus/bin/popufare_monitor
+

+ 143 - 0
busunit/scripts/format_debug_msg.py

@@ -0,0 +1,143 @@
+#!/usr/bin/python
+#
+# Copyright (c) 2019 Clementine Computing LLC.
+#
+# This file is part of PopuFare.
+#
+# PopuFare is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# PopuFare is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
+#
+
+# Messages can be inserted into the IPC hub via the `debug_client` program.
+# The `debug_client` requires messages to be of the format:
+#
+# MAILBOX: XX XX ... XX
+#
+# Where `MAILBOX` is the mailbox of the message and XX are the hex bytes
+# of the payload message in ASCII format.
+#
+# For example, to send a message to the `BILLING_LOG` mailbox with a message
+# of "#testing", the following would be used:
+#
+# echo 'BILLING_LOG: 23 74 65 73 74 69 6e 67' | /home/bus/bin/debug_client -s
+#
+# Where the '#' prefix sends the mesage to the `diagnostic_log` table on the server.
+#
+# See the `MAILBOX_*` defines in `commhub.h` for the full list of mailboxes.
+# As of this writing, the list of mailboxes is:
+#
+# _HELLO        _SUBSCRIBE  _UNSUBSCRIBE    _BROADCAST      _WIRETAP
+# _ERROR        _PING       _PONG           _EXIT           _HUP
+# STATUS_REQ    PASS_STATUS BILL_STATUS     DRIVER_STATUS   GPS_STATUS
+# STOP_STATUS   TOKEN_MAG   TOKEN_RFID      FLUSH_PASSES    UPDATE_PASSES
+# DRIVER_NOTIFY BILLING_LOG PIU_MESSAGE     SET_PADDLE      PADDLE_ACK
+# NEXT_STOP     PREV_STOP   RULE_CALL       VAULT_DROP      PASSDB_CONSIST
+# PASSDB_PULSE
+#
+
+import sys
+import getopt
+
+VERSION = "0.1.0"
+
+VALID_MAILBOX = [
+  "_HELLO", "_SUBSCRIBE", "_UNSUBSCRIBE",
+  "_BROADCAST", "_WIRETAP", "_ERROR",
+  "_PING", "_PONG", "_EXIT", "_HUP",
+  "STATUS_REQ",
+  "PASS_STATUS", "BILL_STATUS", "DRIVER_STATUS",
+  "GPS_STATUS", "STOP_STATUS",
+  "TOKEN_MAG", "TOKEN_RFID",
+  "FLUSH_PASSES", "UPDATE_PASSES",
+  "DRIVER_NOTIFY", "BILLING_LOG", "PIU_MESSAGE",
+  "SET_PADDLE", "PADDLE_ACK",
+  "NEXT_STOP", "PREV_STOP",
+  "RULE_CALL", "VAULT_DROP",
+  "PASSDB_CONSIST", "PASSDB_PULSE" ]
+
+
+def show_version(ofp):
+  ofp.write("Version: " + VERSION + "\n");
+
+def show_help(ofp):
+  ofp.write("\n");
+  ofp.write("usage:");
+  ofp.write("\n");
+  ofp.write("\n");
+  ofp.write("  format_debug_msg.py [-h] [-F] <MAILBOX> [--asciihex] <PAYLOAD>\n");
+  ofp.write("\n");
+  ofp.write("  <MAILBOX>            destination mailbox\n")
+  ofp.write("  <PAYLOAD>            payload to convert\n")
+  ofp.write("  [--show-mailbox]     print out all valid mailboxes\n")
+  ofp.write("  [-F|-force-mailbox]  do not check to see if MAILBOX is in list of valid mailboxes\n")
+  ofp.write("  [--asciihex]         payload in ASCII hex format (with spaces) (example: ' 23 74 65 73 74')\n")
+  ofp.write("  [-h|-help]           help (this screen)\n")
+  ofp.write("\n");
+
+def show_mailboxes():
+  for mb in VALID_MAILBOX:
+    print mb
+
+mailbox = ""
+payload_text = ""
+payload_converted = ""
+asciihex_flag = False
+force_mailbox = False
+
+argv = sys.argv[1:]
+options, rem = getopt.gnu_getopt(sys.argv[1:], "hF", ["asciihex", "help", "version", "show-mailbox", "force-mailbox"])
+for opt, arg in options:
+  if opt in ('-v', '--version'):
+    show_version(sys.stdout)
+    sys.exit(0)
+  if opt in ('-h', '--help'):
+    show_help(sys.stdout)
+    sys.exit(0)
+  elif opt in ('--asciihex'):
+    asciihex_flag = True
+  elif opt in ('-F', '--force-mailbox'):
+    force_mailbox = True
+  elif opt in ('--show-mailbox'):
+    show_mailboxes()
+    sys.exit(0)
+  else:
+    show_help(sys.sdterr)
+    sys.exit(-1)
+
+if len(rem) < 2:
+  sys.stderr.write("must provide MAILBOX and PAYLOAD\n")
+  show_help(sys.stderr)
+  sys.exit(-1)
+
+mailbox = rem[0]
+
+if not force_mailbox:
+  if not mailbox in VALID_MAILBOX:
+    sys.stderr.write("ERROR: could not find '" + mailbox + "' in list of valid mailboxes.\n")
+    sys.stderr.write("Use the --force-mailbox option to use this mailbox anyway.\n")
+    sys.stderr.write("Use the --show-mailbox option to see a list of vlaid options\n\n")
+    sys.exit(-1)
+
+payload_text = rem[1]
+
+if asciihex_flag:
+  payload_converted = payload_text
+else:
+  for ch in payload_text:
+    payload_converted += format(ord(ch), ' 02x')
+
+
+
+print mailbox + ":" + payload_converted
+
+

+ 129 - 0
busunit/scripts/popufare_monitor

@@ -0,0 +1,129 @@
+#!/bin/bash
+#
+# Copyright (c) 2019 Clementine Computing LLC.
+# 
+# This file is part of PopuFare.
+# 
+# PopuFare is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# PopuFare is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
+#
+
+# This script expects to be called as root from a cron job.
+#
+# This script will do some basic monitoring of different pieces
+# of the system.
+# If a problem is discovered, this program will attemp to report
+# and, potentially, try to fix them.
+#
+
+VERBOSE=2
+
+bindir=/home/bus/bin
+cfgdir=/home/bus/config
+i2cdev=/dev/i2c-1
+piddir=/tmp
+ptydir=/tmp
+logdir=/tmp
+
+popmonlog=/tmp/popmon.log
+
+if [[ "$VERBOSE" -gt 0 ]] ; then
+  ts=`date -u +'%s.%N'`
+  echo "# $ts [$$] popufare_monitor running" >> $popmonlog
+fi
+
+function setup_PIU_socat() {
+  if [[ -e $piddir/socat.pid ]] ; then
+    _p=`cat $piddir/socat.pid`
+    if [[ -e /proc/${_p} ]] ; then kill -9 $_p ; fi
+    rm -f $piddir/socat.pid
+  fi
+
+  rm -f /dev/ttyPIU
+  rm -f $ptydir/ttyPIUich
+
+  if [[ "$VERBOSE" -gt 0 ]] ; then
+    ts=`date -u +'%s.%N'`
+    echo "# $ts [$$] popufare_monitor: starting socat" >> $popmonlog
+  fi
+
+  socat -d -d pty,raw,echo=0,link=$ptydir/ttyPIUich pty,raw,echo=0,link=/dev/ttyPIU > $logdir/socat.log 2>&1 &
+  echo "$!" > $piddir/socat.pid
+}
+
+function setup_I2C_PIU_bridge() {
+  if [[ -e $piddir/ichthyic.pid ]] ; then
+    _p=`cat $piddir/ichthyic.pid`
+    if [[ -e /proc/${_p} ]] ; then kill -9 $_p ; fi
+    rm -f $piddir/ichthyic.pid
+  fi
+
+  if [[ "$VERBOSE" -gt 0 ]] ; then
+    ts=`date -u +'%s.%N'`
+    echo "# $ts [$$] popufare_monitor: starting ichthyic i2c to PIU bridge" >> $popmonlog
+  fi
+
+  $bindir/ichthyic-passthrough --i2c $i2cdev --pty $ptydir/ttyPIUich -v -v -L $logdir/ichthyic.log >> $popmonlog 2>&1 &
+  echo "$!" > $piddir/ichthyic.pid
+}
+
+# If we have an appropriate drop file and I2C is enabled, 
+# make sure socat is running and our shim program is running.
+#
+if [[ -e $cfgdir/popufare.ichthyic &&  -e $i2cdev ]] ; then
+
+  ## make sure socat is running to tie ttyPIU to ttyPIUich
+  ## together
+  ##
+  if [[ ! -e $piddir/socat.pid ]] ; then
+    setup_PIU_socat
+  fi
+
+  p=`cat $piddir/socat.pid`
+  if [[ ! -e /proc/${p} ]] ; then
+
+    if [[ "$VERBOSE" -gt 1 ]] ; then
+      ts=`date -u +'%s.%N'`
+      echo "## $ts [$$] stale socat.pid file? no process id found for $p" >> $popmonlog
+    fi
+
+    setup_PIU_socat
+  fi
+
+  ## Now run the shim program to connect the I2C to the ttyPIUich line
+  ##
+  if [[ ! -e $piddir/ichthyic.pid ]] ; then
+    setup_I2C_PIU_bridge
+  fi
+
+  p=`cat $piddir/ichthyic.pid`
+  if [[ ! -e /proc/${p} ]] ; then
+
+    if [[ "$VERBOSE" -gt 1 ]] ; then
+      ts=`date -u +'%s.%N'`
+      echo "## $ts [$$] stale ichthyic.pid file? no process id found for $p" >> $popmonlog
+    fi
+
+    setup_I2C_PIU_bridge
+  fi
+
+fi
+
+if [[ "$VERBOSE" -gt 0 ]] ; then
+  ts=`date -u +'%s.%N'`
+  echo "# $ts [$$] popufare_monitor finished" >> $popmonlog
+fi
+
+exit 0
+
+