plc-checkweigher 1.21.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1126,6 +1126,132 @@ SMBC
1126
1126
  echo ""
1127
1127
  ;;
1128
1128
 
1129
+ # ─────────────────────────────────────────────────────────────────────────────
1130
+ # UPDATE — pull latest code from GitHub, re-lock files, restart services
1131
+ # ─────────────────────────────────────────────────────────────────────────────
1132
+ update)
1133
+ banner "System Update"
1134
+ echo ""
1135
+ need_sudo
1136
+
1137
+ # ── Warn if a batch is actively running ───────────────────────────────────
1138
+ _IS_RUNNING=0
1139
+ if [[ -f "/tmp/plc_live.json" ]]; then
1140
+ _RUN_VAL=$("${PYTHON}" -c \
1141
+ "import json; d=json.load(open('/tmp/plc_live.json')); print(d.get('running',False))" \
1142
+ 2>/dev/null || echo "False")
1143
+ [[ "$_RUN_VAL" == "True" ]] && _IS_RUNNING=1
1144
+ fi
1145
+
1146
+ if [[ $_IS_RUNNING -eq 1 ]]; then
1147
+ warn "A batch is currently RUNNING on the PLC."
1148
+ warn "The update will stop the current batch — any unsaved data will be lost."
1149
+ echo ""
1150
+ read -r -p " Continue anyway? [y/N]: " _UPD_CONT </dev/tty
1151
+ _UPD_CONT="${_UPD_CONT:-N}"
1152
+ [[ "${_UPD_CONT^^}" == "Y" ]] || { echo " Update cancelled."; exit 0; }
1153
+ echo ""
1154
+ fi
1155
+
1156
+ # ── Network reachability ──────────────────────────────────────────────────
1157
+ spin_start "Checking connectivity"
1158
+ if ! ping -c 1 -W 3 8.8.8.8 &>/dev/null && ! ping -c 1 -W 3 github.com &>/dev/null; then
1159
+ spin_err "No internet connection — cannot pull updates"
1160
+ exit 1
1161
+ fi
1162
+ spin_ok "Network reachable"
1163
+
1164
+ # ── Snapshot current commit ───────────────────────────────────────────────
1165
+ _GIT="sudo git -c safe.directory=${INSTALL_DIR} -C ${INSTALL_DIR}"
1166
+ _PREV_HASH=$(${_GIT} rev-parse HEAD 2>/dev/null || echo "unknown")
1167
+ _PREV_SHORT="${_PREV_HASH:0:7}"
1168
+
1169
+ # ── Pull ──────────────────────────────────────────────────────────────────
1170
+ spin_start "Pulling latest code from GitHub"
1171
+ _PULL_OUT=$(${_GIT} pull origin main 2>&1 || echo "GIT_FAILED")
1172
+
1173
+ if echo "$_PULL_OUT" | grep -qE "^GIT_FAILED|^error:|^fatal:"; then
1174
+ spin_err "git pull failed"
1175
+ echo "$_PULL_OUT" | head -6 | sed 's/^/ /'
1176
+ exit 1
1177
+ fi
1178
+
1179
+ _NEW_HASH=$(${_GIT} rev-parse HEAD 2>/dev/null || echo "unknown")
1180
+ _NEW_SHORT="${_NEW_HASH:0:7}"
1181
+
1182
+ if [[ "$_PREV_HASH" == "$_NEW_HASH" ]]; then
1183
+ spin_ok "Already up to date (${_PREV_SHORT})"
1184
+ echo ""
1185
+ info "No changes pulled — services not restarted."
1186
+ echo ""
1187
+ exit 0
1188
+ fi
1189
+ spin_ok "Pulled ${_PREV_SHORT} → ${_NEW_SHORT}"
1190
+
1191
+ # ── Show what changed ─────────────────────────────────────────────────────
1192
+ echo ""
1193
+ info "Changes:"
1194
+ ${_GIT} log --oneline "${_PREV_HASH}..HEAD" 2>/dev/null \
1195
+ | sed 's/^/ /' | head -15
1196
+ echo ""
1197
+
1198
+ # ── Re-lock source files (root:root 644) ──────────────────────────────────
1199
+ spin_start "Re-locking source file permissions"
1200
+ sudo find "${INSTALL_DIR}" -maxdepth 1 -name "*.py" \
1201
+ -exec chown root:root {} \; -exec chmod 644 {} \; 2>/dev/null || true
1202
+ sudo find "${INSTALL_DIR}/web" -name "*.py" \
1203
+ -exec chown root:root {} \; -exec chmod 644 {} \; 2>/dev/null || true
1204
+ sudo chown root:root "${INSTALL_DIR}/bin/plc_checkweigher" 2>/dev/null && \
1205
+ sudo chmod 755 "${INSTALL_DIR}/bin/plc_checkweigher" 2>/dev/null || true
1206
+ # Keep data/ writable by pi
1207
+ sudo chown -R pi:pi "${DATA_DIR}" 2>/dev/null || true
1208
+ sudo chmod 755 "${DATA_DIR}" 2>/dev/null || true
1209
+ spin_ok "Source files locked (root:root 644)"
1210
+
1211
+ # ── Install updated CLI to /usr/local/bin/ ────────────────────────────────
1212
+ spin_start "Installing updated CLI (/usr/local/bin/plc_checkweigher)"
1213
+ if sudo cp "${INSTALL_DIR}/bin/plc_checkweigher" /usr/local/bin/plc_checkweigher \
1214
+ && sudo chmod 755 /usr/local/bin/plc_checkweigher; then
1215
+ spin_ok "CLI updated"
1216
+ else
1217
+ spin_warn "CLI install failed — still running previous version until next install"
1218
+ fi
1219
+
1220
+ # ── Update systemd unit files if they changed ─────────────────────────────
1221
+ _UNITS_UPDATED=0
1222
+ for _svc_src in "${INSTALL_DIR}/plc_watcher.service" \
1223
+ "${INSTALL_DIR}/web/plc_web.service"; do
1224
+ [[ ! -f "$_svc_src" ]] && continue
1225
+ _svc_dst="/etc/systemd/system/$(basename "$_svc_src")"
1226
+ if [[ ! -f "$_svc_dst" ]] || ! diff -q "$_svc_src" "$_svc_dst" &>/dev/null; then
1227
+ sudo cp "$_svc_src" "$_svc_dst" 2>/dev/null && _UNITS_UPDATED=1
1228
+ fi
1229
+ done
1230
+ if [[ $_UNITS_UPDATED -eq 1 ]]; then
1231
+ spin_start "Reloading systemd (unit files changed)"
1232
+ sudo systemctl daemon-reload 2>/dev/null || true
1233
+ spin_ok "systemd reloaded"
1234
+ fi
1235
+
1236
+ # ── Restart services ──────────────────────────────────────────────────────
1237
+ spin_start "Restarting services"
1238
+ sudo systemctl restart plc_watcher plc_web 2>/dev/null
1239
+ sleep 2
1240
+ spin_ok "Services restarted"
1241
+
1242
+ # ── Final status ──────────────────────────────────────────────────────────
1243
+ echo ""
1244
+ _UW=$(systemctl is-active plc_watcher 2>/dev/null || echo "inactive")
1245
+ _UWb=$(systemctl is-active plc_web 2>/dev/null || echo "inactive")
1246
+ [[ "$_UW" == "active" ]] && echo -e " ${G}✓${NC} plc_watcher active" \
1247
+ || echo -e " ${R}✗${NC} plc_watcher ${_UW}"
1248
+ [[ "$_UWb" == "active" ]] && echo -e " ${G}✓${NC} plc_web active" \
1249
+ || echo -e " ${R}✗${NC} plc_web ${_UWb}"
1250
+ echo ""
1251
+ ok "Update complete (${_PREV_SHORT} → ${_NEW_SHORT})"
1252
+ echo ""
1253
+ ;;
1254
+
1129
1255
  # ─────────────────────────────────────────────────────────────────────────────
1130
1256
  # UNINSTALL — two modes: software-only or full drive wipe
1131
1257
  # ─────────────────────────────────────────────────────────────────────────────
@@ -1389,6 +1515,7 @@ help|--help|-h)
1389
1515
  echo -e " ${W}Configuration${NC}"
1390
1516
  echo " smb-config Interactively update SMB delivery target"
1391
1517
  echo " push-test Push latest PDF to SMB target now"
1518
+ echo " update Pull latest code from GitHub and restart services"
1392
1519
  echo ""
1393
1520
  echo -e " ${W}Display${NC}"
1394
1521
  echo " display on Enable display (start LightDM)"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "plc-checkweigher",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "One-command installer for the PLC Check-Weigher system on Raspberry Pi (PREEMPT_RT kernel, Python stack, WiFi, SMB, systemd RT services)",
5
5
  "scripts": {
6
6
  "postinstall": "node bin/cli.js"