From 0cda13b4e156817cc5581af66729849a35472491 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Thu, 5 Sep 2024 13:55:13 +0200 Subject: [PATCH 01/29] refactoring [v0.1.4] fix bill --- _run/values/ver.txt | 2 +- bill_table.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ verControl.go | 6 +++--- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 04e1946..8c43fb4 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.3 +v0.1.4 diff --git a/bill_table.go b/bill_table.go index 3f41c25..105a605 100644 --- a/bill_table.go +++ b/bill_table.go @@ -94,6 +94,7 @@ func (a *CAcceptor) BuildHardCodedBillTable() { a.billTypes = append(a.billTypes, CBill{"USD", 20, '*', '*', '*', '*'}) case 'G': + a.billTypes = append(a.billTypes, CBill{}) a.billTypes = append(a.billTypes, CBill{"ARS", 2, '*', '*', '*', '*'}) a.billTypes = append(a.billTypes, CBill{"ARS", 5, '*', '*', '*', '*'}) a.billTypes = append(a.billTypes, CBill{"ARS", 10, '*', '*', '*', '*'}) @@ -101,6 +102,52 @@ func (a *CAcceptor) BuildHardCodedBillTable() { a.billTypes = append(a.billTypes, CBill{"ARS", 50, '*', '*', '*', '*'}) a.billTypes = append(a.billTypes, CBill{"ARS", 100, '*', '*', '*', '*'}) + case 'A': + a.billTypes = append(a.billTypes, CBill{"AUD", 5, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 10, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 100, '*', '*', '*', '*'}) + + case 15: + a.billTypes = append(a.billTypes, CBill{}) + a.billTypes = append(a.billTypes, CBill{}) + a.billTypes = append(a.billTypes, CBill{"AUD", 5, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 10, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 20, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 50, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"AUD", 100, '*', '*', '*', '*'}) + + case 'W': + a.billTypes = append(a.billTypes, CBill{"BRL", 1, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 2, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 5, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 10, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 20, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 50, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"BRL", 100, '*', '*', '*', '*'}) + + case 'C': + a.billTypes = append(a.billTypes, CBill{}) + a.billTypes = append(a.billTypes, CBill{}) + a.billTypes = append(a.billTypes, CBill{"CAD", 5, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"CAD", 10, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"CAD", 20, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"CAD", 50, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"CAD", 100, '*', '*', '*', '*'}) + + case 'D': + a.billTypes = append(a.billTypes, CBill{"EUR", 5, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"EUR", 10, '*', '*', '*', '*'}) + + case 'M': + a.billTypes = append(a.billTypes, CBill{"MXP", 20, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"MXP", 50, '*', '*', '*', '*'}) + + case 'B': + a.billTypes = append(a.billTypes, CBill{"RUR", 10, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"RUR", 50, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"RUR", 100, '*', '*', '*', '*'}) + a.billTypes = append(a.billTypes, CBill{"RUR", 500, '*', '*', '*', '*'}) + default: a.billTypes = append(a.billTypes, CBill{"USD", 1, '*', '*', '*', '*'}) a.billTypes = append(a.billTypes, CBill{"USD", 2, '*', '*', '*', '*'}) diff --git a/verControl.go b/verControl.go index 8ab7f79..0706264 100644 --- a/verControl.go +++ b/verControl.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-05-2024" - GlobalHash string = "9cf6b8c5447a34ff427159e035b9d58d9d27e904" + GlobalHash string = "5f5d8f6c8c104473b08a993faef7c5a3aacd46c4" - GlobalVersion string = "v0.1.3" + GlobalVersion string = "v0.1.4" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 3 + GlobalVersionPatch uint16 = 4 ) From 25e995a594c21573ad4d64e89f822df22090cd0b Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 13:20:58 +0200 Subject: [PATCH 02/29] refactoring [v0.1.5] made a generator for enum and const --- _run/fileGenerator/const.go | 51 +++++++++++ _run/fileGenerator/enum.go | 177 ++++++++++++++++++++++++++++++++++++ _run/fileGenerator/main.go | 116 +++++++++++++++++++++++ _run/values/ver.txt | 2 +- consts/cmd.go | 37 ++++++++ consts/cmdAux.go | 78 ++++++++++++++++ enum/bezel.go | 31 +++++++ enum/document.go | 37 ++++++++ enum/event.go | 106 +++++++++++++++++++++ enum/orientation.go | 37 ++++++++ enum/orientation_control.go | 31 +++++++ enum/powerUp.go | 34 +++++++ enum/pupExt.go | 40 ++++++++ enum/state.go | 76 ++++++++++++++++ enum/status.go | 34 +++++++ generator/const.go | 8 ++ generator/methods.go | 72 +++++++++++++++ generator/names.go | 153 +++++++++++++++++++++++++++++++ generator/saves.go | 74 +++++++++++++++ generator/struct.go | 28 ++++++ generator/values.go | 93 +++++++++++++++++++ new_log.go | 52 +++++++++++ verControl.go | 6 +- 23 files changed, 1369 insertions(+), 4 deletions(-) create mode 100644 _run/fileGenerator/const.go create mode 100644 _run/fileGenerator/enum.go create mode 100644 _run/fileGenerator/main.go create mode 100644 consts/cmd.go create mode 100644 consts/cmdAux.go create mode 100644 enum/bezel.go create mode 100644 enum/document.go create mode 100644 enum/event.go create mode 100644 enum/orientation.go create mode 100644 enum/orientation_control.go create mode 100644 enum/powerUp.go create mode 100644 enum/pupExt.go create mode 100644 enum/state.go create mode 100644 enum/status.go create mode 100644 generator/const.go create mode 100644 generator/methods.go create mode 100644 generator/names.go create mode 100644 generator/saves.go create mode 100644 generator/struct.go create mode 100644 generator/values.go create mode 100644 new_log.go diff --git a/_run/fileGenerator/const.go b/_run/fileGenerator/const.go new file mode 100644 index 0000000..bb44b35 --- /dev/null +++ b/_run/fileGenerator/const.go @@ -0,0 +1,51 @@ +package main + +import "github.com/hard-soft-ware/mpost/generator" + +//////////////////////////////////// + +const constDir = "consts" + +//// + +func Cmd() { + obj := generator.Init("Cmd", constDir+"/cmd.go") + val := obj.NewVal() + + val.Add(0x10, "Omnibus") + val.Add(0x40, "Calibrate") + val.Add(0x50, "FlashDownload") + val.Add(0x60, "Auxiliary") + val.Add(0x70, "Expanded") + + constGenerator(obj, val) + obj.Save(constDir).End() +} + +func CmdAux() { + obj := generator.Init("CmdAux", constDir+"/cmdAux.go") + val := obj.NewVal() + + val.Add(0x00, "SoftwareCRC") + val.Add(0x01, "CashBoxTotal") + val.Add(0x02, "DeviceResets") + val.Add(0x10, "BNFStatus") + val.Add(0x11, "SetBezel") + val.Add(0x0D, "DeviceCapabilities") + val.Add(0x03, "CleDeviceResetsarCashBoxTotal").Delim() + + val.Add(0x04, "AcceptorType") + val.Add(0x0E, "AcceptorApplicationID") + val.Add(0x0F, "AcceptorVariantID") + val.Add(0x05, "AcceptorSerialNumber") + val.Add(0x06, "AcceptorBootPartNumber") + val.Add(0x07, "AcceptorApplicationPartNumber") + val.Add(0x08, "AcceptorVariantName") + val.Add(0x09, "AcceptorVariantPartNumber") + val.Add(0x0A, "AcceptorAuditLifeTimeTotals") + val.Add(0x0B, "AcceptorAuditQPMeasures") + val.Add(0x0C, "AcceptorAuditPerformanceMeasures") + + constGenerator(obj, val) + obj.Save(constDir).End() +} diff --git a/_run/fileGenerator/enum.go b/_run/fileGenerator/enum.go new file mode 100644 index 0000000..27e3205 --- /dev/null +++ b/_run/fileGenerator/enum.go @@ -0,0 +1,177 @@ +package main + +import ( + "github.com/hard-soft-ware/mpost/generator" +) + +//////////////////////////////////// + +const enumDir = "enum" + +//// + +func Status() { + obj := generator.Init("BNFStatus", enumDir+"/status.go") + + val := []string{ + "Unknown", + "Error", + "OK", + "NotAttached", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func Document() { + obj := generator.Init("Document", enumDir+"/document.go") + + val := []string{ + "None", + "NoValue", + "Bill", + "Barcode", + "Coupon", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func Orientation() { + obj := generator.Init("Orientation", enumDir+"/orientation.go") + + val := []string{ + "RightUp", + "RightDown", + "LeftUp", + "LeftDown", + "UnknownOrientation", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func OrientationControl() { + obj := generator.Init("OrientationControl", enumDir+"/orientation_control.go") + + val := []string{ + "FourWay", + "TwoWay", + "OneWay", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func PowerUp() { + obj := generator.Init("PowerUp", enumDir+"/powerUp.go") + + val := []string{ + "A", + "B", + "C", + "E", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func PupExt() { + obj := generator.Init("PupExt", enumDir+"/pupExt.go") + + val := []string{ + "Return", + "OutOfService", + "Stack", + "StackNoCredit", + "Wait", + "WaitNoCredit", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func State() { + obj := generator.Init("State", enumDir+"/state.go") + + val := []string{ + "Escrow", + "Stacked", + "Returned", + "Rejected", + "Stalled", + "Accepting", + "CalibrateStart", + "Calibrating", + "Connecting", + "Disconnected", + "Downloading", + "DownloadRestart", + "DownloadStart", + "Failed", + "Idling", + "PupEscrow", + "Returning", + "Stacking", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func Bezel() { + obj := generator.Init("Bezel", enumDir+"/bezel.go") + + val := []string{ + "Standard", + "Platform", + "Diagnostic", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} + +func Event() { + obj := generator.Init("Event", enumDir+"/event.go") + + val := []string{ + "_Begin", + "Connected", + "Escrow", + "PUPEscrow", + "Stacked", + "Returned", + "Rejected", + "Cheated", + "StackerFull", + "CalibrateStart", + "CalibrateProgress", + "CalibrateFinish", + "DownloadStart", + "DownloadRestart", + "DownloadProgress", + "DownloadFinish", + "PauseDetected", + "PauseCleared", + "StallDetected", + "StallCleared", + "JamDetected", + "JamCleared", + "PowerUp", + "InvalidCommand", + "CashBoxAttached", + "CashBoxRemoved", + "Disconnected", + "_End", + } + + enumGenerator(obj, val) + obj.Save(enumDir).End() +} diff --git a/_run/fileGenerator/main.go b/_run/fileGenerator/main.go new file mode 100644 index 0000000..45956a4 --- /dev/null +++ b/_run/fileGenerator/main.go @@ -0,0 +1,116 @@ +package main + +import "github.com/hard-soft-ware/mpost/generator" + +//////////////////////////////////// + +func enumGenerator(obj *generator.GeneratorObj, val []string) { + obj.Print("type ").Name.Type().PrintLN(" byte").LN() + + obj.PrintLN("const (") + for pos, text := range val { + obj.Offset(1).Name.SelfCode(text).Print(" ") + obj.Name.Type().Print(" = ").Number(pos).LN() + } + + obj.PrintLN(")").LN() + + // + + obj.PrintLN("const (") + + for _, text := range val { + obj.Offset(1).Name.TextCode(text).Print(" = ") + obj.String(text).LN() + } + + obj.PrintLN(")").LN() + + // + + obj.Print("var ").Name.Map().Print(" = map[").Name.Type().PrintLN("]string{") + + for _, text := range val { + obj.Offset(1).Name.SelfCode(text).Print(": ") + obj.Name.TextCode(text).PrintLN(",") + } + + obj.PrintLN("}").LN() + + // + + obj.Print("func (obj ").Name.Type().PrintLN(") String() string {") + obj.Offset(1).Print("val, ok := ").Name.Map().PrintLN("[obj]") + obj.Offset(1).PrintLN("if ok {").Offset(2).PrintLN("return val").Offset(1).PrintLN("}") + obj.Offset(1).Print("return \"Unknown ").Name.Type().PrintLN("\"") + obj.PrintLN("}") + +} + +func constGenerator(obj *generator.GeneratorObj, val *generator.GeneratorValueObj) { + obj.Print("type ").Name.Type().PrintLN(" int").LN() + + obj.PrintLN("const (") + for _, code := range val.Get.Ints() { + text := val.Get.Text(code) + + obj.Offset(1).Name.SelfCode(text).Print(" ") + obj.Name.Type().Print(" = ").Hex(code).LN() + + if val.Get.IsDelim(code) { + obj.LN() + } + } + obj.PrintLN(")").LN() + + // + + obj.PrintLN("const (") + for _, code := range val.Get.Ints() { + text := val.Get.Text(code) + + obj.Offset(1).Name.TextCode(text).Print(" = ") + obj.String(text).LN() + + if val.Get.IsDelim(code) { + obj.LN() + } + } + obj.PrintLN(")").LN() + + // + + obj.Print("var ").Name.Map().Print(" = map[").Name.Type().PrintLN("]string{") + for _, code := range val.Get.Ints() { + text := val.Get.Text(code) + + obj.Offset(1).Name.SelfCode(text).Print(": ") + obj.Name.TextCode(text).PrintLN(",") + + } + obj.PrintLN("}").LN() + + obj.Print("func (obj ").Name.Type().PrintLN(") String() string {") + obj.Offset(1).Print("val, ok := ").Name.Map().PrintLN("[obj]") + obj.Offset(1).PrintLN("if ok {").Offset(2).PrintLN("return val").Offset(1).PrintLN("}") + obj.Offset(1).Print("return \"Unknown ").Name.Type().PrintLN("\"") + obj.PrintLN("}") + +} + +//////// + +func main() { + Status() + Document() + Orientation() + OrientationControl() + PowerUp() + PupExt() + State() + Bezel() + Event() + + Cmd() + CmdAux() +} diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 8c43fb4..027a383 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.4 +v0.1.5 diff --git a/consts/cmd.go b/consts/cmd.go new file mode 100644 index 0000000..d4a2d5f --- /dev/null +++ b/consts/cmd.go @@ -0,0 +1,37 @@ +package consts + +/* This file is automatically generated */ + +type CmdType int + +const ( + CmdOmnibus CmdType = 0x10 + CmdCalibrate CmdType = 0x40 + CmdFlashDownload CmdType = 0x50 + CmdAuxiliary CmdType = 0x60 + CmdExpanded CmdType = 0x70 +) + +const ( + CmdTextOmnibus = "Omnibus" + CmdTextCalibrate = "Calibrate" + CmdTextFlashDownload = "FlashDownload" + CmdTextAuxiliary = "Auxiliary" + CmdTextExpanded = "Expanded" +) + +var CmdMap = map[CmdType]string{ + CmdOmnibus: CmdTextOmnibus, + CmdCalibrate: CmdTextCalibrate, + CmdFlashDownload: CmdTextFlashDownload, + CmdAuxiliary: CmdTextAuxiliary, + CmdExpanded: CmdTextExpanded, +} + +func (obj CmdType) String() string { + val, ok := CmdMap[obj] + if ok { + return val + } + return "Unknown CmdType" +} diff --git a/consts/cmdAux.go b/consts/cmdAux.go new file mode 100644 index 0000000..9526777 --- /dev/null +++ b/consts/cmdAux.go @@ -0,0 +1,78 @@ +package consts + +/* This file is automatically generated */ + +type CmdAuxType int + +const ( + CmdAuxSoftwareCRC CmdAuxType = 0x00 + CmdAuxCashBoxTotal CmdAuxType = 0x01 + CmdAuxDeviceResets CmdAuxType = 0x02 + CmdAuxBNFStatus CmdAuxType = 0x10 + CmdAuxSetBezel CmdAuxType = 0x11 + CmdAuxDeviceCapabilities CmdAuxType = 0x0d + CmdAuxCleDeviceResetsarCashBoxTotal CmdAuxType = 0x03 + + CmdAuxAcceptorType CmdAuxType = 0x04 + CmdAuxAcceptorApplicationID CmdAuxType = 0x0e + CmdAuxAcceptorVariantID CmdAuxType = 0x0f + CmdAuxAcceptorSerialNumber CmdAuxType = 0x05 + CmdAuxAcceptorBootPartNumber CmdAuxType = 0x06 + CmdAuxAcceptorApplicationPartNumber CmdAuxType = 0x07 + CmdAuxAcceptorVariantName CmdAuxType = 0x08 + CmdAuxAcceptorVariantPartNumber CmdAuxType = 0x09 + CmdAuxAcceptorAuditLifeTimeTotals CmdAuxType = 0x0a + CmdAuxAcceptorAuditQPMeasures CmdAuxType = 0x0b + CmdAuxAcceptorAuditPerformanceMeasures CmdAuxType = 0x0c +) + +const ( + CmdAuxTextSoftwareCRC = "SoftwareCRC" + CmdAuxTextCashBoxTotal = "CashBoxTotal" + CmdAuxTextDeviceResets = "DeviceResets" + CmdAuxTextBNFStatus = "BNFStatus" + CmdAuxTextSetBezel = "SetBezel" + CmdAuxTextDeviceCapabilities = "DeviceCapabilities" + CmdAuxTextCleDeviceResetsarCashBoxTotal = "CleDeviceResetsarCashBoxTotal" + + CmdAuxTextAcceptorType = "AcceptorType" + CmdAuxTextAcceptorApplicationID = "AcceptorApplicationID" + CmdAuxTextAcceptorVariantID = "AcceptorVariantID" + CmdAuxTextAcceptorSerialNumber = "AcceptorSerialNumber" + CmdAuxTextAcceptorBootPartNumber = "AcceptorBootPartNumber" + CmdAuxTextAcceptorApplicationPartNumber = "AcceptorApplicationPartNumber" + CmdAuxTextAcceptorVariantName = "AcceptorVariantName" + CmdAuxTextAcceptorVariantPartNumber = "AcceptorVariantPartNumber" + CmdAuxTextAcceptorAuditLifeTimeTotals = "AcceptorAuditLifeTimeTotals" + CmdAuxTextAcceptorAuditQPMeasures = "AcceptorAuditQPMeasures" + CmdAuxTextAcceptorAuditPerformanceMeasures = "AcceptorAuditPerformanceMeasures" +) + +var CmdAuxMap = map[CmdAuxType]string{ + CmdAuxSoftwareCRC: CmdAuxTextSoftwareCRC, + CmdAuxCashBoxTotal: CmdAuxTextCashBoxTotal, + CmdAuxDeviceResets: CmdAuxTextDeviceResets, + CmdAuxBNFStatus: CmdAuxTextBNFStatus, + CmdAuxSetBezel: CmdAuxTextSetBezel, + CmdAuxDeviceCapabilities: CmdAuxTextDeviceCapabilities, + CmdAuxCleDeviceResetsarCashBoxTotal: CmdAuxTextCleDeviceResetsarCashBoxTotal, + CmdAuxAcceptorType: CmdAuxTextAcceptorType, + CmdAuxAcceptorApplicationID: CmdAuxTextAcceptorApplicationID, + CmdAuxAcceptorVariantID: CmdAuxTextAcceptorVariantID, + CmdAuxAcceptorSerialNumber: CmdAuxTextAcceptorSerialNumber, + CmdAuxAcceptorBootPartNumber: CmdAuxTextAcceptorBootPartNumber, + CmdAuxAcceptorApplicationPartNumber: CmdAuxTextAcceptorApplicationPartNumber, + CmdAuxAcceptorVariantName: CmdAuxTextAcceptorVariantName, + CmdAuxAcceptorVariantPartNumber: CmdAuxTextAcceptorVariantPartNumber, + CmdAuxAcceptorAuditLifeTimeTotals: CmdAuxTextAcceptorAuditLifeTimeTotals, + CmdAuxAcceptorAuditQPMeasures: CmdAuxTextAcceptorAuditQPMeasures, + CmdAuxAcceptorAuditPerformanceMeasures: CmdAuxTextAcceptorAuditPerformanceMeasures, +} + +func (obj CmdAuxType) String() string { + val, ok := CmdAuxMap[obj] + if ok { + return val + } + return "Unknown CmdAuxType" +} diff --git a/enum/bezel.go b/enum/bezel.go new file mode 100644 index 0000000..b08ca1f --- /dev/null +++ b/enum/bezel.go @@ -0,0 +1,31 @@ +package enum + +/* This file is automatically generated */ + +type BezelType byte + +const ( + BezelStandard BezelType = 0 + BezelPlatform BezelType = 1 + BezelDiagnostic BezelType = 2 +) + +const ( + BezelTextStandard = "Standard" + BezelTextPlatform = "Platform" + BezelTextDiagnostic = "Diagnostic" +) + +var BezelMap = map[BezelType]string{ + BezelStandard: BezelTextStandard, + BezelPlatform: BezelTextPlatform, + BezelDiagnostic: BezelTextDiagnostic, +} + +func (obj BezelType) String() string { + val, ok := BezelMap[obj] + if ok { + return val + } + return "Unknown BezelType" +} diff --git a/enum/document.go b/enum/document.go new file mode 100644 index 0000000..60b08c7 --- /dev/null +++ b/enum/document.go @@ -0,0 +1,37 @@ +package enum + +/* This file is automatically generated */ + +type DocumentType byte + +const ( + DocumentNone DocumentType = 0 + DocumentNoValue DocumentType = 1 + DocumentBill DocumentType = 2 + DocumentBarcode DocumentType = 3 + DocumentCoupon DocumentType = 4 +) + +const ( + DocumentTextNone = "None" + DocumentTextNoValue = "NoValue" + DocumentTextBill = "Bill" + DocumentTextBarcode = "Barcode" + DocumentTextCoupon = "Coupon" +) + +var DocumentMap = map[DocumentType]string{ + DocumentNone: DocumentTextNone, + DocumentNoValue: DocumentTextNoValue, + DocumentBill: DocumentTextBill, + DocumentBarcode: DocumentTextBarcode, + DocumentCoupon: DocumentTextCoupon, +} + +func (obj DocumentType) String() string { + val, ok := DocumentMap[obj] + if ok { + return val + } + return "Unknown DocumentType" +} diff --git a/enum/event.go b/enum/event.go new file mode 100644 index 0000000..a2f919b --- /dev/null +++ b/enum/event.go @@ -0,0 +1,106 @@ +package enum + +/* This file is automatically generated */ + +type EventType byte + +const ( + Event_Begin EventType = 0 + EventConnected EventType = 1 + EventEscrow EventType = 2 + EventPUPEscrow EventType = 3 + EventStacked EventType = 4 + EventReturned EventType = 5 + EventRejected EventType = 6 + EventCheated EventType = 7 + EventStackerFull EventType = 8 + EventCalibrateStart EventType = 9 + EventCalibrateProgress EventType = 10 + EventCalibrateFinish EventType = 11 + EventDownloadStart EventType = 12 + EventDownloadRestart EventType = 13 + EventDownloadProgress EventType = 14 + EventDownloadFinish EventType = 15 + EventPauseDetected EventType = 16 + EventPauseCleared EventType = 17 + EventStallDetected EventType = 18 + EventStallCleared EventType = 19 + EventJamDetected EventType = 20 + EventJamCleared EventType = 21 + EventPowerUp EventType = 22 + EventInvalidCommand EventType = 23 + EventCashBoxAttached EventType = 24 + EventCashBoxRemoved EventType = 25 + EventDisconnected EventType = 26 + Event_End EventType = 27 +) + +const ( + EventText_Begin = "_Begin" + EventTextConnected = "Connected" + EventTextEscrow = "Escrow" + EventTextPUPEscrow = "PUPEscrow" + EventTextStacked = "Stacked" + EventTextReturned = "Returned" + EventTextRejected = "Rejected" + EventTextCheated = "Cheated" + EventTextStackerFull = "StackerFull" + EventTextCalibrateStart = "CalibrateStart" + EventTextCalibrateProgress = "CalibrateProgress" + EventTextCalibrateFinish = "CalibrateFinish" + EventTextDownloadStart = "DownloadStart" + EventTextDownloadRestart = "DownloadRestart" + EventTextDownloadProgress = "DownloadProgress" + EventTextDownloadFinish = "DownloadFinish" + EventTextPauseDetected = "PauseDetected" + EventTextPauseCleared = "PauseCleared" + EventTextStallDetected = "StallDetected" + EventTextStallCleared = "StallCleared" + EventTextJamDetected = "JamDetected" + EventTextJamCleared = "JamCleared" + EventTextPowerUp = "PowerUp" + EventTextInvalidCommand = "InvalidCommand" + EventTextCashBoxAttached = "CashBoxAttached" + EventTextCashBoxRemoved = "CashBoxRemoved" + EventTextDisconnected = "Disconnected" + EventText_End = "_End" +) + +var EventMap = map[EventType]string{ + Event_Begin: EventText_Begin, + EventConnected: EventTextConnected, + EventEscrow: EventTextEscrow, + EventPUPEscrow: EventTextPUPEscrow, + EventStacked: EventTextStacked, + EventReturned: EventTextReturned, + EventRejected: EventTextRejected, + EventCheated: EventTextCheated, + EventStackerFull: EventTextStackerFull, + EventCalibrateStart: EventTextCalibrateStart, + EventCalibrateProgress: EventTextCalibrateProgress, + EventCalibrateFinish: EventTextCalibrateFinish, + EventDownloadStart: EventTextDownloadStart, + EventDownloadRestart: EventTextDownloadRestart, + EventDownloadProgress: EventTextDownloadProgress, + EventDownloadFinish: EventTextDownloadFinish, + EventPauseDetected: EventTextPauseDetected, + EventPauseCleared: EventTextPauseCleared, + EventStallDetected: EventTextStallDetected, + EventStallCleared: EventTextStallCleared, + EventJamDetected: EventTextJamDetected, + EventJamCleared: EventTextJamCleared, + EventPowerUp: EventTextPowerUp, + EventInvalidCommand: EventTextInvalidCommand, + EventCashBoxAttached: EventTextCashBoxAttached, + EventCashBoxRemoved: EventTextCashBoxRemoved, + EventDisconnected: EventTextDisconnected, + Event_End: EventText_End, +} + +func (obj EventType) String() string { + val, ok := EventMap[obj] + if ok { + return val + } + return "Unknown EventType" +} diff --git a/enum/orientation.go b/enum/orientation.go new file mode 100644 index 0000000..0b37af4 --- /dev/null +++ b/enum/orientation.go @@ -0,0 +1,37 @@ +package enum + +/* This file is automatically generated */ + +type OrientationType byte + +const ( + OrientationRightUp OrientationType = 0 + OrientationRightDown OrientationType = 1 + OrientationLeftUp OrientationType = 2 + OrientationLeftDown OrientationType = 3 + OrientationUnknownOrientation OrientationType = 4 +) + +const ( + OrientationTextRightUp = "RightUp" + OrientationTextRightDown = "RightDown" + OrientationTextLeftUp = "LeftUp" + OrientationTextLeftDown = "LeftDown" + OrientationTextUnknownOrientation = "UnknownOrientation" +) + +var OrientationMap = map[OrientationType]string{ + OrientationRightUp: OrientationTextRightUp, + OrientationRightDown: OrientationTextRightDown, + OrientationLeftUp: OrientationTextLeftUp, + OrientationLeftDown: OrientationTextLeftDown, + OrientationUnknownOrientation: OrientationTextUnknownOrientation, +} + +func (obj OrientationType) String() string { + val, ok := OrientationMap[obj] + if ok { + return val + } + return "Unknown OrientationType" +} diff --git a/enum/orientation_control.go b/enum/orientation_control.go new file mode 100644 index 0000000..280170a --- /dev/null +++ b/enum/orientation_control.go @@ -0,0 +1,31 @@ +package enum + +/* This file is automatically generated */ + +type OrientationControlType byte + +const ( + OrientationControlFourWay OrientationControlType = 0 + OrientationControlTwoWay OrientationControlType = 1 + OrientationControlOneWay OrientationControlType = 2 +) + +const ( + OrientationControlTextFourWay = "FourWay" + OrientationControlTextTwoWay = "TwoWay" + OrientationControlTextOneWay = "OneWay" +) + +var OrientationControlMap = map[OrientationControlType]string{ + OrientationControlFourWay: OrientationControlTextFourWay, + OrientationControlTwoWay: OrientationControlTextTwoWay, + OrientationControlOneWay: OrientationControlTextOneWay, +} + +func (obj OrientationControlType) String() string { + val, ok := OrientationControlMap[obj] + if ok { + return val + } + return "Unknown OrientationControlType" +} diff --git a/enum/powerUp.go b/enum/powerUp.go new file mode 100644 index 0000000..eca4a03 --- /dev/null +++ b/enum/powerUp.go @@ -0,0 +1,34 @@ +package enum + +/* This file is automatically generated */ + +type PowerUpType byte + +const ( + PowerUpA PowerUpType = 0 + PowerUpB PowerUpType = 1 + PowerUpC PowerUpType = 2 + PowerUpE PowerUpType = 3 +) + +const ( + PowerUpTextA = "A" + PowerUpTextB = "B" + PowerUpTextC = "C" + PowerUpTextE = "E" +) + +var PowerUpMap = map[PowerUpType]string{ + PowerUpA: PowerUpTextA, + PowerUpB: PowerUpTextB, + PowerUpC: PowerUpTextC, + PowerUpE: PowerUpTextE, +} + +func (obj PowerUpType) String() string { + val, ok := PowerUpMap[obj] + if ok { + return val + } + return "Unknown PowerUpType" +} diff --git a/enum/pupExt.go b/enum/pupExt.go new file mode 100644 index 0000000..9c5e291 --- /dev/null +++ b/enum/pupExt.go @@ -0,0 +1,40 @@ +package enum + +/* This file is automatically generated */ + +type PupExtType byte + +const ( + PupExtReturn PupExtType = 0 + PupExtOutOfService PupExtType = 1 + PupExtStack PupExtType = 2 + PupExtStackNoCredit PupExtType = 3 + PupExtWait PupExtType = 4 + PupExtWaitNoCredit PupExtType = 5 +) + +const ( + PupExtTextReturn = "Return" + PupExtTextOutOfService = "OutOfService" + PupExtTextStack = "Stack" + PupExtTextStackNoCredit = "StackNoCredit" + PupExtTextWait = "Wait" + PupExtTextWaitNoCredit = "WaitNoCredit" +) + +var PupExtMap = map[PupExtType]string{ + PupExtReturn: PupExtTextReturn, + PupExtOutOfService: PupExtTextOutOfService, + PupExtStack: PupExtTextStack, + PupExtStackNoCredit: PupExtTextStackNoCredit, + PupExtWait: PupExtTextWait, + PupExtWaitNoCredit: PupExtTextWaitNoCredit, +} + +func (obj PupExtType) String() string { + val, ok := PupExtMap[obj] + if ok { + return val + } + return "Unknown PupExtType" +} diff --git a/enum/state.go b/enum/state.go new file mode 100644 index 0000000..a672133 --- /dev/null +++ b/enum/state.go @@ -0,0 +1,76 @@ +package enum + +/* This file is automatically generated */ + +type StateType byte + +const ( + StateEscrow StateType = 0 + StateStacked StateType = 1 + StateReturned StateType = 2 + StateRejected StateType = 3 + StateStalled StateType = 4 + StateAccepting StateType = 5 + StateCalibrateStart StateType = 6 + StateCalibrating StateType = 7 + StateConnecting StateType = 8 + StateDisconnected StateType = 9 + StateDownloading StateType = 10 + StateDownloadRestart StateType = 11 + StateDownloadStart StateType = 12 + StateFailed StateType = 13 + StateIdling StateType = 14 + StatePupEscrow StateType = 15 + StateReturning StateType = 16 + StateStacking StateType = 17 +) + +const ( + StateTextEscrow = "Escrow" + StateTextStacked = "Stacked" + StateTextReturned = "Returned" + StateTextRejected = "Rejected" + StateTextStalled = "Stalled" + StateTextAccepting = "Accepting" + StateTextCalibrateStart = "CalibrateStart" + StateTextCalibrating = "Calibrating" + StateTextConnecting = "Connecting" + StateTextDisconnected = "Disconnected" + StateTextDownloading = "Downloading" + StateTextDownloadRestart = "DownloadRestart" + StateTextDownloadStart = "DownloadStart" + StateTextFailed = "Failed" + StateTextIdling = "Idling" + StateTextPupEscrow = "PupEscrow" + StateTextReturning = "Returning" + StateTextStacking = "Stacking" +) + +var StateMap = map[StateType]string{ + StateEscrow: StateTextEscrow, + StateStacked: StateTextStacked, + StateReturned: StateTextReturned, + StateRejected: StateTextRejected, + StateStalled: StateTextStalled, + StateAccepting: StateTextAccepting, + StateCalibrateStart: StateTextCalibrateStart, + StateCalibrating: StateTextCalibrating, + StateConnecting: StateTextConnecting, + StateDisconnected: StateTextDisconnected, + StateDownloading: StateTextDownloading, + StateDownloadRestart: StateTextDownloadRestart, + StateDownloadStart: StateTextDownloadStart, + StateFailed: StateTextFailed, + StateIdling: StateTextIdling, + StatePupEscrow: StateTextPupEscrow, + StateReturning: StateTextReturning, + StateStacking: StateTextStacking, +} + +func (obj StateType) String() string { + val, ok := StateMap[obj] + if ok { + return val + } + return "Unknown StateType" +} diff --git a/enum/status.go b/enum/status.go new file mode 100644 index 0000000..3e0c4cf --- /dev/null +++ b/enum/status.go @@ -0,0 +1,34 @@ +package enum + +/* This file is automatically generated */ + +type BNFStatusType byte + +const ( + BNFStatusUnknown BNFStatusType = 0 + BNFStatusError BNFStatusType = 1 + BNFStatusOK BNFStatusType = 2 + BNFStatusNotAttached BNFStatusType = 3 +) + +const ( + BNFStatusTextUnknown = "Unknown" + BNFStatusTextError = "Error" + BNFStatusTextOK = "OK" + BNFStatusTextNotAttached = "NotAttached" +) + +var BNFStatusMap = map[BNFStatusType]string{ + BNFStatusUnknown: BNFStatusTextUnknown, + BNFStatusError: BNFStatusTextError, + BNFStatusOK: BNFStatusTextOK, + BNFStatusNotAttached: BNFStatusTextNotAttached, +} + +func (obj BNFStatusType) String() string { + val, ok := BNFStatusMap[obj] + if ok { + return val + } + return "Unknown BNFStatusType" +} diff --git a/generator/const.go b/generator/const.go new file mode 100644 index 0000000..e5b990d --- /dev/null +++ b/generator/const.go @@ -0,0 +1,8 @@ +package generator + +//###########################################################// + +const ( + hText = "/* This file is automatically generated */" + re = `[^a-zA-Z0-9_]` +) diff --git a/generator/methods.go b/generator/methods.go new file mode 100644 index 0000000..eba9170 --- /dev/null +++ b/generator/methods.go @@ -0,0 +1,72 @@ +package generator + +import ( + "fmt" + "strings" +) + +//###########################################################// + +func (gen *GeneratorObj) NewVal() *GeneratorValueObj { + return generatorValueInit(&gen.name) +} + +//// + +func (gen *GeneratorObj) Len() int { + return gen.buf.Len() +} + +func (gen *GeneratorObj) Write(data []byte) *GeneratorObj { + gen.buf.Write(data) + return gen +} + +func (gen *GeneratorObj) Del(len int) *GeneratorObj { + gen.buf.Truncate(gen.Len() - len) + return gen +} + +// + +func (gen *GeneratorObj) LN() *GeneratorObj { + gen.Write([]byte("\n")) + return gen +} + +func (gen *GeneratorObj) Print(text string) *GeneratorObj { + gen.Write([]byte(text)) + return gen +} + +func (gen *GeneratorObj) PrintLN(text string) *GeneratorObj { + gen.Print(text).LN() + return gen +} + +func (gen *GeneratorObj) Repeat(chat string, size int) *GeneratorObj { + gen.Print(strings.Repeat(chat, size)) + return gen +} + +func (gen *GeneratorObj) Offset(size int) *GeneratorObj { + gen.Repeat("\t", size) + return gen +} + +// + +func (gen *GeneratorObj) String(a any) *GeneratorObj { + gen.Print(fmt.Sprintf("\"%s\"", a)) + return gen +} + +func (gen *GeneratorObj) Hex(a any) *GeneratorObj { + gen.Print(fmt.Sprintf("0x%02x", a)) + return gen +} + +func (gen *GeneratorObj) Number(a any) *GeneratorObj { + gen.Print(fmt.Sprintf("%d", a)) + return gen +} diff --git a/generator/names.go b/generator/names.go new file mode 100644 index 0000000..9174c53 --- /dev/null +++ b/generator/names.go @@ -0,0 +1,153 @@ +package generator + +import ( + "regexp" + "strings" + "unicode" +) + +//###########################################################// + +func (n *GeneratorNameObj) ToTitleCase(s string) string { + s = strings.ReplaceAll(s, "-", " ") + r := regexp.MustCompile(re) + words := strings.Fields(s) + + for i, word := range words { + if len(word) > 0 { + word = r.ReplaceAllString(word, "") + if len(word) > 0 { + words[i] = string(unicode.ToUpper(rune(word[0]))) + word[1:] + } + + } + } + + return strings.Join(words, "") +} + +//// + +func (n *GeneratorNameObj) Get() string { + return n.ToTitleCase(n.gen.name) +} + +func (n *GeneratorNameObj) GetParam(param string) string { + return n.Get() + n.ToTitleCase(param) +} + +func (n *GeneratorNameObj) GetObj() string { + return n.GetParam("Obj") +} + +func (n *GeneratorNameObj) GetMap() string { + return n.GetParam("Map") +} + +func (n *GeneratorNameObj) GetType() string { + return n.GetParam("Type") +} + +func (n *GeneratorNameObj) GetText() string { + return n.GetParam("Text") +} + +// + +func (n *GeneratorNameObj) GetCode(code string) string { + return n.Get() + n.ToTitleCase(code) +} + +func (n *GeneratorNameObj) GetParamCode(param string, code string) string { + return n.GetParam(param) + n.ToTitleCase(code) +} + +func (n *GeneratorNameObj) GetObjCode(code string) string { + return n.GetObj() + n.ToTitleCase(code) +} + +func (n *GeneratorNameObj) GetMapCode(code string) string { + return n.GetMap() + n.ToTitleCase(code) +} + +func (n *GeneratorNameObj) GetTypeCode(code string) string { + return n.GetType() + n.ToTitleCase(code) +} + +func (n *GeneratorNameObj) GetTextCode(code string) string { + return n.GetText() + n.ToTitleCase(code) +} + +// + +func (n *GeneratorNameObj) Self() *GeneratorObj { + n.gen.Print(n.Get()) + return n.gen +} + +func (n *GeneratorNameObj) Param(param string) *GeneratorObj { + n.gen.Print(n.GetParam(param)) + return n.gen +} + +func (n *GeneratorNameObj) Obj() *GeneratorObj { + n.gen.Print(n.GetObj()) + return n.gen +} + +func (n *GeneratorNameObj) Map() *GeneratorObj { + n.gen.Print(n.GetMap()) + return n.gen +} + +func (n *GeneratorNameObj) Type() *GeneratorObj { + n.gen.Print(n.GetType()) + return n.gen +} + +func (n *GeneratorNameObj) Text() *GeneratorObj { + n.gen.Print(n.GetText()) + return n.gen +} + +// + +func (n *GeneratorNameObj) CodeToTitleCase(code string) *GeneratorObj { + n.gen.Print(n.ToTitleCase(code)) + return n.gen +} + +func (n *GeneratorNameObj) SelfCode(code string) *GeneratorObj { + n.gen.Print(n.GetCode(code)) + return n.gen +} + +func (n *GeneratorNameObj) SelfParam(param string, code string) *GeneratorObj { + n.gen.Print(n.GetParamCode(param, code)) + return n.gen +} + +func (n *GeneratorNameObj) SelfParamCode(param string, code string) *GeneratorObj { + n.gen.Print(n.ToTitleCase(param) + n.ToTitleCase(code)) + return n.gen +} + +func (n *GeneratorNameObj) ObjCode(code string) *GeneratorObj { + n.gen.Print(n.GetObjCode(code)) + return n.gen +} + +func (n *GeneratorNameObj) MapCode(code string) *GeneratorObj { + n.gen.Print(n.GetMapCode(code)) + return n.gen +} + +func (n *GeneratorNameObj) TypeCode(code string) *GeneratorObj { + n.gen.Print(n.GetTypeCode(code)) + return n.gen +} + +func (n *GeneratorNameObj) TextCode(code string) *GeneratorObj { + n.gen.Print(n.GetTextCode(code)) + return n.gen +} diff --git a/generator/saves.go b/generator/saves.go new file mode 100644 index 0000000..150bda4 --- /dev/null +++ b/generator/saves.go @@ -0,0 +1,74 @@ +package generator + +import "os" + +//###########################################################// + +type GeneratorSaveObj struct { + gen *GeneratorObj + Add GeneratorSaveAddObj + + pack string + imports []string + types []string +} + +func (gen *GeneratorObj) Save(pack string) *GeneratorSaveObj { + save := GeneratorSaveObj{} + save.Add.save = &save + + save.gen = gen + save.pack = pack + + return &save +} + +func (save *GeneratorSaveObj) End() error { + file, err := os.OpenFile(save.gen.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer file.Close() + + file.WriteString("package " + save.pack + "\n\n") + + switch { + case len(save.imports) == 1: + file.WriteString("import \"" + save.imports[0] + "\";\n\n") + + case len(save.imports) > 1: + file.WriteString("import (\n") + for _, i := range save.imports { + file.WriteString("\t\"" + i + "\"\n") + } + file.WriteString(")\n\n") + } + + file.WriteString(hText + "\n\n") + + if len(save.types) > 0 { + for _, i := range save.types { + file.WriteString("type " + i + "\n") + } + file.WriteString("\n") + } + + file.Write(save.gen.buf.Bytes()) + return nil +} + +// + +type GeneratorSaveAddObj struct { + save *GeneratorSaveObj +} + +func (add *GeneratorSaveAddObj) Type(name string, types string) *GeneratorSaveObj { + add.save.types = append(add.save.types, name+" "+types) + return add.save +} + +func (add *GeneratorSaveAddObj) Import(imports string) *GeneratorSaveObj { + add.save.imports = append(add.save.imports, imports) + return add.save +} diff --git a/generator/struct.go b/generator/struct.go new file mode 100644 index 0000000..8979294 --- /dev/null +++ b/generator/struct.go @@ -0,0 +1,28 @@ +package generator + +import "bytes" + +//###########################################################// + +type GeneratorNameObj struct { + gen *GeneratorObj +} + +type GeneratorObj struct { + name string + filename string + buf bytes.Buffer + + Name GeneratorNameObj +} + +func Init(name string, file string) *GeneratorObj { + obj := GeneratorObj{} + + obj.name = name + obj.filename = file + + obj.Name.gen = &obj + + return &obj +} diff --git a/generator/values.go b/generator/values.go new file mode 100644 index 0000000..a0c8133 --- /dev/null +++ b/generator/values.go @@ -0,0 +1,93 @@ +package generator + +//###########################################################// + +type GeneratorValueObj struct { + maps map[any]string + delim map[any]bool + list []any + lastKey any + + Get GeneratorValueGetObj + + name *string +} + +func generatorValueInit(name *string) *GeneratorValueObj { + obj := GeneratorValueObj{} + + obj.maps = make(map[any]string) + obj.delim = make(map[any]bool) + + obj.name = name + obj.Get.arr = &obj + + return &obj +} + +func (arr *GeneratorValueObj) Add(code any, text string) *GeneratorValueObj { + arr.lastKey = code + + arr.list = append(arr.list, code) + arr.maps[code] = text + + return arr +} + +func (arr *GeneratorValueObj) AddByte(code byte, text string) *GeneratorValueObj { + arr.Add(code, text) + return arr +} + +func (arr *GeneratorValueObj) Delim() { + arr.delim[arr.lastKey] = true +} + +//// + +type GeneratorValueGetObj struct { + arr *GeneratorValueObj +} + +func (get *GeneratorValueGetObj) Text(code any) string { + return get.arr.maps[code] +} + +func (get *GeneratorValueGetObj) IsDelim(code any) bool { + _, ok := get.arr.delim[code] + return ok +} + +// + +func (get *GeneratorValueGetObj) Bytes() []byte { + bufArr := make([]byte, len(get.arr.list)) + for pos, val := range get.arr.list { + bufArr[pos] = val.(uint8) + } + return bufArr +} + +func (get *GeneratorValueGetObj) Ints() []int { + bufArr := make([]int, len(get.arr.list)) + for pos, val := range get.arr.list { + bufArr[pos] = val.(int) + } + return bufArr +} + +func (get *GeneratorValueGetObj) Uints() []uint { + bufArr := make([]uint, len(get.arr.list)) + for pos, val := range get.arr.list { + bufArr[pos] = val.(uint) + } + return bufArr +} + +func (get *GeneratorValueGetObj) Strings() []string { + bufArr := make([]string, len(get.arr.list)) + for pos, val := range get.arr.list { + bufArr[pos] = val.(string) + } + return bufArr +} diff --git a/new_log.go b/new_log.go new file mode 100644 index 0000000..8f24ec6 --- /dev/null +++ b/new_log.go @@ -0,0 +1,52 @@ +package mpost + +import ( + "fmt" + "github.com/rs/zerolog" + "strings" +) + +///////////////////////////////////////////////////////// + +type LogStruct struct { + log *zerolog.Logger + index string +} + +func NewLog2(log *zerolog.Logger, root string) *LogStruct { + obj := LogStruct{index: root, log: log} + return &obj +} + +func (obj *LogStruct) New(point string) *LogStruct { + newObj := LogStruct{index: obj.index + "/" + point} + return &newObj +} + +//// + +func (obj *LogStruct) Msg(msg string) { + obj.log.Debug().Msg(msg) +} + +func (obj *LogStruct) Err(msg string, err error) { + obj.log.Debug().Err(err).Msg(msg) +} + +func (obj *LogStruct) Bytes(msg string, data []byte) { + var sb strings.Builder + for i, byteVal := range data { + if i > 0 { + sb.WriteString(" ") + } + fmt.Fprintf(&sb, "%02X", byteVal) + } + + obj.log.Debug().Str("data", sb.String()).Msg(msg) +} + +// + +func (obj *LogStruct) Event(event Event) { + obj.log.Debug().Str("event", "lool").Msg("Event") +} diff --git a/verControl.go b/verControl.go index 0706264..47d3816 100644 --- a/verControl.go +++ b/verControl.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-05-2024" - GlobalHash string = "5f5d8f6c8c104473b08a993faef7c5a3aacd46c4" + GlobalHash string = "0cda13b4e156817cc5581af66729849a35472491" - GlobalVersion string = "v0.1.4" + GlobalVersion string = "v0.1.5" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 4 + GlobalVersionPatch uint16 = 5 ) From ac62904c321306765bd4926f1f9a7adca7045e53 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 13:56:22 +0200 Subject: [PATCH 03/29] refactoring [v0.1.6] applied generator variables --- .github/workflows/tests.yml | 4 +- 0_test.go | 3 +- _run/fileGenerator/const.go | 12 +++ _run/fileGenerator/main.go | 7 +- _run/scripts/creator_const_Go.sh | 2 +- _run/values/ver.txt | 2 +- acceptor.go | 23 ++--- acceptor_command.go | 15 ++-- acceptor_loop.go | 10 ++- acceptor_methods.go | 12 ++- acceptor_process.go | 56 ++++++------ acceptor_raise.go | 12 +-- acceptor_serial.go | 6 +- acceptor_thread.go | 9 +- bill.go | 13 +-- bill_table.go | 3 +- const.go | 40 ++------- consts/cmd.go | 6 +- consts/cmdAux.go | 6 +- consts/data.go | 35 ++++++++ data.go | 6 -- data_escrow.go | 19 ++-- data_process.go | 12 +-- data_send_replay.go | 30 ++++--- enum.go | 144 ------------------------------- new_log.go | 3 +- timeouts.go | 11 +++ verControl.go | 12 --- 28 files changed, 216 insertions(+), 297 deletions(-) create mode 100644 consts/data.go delete mode 100644 enum.go create mode 100644 timeouts.go delete mode 100644 verControl.go diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 33f1332..dd62118 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,14 +4,14 @@ on: push: paths-ignore: - '**/*.yml' - - 'verControl.go' + - 'const.go' - '**/*.txt' - '*.md' - '*.pdf' pull_request: paths-ignore: - '**/*.yml' - - 'verControl.go' + - 'const.go' - '**/*.txt' - '*.md' - '*.pdf' diff --git a/0_test.go b/0_test.go index aefbf72..93bd55b 100644 --- a/0_test.go +++ b/0_test.go @@ -1,13 +1,14 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/enum" "testing" "time" ) func TestName(t *testing.T) { a := NewCAcceptor(30*time.Second, 30*time.Second) - a.Open("/dev/ttyUSB0", B) + a.Open("/dev/ttyUSB0", enum.PowerUpB) time.Sleep(5 * time.Second) t.Log(a.GetDeviceSerialNumber()) diff --git a/_run/fileGenerator/const.go b/_run/fileGenerator/const.go index bb44b35..972c30b 100644 --- a/_run/fileGenerator/const.go +++ b/_run/fileGenerator/const.go @@ -49,3 +49,15 @@ func CmdAux() { constGenerator(obj, val) obj.Save(constDir).End() } + +func Data() { + obj := generator.Init("Data", constDir+"/data.go") + val := obj.NewVal() + + val.Add(0x02, "STX") // Начало текста + val.Add(0x03, "ETX") // Конец текста + val.Add(0x06, "ACKMask") // Маска подтверждения + + constGenerator(obj, val) + obj.Save(constDir).End() +} diff --git a/_run/fileGenerator/main.go b/_run/fileGenerator/main.go index 45956a4..11661a6 100644 --- a/_run/fileGenerator/main.go +++ b/_run/fileGenerator/main.go @@ -48,7 +48,7 @@ func enumGenerator(obj *generator.GeneratorObj, val []string) { } func constGenerator(obj *generator.GeneratorObj, val *generator.GeneratorValueObj) { - obj.Print("type ").Name.Type().PrintLN(" int").LN() + obj.Print("type ").Name.Type().PrintLN(" byte").LN() obj.PrintLN("const (") for _, code := range val.Get.Ints() { @@ -94,6 +94,10 @@ func constGenerator(obj *generator.GeneratorObj, val *generator.GeneratorValueOb obj.Offset(1).Print("val, ok := ").Name.Map().PrintLN("[obj]") obj.Offset(1).PrintLN("if ok {").Offset(2).PrintLN("return val").Offset(1).PrintLN("}") obj.Offset(1).Print("return \"Unknown ").Name.Type().PrintLN("\"") + obj.PrintLN("}").LN() + + obj.Print("func (obj ").Name.Type().PrintLN(") Byte() byte {") + obj.Offset(1).PrintLN("return byte(obj)") obj.PrintLN("}") } @@ -113,4 +117,5 @@ func main() { Cmd() CmdAux() + Data() } diff --git a/_run/scripts/creator_const_Go.sh b/_run/scripts/creator_const_Go.sh index 5a25449..a7ec672 100644 --- a/_run/scripts/creator_const_Go.sh +++ b/_run/scripts/creator_const_Go.sh @@ -2,7 +2,7 @@ # Original source: https://github.com/Bookshelf-Writer/scripts-for-integration/blob/main/_run/scripts/creator_const_Go.sh dir_path="" -file_name="verControl.go" +file_name="const.go" package_name="mpost" ############################################################################# diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 027a383..82942c3 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.5 +v0.1.6 diff --git a/acceptor.go b/acceptor.go index 7e47557..75ccc12 100644 --- a/acceptor.go +++ b/acceptor.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/enum" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "go.bug.st/serial" @@ -11,6 +12,8 @@ import ( //////////////////////////////////// +type EventHandler func(*CAcceptor, int) + type CAcceptor struct { port serial.Port auditLifeTimeTotals []int @@ -25,7 +28,7 @@ type CAcceptor struct { billValues []CBill billValueEnables []bool - bnfStatus BNFStatus + bnfStatus enum.BNFStatusType bootPN string capApplicationID bool capApplicationPN bool @@ -61,23 +64,23 @@ type CAcceptor struct { deviceModel int devicePaused bool devicePortName string - devicePowerUp PowerUp + devicePowerUp enum.PowerUpType deviceResets int deviceRevision int deviceSerialNumber string deviceStalled bool - deviceState State + deviceState enum.StateType deviceType string - docType DocumentType + docType enum.DocumentType enableAcceptance bool enableBarCodes bool enableBookmarks bool enableCouponExt bool enableNoPush bool - escrowOrientation Orientation + escrowOrientation enum.OrientationType highSecurity bool - orientationCtl OrientationControl - orientationCtlExt OrientationControl + orientationCtl enum.OrientationControlType + orientationCtlExt enum.OrientationControlType version string transactionTimeout time.Duration downloadTimeout time.Duration @@ -136,7 +139,7 @@ type CAcceptor struct { isReplyAcked bool signalMainThreadEvent int - eventHandlers map[Event]EventHandler + eventHandlers map[enum.EventType]EventHandler log *LogGlobalStruct } @@ -145,7 +148,7 @@ func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor a := &CAcceptor{ transactionTimeout: transactionTimeout, downloadTimeout: downloadTimeout, - eventHandlers: make(map[Event]EventHandler, Events_End), + eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), messageQueue: make(chan *CMessage, 1), replyQueue: make(chan []byte, 1), @@ -171,6 +174,6 @@ func (a *CAcceptor) getTickCount() int64 { return time.Now().UnixNano() / int64(time.Millisecond) } -func (a *CAcceptor) SetEventHandler(event Event, eventHandler func(*CAcceptor, int)) { +func (a *CAcceptor) SetEventHandler(event enum.EventType, eventHandler func(*CAcceptor, int)) { a.eventHandlers[event] = eventHandler } diff --git a/acceptor_command.go b/acceptor_command.go index 8320d25..416e40d 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -2,6 +2,7 @@ package mpost import ( "errors" + "github.com/hard-soft-ware/mpost/enum" "time" ) @@ -10,13 +11,13 @@ import ( func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, data0Index int) { payload[0] = controlCode - if a.enableBookmarks && a.enableAcceptance && a.deviceState != Calibrating { + if a.enableBookmarks && a.enableAcceptance && a.deviceState != enum.StateCalibrating { payload[0] |= 0x20 } data0 := byte(0) - if a.enableAcceptance && a.deviceState != Calibrating { + if a.enableAcceptance && a.deviceState != enum.StateCalibrating { if a.expandedNoteReporting { data0 |= 0x7F } else { @@ -39,9 +40,9 @@ func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, da } switch a.orientationCtl { - case TwoWay: + case enum.OrientationControlTwoWay: data1 |= 0x04 - case FourWay: + case enum.OrientationControlFourWay: data1 |= 0x0C } @@ -53,14 +54,14 @@ func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, da data2 |= 0x01 } - if a.enableBarCodes && a.enableAcceptance && a.deviceState != Calibrating { + if a.enableBarCodes && a.enableAcceptance && a.deviceState != enum.StateCalibrating { data2 |= 0x02 } switch a.devicePowerUp { - case B: + case enum.PowerUpB: data2 |= 0x04 - case C: + case enum.PowerUpC: data2 |= 0x0C } diff --git a/acceptor_loop.go b/acceptor_loop.go index d35a477..645ef0f 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -1,6 +1,10 @@ package mpost -import "time" +import ( + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" + "time" +) //////////////////////////////////// @@ -8,7 +12,7 @@ func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { startTickCount := time.Now() for { - payload := []byte{CmdOmnibus, 0x00, 0x10, 0x00} + payload := []byte{consts.CmdOmnibus.Byte(), 0x00, 0x10, 0x00} reply, err := a.SendSynchronousCommand(payload) if err != nil { @@ -26,7 +30,7 @@ func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { if a.stopFlashDownloadThread { a.stopFlashDownloadThread = true <-a.flashDownloadThread - a.deviceState = Idling + a.deviceState = enum.StateIdling a.wasStopped = true return nil } diff --git a/acceptor_methods.go b/acceptor_methods.go index 930cff7..8e629a6 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -1,6 +1,10 @@ package mpost -import "fmt" +import ( + "fmt" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" +) // ////////////////////////////////// func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName string) error { @@ -13,9 +17,9 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st } switch a.deviceState { - case DownloadStart, Downloading: + case enum.StateDownloadStart, enum.StateDownloading: return fmt.Errorf("Calling %s not allowed during flash download.", propertyName) - case CalibrateStart, Calibrating: + case enum.StateCalibrateStart, enum.StateCalibrating: return fmt.Errorf("Calling %s not allowed during calibration.", propertyName) } @@ -29,7 +33,7 @@ func (a *CAcceptor) GetDeviceSerialNumber() string { return "" } - payload := []byte{CmdAuxiliary, 0, 0, CmdAuxQueryAcceptorSerialNumber} + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} reply, err := a.SendSynchronousCommand(payload) if err != nil { a.log.Debug().Err(err).Msg("GetDeviceSerialNumber") diff --git a/acceptor_process.go b/acceptor_process.go index d44410a..e39fa39 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -1,22 +1,24 @@ package mpost +import "github.com/hard-soft-ware/mpost/enum" + //////////////////////////////////// func (a *CAcceptor) processData0(data0 byte) { if (data0 & 0x01) != 0 { - if a.deviceState != Calibrating && a.deviceState != CalibrateStart { - a.deviceState = Idling + if a.deviceState != enum.StateCalibrating && a.deviceState != enum.StateCalibrateStart { + a.deviceState = enum.StateIdling } } if (data0 & 0x02) != 0 { - if a.deviceState != Calibrating && a.deviceState != CalibrateStart { - a.deviceState = Accepting + if a.deviceState != enum.StateCalibrating && a.deviceState != enum.StateCalibrateStart { + a.deviceState = enum.StateAccepting } } if (data0 & 0x04) != 0 { - a.deviceState = Escrow + a.deviceState = enum.StateEscrow if a.autoStack { a.shouldRaiseEscrowEvent = false } @@ -25,21 +27,21 @@ func (a *CAcceptor) processData0(data0 byte) { } if (data0 & 0x08) != 0 { - a.deviceState = Stacking + a.deviceState = enum.StateStacking } if (data0 & 0x10) != 0 { - a.deviceState = Stacked + a.deviceState = enum.StateStacked } else { a.shouldRaiseStackedEvent = true } if (data0 & 0x20) != 0 { - a.deviceState = Returning + a.deviceState = enum.StateReturning } if (data0 & 0x40) != 0 { - a.deviceState = Returned + a.deviceState = enum.StateReturned a.bill = CBill{} // Resetting the bill } else { a.shouldRaiseReturnedEvent = true @@ -55,7 +57,7 @@ func (a *CAcceptor) processData1(data1 byte) { } if (data1 & 0x02) != 0 { - a.deviceState = Rejected + a.deviceState = enum.StateRejected } else { a.shouldRaiseRejectedEvent = true } @@ -91,14 +93,14 @@ func (a *CAcceptor) processData1(data1 byte) { } if (data1 & 0x40) != 0 { - a.deviceState = Calibrating + a.deviceState = enum.StateCalibrating if a.shouldRaiseCalibrateProgressEvent { a.RaiseCalibrateProgressEvent() } } else { - if a.deviceState == Calibrating { + if a.deviceState == enum.StateCalibrating { a.shouldRaiseCalibrateFinishEvent = true - a.deviceState = Idling + a.deviceState = enum.StateIdling } } } @@ -107,32 +109,32 @@ func (a *CAcceptor) processData2(data2 byte) { if !a.expandedNoteReporting { billTypeIndex := (data2 & 0x38) >> 3 if billTypeIndex > 0 { - if a.deviceState == Escrow || (a.deviceState == Stacked && !a.wasDocTypeSetOnEscrow) { + if a.deviceState == enum.StateEscrow || (a.deviceState == enum.StateStacked && !a.wasDocTypeSetOnEscrow) { a.bill = a.billTypes[billTypeIndex-1] - a.docType = Bill - a.wasDocTypeSetOnEscrow = a.deviceState == Escrow + a.docType = enum.DocumentBill + a.wasDocTypeSetOnEscrow = a.deviceState == enum.StateEscrow } } else { - if a.deviceState == Stacked || a.deviceState == Escrow { + if a.deviceState == enum.StateStacked || a.deviceState == enum.StateEscrow { a.bill = CBill{} - a.docType = NoValue + a.docType = enum.DocumentNoValue a.wasDocTypeSetOnEscrow = false } } } else { - if a.deviceState == Stacked { - if a.docType == Bill && a.bill.Value == 0.0 { - a.docType = NoValue + if a.deviceState == enum.StateStacked { + if a.docType == enum.DocumentBill && a.bill.Value == 0.0 { + a.docType = enum.DocumentNoValue } - } else if a.deviceState == Escrow { + } else if a.deviceState == enum.StateEscrow { a.bill = CBill{} - a.docType = NoValue + a.docType = enum.DocumentNoValue } } if (data2 & 0x01) != 0 { a.isPoweredUp = true - a.docType = NoValue + a.docType = enum.DocumentNoValue } else { a.shouldRaisePowerUpEvent = true if !a.isVeryFirstPoll { @@ -148,20 +150,20 @@ func (a *CAcceptor) processData2(data2 byte) { } if (data2 & 0x04) != 0 { - a.deviceState = Failed + a.deviceState = enum.StateFailed } } func (a *CAcceptor) processData3(data3 byte) { if (data3 & 0x01) != 0 { - a.deviceState = Stalled + a.deviceState = enum.StateStalled a.shouldRaiseStallClearedEvent = true } else { a.shouldRaiseStallDetectedEvent = true } if (data3 & 0x02) != 0 { - a.deviceState = DownloadRestart + a.deviceState = enum.StateDownloadRestart } if (data3 & 0x08) != 0 { diff --git a/acceptor_raise.go b/acceptor_raise.go index c74a1f8..43477b0 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -1,8 +1,10 @@ package mpost +import "github.com/hard-soft-ware/mpost/enum" + //////////////////////////////////// -func (a *CAcceptor) raiseXX(e Event) { +func (a *CAcceptor) raiseXX(e enum.EventType) { if handler, exists := a.eventHandlers[e]; exists && handler != nil { handler(a, 0) } @@ -12,26 +14,26 @@ func (a *CAcceptor) raiseXX(e Event) { func (a *CAcceptor) RaiseConnectedEvent() { a.log.Debug().Str("Event", "ConnectedEvent").Msg("Raise") - a.raiseXX(ConnectedEvent) + a.raiseXX(enum.EventConnected) a.shouldRaiseDisconnectedEvent = true a.shouldRaiseConnectedEvent = false } func (a *CAcceptor) RaiseDisconnectedEvent() { a.log.Debug().Str("Event", "DisconnectedEvent").Msg("Raise") - a.raiseXX(DisconnectedEvent) + a.raiseXX(enum.EventDisconnected) a.shouldRaiseDisconnectedEvent = false a.shouldRaiseConnectedEvent = true } func (a *CAcceptor) RaiseDownloadRestartEvent() { a.log.Debug().Str("Event", "DownloadRestartEvent").Msg("Raise") - a.raiseXX(DownloadRestartEvent) + a.raiseXX(enum.EventDownloadRestart) a.shouldRaiseDownloadRestartEvent = false } func (a *CAcceptor) RaiseCalibrateProgressEvent() { a.log.Debug().Str("Event", "CalibrateProgressEvent").Msg("Raise") - a.raiseXX(CalibrateProgressEvent) + a.raiseXX(enum.EventCalibrateProgress) a.shouldRaiseCalibrateProgressEvent = false } diff --git a/acceptor_serial.go b/acceptor_serial.go index c6313a5..081d896 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -2,6 +2,8 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" "go.bug.st/serial" "strings" "time" @@ -20,7 +22,7 @@ func byteSliceToString(b []byte) string { //////////////////////////////////// -func (a *CAcceptor) Open(portName string, powerUp PowerUp) error { +func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { lg := a.log.NewLog("OpenSerial") if a.connected { @@ -106,7 +108,7 @@ func (a *CAcceptor) QueryDeviceCapabilities(lg *LogGlobalStruct) { return } - payload := []byte{CmdAuxiliary, 0x00, 0x00, CmdAuxQueryDeviceCapabilities} + payload := []byte{consts.CmdAuxiliary.Byte(), 0x00, 0x00, consts.CmdAuxDeviceCapabilities.Byte()} reply, err := a.SendSynchronousCommand(payload) if len(reply) < 4 { diff --git a/acceptor_thread.go b/acceptor_thread.go index 69ffaf0..8833897 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -1,6 +1,9 @@ package mpost -import "time" +import ( + "github.com/hard-soft-ware/mpost/enum" + "time" +) //////////////////////////////////// @@ -17,7 +20,7 @@ func (a *CAcceptor) OpenThread() { a.dataLinkLayer.ProcessReply(replay) a.QueryDeviceCapabilities(lg) - if a.deviceState != DownloadRestart { + if a.deviceState != enum.StateDownloadRestart { a.SetUpBillTable() a.connected = true if a.shouldRaiseConnectedEvent { @@ -44,7 +47,7 @@ func (a *CAcceptor) MessageLoopThread() { } if time.Since(timeoutStart) > 30*time.Second { - if a.deviceState != Downloading && a.deviceState != DownloadRestart { + if a.deviceState != enum.StateDownloading && a.deviceState != enum.StateDownloadRestart { a.connected = false if a.shouldRaiseDisconnectedEvent { a.RaiseDisconnectedEvent() diff --git a/bill.go b/bill.go index 0295f94..178c3fc 100644 --- a/bill.go +++ b/bill.go @@ -2,6 +2,7 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/enum" "strconv" "strings" ) @@ -52,19 +53,19 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) CBill { } bill.Value = billValue - a.docType = Bill - a.wasDocTypeSetOnEscrow = a.deviceState == Escrow + a.docType = enum.DocumentBill + a.wasDocTypeSetOnEscrow = a.deviceState == enum.StateEscrow orientation := reply[extDataIndex+10] switch orientation { case 0x00: - a.escrowOrientation = RightUp + a.escrowOrientation = enum.OrientationRightUp case 0x01: - a.escrowOrientation = RightDown + a.escrowOrientation = enum.OrientationRightDown case 0x02: - a.escrowOrientation = LeftUp + a.escrowOrientation = enum.OrientationLeftUp case 0x03: - a.escrowOrientation = LeftDown + a.escrowOrientation = enum.OrientationLeftDown } bill.Type = rune(reply[extDataIndex+11]) diff --git a/bill_table.go b/bill_table.go index 105a605..6429e96 100644 --- a/bill_table.go +++ b/bill_table.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/consts" "time" ) @@ -33,7 +34,7 @@ func (a *CAcceptor) RetrieveBillTable() { index := 1 for { payload := make([]byte, 6) - a.ConstructOmnibusCommand(payload, CmdExpanded, 2) + a.ConstructOmnibusCommand(payload, consts.CmdExpanded.Byte(), 2) payload[1] = 0x02 payload[5] = byte(index) diff --git a/const.go b/const.go index 6d6cc39..d1946d2 100644 --- a/const.go +++ b/const.go @@ -1,36 +1,12 @@ package mpost -//////////////////////////////////// - -const ( - CmdOmnibus = 0x10 - CmdCalibrate = 0x40 - CmdFlashDownload = 0x50 - CmdAuxiliary = 0x60 - CmdExpanded = 0x70 - - CmdAuxQuerySoftwareCRC = 0x00 - CmdAuxQueryCashBoxTotal = 0x01 - CmdAuxQueryDeviceResets = 0x02 - CmdAuxClearCashBoxTotal = 0x03 - CmdAuxQueryAcceptorType = 0x04 - CmdAuxQueryAcceptorSerialNumber = 0x05 - CmdAuxQueryAcceptorBootPartNumber = 0x06 - CmdAuxQueryAcceptorApplicationPartNumber = 0x07 - CmdAuxQueryAcceptorVariantName = 0x08 - CmdAuxQueryAcceptorVariantPartNumber = 0x09 - CmdAuxQueryAcceptorAuditLifeTimeTotals = 0x0A - CmdAuxQueryAcceptorAuditQPMeasures = 0x0B - CmdAuxQueryAcceptorAuditPerformanceMeasures = 0x0C - CmdAuxQueryDeviceCapabilities = 0x0D - CmdAuxQueryAcceptorApplicationID = 0x0E - CmdAuxQueryAcceptorVariantID = 0x0F - CmdAuxQueryBNFStatus = 0x10 - CmdAuxSetBezel = 0x11 -) - const ( - CommunicationDisconnectTimeout = 3000 - PollingDisconnectTimeout = 3000 - CalibrateTimeout = 3000 + GlobalName string = "mpost" + GlobalDateUpdate string = "09-06-2024" + GlobalHash string = "25e995a594c21573ad4d64e89f822df22090cd0b" + + GlobalVersion string = "v0.1.6" + GlobalVersionMajor string = "v0" + GlobalVersionMinor uint16 = 1 + GlobalVersionPatch uint16 = 6 ) diff --git a/consts/cmd.go b/consts/cmd.go index d4a2d5f..4c987bc 100644 --- a/consts/cmd.go +++ b/consts/cmd.go @@ -2,7 +2,7 @@ package consts /* This file is automatically generated */ -type CmdType int +type CmdType byte const ( CmdOmnibus CmdType = 0x10 @@ -35,3 +35,7 @@ func (obj CmdType) String() string { } return "Unknown CmdType" } + +func (obj CmdType) Byte() byte { + return byte(obj) +} diff --git a/consts/cmdAux.go b/consts/cmdAux.go index 9526777..220b87d 100644 --- a/consts/cmdAux.go +++ b/consts/cmdAux.go @@ -2,7 +2,7 @@ package consts /* This file is automatically generated */ -type CmdAuxType int +type CmdAuxType byte const ( CmdAuxSoftwareCRC CmdAuxType = 0x00 @@ -76,3 +76,7 @@ func (obj CmdAuxType) String() string { } return "Unknown CmdAuxType" } + +func (obj CmdAuxType) Byte() byte { + return byte(obj) +} diff --git a/consts/data.go b/consts/data.go new file mode 100644 index 0000000..f9becf3 --- /dev/null +++ b/consts/data.go @@ -0,0 +1,35 @@ +package consts + +/* This file is automatically generated */ + +type DataType byte + +const ( + DataSTX DataType = 0x02 + DataETX DataType = 0x03 + DataACKMask DataType = 0x06 +) + +const ( + DataTextSTX = "STX" + DataTextETX = "ETX" + DataTextACKMask = "ACKMask" +) + +var DataMap = map[DataType]string{ + DataSTX: DataTextSTX, + DataETX: DataTextETX, + DataACKMask: DataTextACKMask, +} + +func (obj DataType) String() string { + val, ok := DataMap[obj] + if ok { + return val + } + return "Unknown DataType" +} + +func (obj DataType) Byte() byte { + return byte(obj) +} diff --git a/data.go b/data.go index e3d5abb..5b31203 100644 --- a/data.go +++ b/data.go @@ -13,12 +13,6 @@ type CDataLinkLayer struct { IdenticalCommandAndReplyCount int } -const ( - STX = 0x02 // Начало текста - ETX = 0x03 // Конец текста - ACKMask = 0x06 // Маска подтверждения -) - func (a *CAcceptor) NewCDataLinkLayer(lg *LogGlobalStruct) *CDataLinkLayer { return &CDataLinkLayer{ Acceptor: a, diff --git a/data_escrow.go b/data_escrow.go index 7af41dc..14d1c88 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -1,5 +1,10 @@ package mpost +import ( + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" +) + //////////////////////////////////// func (dl *CDataLinkLayer) escrowXX(b byte) { @@ -9,7 +14,7 @@ func (dl *CDataLinkLayer) escrowXX(b byte) { } payload := make([]byte, 4) - dl.Acceptor.ConstructOmnibusCommand(payload, CmdOmnibus, 1) + dl.Acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus.Byte(), 1) payload[2] |= b @@ -38,7 +43,7 @@ func (dl *CDataLinkLayer) RaiseEvents() { } switch dl.Acceptor.deviceState { - case Escrow: + case enum.StateEscrow: if dl.Acceptor.isPoweredUp && dl.Acceptor.shouldRaisePUPEscrowEvent { dl.log.Debug().Msg("PUP Escrow Event Raised") dl.Acceptor.shouldRaisePUPEscrowEvent = false @@ -46,29 +51,29 @@ func (dl *CDataLinkLayer) RaiseEvents() { dl.log.Debug().Msg("Escrow Event Raised") dl.Acceptor.shouldRaiseEscrowEvent = false } - case Stacked: + case enum.StateStacked: if dl.Acceptor.shouldRaiseStackedEvent { dl.log.Debug().Msg("Stacked Event Raised") dl.Acceptor.shouldRaiseStackedEvent = false } - case Returned: + case enum.StateReturned: if dl.Acceptor.shouldRaiseReturnedEvent { dl.log.Debug().Msg("Returned Event Raised") dl.Acceptor.shouldRaiseReturnedEvent = false } - case Rejected: + case enum.StateRejected: if dl.Acceptor.shouldRaiseRejectedEvent { dl.log.Debug().Msg("Rejected Event Raised") dl.Acceptor.shouldRaiseRejectedEvent = false } - case Stalled: + case enum.StateStalled: if dl.Acceptor.shouldRaiseStallDetectedEvent { dl.log.Debug().Msg("Stall Detected Event Raised") dl.Acceptor.shouldRaiseStallDetectedEvent = false } } - if dl.Acceptor.deviceState != Stalled && dl.Acceptor.shouldRaiseStallClearedEvent { + if dl.Acceptor.deviceState != enum.StateStalled && dl.Acceptor.shouldRaiseStallClearedEvent { dl.log.Debug().Msg("Stall Cleared Event Raised") dl.Acceptor.shouldRaiseStallClearedEvent = false } diff --git a/data_process.go b/data_process.go index 092ac0a..ff50ae6 100644 --- a/data_process.go +++ b/data_process.go @@ -1,5 +1,7 @@ package mpost +import "github.com/hard-soft-ware/mpost/enum" + //////////////////////////////////// func (dl *CDataLinkLayer) ProcessStandardOmnibusReply(reply []byte) { @@ -29,7 +31,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusBarCodeReply(reply []byte) { dl.Acceptor.processData3(reply[7]) dl.Acceptor.processData5(reply[9]) - if dl.Acceptor.deviceState == Escrow { + if dl.Acceptor.deviceState == enum.StateEscrow { dl.Acceptor.barCode = "" for i := 10; i < 38; i++ { if reply[i] != '(' { @@ -38,7 +40,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusBarCodeReply(reply []byte) { break } } - dl.Acceptor.docType = Barcode + dl.Acceptor.docType = enum.DocumentBarcode } } @@ -67,7 +69,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.processData3(reply[7]) dl.Acceptor.processData5(reply[9]) - if dl.Acceptor.deviceState == Escrow || (dl.Acceptor.deviceState == Stacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if dl.Acceptor.deviceState == enum.StateEscrow || (dl.Acceptor.deviceState == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { couponData := ((int(reply[10]) & 0x0F) << 12) + ((int(reply[11]) & 0x0F) << 8) + ((int(reply[12]) & 0x0F) << 4) + @@ -82,7 +84,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.coupon = NewCCoupon(ownerID, value) - dl.Acceptor.docType = Coupon - dl.Acceptor.wasDocTypeSetOnEscrow = dl.Acceptor.deviceState == Escrow + dl.Acceptor.docType = enum.DocumentCoupon + dl.Acceptor.wasDocTypeSetOnEscrow = dl.Acceptor.deviceState == enum.StateEscrow } } diff --git a/data_send_replay.go b/data_send_replay.go index 71fb028..64b1275 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -3,6 +3,8 @@ package mpost import ( "bufio" "errors" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" "io" "time" ) @@ -14,13 +16,13 @@ func (dl *CDataLinkLayer) SendPacket(payload []byte) { commandLength := payloadLength + 4 // STX + Length char + ETX + Checksum command := make([]byte, 0, commandLength) - command = append(command, STX) + command = append(command, consts.DataSTX.Byte()) command = append(command, byte(commandLength)) command = append(command, payload...) command[2] |= dl.AckToggleBit - command = append(command, ETX) + command = append(command, consts.DataETX.Byte()) command = append(command, dl.ComputeCheckSum(command)) dl.CurrentCommand = command @@ -56,7 +58,7 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { reply := []byte{} timeout := dl.Acceptor.transactionTimeout - if dl.Acceptor.deviceState == DownloadStart || dl.Acceptor.deviceState == Downloading { + if dl.Acceptor.deviceState == enum.StateDownloadStart || dl.Acceptor.deviceState == enum.StateDownloading { timeout = dl.Acceptor.downloadTimeout } @@ -71,7 +73,7 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { if bytesRead < 2 { return nil, errors.New("received insufficient bytes") } - if stxAndLength[0] != STX { + if stxAndLength[0] != consts.DataSTX.Byte() { return nil, errors.New("invalid STX received") } @@ -110,7 +112,7 @@ func (dl *CDataLinkLayer) ReplyAcked(reply []byte) bool { return false } - if (reply[2] & ACKMask) == dl.AckToggleBit { + if (reply[2] & consts.DataACKMask.Byte()) == dl.AckToggleBit { dl.AckToggleBit ^= 0x01 // Переключаем бит подтверждения dl.NakCount = 0 @@ -141,7 +143,7 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { } if (ctl & 0x70) == 0x50 { - dl.Acceptor.deviceState = DownloadRestart + dl.Acceptor.deviceState = enum.StateDownloadRestart } if (ctl & 0x70) == 0x70 { @@ -151,18 +153,18 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.ProcessExtendedOmnibusBarCodeReply(reply) case 0x02: dl.ProcessExtendedOmnibusExpandedNoteReply(reply) - if dl.Acceptor.deviceState == Escrow || (dl.Acceptor.deviceState == Stacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if dl.Acceptor.deviceState == enum.StateEscrow || (dl.Acceptor.deviceState == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { if dl.Acceptor.capOrientationExt { switch dl.Acceptor.orientationCtlExt { - case OneWay: - if dl.Acceptor.escrowOrientation != RightUp { + case enum.OrientationControlOneWay: + if dl.Acceptor.escrowOrientation != enum.OrientationRightUp { dl.EscrowReturn() } - case TwoWay: - if dl.Acceptor.escrowOrientation != RightUp && dl.Acceptor.escrowOrientation != LeftUp { + case enum.OrientationControlTwoWay: + if dl.Acceptor.escrowOrientation != enum.OrientationRightUp && dl.Acceptor.escrowOrientation != enum.OrientationLeftUp { dl.EscrowReturn() } - case FourWay: + case enum.OrientationControlFourWay: // Accept all orientations. } } @@ -173,12 +175,12 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.RaiseEvents() } - if dl.Acceptor.deviceState == Escrow && dl.Acceptor.autoStack { + if dl.Acceptor.deviceState == enum.StateEscrow && dl.Acceptor.autoStack { dl.EscrowStack() dl.Acceptor.shouldRaiseEscrowEvent = false } - if dl.Acceptor.deviceState != Escrow && dl.Acceptor.deviceState != Stacking { + if dl.Acceptor.deviceState != enum.StateEscrow && dl.Acceptor.deviceState != enum.StateStacking { dl.Acceptor.wasDocTypeSetOnEscrow = false } } diff --git a/enum.go b/enum.go deleted file mode 100644 index ed28fad..0000000 --- a/enum.go +++ /dev/null @@ -1,144 +0,0 @@ -package mpost - -//////////////////////////////////// - -type BNFStatus byte - -const ( - Unknown BNFStatus = iota - OK - NotAttached - Error -) - -//// - -type DocumentType byte - -const ( - None DocumentType = iota - NoValue - Bill - Barcode - Coupon -) - -//// - -type Orientation byte - -const ( - RightUp Orientation = iota - RightDown - LeftUp - LeftDown - UnknownOrientation -) - -//// - -type OrientationControl byte - -const ( - FourWay OrientationControl = iota - TwoWay - OneWay -) - -//// - -type PowerUp byte - -const ( - A PowerUp = iota - B - C - E -) - -//// - -type PupExt byte - -const ( - Return PupExt = iota - OutOfService - StackNoCredit - Stack - WaitNoCredit - Wait -) - -//// - -type State byte - -const ( - Escrow State = iota - Stacked - Returned - Rejected - Stalled - Accepting - CalibrateStart - Calibrating - Connecting - Disconnected - Downloading - DownloadRestart - DownloadStart - Failed - Idling - PupEscrow - Returning - Stacking -) - -//// - -type Bezel byte - -const ( - Standard Bezel = iota - Platform - Diagnostic -) - -//// - -type Event byte - -type EventHandler func(*CAcceptor, int) - -const ( - Events_Begin Event = iota - ConnectedEvent - EscrowEvent - PUPEscrowEvent - StackedEvent - ReturnedEvent - RejectedEvent - CheatedEvent - StackerFullEvent - CalibrateStartEvent - CalibrateProgressEvent - CalibrateFinishEvent - DownloadStartEvent - DownloadRestartEvent - DownloadProgressEvent - DownloadFinishEvent - PauseDetectedEvent - PauseClearedEvent - StallDetectedEvent - StallClearedEvent - JamDetectedEvent - JamClearedEvent - PowerUpEvent - InvalidCommandEvent - CashBoxAttachedEvent - CashBoxRemovedEvent - DisconnectedEvent - Events_End -) - -//// diff --git a/new_log.go b/new_log.go index 8f24ec6..d2f92f4 100644 --- a/new_log.go +++ b/new_log.go @@ -2,6 +2,7 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/enum" "github.com/rs/zerolog" "strings" ) @@ -47,6 +48,6 @@ func (obj *LogStruct) Bytes(msg string, data []byte) { // -func (obj *LogStruct) Event(event Event) { +func (obj *LogStruct) Event(event enum.EventType) { obj.log.Debug().Str("event", "lool").Msg("Event") } diff --git a/timeouts.go b/timeouts.go new file mode 100644 index 0000000..f4ebc0b --- /dev/null +++ b/timeouts.go @@ -0,0 +1,11 @@ +package mpost + +import "time" + +//////////////////////////////////// + +const ( + CommunicationDisconnectTimeout = time.Second * 3 + PollingDisconnectTimeout = time.Second * 3 + CalibrateTimeout = time.Second * 3 +) diff --git a/verControl.go b/verControl.go deleted file mode 100644 index 47d3816..0000000 --- a/verControl.go +++ /dev/null @@ -1,12 +0,0 @@ -package mpost - -const ( - GlobalName string = "mpost" - GlobalDateUpdate string = "09-05-2024" - GlobalHash string = "0cda13b4e156817cc5581af66729849a35472491" - - GlobalVersion string = "v0.1.5" - GlobalVersionMajor string = "v0" - GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 5 -) From 99d450de8d21c2933e590b1b5c99000326f71a4d Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 14:01:10 +0200 Subject: [PATCH 04/29] refactoring [v0.1.7] work with the generator is completed --- _run/fileGenerator/const.go | 8 +++++++- _run/fileGenerator/enum.go | 12 +++++++++++- {generator => _run/fileGenerator/generator}/const.go | 0 .../fileGenerator/generator}/methods.go | 0 {generator => _run/fileGenerator/generator}/names.go | 0 {generator => _run/fileGenerator/generator}/saves.go | 0 .../fileGenerator/generator}/struct.go | 0 .../fileGenerator/generator}/values.go | 0 _run/fileGenerator/main.go | 8 +++++--- _run/values/ver.txt | 2 +- const.go | 6 +++--- 11 files changed, 27 insertions(+), 9 deletions(-) rename {generator => _run/fileGenerator/generator}/const.go (100%) rename {generator => _run/fileGenerator/generator}/methods.go (100%) rename {generator => _run/fileGenerator/generator}/names.go (100%) rename {generator => _run/fileGenerator/generator}/saves.go (100%) rename {generator => _run/fileGenerator/generator}/struct.go (100%) rename {generator => _run/fileGenerator/generator}/values.go (100%) diff --git a/_run/fileGenerator/const.go b/_run/fileGenerator/const.go index 972c30b..bbbac65 100644 --- a/_run/fileGenerator/const.go +++ b/_run/fileGenerator/const.go @@ -1,6 +1,9 @@ package main -import "github.com/hard-soft-ware/mpost/generator" +import ( + "fmt" + "github.com/hard-soft-ware/mpost/_run/fileGenerator/generator" +) //////////////////////////////////// @@ -20,6 +23,7 @@ func Cmd() { constGenerator(obj, val) obj.Save(constDir).End() + fmt.Printf("Add: %s %s\n", constDir, obj.Name.Get()) } func CmdAux() { @@ -48,6 +52,7 @@ func CmdAux() { constGenerator(obj, val) obj.Save(constDir).End() + fmt.Printf("Add: %s %s\n", constDir, obj.Name.Get()) } func Data() { @@ -60,4 +65,5 @@ func Data() { constGenerator(obj, val) obj.Save(constDir).End() + fmt.Printf("Add: %s %s\n", constDir, obj.Name.Get()) } diff --git a/_run/fileGenerator/enum.go b/_run/fileGenerator/enum.go index 27e3205..55b0a34 100644 --- a/_run/fileGenerator/enum.go +++ b/_run/fileGenerator/enum.go @@ -1,7 +1,8 @@ package main import ( - "github.com/hard-soft-ware/mpost/generator" + "fmt" + "github.com/hard-soft-ware/mpost/_run/fileGenerator/generator" ) //////////////////////////////////// @@ -22,6 +23,7 @@ func Status() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func Document() { @@ -37,6 +39,7 @@ func Document() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func Orientation() { @@ -52,6 +55,7 @@ func Orientation() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func OrientationControl() { @@ -65,6 +69,7 @@ func OrientationControl() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func PowerUp() { @@ -79,6 +84,7 @@ func PowerUp() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func PupExt() { @@ -95,6 +101,7 @@ func PupExt() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func State() { @@ -123,6 +130,7 @@ func State() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func Bezel() { @@ -136,6 +144,7 @@ func Bezel() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } func Event() { @@ -174,4 +183,5 @@ func Event() { enumGenerator(obj, val) obj.Save(enumDir).End() + fmt.Printf("Add: %s %s\n", enumDir, obj.Name.Get()) } diff --git a/generator/const.go b/_run/fileGenerator/generator/const.go similarity index 100% rename from generator/const.go rename to _run/fileGenerator/generator/const.go diff --git a/generator/methods.go b/_run/fileGenerator/generator/methods.go similarity index 100% rename from generator/methods.go rename to _run/fileGenerator/generator/methods.go diff --git a/generator/names.go b/_run/fileGenerator/generator/names.go similarity index 100% rename from generator/names.go rename to _run/fileGenerator/generator/names.go diff --git a/generator/saves.go b/_run/fileGenerator/generator/saves.go similarity index 100% rename from generator/saves.go rename to _run/fileGenerator/generator/saves.go diff --git a/generator/struct.go b/_run/fileGenerator/generator/struct.go similarity index 100% rename from generator/struct.go rename to _run/fileGenerator/generator/struct.go diff --git a/generator/values.go b/_run/fileGenerator/generator/values.go similarity index 100% rename from generator/values.go rename to _run/fileGenerator/generator/values.go diff --git a/_run/fileGenerator/main.go b/_run/fileGenerator/main.go index 11661a6..aba49dc 100644 --- a/_run/fileGenerator/main.go +++ b/_run/fileGenerator/main.go @@ -1,10 +1,12 @@ package main -import "github.com/hard-soft-ware/mpost/generator" +import ( + generator2 "github.com/hard-soft-ware/mpost/_run/fileGenerator/generator" +) //////////////////////////////////// -func enumGenerator(obj *generator.GeneratorObj, val []string) { +func enumGenerator(obj *generator2.GeneratorObj, val []string) { obj.Print("type ").Name.Type().PrintLN(" byte").LN() obj.PrintLN("const (") @@ -47,7 +49,7 @@ func enumGenerator(obj *generator.GeneratorObj, val []string) { } -func constGenerator(obj *generator.GeneratorObj, val *generator.GeneratorValueObj) { +func constGenerator(obj *generator2.GeneratorObj, val *generator2.GeneratorValueObj) { obj.Print("type ").Name.Type().PrintLN(" byte").LN() obj.PrintLN("const (") diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 82942c3..fad30c5 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.6 +v0.1.7 diff --git a/const.go b/const.go index d1946d2..18d09bd 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "25e995a594c21573ad4d64e89f822df22090cd0b" + GlobalHash string = "ac62904c321306765bd4926f1f9a7adca7045e53" - GlobalVersion string = "v0.1.6" + GlobalVersion string = "v0.1.7" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 6 + GlobalVersionPatch uint16 = 7 ) From 1abaa937ef49c00564cd48a66464944bcbfca10f Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 14:38:59 +0200 Subject: [PATCH 05/29] refactoring [v0.1.7] structuring variables --- acceptor.go | 105 ++++++---------------------- acceptor/acceptor.go | 5 ++ acceptor/cap.go | 35 ++++++++++ acceptor/cash.go | 13 ++++ acceptor/device.go | 21 ++++++ acceptor/enable.go | 13 ++++ acceptor/shouldRaise.go | 34 ++++++++++ acceptor_command.go | 13 ++-- acceptor_loop.go | 5 +- acceptor_methods.go | 7 +- acceptor_process.go | 147 ++++++++++++++++++++-------------------- acceptor_raise.go | 17 +++-- acceptor_serial.go | 31 +++++---- acceptor_thread.go | 13 ++-- bill.go | 3 +- bill_table.go | 3 +- data_escrow.go | 81 +++++++++++----------- data_process.go | 11 +-- data_send_replay.go | 15 ++-- 19 files changed, 322 insertions(+), 250 deletions(-) create mode 100644 acceptor/acceptor.go create mode 100644 acceptor/cap.go create mode 100644 acceptor/cash.go create mode 100644 acceptor/device.go create mode 100644 acceptor/enable.go create mode 100644 acceptor/shouldRaise.go diff --git a/acceptor.go b/acceptor.go index 75ccc12..a35cc9d 100644 --- a/acceptor.go +++ b/acceptor.go @@ -28,55 +28,13 @@ type CAcceptor struct { billValues []CBill billValueEnables []bool - bnfStatus enum.BNFStatusType - bootPN string - capApplicationID bool - capApplicationPN bool - capAssetNumber bool - capAudit bool - capBarCodes bool - capBarCodesExt bool - capBNFStatus bool - capBookmark bool - capBootPN bool - capCalibrate bool - capCashBoxTotal bool - capCouponExt bool - capDevicePaused bool - capDeviceSoftReset bool - capDeviceType bool - capDeviceResets bool - capDeviceSerialNumber bool - capEscrowTimeout bool - capFlashDownload bool - capNoPush bool - capOrientationExt bool - capPupExt bool - capTestDoc bool - capVariantID bool - capVariantPN bool - cashBoxAttached bool - cashBoxFull bool - cashBoxTotal int - connected bool - coupon *CCoupon - deviceFailure bool - deviceModel int - devicePaused bool - devicePortName string - devicePowerUp enum.PowerUpType - deviceResets int - deviceRevision int - deviceSerialNumber string - deviceStalled bool - deviceState enum.StateType - deviceType string - docType enum.DocumentType - enableAcceptance bool - enableBarCodes bool - enableBookmarks bool - enableCouponExt bool - enableNoPush bool + bnfStatus enum.BNFStatusType + bootPN string + + coupon *CCoupon + + docType enum.DocumentType + escrowOrientation enum.OrientationType highSecurity bool orientationCtl enum.OrientationControlType @@ -95,43 +53,18 @@ type CAcceptor struct { wasDocTypeSetOnEscrow bool wasDisconnected bool isVeryFirstPoll bool - shouldRaiseConnectedEvent bool - shouldRaiseEscrowEvent bool - shouldRaisePUPEscrowEvent bool - shouldRaiseStackedEvent bool - shouldRaiseReturnedEvent bool - shouldRaiseRejectedEvent bool - shouldRaiseCheatedEvent bool - shouldRaiseStackerFullEvent bool - shouldRaiseCalibrateStartEvent bool - shouldRaiseCalibrateProgressEvent bool - shouldRaiseCalibrateFinishEvent bool - shouldRaiseDownloadStartEvent bool - shouldRaiseDownloadRestartEvent bool - shouldRaiseDownloadProgressEvent bool - shouldRaiseDownloadFinishEvent bool - shouldRaisePauseDetectedEvent bool - shouldRaisePauseClearedEvent bool - shouldRaiseStallDetectedEvent bool - shouldRaiseStallClearedEvent bool - shouldRaiseJamDetectedEvent bool - shouldRaiseJamClearedEvent bool - shouldRaisePowerUpEvent bool - shouldRaiseInvalidCommandEvent bool - shouldRaiseCashBoxAttachedEvent bool - shouldRaiseCashBoxRemovedEvent bool - shouldRaiseDisconnectedEvent bool - compressLog bool - workerThread sync.WaitGroup - openThread chan bool - flashDownloadThread chan bool - dataLinkLayer *CDataLinkLayer - replyQueuedEvent int - notInProcessReplyEvent int - stopWorkerThread bool - stopOpenThread bool - stopFlashDownloadThread bool - suppressStandardPoll bool + + compressLog bool + workerThread sync.WaitGroup + openThread chan bool + flashDownloadThread chan bool + dataLinkLayer *CDataLinkLayer + replyQueuedEvent int + notInProcessReplyEvent int + stopWorkerThread bool + stopOpenThread bool + stopFlashDownloadThread bool + suppressStandardPoll bool messageQueue chan *CMessage replyQueue chan []byte diff --git a/acceptor/acceptor.go b/acceptor/acceptor.go new file mode 100644 index 0000000..72c1fe5 --- /dev/null +++ b/acceptor/acceptor.go @@ -0,0 +1,5 @@ +package acceptor + +//////////////////////////////////// + +var Connected bool diff --git a/acceptor/cap.go b/acceptor/cap.go new file mode 100644 index 0000000..8c9a60e --- /dev/null +++ b/acceptor/cap.go @@ -0,0 +1,35 @@ +package acceptor + +//////////////////////////////////// + +type CapStruct struct { + ApplicationID bool + ApplicationPN bool + AssetNumber bool + Audit bool + BarCodes bool + BarCodesExt bool + BNFStatus bool + Bookmark bool + BootPN bool + Calibrate bool + CashBoxTotal bool + CouponExt bool + DevicePaused bool + DeviceSoftReset bool + DeviceType bool + DeviceResets bool + DeviceSerialNumber bool + EscrowTimeout bool + FlashDownload bool + NoPush bool + OrientationExt bool + PupExt bool + TestDoc bool + VariantID bool + VariantPN bool +} + +var Cap CapStruct + +//// diff --git a/acceptor/cash.go b/acceptor/cash.go new file mode 100644 index 0000000..0f07f27 --- /dev/null +++ b/acceptor/cash.go @@ -0,0 +1,13 @@ +package acceptor + +//////////////////////////////////// + +type CashStruct struct { + BoxAttached bool + BoxFull bool + BoxTotal int +} + +var Cash CashStruct + +//// diff --git a/acceptor/device.go b/acceptor/device.go new file mode 100644 index 0000000..36eb477 --- /dev/null +++ b/acceptor/device.go @@ -0,0 +1,21 @@ +package acceptor + +import "github.com/hard-soft-ware/mpost/enum" + +//////////////////////////////////// + +type DeviceStruct struct { + Failure bool + Model int + Paused bool + PortName string + PowerUp enum.PowerUpType + Resets int + Revision int + SerialNumber string + Stalled bool + State enum.StateType + Type string +} + +var Device DeviceStruct diff --git a/acceptor/enable.go b/acceptor/enable.go new file mode 100644 index 0000000..2ee6d59 --- /dev/null +++ b/acceptor/enable.go @@ -0,0 +1,13 @@ +package acceptor + +//////////////////////////////////// + +type EnableStruct struct { + Acceptance bool + BarCodes bool + Bookmarks bool + CouponExt bool + NoPush bool +} + +var Enable EnableStruct diff --git a/acceptor/shouldRaise.go b/acceptor/shouldRaise.go new file mode 100644 index 0000000..86bcdc3 --- /dev/null +++ b/acceptor/shouldRaise.go @@ -0,0 +1,34 @@ +package acceptor + +//////////////////////////////////// + +type ShouldRaiseStruct struct { + ConnectedEvent bool + EscrowEvent bool + PUPEscrowEvent bool + StackedEvent bool + ReturnedEvent bool + RejectedEvent bool + CheatedEvent bool + StackerFullEvent bool + CalibrateStartEvent bool + CalibrateProgressEvent bool + CalibrateFinishEvent bool + DownloadStartEvent bool + DownloadRestartEvent bool + DownloadProgressEvent bool + DownloadFinishEvent bool + PauseDetectedEvent bool + PauseClearedEvent bool + StallDetectedEvent bool + StallClearedEvent bool + JamDetectedEvent bool + JamClearedEvent bool + PowerUpEvent bool + InvalidCommandEvent bool + CashBoxAttachedEvent bool + CashBoxRemovedEvent bool + DisconnectedEvent bool +} + +var ShouldRaise ShouldRaiseStruct diff --git a/acceptor_command.go b/acceptor_command.go index 416e40d..03d20cc 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -2,6 +2,7 @@ package mpost import ( "errors" + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/enum" "time" ) @@ -11,13 +12,13 @@ import ( func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, data0Index int) { payload[0] = controlCode - if a.enableBookmarks && a.enableAcceptance && a.deviceState != enum.StateCalibrating { + if acceptor.Enable.Bookmarks && acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { payload[0] |= 0x20 } data0 := byte(0) - if a.enableAcceptance && a.deviceState != enum.StateCalibrating { + if acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { if a.expandedNoteReporting { data0 |= 0x7F } else { @@ -50,15 +51,15 @@ func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, da data2 := byte(0) - if a.enableNoPush { + if acceptor.Enable.NoPush { data2 |= 0x01 } - if a.enableBarCodes && a.enableAcceptance && a.deviceState != enum.StateCalibrating { + if acceptor.Enable.BarCodes && acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { data2 |= 0x02 } - switch a.devicePowerUp { + switch acceptor.Device.PowerUp { case enum.PowerUpB: data2 |= 0x04 case enum.PowerUpC: @@ -69,7 +70,7 @@ func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, da data2 |= 0x10 } - if a.enableCouponExt && a.capCouponExt { + if acceptor.Enable.CouponExt && acceptor.Cap.CouponExt { data2 |= 0x20 } diff --git a/acceptor_loop.go b/acceptor_loop.go index 645ef0f..21cd7ca 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "time" @@ -20,7 +21,7 @@ func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { } if time.Since(startTickCount) > PollingDisconnectTimeout { - if a.shouldRaiseDisconnectedEvent { + if acceptor.ShouldRaise.DisconnectedEvent { a.RaiseDisconnectedEvent() } startTickCount = time.Now() @@ -30,7 +31,7 @@ func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { if a.stopFlashDownloadThread { a.stopFlashDownloadThread = true <-a.flashDownloadThread - a.deviceState = enum.StateIdling + acceptor.Device.State = enum.StateIdling a.wasStopped = true return nil } diff --git a/acceptor_methods.go b/acceptor_methods.go index 8e629a6..7a454a5 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -2,13 +2,14 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" ) // ////////////////////////////////// func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName string) error { - if !a.connected { + if !acceptor.Connected { return fmt.Errorf("Calling %s not allowed when not connected.", propertyName) } @@ -16,7 +17,7 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st return fmt.Errorf("Device does not support %s.", propertyName) } - switch a.deviceState { + switch acceptor.Device.State { case enum.StateDownloadStart, enum.StateDownloading: return fmt.Errorf("Calling %s not allowed during flash download.", propertyName) case enum.StateCalibrateStart, enum.StateCalibrating: @@ -27,7 +28,7 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st } func (a *CAcceptor) GetDeviceSerialNumber() string { - err := a.verifyPropertyIsAllowed(a.capDeviceSerialNumber, "DeviceSerialNumber") + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") if err != nil { a.log.Debug().Err(err).Msg("GetDeviceSerialNumber") return "" diff --git a/acceptor_process.go b/acceptor_process.go index e39fa39..38e5a7a 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -1,50 +1,53 @@ package mpost -import "github.com/hard-soft-ware/mpost/enum" +import ( + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/enum" +) //////////////////////////////////// func (a *CAcceptor) processData0(data0 byte) { if (data0 & 0x01) != 0 { - if a.deviceState != enum.StateCalibrating && a.deviceState != enum.StateCalibrateStart { - a.deviceState = enum.StateIdling + if acceptor.Device.State != enum.StateCalibrating && acceptor.Device.State != enum.StateCalibrateStart { + acceptor.Device.State = enum.StateIdling } } if (data0 & 0x02) != 0 { - if a.deviceState != enum.StateCalibrating && a.deviceState != enum.StateCalibrateStart { - a.deviceState = enum.StateAccepting + if acceptor.Device.State != enum.StateCalibrating && acceptor.Device.State != enum.StateCalibrateStart { + acceptor.Device.State = enum.StateAccepting } } if (data0 & 0x04) != 0 { - a.deviceState = enum.StateEscrow + acceptor.Device.State = enum.StateEscrow if a.autoStack { - a.shouldRaiseEscrowEvent = false + acceptor.ShouldRaise.EscrowEvent = false } } else { - a.shouldRaiseEscrowEvent = true + acceptor.ShouldRaise.EscrowEvent = true } if (data0 & 0x08) != 0 { - a.deviceState = enum.StateStacking + acceptor.Device.State = enum.StateStacking } if (data0 & 0x10) != 0 { - a.deviceState = enum.StateStacked + acceptor.Device.State = enum.StateStacked } else { - a.shouldRaiseStackedEvent = true + acceptor.ShouldRaise.StackedEvent = true } if (data0 & 0x20) != 0 { - a.deviceState = enum.StateReturning + acceptor.Device.State = enum.StateReturning } if (data0 & 0x40) != 0 { - a.deviceState = enum.StateReturned + acceptor.Device.State = enum.StateReturned a.bill = CBill{} // Resetting the bill } else { - a.shouldRaiseReturnedEvent = true + acceptor.ShouldRaise.ReturnedEvent = true } } @@ -53,54 +56,54 @@ func (a *CAcceptor) processData1(data1 byte) { a.isCheated = true } else { a.isCheated = false - a.shouldRaiseCheatedEvent = true + acceptor.ShouldRaise.CheatedEvent = true } if (data1 & 0x02) != 0 { - a.deviceState = enum.StateRejected + acceptor.Device.State = enum.StateRejected } else { - a.shouldRaiseRejectedEvent = true + acceptor.ShouldRaise.RejectedEvent = true } if (data1 & 0x04) != 0 { a.isDeviceJammed = true - a.shouldRaiseJamDetectedEvent = true + acceptor.ShouldRaise.JamDetectedEvent = true } else { a.isDeviceJammed = false - a.shouldRaiseJamClearedEvent = true + acceptor.ShouldRaise.JamClearedEvent = true } if (data1 & 0x08) != 0 { - a.cashBoxFull = true + acceptor.Cash.BoxFull = true } else { - a.cashBoxFull = false - a.shouldRaiseStackerFullEvent = true + acceptor.Cash.BoxFull = false + acceptor.ShouldRaise.StackerFullEvent = true } - a.cashBoxAttached = (data1 & 0x10) != 0 + acceptor.Cash.BoxAttached = (data1 & 0x10) != 0 - if !a.cashBoxAttached { + if !acceptor.Cash.BoxAttached { // Assume a DocumentType exists that handles this // _docType = NoValue } if (data1 & 0x20) != 0 { - a.devicePaused = true - a.shouldRaisePauseClearedEvent = true + acceptor.Device.Paused = true + acceptor.ShouldRaise.PauseClearedEvent = true } else { - a.devicePaused = false - a.shouldRaisePauseDetectedEvent = true + acceptor.Device.Paused = false + acceptor.ShouldRaise.PauseDetectedEvent = true } if (data1 & 0x40) != 0 { - a.deviceState = enum.StateCalibrating - if a.shouldRaiseCalibrateProgressEvent { + acceptor.Device.State = enum.StateCalibrating + if acceptor.ShouldRaise.CalibrateProgressEvent { a.RaiseCalibrateProgressEvent() } } else { - if a.deviceState == enum.StateCalibrating { - a.shouldRaiseCalibrateFinishEvent = true - a.deviceState = enum.StateIdling + if acceptor.Device.State == enum.StateCalibrating { + acceptor.ShouldRaise.CalibrateFinishEvent = true + acceptor.Device.State = enum.StateIdling } } } @@ -109,24 +112,24 @@ func (a *CAcceptor) processData2(data2 byte) { if !a.expandedNoteReporting { billTypeIndex := (data2 & 0x38) >> 3 if billTypeIndex > 0 { - if a.deviceState == enum.StateEscrow || (a.deviceState == enum.StateStacked && !a.wasDocTypeSetOnEscrow) { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !a.wasDocTypeSetOnEscrow) { a.bill = a.billTypes[billTypeIndex-1] a.docType = enum.DocumentBill - a.wasDocTypeSetOnEscrow = a.deviceState == enum.StateEscrow + a.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow } } else { - if a.deviceState == enum.StateStacked || a.deviceState == enum.StateEscrow { + if acceptor.Device.State == enum.StateStacked || acceptor.Device.State == enum.StateEscrow { a.bill = CBill{} a.docType = enum.DocumentNoValue a.wasDocTypeSetOnEscrow = false } } } else { - if a.deviceState == enum.StateStacked { + if acceptor.Device.State == enum.StateStacked { if a.docType == enum.DocumentBill && a.bill.Value == 0.0 { a.docType = enum.DocumentNoValue } - } else if a.deviceState == enum.StateEscrow { + } else if acceptor.Device.State == enum.StateEscrow { a.bill = CBill{} a.docType = enum.DocumentNoValue } @@ -136,7 +139,7 @@ func (a *CAcceptor) processData2(data2 byte) { a.isPoweredUp = true a.docType = enum.DocumentNoValue } else { - a.shouldRaisePowerUpEvent = true + acceptor.ShouldRaise.PowerUpEvent = true if !a.isVeryFirstPoll { a.isPoweredUp = false } @@ -146,28 +149,28 @@ func (a *CAcceptor) processData2(data2 byte) { a.isInvalidCommand = true } else { a.isInvalidCommand = false - a.shouldRaiseInvalidCommandEvent = true + acceptor.ShouldRaise.InvalidCommandEvent = true } if (data2 & 0x04) != 0 { - a.deviceState = enum.StateFailed + acceptor.Device.State = enum.StateFailed } } func (a *CAcceptor) processData3(data3 byte) { if (data3 & 0x01) != 0 { - a.deviceState = enum.StateStalled - a.shouldRaiseStallClearedEvent = true + acceptor.Device.State = enum.StateStalled + acceptor.ShouldRaise.StallClearedEvent = true } else { - a.shouldRaiseStallDetectedEvent = true + acceptor.ShouldRaise.StallDetectedEvent = true } if (data3 & 0x02) != 0 { - a.deviceState = enum.StateDownloadRestart + acceptor.Device.State = enum.StateDownloadRestart } if (data3 & 0x08) != 0 { - a.capBarCodesExt = true + acceptor.Cap.BarCodesExt = true } if (data3 & 0x10) != 0 { @@ -176,40 +179,40 @@ func (a *CAcceptor) processData3(data3 byte) { } func (a *CAcceptor) processData4(data4 byte) { - a.deviceModel = int(data4 & 0x7F) //todo проверить валидность перевода - m := a.deviceModel + acceptor.Device.Model = int(data4 & 0x7F) //todo проверить валидность перевода + m := acceptor.Device.Model d := m - a.capApplicationPN = m == 'T' || m == 'U' - a.capAssetNumber = m == 'T' || m == 'U' - a.capAudit = m == 'T' || m == 'U' - a.capBarCodes = m == 'T' || m == 'U' || d == 15 || d == 23 - a.capBookmark = true - a.capBootPN = m == 'T' || m == 'U' - a.capCalibrate = true - a.capCashBoxTotal = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'W' || m == 'X' - a.capCouponExt = m == 'P' || m == 'X' - a.capDevicePaused = m == 'P' || m == 'X' || d == 31 - a.capDeviceSoftReset = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'T' || m == 'U' || m == 'W' || m == 'X' || d == 31 - a.capDeviceType = m == 'T' || m == 'U' - a.capDeviceResets = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'T' || m == 'U' || m == 'W' || m == 'X' - a.capDeviceSerialNumber = m == 'T' || m == 'U' - a.capFlashDownload = true - a.capEscrowTimeout = m == 'T' || m == 'U' - a.capNoPush = m == 'P' || m == 'X' || d == 31 || d == 23 - a.capVariantPN = m == 'T' || m == 'U' + acceptor.Cap.ApplicationPN = m == 'T' || m == 'U' + acceptor.Cap.AssetNumber = m == 'T' || m == 'U' + acceptor.Cap.Audit = m == 'T' || m == 'U' + acceptor.Cap.BarCodes = m == 'T' || m == 'U' || d == 15 || d == 23 + acceptor.Cap.Bookmark = true + acceptor.Cap.BootPN = m == 'T' || m == 'U' + acceptor.Cap.Calibrate = true + acceptor.Cap.CashBoxTotal = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'W' || m == 'X' + acceptor.Cap.CouponExt = m == 'P' || m == 'X' + acceptor.Cap.DevicePaused = m == 'P' || m == 'X' || d == 31 + acceptor.Cap.DeviceSoftReset = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'T' || m == 'U' || m == 'W' || m == 'X' || d == 31 + acceptor.Cap.DeviceType = m == 'T' || m == 'U' + acceptor.Cap.DeviceResets = m == 'A' || m == 'B' || m == 'C' || m == 'D' || m == 'G' || m == 'M' || m == 'P' || m == 'T' || m == 'U' || m == 'W' || m == 'X' + acceptor.Cap.DeviceSerialNumber = m == 'T' || m == 'U' + acceptor.Cap.FlashDownload = true + acceptor.Cap.EscrowTimeout = m == 'T' || m == 'U' + acceptor.Cap.NoPush = m == 'P' || m == 'X' || d == 31 || d == 23 + acceptor.Cap.VariantPN = m == 'T' || m == 'U' a.expandedNoteReporting = m == 'T' || m == 'U' // This setting might be toggled in debug or production builds } func (a *CAcceptor) processData5(data5 byte) { switch { - case a.deviceModel < 23, // S1K - a.deviceModel == 30 || a.deviceModel == 31, // S3K - a.deviceModel == 74, // CFMC - a.deviceModel == 84 || a.deviceModel == 85: // CFSC - a.deviceRevision = int(data5 & 0x7F) + case acceptor.Device.Model < 23, // S1K + acceptor.Device.Model == 30 || acceptor.Device.Model == 31, // S3K + acceptor.Device.Model == 74, // CFMC + acceptor.Device.Model == 84 || acceptor.Device.Model == 85: // CFSC + acceptor.Device.Revision = int(data5 & 0x7F) default: // S2K - a.deviceRevision = int(data5&0x0F) + (int(data5&0x70)>>4)*10 + acceptor.Device.Revision = int(data5&0x0F) + (int(data5&0x70)>>4)*10 } } diff --git a/acceptor_raise.go b/acceptor_raise.go index 43477b0..75b0111 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -1,6 +1,9 @@ package mpost -import "github.com/hard-soft-ware/mpost/enum" +import ( + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/enum" +) //////////////////////////////////// @@ -15,25 +18,25 @@ func (a *CAcceptor) raiseXX(e enum.EventType) { func (a *CAcceptor) RaiseConnectedEvent() { a.log.Debug().Str("Event", "ConnectedEvent").Msg("Raise") a.raiseXX(enum.EventConnected) - a.shouldRaiseDisconnectedEvent = true - a.shouldRaiseConnectedEvent = false + acceptor.ShouldRaise.DisconnectedEvent = true + acceptor.ShouldRaise.ConnectedEvent = false } func (a *CAcceptor) RaiseDisconnectedEvent() { a.log.Debug().Str("Event", "DisconnectedEvent").Msg("Raise") a.raiseXX(enum.EventDisconnected) - a.shouldRaiseDisconnectedEvent = false - a.shouldRaiseConnectedEvent = true + acceptor.ShouldRaise.DisconnectedEvent = false + acceptor.ShouldRaise.ConnectedEvent = true } func (a *CAcceptor) RaiseDownloadRestartEvent() { a.log.Debug().Str("Event", "DownloadRestartEvent").Msg("Raise") a.raiseXX(enum.EventDownloadRestart) - a.shouldRaiseDownloadRestartEvent = false + acceptor.ShouldRaise.DownloadRestartEvent = false } func (a *CAcceptor) RaiseCalibrateProgressEvent() { a.log.Debug().Str("Event", "CalibrateProgressEvent").Msg("Raise") a.raiseXX(enum.EventCalibrateProgress) - a.shouldRaiseCalibrateProgressEvent = false + acceptor.ShouldRaise.CalibrateProgressEvent = false } diff --git a/acceptor_serial.go b/acceptor_serial.go index 081d896..d46cf2b 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -2,6 +2,7 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "go.bug.st/serial" @@ -25,13 +26,13 @@ func byteSliceToString(b []byte) string { func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { lg := a.log.NewLog("OpenSerial") - if a.connected { + if acceptor.Connected { lg.Debug().Msg("already connected") return nil } - a.devicePortName = portName - a.devicePowerUp = powerUp + acceptor.Device.PortName = portName + acceptor.Device.PowerUp = powerUp err := a.OpenPort(lg) if err != nil { @@ -53,7 +54,7 @@ func (a *CAcceptor) OpenPort(lg *LogGlobalStruct) error { StopBits: serial.OneStopBit, } - port, err := serial.Open(a.devicePortName, mode) + port, err := serial.Open(acceptor.Device.PortName, mode) if err != nil { lg.Debug().Err(err).Msg("failed to open serial port") return err @@ -73,20 +74,20 @@ func (a *CAcceptor) OpenPort(lg *LogGlobalStruct) error { port.ResetInputBuffer() a.port = port - a.connected = true + acceptor.Connected = true lg.Debug().Msg("Connected") return nil } func (a *CAcceptor) Close() { - if !a.connected { + if !acceptor.Connected { a.stopOpenThread = true return } - if a.enableAcceptance { - a.enableAcceptance = false + if acceptor.Enable.Acceptance { + acceptor.Enable.Acceptance = false } if a.dataLinkLayer != nil { @@ -98,7 +99,7 @@ func (a *CAcceptor) Close() { a.port.Close() a.port = nil - a.connected = false + acceptor.Connected = false } //// @@ -117,19 +118,19 @@ func (a *CAcceptor) QueryDeviceCapabilities(lg *LogGlobalStruct) { } if reply[3]&0x01 != 0 { - a.capPupExt = true + acceptor.Cap.PupExt = true } if reply[3]&0x02 != 0 { - a.capOrientationExt = true + acceptor.Cap.OrientationExt = true } if reply[3]&0x04 != 0 { - a.capApplicationID = true - a.capVariantID = true + acceptor.Cap.ApplicationID = true + acceptor.Cap.VariantID = true } if reply[3]&0x08 != 0 { - a.capBNFStatus = true + acceptor.Cap.BNFStatus = true } if reply[3]&0x10 != 0 { - a.capTestDoc = true + acceptor.Cap.TestDoc = true } } diff --git a/acceptor_thread.go b/acceptor_thread.go index 8833897..f5d5938 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/enum" "time" ) @@ -20,10 +21,10 @@ func (a *CAcceptor) OpenThread() { a.dataLinkLayer.ProcessReply(replay) a.QueryDeviceCapabilities(lg) - if a.deviceState != enum.StateDownloadRestart { + if acceptor.Device.State != enum.StateDownloadRestart { a.SetUpBillTable() - a.connected = true - if a.shouldRaiseConnectedEvent { + acceptor.Connected = true + if acceptor.ShouldRaise.ConnectedEvent { a.RaiseConnectedEvent() } } else { @@ -47,9 +48,9 @@ func (a *CAcceptor) MessageLoopThread() { } if time.Since(timeoutStart) > 30*time.Second { - if a.deviceState != enum.StateDownloading && a.deviceState != enum.StateDownloadRestart { - a.connected = false - if a.shouldRaiseDisconnectedEvent { + if acceptor.Device.State != enum.StateDownloading && acceptor.Device.State != enum.StateDownloadRestart { + acceptor.Connected = false + if acceptor.ShouldRaise.DisconnectedEvent { a.RaiseDisconnectedEvent() } a.wasDisconnected = true diff --git a/bill.go b/bill.go index 178c3fc..bfc03b3 100644 --- a/bill.go +++ b/bill.go @@ -2,6 +2,7 @@ package mpost import ( "fmt" + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/enum" "strconv" "strings" @@ -54,7 +55,7 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) CBill { bill.Value = billValue a.docType = enum.DocumentBill - a.wasDocTypeSetOnEscrow = a.deviceState == enum.StateEscrow + a.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow orientation := reply[extDataIndex+10] switch orientation { diff --git a/bill_table.go b/bill_table.go index 6429e96..11e50f1 100644 --- a/bill_table.go +++ b/bill_table.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "time" ) @@ -77,7 +78,7 @@ func (a *CAcceptor) RetrieveBillTable() { func (a *CAcceptor) BuildHardCodedBillTable() { a.billTypes = []CBill{} - switch a.deviceModel { + switch acceptor.Device.Model { case 1, 12, 23, 30, 31, 'J', 'X', 'T': a.billTypes = append(a.billTypes, CBill{"USD", 1, '*', '*', '*', '*'}) a.billTypes = append(a.billTypes, CBill{"USD", 2, '*', '*', '*', '*'}) diff --git a/data_escrow.go b/data_escrow.go index 14d1c88..8eadc83 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" ) @@ -8,7 +9,7 @@ import ( //////////////////////////////////// func (dl *CDataLinkLayer) escrowXX(b byte) { - if !dl.Acceptor.connected { + if !acceptor.Connected { dl.log.Debug().Msg("serial not connected") return } @@ -32,9 +33,9 @@ func (dl *CDataLinkLayer) EscrowStack() { //// func (dl *CDataLinkLayer) RaiseEvents() { - if dl.Acceptor.isPoweredUp && dl.Acceptor.shouldRaisePowerUpEvent { + if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PowerUpEvent { dl.log.Debug().Msg("Power Up Event Raised") - dl.Acceptor.shouldRaisePowerUpEvent = false + acceptor.ShouldRaise.PowerUpEvent = false } if dl.Acceptor.isVeryFirstPoll { @@ -42,91 +43,91 @@ func (dl *CDataLinkLayer) RaiseEvents() { return } - switch dl.Acceptor.deviceState { + switch acceptor.Device.State { case enum.StateEscrow: - if dl.Acceptor.isPoweredUp && dl.Acceptor.shouldRaisePUPEscrowEvent { + if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PUPEscrowEvent { dl.log.Debug().Msg("PUP Escrow Event Raised") - dl.Acceptor.shouldRaisePUPEscrowEvent = false - } else if dl.Acceptor.shouldRaiseEscrowEvent { + acceptor.ShouldRaise.PUPEscrowEvent = false + } else if acceptor.ShouldRaise.EscrowEvent { dl.log.Debug().Msg("Escrow Event Raised") - dl.Acceptor.shouldRaiseEscrowEvent = false + acceptor.ShouldRaise.EscrowEvent = false } case enum.StateStacked: - if dl.Acceptor.shouldRaiseStackedEvent { + if acceptor.ShouldRaise.StackedEvent { dl.log.Debug().Msg("Stacked Event Raised") - dl.Acceptor.shouldRaiseStackedEvent = false + acceptor.ShouldRaise.StackedEvent = false } case enum.StateReturned: - if dl.Acceptor.shouldRaiseReturnedEvent { + if acceptor.ShouldRaise.ReturnedEvent { dl.log.Debug().Msg("Returned Event Raised") - dl.Acceptor.shouldRaiseReturnedEvent = false + acceptor.ShouldRaise.ReturnedEvent = false } case enum.StateRejected: - if dl.Acceptor.shouldRaiseRejectedEvent { + if acceptor.ShouldRaise.RejectedEvent { dl.log.Debug().Msg("Rejected Event Raised") - dl.Acceptor.shouldRaiseRejectedEvent = false + acceptor.ShouldRaise.RejectedEvent = false } case enum.StateStalled: - if dl.Acceptor.shouldRaiseStallDetectedEvent { + if acceptor.ShouldRaise.StallDetectedEvent { dl.log.Debug().Msg("Stall Detected Event Raised") - dl.Acceptor.shouldRaiseStallDetectedEvent = false + acceptor.ShouldRaise.StallDetectedEvent = false } } - if dl.Acceptor.deviceState != enum.StateStalled && dl.Acceptor.shouldRaiseStallClearedEvent { + if acceptor.Device.State != enum.StateStalled && acceptor.ShouldRaise.StallClearedEvent { dl.log.Debug().Msg("Stall Cleared Event Raised") - dl.Acceptor.shouldRaiseStallClearedEvent = false + acceptor.ShouldRaise.StallClearedEvent = false } - if dl.Acceptor.cashBoxFull && dl.Acceptor.shouldRaiseStackerFullEvent { + if acceptor.Cash.BoxFull && acceptor.ShouldRaise.StackerFullEvent { dl.log.Debug().Msg("Stacker Full Event Raised") - dl.Acceptor.shouldRaiseStackerFullEvent = false + acceptor.ShouldRaise.StackerFullEvent = false } - if dl.Acceptor.isCheated && dl.Acceptor.shouldRaiseCheatedEvent { + if dl.Acceptor.isCheated && acceptor.ShouldRaise.CheatedEvent { dl.log.Debug().Msg("Cheated Event Raised") - dl.Acceptor.shouldRaiseCheatedEvent = false + acceptor.ShouldRaise.CheatedEvent = false } - if dl.Acceptor.cashBoxAttached && dl.Acceptor.shouldRaiseCashBoxAttachedEvent { + if acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxAttachedEvent { dl.log.Debug().Msg("Cash Box Attached Event Raised") - dl.Acceptor.shouldRaiseCashBoxAttachedEvent = false - dl.Acceptor.shouldRaiseCashBoxRemovedEvent = true + acceptor.ShouldRaise.CashBoxAttachedEvent = false + acceptor.ShouldRaise.CashBoxRemovedEvent = true } - if !dl.Acceptor.cashBoxAttached && dl.Acceptor.shouldRaiseCashBoxRemovedEvent { + if !acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxRemovedEvent { dl.log.Debug().Msg("Cash Box Removed Event Raised") - dl.Acceptor.shouldRaiseCashBoxRemovedEvent = false - dl.Acceptor.shouldRaiseCashBoxAttachedEvent = true + acceptor.ShouldRaise.CashBoxRemovedEvent = false + acceptor.ShouldRaise.CashBoxAttachedEvent = true } - if dl.Acceptor.devicePaused && dl.Acceptor.shouldRaisePauseDetectedEvent { + if acceptor.Device.Paused && acceptor.ShouldRaise.PauseDetectedEvent { dl.log.Debug().Msg("Pause Detected Event Raised") - dl.Acceptor.shouldRaisePauseDetectedEvent = false + acceptor.ShouldRaise.PauseDetectedEvent = false } - if !dl.Acceptor.devicePaused && dl.Acceptor.shouldRaisePauseClearedEvent { + if !acceptor.Device.Paused && acceptor.ShouldRaise.PauseClearedEvent { dl.log.Debug().Msg("Pause Cleared Event Raised") - dl.Acceptor.shouldRaisePauseClearedEvent = false + acceptor.ShouldRaise.PauseClearedEvent = false } - if dl.Acceptor.isDeviceJammed && dl.Acceptor.shouldRaiseJamDetectedEvent { + if dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamDetectedEvent { dl.log.Debug().Msg("Jam Detected Event Raised") - dl.Acceptor.shouldRaiseJamDetectedEvent = false + acceptor.ShouldRaise.JamDetectedEvent = false } - if !dl.Acceptor.isDeviceJammed && dl.Acceptor.shouldRaiseJamClearedEvent { + if !dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamClearedEvent { dl.log.Debug().Msg("Jam Cleared Event Raised") - dl.Acceptor.shouldRaiseJamClearedEvent = false + acceptor.ShouldRaise.JamClearedEvent = false } - if dl.Acceptor.isInvalidCommand && dl.Acceptor.shouldRaiseInvalidCommandEvent { + if dl.Acceptor.isInvalidCommand && acceptor.ShouldRaise.InvalidCommandEvent { dl.log.Debug().Msg("Invalid Command Event Raised") - dl.Acceptor.shouldRaiseInvalidCommandEvent = false + acceptor.ShouldRaise.InvalidCommandEvent = false } - if dl.Acceptor.shouldRaiseCalibrateFinishEvent { + if acceptor.ShouldRaise.CalibrateFinishEvent { dl.log.Debug().Msg("Calibrate Finish Event Raised") - dl.Acceptor.shouldRaiseCalibrateFinishEvent = false + acceptor.ShouldRaise.CalibrateFinishEvent = false } } diff --git a/data_process.go b/data_process.go index ff50ae6..1136295 100644 --- a/data_process.go +++ b/data_process.go @@ -1,6 +1,9 @@ package mpost -import "github.com/hard-soft-ware/mpost/enum" +import ( + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/enum" +) //////////////////////////////////// @@ -31,7 +34,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusBarCodeReply(reply []byte) { dl.Acceptor.processData3(reply[7]) dl.Acceptor.processData5(reply[9]) - if dl.Acceptor.deviceState == enum.StateEscrow { + if acceptor.Device.State == enum.StateEscrow { dl.Acceptor.barCode = "" for i := 10; i < 38; i++ { if reply[i] != '(' { @@ -69,7 +72,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.processData3(reply[7]) dl.Acceptor.processData5(reply[9]) - if dl.Acceptor.deviceState == enum.StateEscrow || (dl.Acceptor.deviceState == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { couponData := ((int(reply[10]) & 0x0F) << 12) + ((int(reply[11]) & 0x0F) << 8) + ((int(reply[12]) & 0x0F) << 4) + @@ -85,6 +88,6 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.coupon = NewCCoupon(ownerID, value) dl.Acceptor.docType = enum.DocumentCoupon - dl.Acceptor.wasDocTypeSetOnEscrow = dl.Acceptor.deviceState == enum.StateEscrow + dl.Acceptor.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow } } diff --git a/data_send_replay.go b/data_send_replay.go index 64b1275..cd282cc 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -3,6 +3,7 @@ package mpost import ( "bufio" "errors" + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "io" @@ -58,7 +59,7 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { reply := []byte{} timeout := dl.Acceptor.transactionTimeout - if dl.Acceptor.deviceState == enum.StateDownloadStart || dl.Acceptor.deviceState == enum.StateDownloading { + if acceptor.Device.State == enum.StateDownloadStart || acceptor.Device.State == enum.StateDownloading { timeout = dl.Acceptor.downloadTimeout } @@ -143,7 +144,7 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { } if (ctl & 0x70) == 0x50 { - dl.Acceptor.deviceState = enum.StateDownloadRestart + acceptor.Device.State = enum.StateDownloadRestart } if (ctl & 0x70) == 0x70 { @@ -153,8 +154,8 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.ProcessExtendedOmnibusBarCodeReply(reply) case 0x02: dl.ProcessExtendedOmnibusExpandedNoteReply(reply) - if dl.Acceptor.deviceState == enum.StateEscrow || (dl.Acceptor.deviceState == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { - if dl.Acceptor.capOrientationExt { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if acceptor.Cap.OrientationExt { switch dl.Acceptor.orientationCtlExt { case enum.OrientationControlOneWay: if dl.Acceptor.escrowOrientation != enum.OrientationRightUp { @@ -175,12 +176,12 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.RaiseEvents() } - if dl.Acceptor.deviceState == enum.StateEscrow && dl.Acceptor.autoStack { + if acceptor.Device.State == enum.StateEscrow && dl.Acceptor.autoStack { dl.EscrowStack() - dl.Acceptor.shouldRaiseEscrowEvent = false + acceptor.ShouldRaise.EscrowEvent = false } - if dl.Acceptor.deviceState != enum.StateEscrow && dl.Acceptor.deviceState != enum.StateStacking { + if acceptor.Device.State != enum.StateEscrow && acceptor.Device.State != enum.StateStacking { dl.Acceptor.wasDocTypeSetOnEscrow = false } } From 8a3e74ffe7f5c214ec451e5d4f9e3de30207c4db Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 14:54:08 +0200 Subject: [PATCH 06/29] refactoring [v0.1.7] moved ConstructOmnibusCommand --- acceptor.go | 5 --- acceptor/acceptor.go | 85 ++++++++++++++++++++++++++++++++++++++++++++ acceptor_command.go | 74 -------------------------------------- bill.go | 8 ++--- bill_table.go | 2 +- data_escrow.go | 2 +- data_send_replay.go | 6 ++-- 7 files changed, 94 insertions(+), 88 deletions(-) diff --git a/acceptor.go b/acceptor.go index a35cc9d..e32f312 100644 --- a/acceptor.go +++ b/acceptor.go @@ -35,11 +35,6 @@ type CAcceptor struct { docType enum.DocumentType - escrowOrientation enum.OrientationType - highSecurity bool - orientationCtl enum.OrientationControlType - orientationCtlExt enum.OrientationControlType - version string transactionTimeout time.Duration downloadTimeout time.Duration inSoftResetOneSecondIgnore bool diff --git a/acceptor/acceptor.go b/acceptor/acceptor.go index 72c1fe5..339540e 100644 --- a/acceptor/acceptor.go +++ b/acceptor/acceptor.go @@ -1,5 +1,90 @@ package acceptor +import ( + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" +) + //////////////////////////////////// var Connected bool + +var EscrowOrientation enum.OrientationType +var HighSecurity bool +var OrientationCtl enum.OrientationControlType +var OrientationCtlExt enum.OrientationControlType +var Version string + +var ExpandedNoteReporting bool + +//// + +func ConstructOmnibusCommand(payload []byte, controlCode consts.CmdType, data0Index int, billTypeEnables []bool) { + payload[0] = controlCode.Byte() + + if Enable.Bookmarks && Enable.Acceptance && Device.State != enum.StateCalibrating { + payload[0] |= 0x20 + } + + data0 := byte(0) + + if Enable.Acceptance && Device.State != enum.StateCalibrating { + if ExpandedNoteReporting { + data0 |= 0x7F + } else { + if len(billTypeEnables) == 0 { + data0 |= 0x7F + } else { + for i, enable := range billTypeEnables { + if enable { + data0 |= 1 << i + } + } + } + } + } + + data1 := byte(0) // Ignore bit 0 since we are not supporting special interrupt mode. + + if HighSecurity { + data1 |= 0x02 + } + + switch OrientationCtl { + case enum.OrientationControlTwoWay: + data1 |= 0x04 + case enum.OrientationControlFourWay: + data1 |= 0x0C + } + + data1 |= 0x10 // Always enable escrow mode. + + data2 := byte(0) + + if Enable.NoPush { + data2 |= 0x01 + } + + if Enable.BarCodes && Enable.Acceptance && Device.State != enum.StateCalibrating { + data2 |= 0x02 + } + + switch Device.PowerUp { + case enum.PowerUpB: + data2 |= 0x04 + case enum.PowerUpC: + data2 |= 0x0C + } + + if ExpandedNoteReporting { + data2 |= 0x10 + } + + if Enable.CouponExt && Cap.CouponExt { + data2 |= 0x20 + } + + payload[data0Index] = data0 + payload[data0Index+1] = data1 + payload[data0Index+2] = data2 +} diff --git a/acceptor_command.go b/acceptor_command.go index 03d20cc..1dfaac1 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -2,85 +2,11 @@ package mpost import ( "errors" - "github.com/hard-soft-ware/mpost/acceptor" - "github.com/hard-soft-ware/mpost/enum" "time" ) //////////////////////////////////// -func (a *CAcceptor) ConstructOmnibusCommand(payload []byte, controlCode byte, data0Index int) { - payload[0] = controlCode - - if acceptor.Enable.Bookmarks && acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { - payload[0] |= 0x20 - } - - data0 := byte(0) - - if acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { - if a.expandedNoteReporting { - data0 |= 0x7F - } else { - if len(a.billTypeEnables) == 0 { - data0 |= 0x7F - } else { - for i, enable := range a.billTypeEnables { - if enable { - data0 |= 1 << i - } - } - } - } - } - - data1 := byte(0) // Ignore bit 0 since we are not supporting special interrupt mode. - - if a.highSecurity { - data1 |= 0x02 - } - - switch a.orientationCtl { - case enum.OrientationControlTwoWay: - data1 |= 0x04 - case enum.OrientationControlFourWay: - data1 |= 0x0C - } - - data1 |= 0x10 // Always enable escrow mode. - - data2 := byte(0) - - if acceptor.Enable.NoPush { - data2 |= 0x01 - } - - if acceptor.Enable.BarCodes && acceptor.Enable.Acceptance && acceptor.Device.State != enum.StateCalibrating { - data2 |= 0x02 - } - - switch acceptor.Device.PowerUp { - case enum.PowerUpB: - data2 |= 0x04 - case enum.PowerUpC: - data2 |= 0x0C - } - - if a.expandedNoteReporting { - data2 |= 0x10 - } - - if acceptor.Enable.CouponExt && acceptor.Cap.CouponExt { - data2 |= 0x20 - } - - payload[data0Index] = data0 - payload[data0Index+1] = data1 - payload[data0Index+2] = data2 -} - -//// - func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { a.log.Debug().Bytes("payload", payload).Msg("SendCommand") a.messageQueue <- NewCMessage(payload, true) diff --git a/bill.go b/bill.go index bfc03b3..9c12571 100644 --- a/bill.go +++ b/bill.go @@ -60,13 +60,13 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) CBill { orientation := reply[extDataIndex+10] switch orientation { case 0x00: - a.escrowOrientation = enum.OrientationRightUp + acceptor.EscrowOrientation = enum.OrientationRightUp case 0x01: - a.escrowOrientation = enum.OrientationRightDown + acceptor.EscrowOrientation = enum.OrientationRightDown case 0x02: - a.escrowOrientation = enum.OrientationLeftUp + acceptor.EscrowOrientation = enum.OrientationLeftUp case 0x03: - a.escrowOrientation = enum.OrientationLeftDown + acceptor.EscrowOrientation = enum.OrientationLeftDown } bill.Type = rune(reply[extDataIndex+11]) diff --git a/bill_table.go b/bill_table.go index 11e50f1..f48f2c7 100644 --- a/bill_table.go +++ b/bill_table.go @@ -35,7 +35,7 @@ func (a *CAcceptor) RetrieveBillTable() { index := 1 for { payload := make([]byte, 6) - a.ConstructOmnibusCommand(payload, consts.CmdExpanded.Byte(), 2) + acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, a.billTypeEnables) payload[1] = 0x02 payload[5] = byte(index) diff --git a/data_escrow.go b/data_escrow.go index 8eadc83..47d1055 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -15,7 +15,7 @@ func (dl *CDataLinkLayer) escrowXX(b byte) { } payload := make([]byte, 4) - dl.Acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus.Byte(), 1) + acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus, 1, dl.Acceptor.billTypeEnables) payload[2] |= b diff --git a/data_send_replay.go b/data_send_replay.go index cd282cc..ffb0b53 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -156,13 +156,13 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.ProcessExtendedOmnibusExpandedNoteReply(reply) if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { if acceptor.Cap.OrientationExt { - switch dl.Acceptor.orientationCtlExt { + switch acceptor.OrientationCtlExt { case enum.OrientationControlOneWay: - if dl.Acceptor.escrowOrientation != enum.OrientationRightUp { + if acceptor.EscrowOrientation != enum.OrientationRightUp { dl.EscrowReturn() } case enum.OrientationControlTwoWay: - if dl.Acceptor.escrowOrientation != enum.OrientationRightUp && dl.Acceptor.escrowOrientation != enum.OrientationLeftUp { + if acceptor.EscrowOrientation != enum.OrientationRightUp && acceptor.EscrowOrientation != enum.OrientationLeftUp { dl.EscrowReturn() } case enum.OrientationControlFourWay: From 94579e8d4b10f02c4ad5d857a71764f0333511af Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 15:12:13 +0200 Subject: [PATCH 07/29] refactoring [v0.1.8] corrected logs --- _run/values/ver.txt | 2 +- acceptor.go | 2 +- acceptor_command.go | 2 -- acceptor_loop.go | 4 +-- acceptor_methods.go | 4 +-- acceptor_raise.go | 5 +-- acceptor_serial.go | 29 +++++---------- acceptor_thread.go | 11 +++--- bill_table.go | 10 +++--- const.go | 6 ++-- data.go | 6 ++-- data_escrow.go | 38 ++++++++++---------- data_log.go | 13 ------- data_send_replay.go | 6 ++-- log.go | 88 ++++++++++++++++++++++++--------------------- new_log.go | 53 --------------------------- 16 files changed, 100 insertions(+), 179 deletions(-) delete mode 100644 data_log.go delete mode 100644 new_log.go diff --git a/_run/values/ver.txt b/_run/values/ver.txt index fad30c5..da14730 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.7 +v0.1.8 diff --git a/acceptor.go b/acceptor.go index e32f312..cf0decc 100644 --- a/acceptor.go +++ b/acceptor.go @@ -69,7 +69,7 @@ type CAcceptor struct { signalMainThreadEvent int eventHandlers map[enum.EventType]EventHandler - log *LogGlobalStruct + log LogStruct } func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor { diff --git a/acceptor_command.go b/acceptor_command.go index 1dfaac1..3e29acb 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -8,12 +8,10 @@ import ( //////////////////////////////////// func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { - a.log.Debug().Bytes("payload", payload).Msg("SendCommand") a.messageQueue <- NewCMessage(payload, true) select { case reply := <-a.replyQueue: - a.log.Debug().Bytes("payload", reply).Msg("Reply queued") return reply, nil case <-time.After(30 * time.Second): return nil, errors.New("timeout waiting for response") diff --git a/acceptor_loop.go b/acceptor_loop.go index 21cd7ca..5ade80f 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -9,7 +9,7 @@ import ( //////////////////////////////////// -func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { +func (a *CAcceptor) PollingLoop(lg *LogStruct) []byte { startTickCount := time.Now() for { @@ -17,7 +17,7 @@ func (a *CAcceptor) PollingLoop(lg *LogGlobalStruct) []byte { reply, err := a.SendSynchronousCommand(payload) if err != nil { - lg.Debug().Err(err).Msg("PollingLoop") + a.log.Err("PollingLoop", err) } if time.Since(startTickCount) > PollingDisconnectTimeout { diff --git a/acceptor_methods.go b/acceptor_methods.go index 7a454a5..894e1fe 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -30,14 +30,14 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st func (a *CAcceptor) GetDeviceSerialNumber() string { err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") if err != nil { - a.log.Debug().Err(err).Msg("GetDeviceSerialNumber") + a.log.Err("GetDeviceSerialNumber", err) return "" } payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} reply, err := a.SendSynchronousCommand(payload) if err != nil { - a.log.Debug().Err(err).Msg("GetDeviceSerialNumber") + a.log.Err("GetDeviceSerialNumber", err) return "" } diff --git a/acceptor_raise.go b/acceptor_raise.go index 75b0111..504f2b1 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -8,6 +8,7 @@ import ( //////////////////////////////////// func (a *CAcceptor) raiseXX(e enum.EventType) { + a.log.Event(e) if handler, exists := a.eventHandlers[e]; exists && handler != nil { handler(a, 0) } @@ -16,27 +17,23 @@ func (a *CAcceptor) raiseXX(e enum.EventType) { //// func (a *CAcceptor) RaiseConnectedEvent() { - a.log.Debug().Str("Event", "ConnectedEvent").Msg("Raise") a.raiseXX(enum.EventConnected) acceptor.ShouldRaise.DisconnectedEvent = true acceptor.ShouldRaise.ConnectedEvent = false } func (a *CAcceptor) RaiseDisconnectedEvent() { - a.log.Debug().Str("Event", "DisconnectedEvent").Msg("Raise") a.raiseXX(enum.EventDisconnected) acceptor.ShouldRaise.DisconnectedEvent = false acceptor.ShouldRaise.ConnectedEvent = true } func (a *CAcceptor) RaiseDownloadRestartEvent() { - a.log.Debug().Str("Event", "DownloadRestartEvent").Msg("Raise") a.raiseXX(enum.EventDownloadRestart) acceptor.ShouldRaise.DownloadRestartEvent = false } func (a *CAcceptor) RaiseCalibrateProgressEvent() { - a.log.Debug().Str("Event", "CalibrateProgressEvent").Msg("Raise") a.raiseXX(enum.EventCalibrateProgress) acceptor.ShouldRaise.CalibrateProgressEvent = false } diff --git a/acceptor_serial.go b/acceptor_serial.go index d46cf2b..0cd9515 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -6,28 +6,16 @@ import ( "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "go.bug.st/serial" - "strings" "time" ) -func byteSliceToString(b []byte) string { - var sb strings.Builder - for i, byteVal := range b { - if i > 0 { - sb.WriteString(" ") - } - fmt.Fprintf(&sb, "%02X", byteVal) - } - return sb.String() -} - //////////////////////////////////// func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { - lg := a.log.NewLog("OpenSerial") + lg := a.log.New("OpenSerial") if acceptor.Connected { - lg.Debug().Msg("already connected") + lg.Msg("already connected") return nil } @@ -36,7 +24,7 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { err := a.OpenPort(lg) if err != nil { - lg.Debug().Err(err).Msg("failed to open serial port") + a.log.Err("failed to open serial port", err) return err } @@ -46,7 +34,7 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { return nil } -func (a *CAcceptor) OpenPort(lg *LogGlobalStruct) error { +func (a *CAcceptor) OpenPort(lg *LogStruct) error { mode := &serial.Mode{ BaudRate: 9600, DataBits: 7, @@ -56,7 +44,6 @@ func (a *CAcceptor) OpenPort(lg *LogGlobalStruct) error { port, err := serial.Open(acceptor.Device.PortName, mode) if err != nil { - lg.Debug().Err(err).Msg("failed to open serial port") return err } @@ -75,7 +62,7 @@ func (a *CAcceptor) OpenPort(lg *LogGlobalStruct) error { a.port = port acceptor.Connected = true - lg.Debug().Msg("Connected") + lg.Msg("Connected") return nil } @@ -91,7 +78,7 @@ func (a *CAcceptor) Close() { } if a.dataLinkLayer != nil { - a.dataLinkLayer.FlushIdenticalTransactionsToLog() + a.log.Msg(fmt.Sprintf("IdenticalCommandAndReplyCount: %d", a.dataLinkLayer.IdenticalCommandAndReplyCount)) } a.stopWorkerThread = true @@ -104,7 +91,7 @@ func (a *CAcceptor) Close() { //// -func (a *CAcceptor) QueryDeviceCapabilities(lg *LogGlobalStruct) { +func (a *CAcceptor) QueryDeviceCapabilities(lg *LogStruct) { if !a.isQueryDeviceCapabilitiesSupported { return } @@ -113,7 +100,7 @@ func (a *CAcceptor) QueryDeviceCapabilities(lg *LogGlobalStruct) { reply, err := a.SendSynchronousCommand(payload) if len(reply) < 4 { - lg.Debug().Err(err).Msg("Reply too short, unable to process.") + a.log.Err("Reply too short, unable to process.", err) return } diff --git a/acceptor_thread.go b/acceptor_thread.go index f5d5938..a86a8e0 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -9,12 +9,12 @@ import ( //////////////////////////////////// func (a *CAcceptor) OpenThread() { - lg := a.log.NewLog("Thread") + lg := a.log.New("Thread") replay := a.PollingLoop(lg) if a.wasStopped { - lg.Debug().Msg("thread is stopped") + lg.Msg("thread is stopped") return } @@ -35,7 +35,7 @@ func (a *CAcceptor) OpenThread() { //// func (a *CAcceptor) MessageLoopThread() { - lg := a.log.NewLog("LoopThread") + lg := a.log.New("LoopThread") a.dataLinkLayer = a.NewCDataLinkLayer(lg) timeoutStart := time.Now() @@ -60,18 +60,17 @@ func (a *CAcceptor) MessageLoopThread() { if a.stopWorkerThread { a.stopWorkerThread = false - lg.Debug().Msg("thread is stopped") + lg.Msg("thread is stopped") return } select { case message := <-a.messageQueue: - lg.Debug().Bytes("payload", message.Payload).Msg("MessageLoopThread") a.dataLinkLayer.SendPacket(message.Payload) reply, err := a.dataLinkLayer.ReceiveReply() if err != nil { - lg.Error().Err(err).Msg("Invalid ReceiveReply") + a.log.Err("Invalid ReceiveReply", err) continue } diff --git a/bill_table.go b/bill_table.go index f48f2c7..979d8a1 100644 --- a/bill_table.go +++ b/bill_table.go @@ -28,7 +28,7 @@ func (a *CAcceptor) ClearBillTable() { a.billValues = []CBill{} a.billValueEnables = []bool{} - a.log.Debug().Msg("Bill table cleared") + a.log.Msg("Bill table cleared") } func (a *CAcceptor) RetrieveBillTable() { @@ -50,7 +50,7 @@ func (a *CAcceptor) RetrieveBillTable() { } if err != nil || len(reply) != 30 { - a.log.Debug().Err(err).Msg("Error sending command") + a.log.Err("Error sending command", err) break } @@ -72,7 +72,7 @@ func (a *CAcceptor) RetrieveBillTable() { a.billTypeEnables = append(a.billTypeEnables, true) } - a.log.Debug().Msg("Bill table retrieved") + a.log.Msg("Bill table retrieved") } func (a *CAcceptor) BuildHardCodedBillTable() { @@ -169,7 +169,7 @@ func (a *CAcceptor) BuildHardCodedBillTable() { } } - a.log.Debug().Msg("Hardcoded bill table built") + a.log.Msg("Hardcoded bill table built") } func (a *CAcceptor) BuildBillValues() { @@ -199,5 +199,5 @@ func (a *CAcceptor) BuildBillValues() { } } - a.log.Debug().Msg("Bill values built") + a.log.Msg("Bill values built") } diff --git a/const.go b/const.go index 18d09bd..98313ce 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "ac62904c321306765bd4926f1f9a7adca7045e53" + GlobalHash string = "8a3e74ffe7f5c214ec451e5d4f9e3de30207c4db" - GlobalVersion string = "v0.1.7" + GlobalVersion string = "v0.1.8" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 7 + GlobalVersionPatch uint16 = 8 ) diff --git a/data.go b/data.go index 5b31203..94603a5 100644 --- a/data.go +++ b/data.go @@ -3,7 +3,7 @@ package mpost //////////////////////////////////// type CDataLinkLayer struct { - log *LogGlobalStruct + log *LogStruct Acceptor *CAcceptor AckToggleBit byte @@ -13,10 +13,10 @@ type CDataLinkLayer struct { IdenticalCommandAndReplyCount int } -func (a *CAcceptor) NewCDataLinkLayer(lg *LogGlobalStruct) *CDataLinkLayer { +func (a *CAcceptor) NewCDataLinkLayer(lg *LogStruct) *CDataLinkLayer { return &CDataLinkLayer{ Acceptor: a, - log: lg.NewLog("Data"), + log: lg.New("Data"), } } diff --git a/data_escrow.go b/data_escrow.go index 47d1055..3de5f42 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -10,7 +10,7 @@ import ( func (dl *CDataLinkLayer) escrowXX(b byte) { if !acceptor.Connected { - dl.log.Debug().Msg("serial not connected") + dl.log.Msg("serial not connected") return } @@ -34,7 +34,7 @@ func (dl *CDataLinkLayer) EscrowStack() { func (dl *CDataLinkLayer) RaiseEvents() { if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PowerUpEvent { - dl.log.Debug().Msg("Power Up Event Raised") + dl.log.Msg("Power Up Event Raised") acceptor.ShouldRaise.PowerUpEvent = false } @@ -46,88 +46,88 @@ func (dl *CDataLinkLayer) RaiseEvents() { switch acceptor.Device.State { case enum.StateEscrow: if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PUPEscrowEvent { - dl.log.Debug().Msg("PUP Escrow Event Raised") + dl.log.Msg("PUP Escrow Event Raised") acceptor.ShouldRaise.PUPEscrowEvent = false } else if acceptor.ShouldRaise.EscrowEvent { - dl.log.Debug().Msg("Escrow Event Raised") + dl.log.Msg("Escrow Event Raised") acceptor.ShouldRaise.EscrowEvent = false } case enum.StateStacked: if acceptor.ShouldRaise.StackedEvent { - dl.log.Debug().Msg("Stacked Event Raised") + dl.log.Msg("Stacked Event Raised") acceptor.ShouldRaise.StackedEvent = false } case enum.StateReturned: if acceptor.ShouldRaise.ReturnedEvent { - dl.log.Debug().Msg("Returned Event Raised") + dl.log.Msg("Returned Event Raised") acceptor.ShouldRaise.ReturnedEvent = false } case enum.StateRejected: if acceptor.ShouldRaise.RejectedEvent { - dl.log.Debug().Msg("Rejected Event Raised") + dl.log.Msg("Rejected Event Raised") acceptor.ShouldRaise.RejectedEvent = false } case enum.StateStalled: if acceptor.ShouldRaise.StallDetectedEvent { - dl.log.Debug().Msg("Stall Detected Event Raised") + dl.log.Msg("Stall Detected Event Raised") acceptor.ShouldRaise.StallDetectedEvent = false } } if acceptor.Device.State != enum.StateStalled && acceptor.ShouldRaise.StallClearedEvent { - dl.log.Debug().Msg("Stall Cleared Event Raised") + dl.log.Msg("Stall Cleared Event Raised") acceptor.ShouldRaise.StallClearedEvent = false } if acceptor.Cash.BoxFull && acceptor.ShouldRaise.StackerFullEvent { - dl.log.Debug().Msg("Stacker Full Event Raised") + dl.log.Msg("Stacker Full Event Raised") acceptor.ShouldRaise.StackerFullEvent = false } if dl.Acceptor.isCheated && acceptor.ShouldRaise.CheatedEvent { - dl.log.Debug().Msg("Cheated Event Raised") + dl.log.Msg("Cheated Event Raised") acceptor.ShouldRaise.CheatedEvent = false } if acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxAttachedEvent { - dl.log.Debug().Msg("Cash Box Attached Event Raised") + dl.log.Msg("Cash Box Attached Event Raised") acceptor.ShouldRaise.CashBoxAttachedEvent = false acceptor.ShouldRaise.CashBoxRemovedEvent = true } if !acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxRemovedEvent { - dl.log.Debug().Msg("Cash Box Removed Event Raised") + dl.log.Msg("Cash Box Removed Event Raised") acceptor.ShouldRaise.CashBoxRemovedEvent = false acceptor.ShouldRaise.CashBoxAttachedEvent = true } if acceptor.Device.Paused && acceptor.ShouldRaise.PauseDetectedEvent { - dl.log.Debug().Msg("Pause Detected Event Raised") + dl.log.Msg("Pause Detected Event Raised") acceptor.ShouldRaise.PauseDetectedEvent = false } if !acceptor.Device.Paused && acceptor.ShouldRaise.PauseClearedEvent { - dl.log.Debug().Msg("Pause Cleared Event Raised") + dl.log.Msg("Pause Cleared Event Raised") acceptor.ShouldRaise.PauseClearedEvent = false } if dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamDetectedEvent { - dl.log.Debug().Msg("Jam Detected Event Raised") + dl.log.Msg("Jam Detected Event Raised") acceptor.ShouldRaise.JamDetectedEvent = false } if !dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamClearedEvent { - dl.log.Debug().Msg("Jam Cleared Event Raised") + dl.log.Msg("Jam Cleared Event Raised") acceptor.ShouldRaise.JamClearedEvent = false } if dl.Acceptor.isInvalidCommand && acceptor.ShouldRaise.InvalidCommandEvent { - dl.log.Debug().Msg("Invalid Command Event Raised") + dl.log.Msg("Invalid Command Event Raised") acceptor.ShouldRaise.InvalidCommandEvent = false } if acceptor.ShouldRaise.CalibrateFinishEvent { - dl.log.Debug().Msg("Calibrate Finish Event Raised") + dl.log.Msg("Calibrate Finish Event Raised") acceptor.ShouldRaise.CalibrateFinishEvent = false } } diff --git a/data_log.go b/data_log.go deleted file mode 100644 index 534423f..0000000 --- a/data_log.go +++ /dev/null @@ -1,13 +0,0 @@ -package mpost - -//////////////////////////////////// - -func (dl *CDataLinkLayer) LogCommandAndReply(command, reply []byte, wasEchoDiscarded bool) { - dl.log.Debug().Bytes("command", command).Bytes("reply", reply).Bool("wasEchoDiscarded", wasEchoDiscarded).Bool("wasEchoDiscarded", wasEchoDiscarded).Send() -} - -func (dl *CDataLinkLayer) FlushIdenticalTransactionsToLog() { - if dl.IdenticalCommandAndReplyCount > 0 { - dl.log.Debug().Int("IdenticalCommandAndReplyCount", dl.IdenticalCommandAndReplyCount).Send() - } -} diff --git a/data_send_replay.go b/data_send_replay.go index ffb0b53..961a06c 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -29,10 +29,10 @@ func (dl *CDataLinkLayer) SendPacket(payload []byte) { dl.CurrentCommand = command dl.EchoDetect = command - dl.log.Debug().Str("data", byteSliceToString(command)).Msg("SERIAL SEND") + dl.log.Bytes("SERIAL SEND >>> ", command) n, err := dl.Acceptor.port.Write(command) if err != nil || n == 0 { - dl.log.Debug().Err(err).Msg("Failed to write to port") + dl.log.Err("Failed to write to port", err) dl.Acceptor.port.Close() dl.Acceptor.OpenPort(dl.log) @@ -102,7 +102,7 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { } } - dl.log.Debug().Str("data", byteSliceToString(reply)).Msg("SERIAL READ") + dl.log.Bytes("SERIAL READ <<< ", reply) return reply, nil } diff --git a/log.go b/log.go index 374896b..0bc693f 100644 --- a/log.go +++ b/log.go @@ -1,63 +1,69 @@ package mpost import ( + "fmt" + "github.com/hard-soft-ware/mpost/enum" "github.com/rs/zerolog" + "strings" ) ///////////////////////////////////////////////////////// -const TextLogMsgInit = "INIT" +type LogStruct struct { + isEnable bool + log zerolog.Logger + index string +} -type LogGlobalStruct struct { - log zerolog.Logger - index string +func NewLog(log zerolog.Logger, root string) LogStruct { + obj := LogStruct{ + isEnable: true, + index: root, + log: log, + } + return obj } -//// +func (obj *LogStruct) New(point string) *LogStruct { + newObj := *obj + newObj.index = obj.index + "/" + point + return &newObj +} -func NewLog(log zerolog.Logger, root string) *LogGlobalStruct { - obj := LogGlobalStruct{index: root} +//// - newLogger := log.With().Str("index", obj.index).Logger() - obj.log = newLogger - newLogger.Debug().Msg(TextLogMsgInit) +func (obj *LogStruct) Msg(msg string) { + if !obj.isEnable { + return + } + obj.log.Debug().Msg(msg) +} - return &obj +func (obj *LogStruct) Err(msg string, err error) { + if !obj.isEnable { + return + } + obj.log.Debug().Err(err).Msg(msg) } -func (obj *LogGlobalStruct) NewLog(point string) *LogGlobalStruct { - newObj := LogGlobalStruct{index: obj.index + "/" + point} +func (obj *LogStruct) Bytes(msg string, data []byte) { + if !obj.isEnable { + return + } - newLogger := obj.log.With().Str("index", newObj.index).Logger() - newObj.log = newLogger - newLogger.Debug().Msg(TextLogMsgInit) + var sb strings.Builder + for i, byteVal := range data { + if i > 0 { + sb.WriteString(" ") + } + fmt.Fprintf(&sb, "%02X", byteVal) + } - return &newObj + obj.log.Debug().Str("data", sb.String()).Msg(msg) } -//// +// -func (obj *LogGlobalStruct) Debug() *zerolog.Event { - return obj.log.Debug() -} -func (obj *LogGlobalStruct) Info() *zerolog.Event { - return obj.log.Info() -} -func (obj *LogGlobalStruct) Log() *zerolog.Event { - return obj.log.Log() -} -func (obj *LogGlobalStruct) Error() *zerolog.Event { - return obj.log.Error() -} -func (obj *LogGlobalStruct) Panic() *zerolog.Event { - return obj.log.Panic() -} -func (obj *LogGlobalStruct) Fatal() *zerolog.Event { - return obj.log.Fatal() -} -func (obj *LogGlobalStruct) Warn() *zerolog.Event { - return obj.log.Warn() -} -func (obj *LogGlobalStruct) Trace() *zerolog.Event { - return obj.log.Trace() +func (obj *LogStruct) Event(event enum.EventType) { + obj.log.Debug().Str("event", event.String()).Msg("Event") } diff --git a/new_log.go b/new_log.go deleted file mode 100644 index d2f92f4..0000000 --- a/new_log.go +++ /dev/null @@ -1,53 +0,0 @@ -package mpost - -import ( - "fmt" - "github.com/hard-soft-ware/mpost/enum" - "github.com/rs/zerolog" - "strings" -) - -///////////////////////////////////////////////////////// - -type LogStruct struct { - log *zerolog.Logger - index string -} - -func NewLog2(log *zerolog.Logger, root string) *LogStruct { - obj := LogStruct{index: root, log: log} - return &obj -} - -func (obj *LogStruct) New(point string) *LogStruct { - newObj := LogStruct{index: obj.index + "/" + point} - return &newObj -} - -//// - -func (obj *LogStruct) Msg(msg string) { - obj.log.Debug().Msg(msg) -} - -func (obj *LogStruct) Err(msg string, err error) { - obj.log.Debug().Err(err).Msg(msg) -} - -func (obj *LogStruct) Bytes(msg string, data []byte) { - var sb strings.Builder - for i, byteVal := range data { - if i > 0 { - sb.WriteString(" ") - } - fmt.Fprintf(&sb, "%02X", byteVal) - } - - obj.log.Debug().Str("data", sb.String()).Msg(msg) -} - -// - -func (obj *LogStruct) Event(event enum.EventType) { - obj.log.Debug().Str("event", "lool").Msg("Event") -} From 9c0c999735c186d1c9d4e5020ab952678354eb94 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 16:10:53 +0200 Subject: [PATCH 08/29] refactoring [v0.1.9] moved Bill --- _run/values/ver.txt | 2 +- acceptor.go | 6 -- acceptor_process.go | 11 +-- bill.go | 80 ++++++++++------- bill/bill.go | 56 ++++++++++++ bill/table.go | 151 ++++++++++++++++++++++++++++++++ bill_table.go | 203 -------------------------------------------- const.go | 6 +- data_escrow.go | 3 +- 9 files changed, 266 insertions(+), 252 deletions(-) create mode 100644 bill/bill.go create mode 100644 bill/table.go delete mode 100644 bill_table.go diff --git a/_run/values/ver.txt b/_run/values/ver.txt index da14730..549f177 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.8 +v0.1.9 diff --git a/acceptor.go b/acceptor.go index cf0decc..90ff6b8 100644 --- a/acceptor.go +++ b/acceptor.go @@ -22,12 +22,6 @@ type CAcceptor struct { autoStack bool barCode string - bill CBill - billTypes []CBill - billTypeEnables []bool - billValues []CBill - billValueEnables []bool - bnfStatus enum.BNFStatusType bootPN string diff --git a/acceptor_process.go b/acceptor_process.go index 38e5a7a..6476ee9 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -2,6 +2,7 @@ package mpost import ( "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/bill" "github.com/hard-soft-ware/mpost/enum" ) @@ -45,7 +46,7 @@ func (a *CAcceptor) processData0(data0 byte) { if (data0 & 0x40) != 0 { acceptor.Device.State = enum.StateReturned - a.bill = CBill{} // Resetting the bill + bill.Reset() // Resetting the bill } else { acceptor.ShouldRaise.ReturnedEvent = true } @@ -113,24 +114,24 @@ func (a *CAcceptor) processData2(data2 byte) { billTypeIndex := (data2 & 0x38) >> 3 if billTypeIndex > 0 { if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !a.wasDocTypeSetOnEscrow) { - a.bill = a.billTypes[billTypeIndex-1] + bill.Bill = bill.Types[billTypeIndex-1] a.docType = enum.DocumentBill a.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow } } else { if acceptor.Device.State == enum.StateStacked || acceptor.Device.State == enum.StateEscrow { - a.bill = CBill{} + bill.Reset() a.docType = enum.DocumentNoValue a.wasDocTypeSetOnEscrow = false } } } else { if acceptor.Device.State == enum.StateStacked { - if a.docType == enum.DocumentBill && a.bill.Value == 0.0 { + if a.docType == enum.DocumentBill && bill.Bill.Value == 0.0 { a.docType = enum.DocumentNoValue } } else if acceptor.Device.State == enum.StateEscrow { - a.bill = CBill{} + bill.Reset() a.docType = enum.DocumentNoValue } } diff --git a/bill.go b/bill.go index 9c12571..9722be0 100644 --- a/bill.go +++ b/bill.go @@ -1,27 +1,19 @@ package mpost import ( - "fmt" "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/bill" + "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "strconv" "strings" + "time" ) //////////////////////////////////// -// представление купюры -type CBill struct { - Country string - Value float64 - Type rune - Series rune - Compatibility rune - Version rune -} - -func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) CBill { - var bill CBill +func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) bill.BillStruct { + var bill bill.BillStruct if len(reply) < extDataIndex+15 { return bill @@ -77,32 +69,54 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) CBill { return bill } -//// +//////// + +func (a *CAcceptor) RetrieveBillTable() { + index := 1 + for { + payload := make([]byte, 6) + acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, bill.TypeEnables) + payload[1] = 0x02 + payload[5] = byte(index) + + var reply []byte + var err error + for { + reply, err = a.SendSynchronousCommand(payload) + if err != nil || len(reply) == 30 { + break + } + time.Sleep(100 * time.Millisecond) + } -func (b *CBill) ToString() string { - return fmt.Sprintf("%s %.2f %c %c %c %c", b.Country, b.Value, b.Series, b.Type, b.Compatibility, b.Version) -} + if err != nil || len(reply) != 30 { + a.log.Err("Error sending command", err) + break + } -func (b *CBill) GetCountry() string { - return b.Country -} + ctl := reply[2] + if (ctl&0x70) != 0x70 || reply[3] != 0x02 { + break + } -func (b *CBill) GetValue() float64 { - return b.Value -} + if reply[10] == 0 { + break + } -func (b *CBill) GetSeries() rune { - return b.Series -} + billFromTable := a.ParseBillData(reply, 10) + bill.Types = append(bill.Types, billFromTable) + index++ + } -func (b *CBill) GetType() rune { - return b.Type -} + for range bill.Types { + bill.TypeEnables = append(bill.TypeEnables, true) + } -func (b *CBill) GetCompatibility() rune { - return b.Compatibility + a.log.Msg("Bill table retrieved") } -func (b *CBill) GetVersion() rune { - return b.Version +func (a *CAcceptor) SetUpBillTable() { + bill.SetUpTable(a.expandedNoteReporting, func() { + a.RetrieveBillTable() + }) } diff --git a/bill/bill.go b/bill/bill.go new file mode 100644 index 0000000..917da4f --- /dev/null +++ b/bill/bill.go @@ -0,0 +1,56 @@ +package bill + +import "fmt" + +//////////////////////////////////// + +type BillStruct struct { + Country string + Value float64 + Type rune + Series rune + Compatibility rune + Version rune +} + +var Bill BillStruct + +var Types []BillStruct +var TypeEnables []bool + +var Values []BillStruct +var ValueEnables []bool + +//// + +func Reset() { + Bill = BillStruct{} +} + +func (b *BillStruct) ToString() string { + return fmt.Sprintf("%s %.2f %c %c %c %c", b.Country, b.Value, b.Series, b.Type, b.Compatibility, b.Version) +} + +func (b *BillStruct) GetCountry() string { + return b.Country +} + +func (b *BillStruct) GetValue() float64 { + return b.Value +} + +func (b *BillStruct) GetSeries() rune { + return b.Series +} + +func (b *BillStruct) GetType() rune { + return b.Type +} + +func (b *BillStruct) GetCompatibility() rune { + return b.Compatibility +} + +func (b *BillStruct) GetVersion() rune { + return b.Version +} diff --git a/bill/table.go b/bill/table.go new file mode 100644 index 0000000..959dce5 --- /dev/null +++ b/bill/table.go @@ -0,0 +1,151 @@ +package bill + +import ( + "github.com/hard-soft-ware/mpost/acceptor" +) + +//////////////////////////////////// + +func ClearTable() { + Types = []BillStruct{} + TypeEnables = []bool{} + Values = []BillStruct{} + ValueEnables = []bool{} +} + +func BuildHardCodedTable() { + Types = []BillStruct{} + + switch acceptor.Device.Model { + case 1, 12, 23, 30, 31, 'J', 'X', 'T': + Types = append(Types, BillStruct{"USD", 1, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 2, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 100, '*', '*', '*', '*'}) + + case 'P': + Types = append(Types, BillStruct{"USD", 1, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 2, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 20, '*', '*', '*', '*'}) + + case 'G': + Types = append(Types, BillStruct{}) + Types = append(Types, BillStruct{"ARS", 2, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"ARS", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"ARS", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"ARS", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"ARS", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"ARS", 100, '*', '*', '*', '*'}) + + case 'A': + Types = append(Types, BillStruct{"AUD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 100, '*', '*', '*', '*'}) + + case 15: + Types = append(Types, BillStruct{}) + Types = append(Types, BillStruct{}) + Types = append(Types, BillStruct{"AUD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"AUD", 100, '*', '*', '*', '*'}) + + case 'W': + Types = append(Types, BillStruct{"BRL", 1, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 2, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"BRL", 100, '*', '*', '*', '*'}) + + case 'C': + Types = append(Types, BillStruct{}) + Types = append(Types, BillStruct{}) + Types = append(Types, BillStruct{"CAD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"CAD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"CAD", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"CAD", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"CAD", 100, '*', '*', '*', '*'}) + + case 'D': + Types = append(Types, BillStruct{"EUR", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"EUR", 10, '*', '*', '*', '*'}) + + case 'M': + Types = append(Types, BillStruct{"MXP", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"MXP", 50, '*', '*', '*', '*'}) + + case 'B': + Types = append(Types, BillStruct{"RUR", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"RUR", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"RUR", 100, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"RUR", 500, '*', '*', '*', '*'}) + + default: + Types = append(Types, BillStruct{"USD", 1, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 2, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 5, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 10, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 20, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 50, '*', '*', '*', '*'}) + Types = append(Types, BillStruct{"USD", 100, '*', '*', '*', '*'}) + } + + TypeEnables = make([]bool, len(Types)) + for i, bill := range Types { + if bill.Value > 0 { + TypeEnables[i] = true + } else { + TypeEnables[i] = false + } + } +} + +func BuildValues() { + Values = []BillStruct{} + ValueEnables = []bool{} + + for i := range Types { + valueExists := false + + for j := range Values { + if Types[i].Value == Values[j].Value && Types[i].Country == Values[j].Country { + valueExists = true + break + } + } + + if !valueExists { + Values = append(Values, BillStruct{ + Country: Types[i].Country, + Value: Types[i].Value, + Type: '*', + Series: '*', + Compatibility: '*', + Version: '*', + }) + ValueEnables = append(ValueEnables, Types[i].Value > 0) + } + } +} + +//// + +func SetUpTable(expandedNoteReporting bool, RetrieveBillTable func()) { + ClearTable() + + if expandedNoteReporting { + RetrieveBillTable() + } else { + BuildHardCodedTable() + } + + BuildValues() +} diff --git a/bill_table.go b/bill_table.go deleted file mode 100644 index 979d8a1..0000000 --- a/bill_table.go +++ /dev/null @@ -1,203 +0,0 @@ -package mpost - -import ( - "github.com/hard-soft-ware/mpost/acceptor" - "github.com/hard-soft-ware/mpost/consts" - "time" -) - -//////////////////////////////////// - -func (a *CAcceptor) SetUpBillTable() { - a.ClearBillTable() - - if a.expandedNoteReporting { - a.RetrieveBillTable() - } else { - a.BuildHardCodedBillTable() - } - - a.BuildBillValues() -} - -//// - -func (a *CAcceptor) ClearBillTable() { - a.billTypes = []CBill{} - a.billTypeEnables = []bool{} - a.billValues = []CBill{} - a.billValueEnables = []bool{} - - a.log.Msg("Bill table cleared") -} - -func (a *CAcceptor) RetrieveBillTable() { - index := 1 - for { - payload := make([]byte, 6) - acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, a.billTypeEnables) - payload[1] = 0x02 - payload[5] = byte(index) - - var reply []byte - var err error - for { - reply, err = a.SendSynchronousCommand(payload) - if err != nil || len(reply) == 30 { - break - } - time.Sleep(100 * time.Millisecond) - } - - if err != nil || len(reply) != 30 { - a.log.Err("Error sending command", err) - break - } - - ctl := reply[2] - if (ctl&0x70) != 0x70 || reply[3] != 0x02 { - break - } - - if reply[10] == 0 { - break - } - - billFromTable := a.ParseBillData(reply, 10) - a.billTypes = append(a.billTypes, billFromTable) - index++ - } - - for range a.billTypes { - a.billTypeEnables = append(a.billTypeEnables, true) - } - - a.log.Msg("Bill table retrieved") -} - -func (a *CAcceptor) BuildHardCodedBillTable() { - a.billTypes = []CBill{} - - switch acceptor.Device.Model { - case 1, 12, 23, 30, 31, 'J', 'X', 'T': - a.billTypes = append(a.billTypes, CBill{"USD", 1, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 2, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 100, '*', '*', '*', '*'}) - - case 'P': - a.billTypes = append(a.billTypes, CBill{"USD", 1, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 2, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 20, '*', '*', '*', '*'}) - - case 'G': - a.billTypes = append(a.billTypes, CBill{}) - a.billTypes = append(a.billTypes, CBill{"ARS", 2, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"ARS", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"ARS", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"ARS", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"ARS", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"ARS", 100, '*', '*', '*', '*'}) - - case 'A': - a.billTypes = append(a.billTypes, CBill{"AUD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 100, '*', '*', '*', '*'}) - - case 15: - a.billTypes = append(a.billTypes, CBill{}) - a.billTypes = append(a.billTypes, CBill{}) - a.billTypes = append(a.billTypes, CBill{"AUD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"AUD", 100, '*', '*', '*', '*'}) - - case 'W': - a.billTypes = append(a.billTypes, CBill{"BRL", 1, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 2, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"BRL", 100, '*', '*', '*', '*'}) - - case 'C': - a.billTypes = append(a.billTypes, CBill{}) - a.billTypes = append(a.billTypes, CBill{}) - a.billTypes = append(a.billTypes, CBill{"CAD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"CAD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"CAD", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"CAD", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"CAD", 100, '*', '*', '*', '*'}) - - case 'D': - a.billTypes = append(a.billTypes, CBill{"EUR", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"EUR", 10, '*', '*', '*', '*'}) - - case 'M': - a.billTypes = append(a.billTypes, CBill{"MXP", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"MXP", 50, '*', '*', '*', '*'}) - - case 'B': - a.billTypes = append(a.billTypes, CBill{"RUR", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"RUR", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"RUR", 100, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"RUR", 500, '*', '*', '*', '*'}) - - default: - a.billTypes = append(a.billTypes, CBill{"USD", 1, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 2, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 5, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 10, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 20, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 50, '*', '*', '*', '*'}) - a.billTypes = append(a.billTypes, CBill{"USD", 100, '*', '*', '*', '*'}) - } - - a.billTypeEnables = make([]bool, len(a.billTypes)) - for i, bill := range a.billTypes { - if bill.Value > 0 { - a.billTypeEnables[i] = true - } else { - a.billTypeEnables[i] = false - } - } - - a.log.Msg("Hardcoded bill table built") -} - -func (a *CAcceptor) BuildBillValues() { - a.billValues = []CBill{} - a.billValueEnables = []bool{} - - for i := range a.billTypes { - valueExists := false - - for j := range a.billValues { - if a.billTypes[i].Value == a.billValues[j].Value && a.billTypes[i].Country == a.billValues[j].Country { - valueExists = true - break - } - } - - if !valueExists { - a.billValues = append(a.billValues, CBill{ - Country: a.billTypes[i].Country, - Value: a.billTypes[i].Value, - Type: '*', - Series: '*', - Compatibility: '*', - Version: '*', - }) - a.billValueEnables = append(a.billValueEnables, a.billTypes[i].Value > 0) - } - } - - a.log.Msg("Bill values built") -} diff --git a/const.go b/const.go index 98313ce..ce9480b 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "8a3e74ffe7f5c214ec451e5d4f9e3de30207c4db" + GlobalHash string = "94579e8d4b10f02c4ad5d857a71764f0333511af" - GlobalVersion string = "v0.1.8" + GlobalVersion string = "v0.1.9" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 8 + GlobalVersionPatch uint16 = 9 ) diff --git a/data_escrow.go b/data_escrow.go index 3de5f42..b84087b 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -2,6 +2,7 @@ package mpost import ( "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/bill" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" ) @@ -15,7 +16,7 @@ func (dl *CDataLinkLayer) escrowXX(b byte) { } payload := make([]byte, 4) - acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus, 1, dl.Acceptor.billTypeEnables) + acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus, 1, bill.TypeEnables) payload[2] |= b From d4c719422b0bbfcbac83b5fee4662399502c9168 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 17:54:41 +0200 Subject: [PATCH 09/29] refactoring [v0.1.10] more edits --- 0_test.go | 3 ++- _run/values/ver.txt | 2 +- acceptor.go | 48 ++++++++++---------------------------------- acceptor/acceptor.go | 20 ++++++++++++++++++ acceptor/timeout.go | 12 +++++++++++ acceptor_loop.go | 14 ++++++------- acceptor_methods.go | 36 +++++++++++++++++++++++++++++++++ acceptor_poll.go | 11 ++++++---- acceptor_process.go | 32 ++++++++++++++--------------- acceptor_raise.go | 12 +++++------ acceptor_serial.go | 6 +++--- acceptor_thread.go | 14 ++++++------- bill.go | 4 ++-- const.go | 6 +++--- data_escrow.go | 16 +++++++-------- data_process.go | 8 ++++---- data_send_replay.go | 10 ++++----- 17 files changed, 150 insertions(+), 104 deletions(-) create mode 100644 acceptor/timeout.go diff --git a/0_test.go b/0_test.go index 93bd55b..b9cd231 100644 --- a/0_test.go +++ b/0_test.go @@ -10,7 +10,8 @@ func TestName(t *testing.T) { a := NewCAcceptor(30*time.Second, 30*time.Second) a.Open("/dev/ttyUSB0", enum.PowerUpB) - time.Sleep(5 * time.Second) + time.Sleep(30 * time.Second) + t.Log(a.GetBNFStatus().String()) t.Log(a.GetDeviceSerialNumber()) a.Close() diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 549f177..bf42c7d 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.9 +v0.1.10 diff --git a/acceptor.go b/acceptor.go index 90ff6b8..dbc3e9b 100644 --- a/acceptor.go +++ b/acceptor.go @@ -1,6 +1,7 @@ package mpost import ( + "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/enum" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -19,58 +20,31 @@ type CAcceptor struct { auditLifeTimeTotals []int auditPerformance []int auditQP []int - autoStack bool - barCode string - - bnfStatus enum.BNFStatusType - bootPN string coupon *CCoupon docType enum.DocumentType - transactionTimeout time.Duration - downloadTimeout time.Duration - inSoftResetOneSecondIgnore bool - inSoftResetWaitForReply bool - expandedNoteReporting bool - isQueryDeviceCapabilitiesSupported bool - isDeviceJammed bool - isCheated bool - isPoweredUp bool - isInvalidCommand bool - wasDocTypeSetOnEscrow bool - wasDisconnected bool - isVeryFirstPoll bool - - compressLog bool - workerThread sync.WaitGroup - openThread chan bool - flashDownloadThread chan bool - dataLinkLayer *CDataLinkLayer - replyQueuedEvent int - notInProcessReplyEvent int - stopWorkerThread bool - stopOpenThread bool - stopFlashDownloadThread bool - suppressStandardPoll bool + workerThread sync.WaitGroup + openThread chan bool + flashDownloadThread chan bool + dataLinkLayer *CDataLinkLayer messageQueue chan *CMessage replyQueue chan []byte - wasStopped bool - isReplyAcked bool - signalMainThreadEvent int - eventHandlers map[enum.EventType]EventHandler + eventHandlers map[enum.EventType]EventHandler log LogStruct } func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor { + + acceptor.Timeout.Transaction = transactionTimeout + acceptor.Timeout.Download = downloadTimeout + a := &CAcceptor{ - transactionTimeout: transactionTimeout, - downloadTimeout: downloadTimeout, - eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), + eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), messageQueue: make(chan *CMessage, 1), replyQueue: make(chan []byte, 1), diff --git a/acceptor/acceptor.go b/acceptor/acceptor.go index 339540e..42842cc 100644 --- a/acceptor/acceptor.go +++ b/acceptor/acceptor.go @@ -8,14 +8,34 @@ import ( //////////////////////////////////// var Connected bool +var AutoStack bool var EscrowOrientation enum.OrientationType var HighSecurity bool var OrientationCtl enum.OrientationControlType var OrientationCtlExt enum.OrientationControlType + var Version string +var BarCode string +var BootPN string +var InSoftResetOneSecondIgnore bool +var InSoftResetWaitForReply bool var ExpandedNoteReporting bool +var IsQueryDeviceCapabilitiesSupported bool +var IsDeviceJammed bool +var IsCheated bool +var IsPoweredUp bool +var IsInvalidCommand bool +var WasDocTypeSetOnEscrow bool +var WasDisconnected bool +var IsVeryFirstPoll bool + +var StopWorkerThread bool +var StopOpenThread bool +var StopFlashDownloadThread bool +var SuppressStandardPoll bool +var WasStopped bool //// diff --git a/acceptor/timeout.go b/acceptor/timeout.go new file mode 100644 index 0000000..c4f00e4 --- /dev/null +++ b/acceptor/timeout.go @@ -0,0 +1,12 @@ +package acceptor + +import "time" + +//////////////////////////////////// + +type TimeoutStruct struct { + Transaction time.Duration + Download time.Duration +} + +var Timeout TimeoutStruct diff --git a/acceptor_loop.go b/acceptor_loop.go index 5ade80f..c045ebd 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -28,21 +28,21 @@ func (a *CAcceptor) PollingLoop(lg *LogStruct) []byte { } if a.flashDownloadThread != nil { - if a.stopFlashDownloadThread { - a.stopFlashDownloadThread = true + if acceptor.StopFlashDownloadThread { + acceptor.StopFlashDownloadThread = true <-a.flashDownloadThread acceptor.Device.State = enum.StateIdling - a.wasStopped = true + acceptor.WasStopped = true return nil } } else if a.openThread != nil { - if a.stopOpenThread { - a.stopOpenThread = false - a.stopWorkerThread = true + if acceptor.StopOpenThread { + acceptor.StopOpenThread = false + acceptor.StopWorkerThread = true <-a.openThread - a.wasStopped = true + acceptor.WasStopped = true a.Close() return nil } diff --git a/acceptor_methods.go b/acceptor_methods.go index 894e1fe..9bb8fef 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -27,7 +27,43 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st return nil } +//// + +func (a *CAcceptor) GetBNFStatus() enum.BNFStatusType { + a.log.Msg("Getting BNF status") + err := a.verifyPropertyIsAllowed(acceptor.Cap.BNFStatus, "BNFStatus") + + if err != nil { + a.log.Err("GetBNFStatus", err) + return enum.BNFStatusUnknown + } + + payload := []byte{0x02, 0x00, 0x00, 0x03} // Assuming CmdAuxiliary is 0x02 and CmdAuxQueryBNFStatus is 0x03 + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetBNFStatus", err) + return enum.BNFStatusUnknown + } + + if len(reply) == 9 { + if reply[3] == 0 { + return enum.BNFStatusNotAttached + } else { + if reply[4] == 0 { + return enum.BNFStatusOK + } else { + return enum.BNFStatusError + } + } + } + + return enum.BNFStatusUnknown +} + func (a *CAcceptor) GetDeviceSerialNumber() string { + a.log.Msg("GetDeviceSerialNumber") + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") if err != nil { a.log.Err("GetDeviceSerialNumber", err) diff --git a/acceptor_poll.go b/acceptor_poll.go index 7ee6689..07e2086 100644 --- a/acceptor_poll.go +++ b/acceptor_poll.go @@ -1,19 +1,22 @@ package mpost +import "github.com/hard-soft-ware/mpost/acceptor" + //////////////////////////////////// type CSuppressStandardPoll struct { acceptor *CAcceptor } -func NewCSuppressStandardPoll(acceptor *CAcceptor) *CSuppressStandardPoll { +func NewCSuppressStandardPoll(a *CAcceptor) *CSuppressStandardPoll { s := &CSuppressStandardPoll{ - acceptor: acceptor, + acceptor: a, } - s.acceptor.suppressStandardPoll = true + + acceptor.SuppressStandardPoll = true return s } func (s *CSuppressStandardPoll) Release() { - s.acceptor.suppressStandardPoll = false + acceptor.SuppressStandardPoll = false } diff --git a/acceptor_process.go b/acceptor_process.go index 6476ee9..60ddbdb 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -23,7 +23,7 @@ func (a *CAcceptor) processData0(data0 byte) { if (data0 & 0x04) != 0 { acceptor.Device.State = enum.StateEscrow - if a.autoStack { + if acceptor.AutoStack { acceptor.ShouldRaise.EscrowEvent = false } } else { @@ -54,9 +54,9 @@ func (a *CAcceptor) processData0(data0 byte) { func (a *CAcceptor) processData1(data1 byte) { if (data1 & 0x01) != 0 { - a.isCheated = true + acceptor.IsCheated = true } else { - a.isCheated = false + acceptor.IsCheated = false acceptor.ShouldRaise.CheatedEvent = true } @@ -67,10 +67,10 @@ func (a *CAcceptor) processData1(data1 byte) { } if (data1 & 0x04) != 0 { - a.isDeviceJammed = true + acceptor.IsDeviceJammed = true acceptor.ShouldRaise.JamDetectedEvent = true } else { - a.isDeviceJammed = false + acceptor.IsDeviceJammed = false acceptor.ShouldRaise.JamClearedEvent = true } @@ -110,19 +110,19 @@ func (a *CAcceptor) processData1(data1 byte) { } func (a *CAcceptor) processData2(data2 byte) { - if !a.expandedNoteReporting { + if !acceptor.ExpandedNoteReporting { billTypeIndex := (data2 & 0x38) >> 3 if billTypeIndex > 0 { - if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !a.wasDocTypeSetOnEscrow) { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !acceptor.WasDocTypeSetOnEscrow) { bill.Bill = bill.Types[billTypeIndex-1] a.docType = enum.DocumentBill - a.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow + acceptor.WasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow } } else { if acceptor.Device.State == enum.StateStacked || acceptor.Device.State == enum.StateEscrow { bill.Reset() a.docType = enum.DocumentNoValue - a.wasDocTypeSetOnEscrow = false + acceptor.WasDocTypeSetOnEscrow = false } } } else { @@ -137,19 +137,19 @@ func (a *CAcceptor) processData2(data2 byte) { } if (data2 & 0x01) != 0 { - a.isPoweredUp = true + acceptor.IsPoweredUp = true a.docType = enum.DocumentNoValue } else { acceptor.ShouldRaise.PowerUpEvent = true - if !a.isVeryFirstPoll { - a.isPoweredUp = false + if !acceptor.IsVeryFirstPoll { + acceptor.IsPoweredUp = false } } if (data2 & 0x02) != 0 { - a.isInvalidCommand = true + acceptor.IsInvalidCommand = true } else { - a.isInvalidCommand = false + acceptor.IsInvalidCommand = false acceptor.ShouldRaise.InvalidCommandEvent = true } @@ -175,7 +175,7 @@ func (a *CAcceptor) processData3(data3 byte) { } if (data3 & 0x10) != 0 { - a.isQueryDeviceCapabilitiesSupported = true + acceptor.IsQueryDeviceCapabilitiesSupported = true } } @@ -202,7 +202,7 @@ func (a *CAcceptor) processData4(data4 byte) { acceptor.Cap.EscrowTimeout = m == 'T' || m == 'U' acceptor.Cap.NoPush = m == 'P' || m == 'X' || d == 31 || d == 23 acceptor.Cap.VariantPN = m == 'T' || m == 'U' - a.expandedNoteReporting = m == 'T' || m == 'U' // This setting might be toggled in debug or production builds + acceptor.ExpandedNoteReporting = m == 'T' || m == 'U' // This setting might be toggled in debug or production builds } func (a *CAcceptor) processData5(data5 byte) { diff --git a/acceptor_raise.go b/acceptor_raise.go index 504f2b1..5ec9fc1 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -7,33 +7,33 @@ import ( //////////////////////////////////// -func (a *CAcceptor) raiseXX(e enum.EventType) { +func (a *CAcceptor) raiseXX(e enum.EventType, b int) { a.log.Event(e) if handler, exists := a.eventHandlers[e]; exists && handler != nil { - handler(a, 0) + handler(a, b) } } //// func (a *CAcceptor) RaiseConnectedEvent() { - a.raiseXX(enum.EventConnected) + a.raiseXX(enum.EventConnected, 0) acceptor.ShouldRaise.DisconnectedEvent = true acceptor.ShouldRaise.ConnectedEvent = false } func (a *CAcceptor) RaiseDisconnectedEvent() { - a.raiseXX(enum.EventDisconnected) + a.raiseXX(enum.EventDisconnected, 0) acceptor.ShouldRaise.DisconnectedEvent = false acceptor.ShouldRaise.ConnectedEvent = true } func (a *CAcceptor) RaiseDownloadRestartEvent() { - a.raiseXX(enum.EventDownloadRestart) + a.raiseXX(enum.EventDownloadRestart, 0) acceptor.ShouldRaise.DownloadRestartEvent = false } func (a *CAcceptor) RaiseCalibrateProgressEvent() { - a.raiseXX(enum.EventCalibrateProgress) + a.raiseXX(enum.EventCalibrateProgress, 0) acceptor.ShouldRaise.CalibrateProgressEvent = false } diff --git a/acceptor_serial.go b/acceptor_serial.go index 0cd9515..cebb490 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -69,7 +69,7 @@ func (a *CAcceptor) OpenPort(lg *LogStruct) error { func (a *CAcceptor) Close() { if !acceptor.Connected { - a.stopOpenThread = true + acceptor.StopOpenThread = true return } @@ -81,7 +81,7 @@ func (a *CAcceptor) Close() { a.log.Msg(fmt.Sprintf("IdenticalCommandAndReplyCount: %d", a.dataLinkLayer.IdenticalCommandAndReplyCount)) } - a.stopWorkerThread = true + acceptor.StopWorkerThread = true a.workerThread.Wait() a.port.Close() @@ -92,7 +92,7 @@ func (a *CAcceptor) Close() { //// func (a *CAcceptor) QueryDeviceCapabilities(lg *LogStruct) { - if !a.isQueryDeviceCapabilitiesSupported { + if !acceptor.IsQueryDeviceCapabilitiesSupported { return } diff --git a/acceptor_thread.go b/acceptor_thread.go index a86a8e0..76561ae 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -13,7 +13,7 @@ func (a *CAcceptor) OpenThread() { replay := a.PollingLoop(lg) - if a.wasStopped { + if acceptor.WasStopped { lg.Msg("thread is stopped") return } @@ -41,7 +41,7 @@ func (a *CAcceptor) MessageLoopThread() { timeoutStart := time.Now() for { - if !a.inSoftResetWaitForReply { + if !acceptor.InSoftResetWaitForReply { time.Sleep(10 * time.Millisecond) } else { time.Sleep(1000 * time.Millisecond) @@ -53,13 +53,13 @@ func (a *CAcceptor) MessageLoopThread() { if acceptor.ShouldRaise.DisconnectedEvent { a.RaiseDisconnectedEvent() } - a.wasDisconnected = true + acceptor.WasDisconnected = true timeoutStart = time.Now() } } - if a.stopWorkerThread { - a.stopWorkerThread = false + if acceptor.StopWorkerThread { + acceptor.StopWorkerThread = false lg.Msg("thread is stopped") return } @@ -76,8 +76,8 @@ func (a *CAcceptor) MessageLoopThread() { if len(reply) > 0 { timeoutStart = time.Now() - if a.wasDisconnected { - a.wasDisconnected = false + if acceptor.WasDisconnected { + acceptor.WasDisconnected = false a.RaiseConnectedEvent() } if message.IsSynchronous { diff --git a/bill.go b/bill.go index 9722be0..739914b 100644 --- a/bill.go +++ b/bill.go @@ -47,7 +47,7 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) bill.BillStruc bill.Value = billValue a.docType = enum.DocumentBill - a.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow + acceptor.WasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow orientation := reply[extDataIndex+10] switch orientation { @@ -116,7 +116,7 @@ func (a *CAcceptor) RetrieveBillTable() { } func (a *CAcceptor) SetUpBillTable() { - bill.SetUpTable(a.expandedNoteReporting, func() { + bill.SetUpTable(acceptor.ExpandedNoteReporting, func() { a.RetrieveBillTable() }) } diff --git a/const.go b/const.go index ce9480b..b12fd2a 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "94579e8d4b10f02c4ad5d857a71764f0333511af" + GlobalHash string = "9c0c999735c186d1c9d4e5020ab952678354eb94" - GlobalVersion string = "v0.1.9" + GlobalVersion string = "v0.1.10" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 9 + GlobalVersionPatch uint16 = 10 ) diff --git a/data_escrow.go b/data_escrow.go index b84087b..0f0cbbe 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -34,19 +34,19 @@ func (dl *CDataLinkLayer) EscrowStack() { //// func (dl *CDataLinkLayer) RaiseEvents() { - if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PowerUpEvent { + if acceptor.IsPoweredUp && acceptor.ShouldRaise.PowerUpEvent { dl.log.Msg("Power Up Event Raised") acceptor.ShouldRaise.PowerUpEvent = false } - if dl.Acceptor.isVeryFirstPoll { - dl.Acceptor.isVeryFirstPoll = false + if acceptor.IsVeryFirstPoll { + acceptor.IsVeryFirstPoll = false return } switch acceptor.Device.State { case enum.StateEscrow: - if dl.Acceptor.isPoweredUp && acceptor.ShouldRaise.PUPEscrowEvent { + if acceptor.IsPoweredUp && acceptor.ShouldRaise.PUPEscrowEvent { dl.log.Msg("PUP Escrow Event Raised") acceptor.ShouldRaise.PUPEscrowEvent = false } else if acceptor.ShouldRaise.EscrowEvent { @@ -85,7 +85,7 @@ func (dl *CDataLinkLayer) RaiseEvents() { acceptor.ShouldRaise.StackerFullEvent = false } - if dl.Acceptor.isCheated && acceptor.ShouldRaise.CheatedEvent { + if acceptor.IsCheated && acceptor.ShouldRaise.CheatedEvent { dl.log.Msg("Cheated Event Raised") acceptor.ShouldRaise.CheatedEvent = false } @@ -112,17 +112,17 @@ func (dl *CDataLinkLayer) RaiseEvents() { acceptor.ShouldRaise.PauseClearedEvent = false } - if dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamDetectedEvent { + if acceptor.IsDeviceJammed && acceptor.ShouldRaise.JamDetectedEvent { dl.log.Msg("Jam Detected Event Raised") acceptor.ShouldRaise.JamDetectedEvent = false } - if !dl.Acceptor.isDeviceJammed && acceptor.ShouldRaise.JamClearedEvent { + if !acceptor.IsDeviceJammed && acceptor.ShouldRaise.JamClearedEvent { dl.log.Msg("Jam Cleared Event Raised") acceptor.ShouldRaise.JamClearedEvent = false } - if dl.Acceptor.isInvalidCommand && acceptor.ShouldRaise.InvalidCommandEvent { + if acceptor.IsInvalidCommand && acceptor.ShouldRaise.InvalidCommandEvent { dl.log.Msg("Invalid Command Event Raised") acceptor.ShouldRaise.InvalidCommandEvent = false } diff --git a/data_process.go b/data_process.go index 1136295..0e114d8 100644 --- a/data_process.go +++ b/data_process.go @@ -35,10 +35,10 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusBarCodeReply(reply []byte) { dl.Acceptor.processData5(reply[9]) if acceptor.Device.State == enum.StateEscrow { - dl.Acceptor.barCode = "" + acceptor.BarCode = "" for i := 10; i < 38; i++ { if reply[i] != '(' { - dl.Acceptor.barCode += string(reply[i]) + acceptor.BarCode += string(reply[i]) } else { break } @@ -72,7 +72,7 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.processData3(reply[7]) dl.Acceptor.processData5(reply[9]) - if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !acceptor.WasDocTypeSetOnEscrow) { couponData := ((int(reply[10]) & 0x0F) << 12) + ((int(reply[11]) & 0x0F) << 8) + ((int(reply[12]) & 0x0F) << 4) + @@ -88,6 +88,6 @@ func (dl *CDataLinkLayer) ProcessExtendedOmnibusExpandedCouponReply(reply []byte dl.Acceptor.coupon = NewCCoupon(ownerID, value) dl.Acceptor.docType = enum.DocumentCoupon - dl.Acceptor.wasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow + acceptor.WasDocTypeSetOnEscrow = acceptor.Device.State == enum.StateEscrow } } diff --git a/data_send_replay.go b/data_send_replay.go index 961a06c..3aa8cb6 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -58,9 +58,9 @@ func (dl *CDataLinkLayer) WaitForQuiet() { func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { reply := []byte{} - timeout := dl.Acceptor.transactionTimeout + timeout := acceptor.Timeout.Transaction if acceptor.Device.State == enum.StateDownloadStart || acceptor.Device.State == enum.StateDownloading { - timeout = dl.Acceptor.downloadTimeout + timeout = acceptor.Timeout.Download } dl.Acceptor.port.SetReadTimeout(timeout) @@ -154,7 +154,7 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.ProcessExtendedOmnibusBarCodeReply(reply) case 0x02: dl.ProcessExtendedOmnibusExpandedNoteReply(reply) - if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !dl.Acceptor.wasDocTypeSetOnEscrow) { + if acceptor.Device.State == enum.StateEscrow || (acceptor.Device.State == enum.StateStacked && !acceptor.WasDocTypeSetOnEscrow) { if acceptor.Cap.OrientationExt { switch acceptor.OrientationCtlExt { case enum.OrientationControlOneWay: @@ -176,12 +176,12 @@ func (dl *CDataLinkLayer) ProcessReply(reply []byte) { dl.RaiseEvents() } - if acceptor.Device.State == enum.StateEscrow && dl.Acceptor.autoStack { + if acceptor.Device.State == enum.StateEscrow && acceptor.AutoStack { dl.EscrowStack() acceptor.ShouldRaise.EscrowEvent = false } if acceptor.Device.State != enum.StateEscrow && acceptor.Device.State != enum.StateStacking { - dl.Acceptor.wasDocTypeSetOnEscrow = false + acceptor.WasDocTypeSetOnEscrow = false } } From 8646a2bfbc5c0483149a185d550c52c1c1307321 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 18:09:13 +0200 Subject: [PATCH 10/29] refactoring [v0.1.11] // --- _run/values/ver.txt | 2 +- acceptor_command.go | 2 ++ bill.go | 17 +++++++++++------ const.go | 6 +++--- log.go | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/_run/values/ver.txt b/_run/values/ver.txt index bf42c7d..0034921 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.10 +v0.1.11 diff --git a/acceptor_command.go b/acceptor_command.go index 3e29acb..c0d1d38 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -16,4 +16,6 @@ func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { case <-time.After(30 * time.Second): return nil, errors.New("timeout waiting for response") } + + return nil, errors.New("invalid response") } diff --git a/bill.go b/bill.go index 739914b..ecf117c 100644 --- a/bill.go +++ b/bill.go @@ -81,16 +81,21 @@ func (a *CAcceptor) RetrieveBillTable() { var reply []byte var err error - for { - reply, err = a.SendSynchronousCommand(payload) - if err != nil || len(reply) == 30 { - break + { + for { + reply, err = a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("Error sending command", err) + break + } + if len(reply) == 30 { + break + } + time.Sleep(100 * time.Millisecond) } - time.Sleep(100 * time.Millisecond) } if err != nil || len(reply) != 30 { - a.log.Err("Error sending command", err) break } diff --git a/const.go b/const.go index b12fd2a..7ae571f 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "9c0c999735c186d1c9d4e5020ab952678354eb94" + GlobalHash string = "d4c719422b0bbfcbac83b5fee4662399502c9168" - GlobalVersion string = "v0.1.10" + GlobalVersion string = "v0.1.11" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 10 + GlobalVersionPatch uint16 = 11 ) diff --git a/log.go b/log.go index 0bc693f..e619008 100644 --- a/log.go +++ b/log.go @@ -59,7 +59,7 @@ func (obj *LogStruct) Bytes(msg string, data []byte) { fmt.Fprintf(&sb, "%02X", byteVal) } - obj.log.Debug().Str("data", sb.String()).Msg(msg) + obj.log.Debug().Str("data", sb.String()).Int("len", len(data)).Msg(msg) } // From 6d25b9999ae74ea9cc77a9e5fbe779e4e811be81 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 18:41:00 +0200 Subject: [PATCH 11/29] refactoring [v0.1.11] working protocol --- 0_test.go | 2 +- acceptor.go | 2 ++ acceptor_command.go | 9 +++++++++ bill.go | 4 ++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/0_test.go b/0_test.go index b9cd231..9f646d1 100644 --- a/0_test.go +++ b/0_test.go @@ -8,7 +8,7 @@ import ( func TestName(t *testing.T) { a := NewCAcceptor(30*time.Second, 30*time.Second) - a.Open("/dev/ttyUSB0", enum.PowerUpB) + a.Open("/dev/ttyUSB0", enum.PowerUpE) time.Sleep(30 * time.Second) t.Log(a.GetBNFStatus().String()) diff --git a/acceptor.go b/acceptor.go index dbc3e9b..d9bed42 100644 --- a/acceptor.go +++ b/acceptor.go @@ -36,6 +36,8 @@ type CAcceptor struct { eventHandlers map[enum.EventType]EventHandler log LogStruct + + ss bool } func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor { diff --git a/acceptor_command.go b/acceptor_command.go index c0d1d38..04fbef5 100644 --- a/acceptor_command.go +++ b/acceptor_command.go @@ -8,6 +8,15 @@ import ( //////////////////////////////////// func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { + + //todo неведомая но очень нужная херня + if !a.ss { + a.ss = true + } else { + a.ss = false + payload[0] += 1 + } + a.messageQueue <- NewCMessage(payload, true) select { diff --git a/bill.go b/bill.go index ecf117c..e50f311 100644 --- a/bill.go +++ b/bill.go @@ -72,12 +72,12 @@ func (a *CAcceptor) ParseBillData(reply []byte, extDataIndex int) bill.BillStruc //////// func (a *CAcceptor) RetrieveBillTable() { - index := 1 + var index byte = 1 for { payload := make([]byte, 6) acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, bill.TypeEnables) payload[1] = 0x02 - payload[5] = byte(index) + payload[5] = index var reply []byte var err error From 67b83899ceb79169ae2df37f6cad255cf690a300 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 18:57:41 +0200 Subject: [PATCH 12/29] refactoring [v0.1.12] 11 --- 0_test.go | 3 +-- _run/values/ver.txt | 2 +- acceptor_serial.go | 2 +- acceptor_thread.go | 2 +- const.go | 6 +++--- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/0_test.go b/0_test.go index 9f646d1..9244f54 100644 --- a/0_test.go +++ b/0_test.go @@ -10,8 +10,7 @@ func TestName(t *testing.T) { a := NewCAcceptor(30*time.Second, 30*time.Second) a.Open("/dev/ttyUSB0", enum.PowerUpE) - time.Sleep(30 * time.Second) - t.Log(a.GetBNFStatus().String()) + time.Sleep(5 * time.Second) t.Log(a.GetDeviceSerialNumber()) a.Close() diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 0034921..2b3a1ad 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.11 +v0.1.12 diff --git a/acceptor_serial.go b/acceptor_serial.go index cebb490..20c9f65 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -91,7 +91,7 @@ func (a *CAcceptor) Close() { //// -func (a *CAcceptor) QueryDeviceCapabilities(lg *LogStruct) { +func (a *CAcceptor) QueryDeviceCapabilities() { if !acceptor.IsQueryDeviceCapabilitiesSupported { return } diff --git a/acceptor_thread.go b/acceptor_thread.go index 76561ae..7118eb0 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -19,7 +19,7 @@ func (a *CAcceptor) OpenThread() { } a.dataLinkLayer.ProcessReply(replay) - a.QueryDeviceCapabilities(lg) + a.QueryDeviceCapabilities() if acceptor.Device.State != enum.StateDownloadRestart { a.SetUpBillTable() diff --git a/const.go b/const.go index 7ae571f..bac8aae 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "d4c719422b0bbfcbac83b5fee4662399502c9168" + GlobalHash string = "6d25b9999ae74ea9cc77a9e5fbe779e4e811be81" - GlobalVersion string = "v0.1.11" + GlobalVersion string = "v0.1.12" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 11 + GlobalVersionPatch uint16 = 12 ) From db0cfa265d78800d3c875aeb6b4403bdb3487cd6 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 19:32:12 +0200 Subject: [PATCH 13/29] refactoring [v0.1.13] xx --- 0_test.go | 1 + _run/values/ver.txt | 2 +- acceptor_methods.go | 2 +- const.go | 6 +++--- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/0_test.go b/0_test.go index 9244f54..168d305 100644 --- a/0_test.go +++ b/0_test.go @@ -12,6 +12,7 @@ func TestName(t *testing.T) { time.Sleep(5 * time.Second) t.Log(a.GetDeviceSerialNumber()) + t.Log(a.GetBNFStatus().String()) a.Close() } diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 2b3a1ad..faceed6 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.12 +v0.1.13 diff --git a/acceptor_methods.go b/acceptor_methods.go index 9bb8fef..0d0caef 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -38,7 +38,7 @@ func (a *CAcceptor) GetBNFStatus() enum.BNFStatusType { return enum.BNFStatusUnknown } - payload := []byte{0x02, 0x00, 0x00, 0x03} // Assuming CmdAuxiliary is 0x02 and CmdAuxQueryBNFStatus is 0x03 + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxBNFStatus.Byte()} reply, err := a.SendSynchronousCommand(payload) if err != nil { diff --git a/const.go b/const.go index bac8aae..74400ee 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "6d25b9999ae74ea9cc77a9e5fbe779e4e811be81" + GlobalHash string = "67b83899ceb79169ae2df37f6cad255cf690a300" - GlobalVersion string = "v0.1.12" + GlobalVersion string = "v0.1.13" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 12 + GlobalVersionPatch uint16 = 13 ) From f8e01ba7af318cf8b66406189d7dee42725c47d0 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Fri, 6 Sep 2024 20:54:37 +0200 Subject: [PATCH 14/29] refactoring [v0.1.13] end --- acceptor.go | 4 +-- acceptor/device.go | 20 +++++------ acceptor_serial.go | 45 ++++--------------------- command/command.go | 33 ++++++++++++++++++ data_send_replay.go | 68 ++++++------------------------------- log.go | 8 ++--- serial/errors.go | 13 +++++++ serial/func.go | 26 ++++++++++++++ serial/serial.go | 82 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 185 insertions(+), 114 deletions(-) create mode 100644 command/command.go create mode 100644 serial/errors.go create mode 100644 serial/func.go create mode 100644 serial/serial.go diff --git a/acceptor.go b/acceptor.go index d9bed42..f2b2362 100644 --- a/acceptor.go +++ b/acceptor.go @@ -3,9 +3,9 @@ package mpost import ( "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/enum" + "github.com/hard-soft-ware/mpost/serial" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "go.bug.st/serial" "os" "sync" "time" @@ -16,7 +16,7 @@ import ( type EventHandler func(*CAcceptor, int) type CAcceptor struct { - port serial.Port + port *serial.SerialStruct auditLifeTimeTotals []int auditPerformance []int auditQP []int diff --git a/acceptor/device.go b/acceptor/device.go index 36eb477..c6d8aa2 100644 --- a/acceptor/device.go +++ b/acceptor/device.go @@ -5,17 +5,15 @@ import "github.com/hard-soft-ware/mpost/enum" //////////////////////////////////// type DeviceStruct struct { - Failure bool - Model int - Paused bool - PortName string - PowerUp enum.PowerUpType - Resets int - Revision int - SerialNumber string - Stalled bool - State enum.StateType - Type string + Failure bool + Model int + Paused bool + PowerUp enum.PowerUpType + Resets int + Revision int + Stalled bool + State enum.StateType + Type string } var Device DeviceStruct diff --git a/acceptor_serial.go b/acceptor_serial.go index 20c9f65..0a665b0 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -5,28 +5,26 @@ import ( "github.com/hard-soft-ware/mpost/acceptor" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" - "go.bug.st/serial" - "time" + "github.com/hard-soft-ware/mpost/serial" ) //////////////////////////////////// func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { - lg := a.log.New("OpenSerial") - if acceptor.Connected { - lg.Msg("already connected") + a.log.Msg("already connected") return nil } - acceptor.Device.PortName = portName acceptor.Device.PowerUp = powerUp - err := a.OpenPort(lg) + port, err := serial.Open(portName, &acceptor.Connected) if err != nil { a.log.Err("failed to open serial port", err) return err } + a.port = port + a.log.Msg("Serial Open") go a.MessageLoopThread() go a.OpenThread() @@ -34,38 +32,6 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { return nil } -func (a *CAcceptor) OpenPort(lg *LogStruct) error { - mode := &serial.Mode{ - BaudRate: 9600, - DataBits: 7, - Parity: serial.EvenParity, - StopBits: serial.OneStopBit, - } - - port, err := serial.Open(acceptor.Device.PortName, mode) - if err != nil { - return err - } - - port.SetReadTimeout(100 * time.Millisecond) - port.ResetInputBuffer() - - port.SetDTR(false) - port.SetRTS(true) - time.Sleep(100 * time.Millisecond) - - port.SetDTR(true) - port.SetRTS(false) - time.Sleep(5 * time.Millisecond) - - port.ResetInputBuffer() - a.port = port - - acceptor.Connected = true - lg.Msg("Connected") - return nil -} - func (a *CAcceptor) Close() { if !acceptor.Connected { @@ -87,6 +53,7 @@ func (a *CAcceptor) Close() { a.port.Close() a.port = nil acceptor.Connected = false + a.log.Msg("Close") } //// diff --git a/command/command.go b/command/command.go new file mode 100644 index 0000000..211114c --- /dev/null +++ b/command/command.go @@ -0,0 +1,33 @@ +package command + +import ( + "github.com/hard-soft-ware/mpost/consts" +) + +//////////////////////////////////// + +func CRC(command []byte) byte { + var result byte + + end := int(command[1]) - 2 + for i := 1; i < end; i++ { + result ^= command[i] + } + + return result +} + +func Create(payload []byte) []byte { + commandLength := len(payload) + 4 // STX + Length char + ETX + Checksum + + command := make([]byte, 0, commandLength) + command = append(command, consts.DataSTX.Byte()) + command = append(command, byte(commandLength)) + + command = append(command, payload...) + + command = append(command, consts.DataETX.Byte()) + command = append(command, CRC(command)) + + return command +} diff --git a/data_send_replay.go b/data_send_replay.go index 3aa8cb6..3dd3c38 100644 --- a/data_send_replay.go +++ b/data_send_replay.go @@ -4,51 +4,27 @@ import ( "bufio" "errors" "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/command" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "io" - "time" ) //////////////////////////////////// func (dl *CDataLinkLayer) SendPacket(payload []byte) { - payloadLength := len(payload) - commandLength := payloadLength + 4 // STX + Length char + ETX + Checksum + send := command.Create(payload) - command := make([]byte, 0, commandLength) - command = append(command, consts.DataSTX.Byte()) - command = append(command, byte(commandLength)) + dl.CurrentCommand = send + dl.EchoDetect = send - command = append(command, payload...) - command[2] |= dl.AckToggleBit - - command = append(command, consts.DataETX.Byte()) - command = append(command, dl.ComputeCheckSum(command)) - - dl.CurrentCommand = command - dl.EchoDetect = command - - dl.log.Bytes("SERIAL SEND >>> ", command) - n, err := dl.Acceptor.port.Write(command) + dl.log.Bytes("SERIAL SEND >>> ", send) + n, err := dl.Acceptor.port.Write(send) if err != nil || n == 0 { dl.log.Err("Failed to write to port", err) - - dl.Acceptor.port.Close() - dl.Acceptor.OpenPort(dl.log) - } -} - -func (dl *CDataLinkLayer) WaitForQuiet() { - for { - buf := make([]byte, 1) - timeout := 20 * time.Millisecond - - dl.Acceptor.port.SetReadTimeout(timeout) - - _, err := dl.Acceptor.port.Read(buf) + err = dl.Acceptor.port.Restart() if err != nil { - return + dl.log.Err("Failed restart to port", err) } } } @@ -63,9 +39,9 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { timeout = acceptor.Timeout.Download } - dl.Acceptor.port.SetReadTimeout(timeout) + dl.Acceptor.port.SetTimeout(timeout) - reader := bufio.NewReader(dl.Acceptor.port) + reader := bufio.NewReader(dl.Acceptor.port.Port()) stxAndLength := make([]byte, 2) bytesRead, err := io.ReadFull(reader, stxAndLength) if err != nil { @@ -108,30 +84,6 @@ func (dl *CDataLinkLayer) ReceiveReply() ([]byte, error) { //// -func (dl *CDataLinkLayer) ReplyAcked(reply []byte) bool { - if len(reply) < 3 { - return false - } - - if (reply[2] & consts.DataACKMask.Byte()) == dl.AckToggleBit { - dl.AckToggleBit ^= 0x01 // Переключаем бит подтверждения - - dl.NakCount = 0 - - return true - } else { - dl.NakCount++ - - // Если получено 8 последовательных NAK, принудительно переключаем бит - if dl.NakCount == 8 { - dl.AckToggleBit ^= 0x01 - dl.NakCount = 0 - } - - return false - } -} - func (dl *CDataLinkLayer) ProcessReply(reply []byte) { if len(reply) < 3 { return diff --git a/log.go b/log.go index e619008..79e1789 100644 --- a/log.go +++ b/log.go @@ -36,14 +36,14 @@ func (obj *LogStruct) Msg(msg string) { if !obj.isEnable { return } - obj.log.Debug().Msg(msg) + obj.log.Debug().Str("index", obj.index).Msg(msg) } func (obj *LogStruct) Err(msg string, err error) { if !obj.isEnable { return } - obj.log.Debug().Err(err).Msg(msg) + obj.log.Debug().Str("index", obj.index).Err(err).Msg(msg) } func (obj *LogStruct) Bytes(msg string, data []byte) { @@ -59,11 +59,11 @@ func (obj *LogStruct) Bytes(msg string, data []byte) { fmt.Fprintf(&sb, "%02X", byteVal) } - obj.log.Debug().Str("data", sb.String()).Int("len", len(data)).Msg(msg) + obj.log.Debug().Str("index", obj.index).Str("data", sb.String()).Int("len", len(data)).Msg(msg) } // func (obj *LogStruct) Event(event enum.EventType) { - obj.log.Debug().Str("event", event.String()).Msg("Event") + obj.log.Debug().Str("index", obj.index).Str("event", event.String()).Msg("Event") } diff --git a/serial/errors.go b/serial/errors.go new file mode 100644 index 0000000..b349af2 --- /dev/null +++ b/serial/errors.go @@ -0,0 +1,13 @@ +package serial + +import "errors" + +//////////////////////////////////// + +var ( + ErrNotConnect = errors.New("not connect") + ErrFailSendCommand = errors.New("fail send command") + + ErrReceivedInsufficientBytes = errors.New("received insufficient bytes") + ErrInvalidSTX = errors.New("invalid STX") +) diff --git a/serial/func.go b/serial/func.go new file mode 100644 index 0000000..a8bd33f --- /dev/null +++ b/serial/func.go @@ -0,0 +1,26 @@ +package serial + +import ( + "go.bug.st/serial" + "time" +) + +//////////////////////////////////// + +func (obj *SerialStruct) Port() serial.Port { + return obj.port +} + +func (obj *SerialStruct) Write(p []byte) (n int, err error) { + if !*obj.connect { + return 0, ErrNotConnect + } + return obj.port.Write(p) +} + +func (obj *SerialStruct) SetTimeout(t time.Duration) error { + if !*obj.connect { + return ErrNotConnect + } + return obj.port.SetReadTimeout(t) +} diff --git a/serial/serial.go b/serial/serial.go new file mode 100644 index 0000000..fff64e4 --- /dev/null +++ b/serial/serial.go @@ -0,0 +1,82 @@ +package serial + +import ( + "go.bug.st/serial" + "time" +) + +//////////////////////////////////// + +type SerialStruct struct { + PortName string + connect *bool + + port serial.Port + mode *serial.Mode + + posCoin bool +} + +func Open(portName string, connectStatus *bool) (*SerialStruct, error) { + obj := SerialStruct{} + obj.PortName = portName + obj.connect = connectStatus + + obj.mode = &serial.Mode{ + BaudRate: 9600, + DataBits: 7, + Parity: serial.EvenParity, + StopBits: serial.OneStopBit, + } + + err := obj.open() + if err != nil { + return nil, err + } + + return &obj, nil +} + +func (obj *SerialStruct) open() error { + port, err := serial.Open(obj.PortName, obj.mode) + if err != nil { + return err + } + + port.SetReadTimeout(100 * time.Millisecond) + port.ResetInputBuffer() + + port.SetDTR(false) + port.SetRTS(true) + time.Sleep(100 * time.Millisecond) + + port.SetDTR(true) + port.SetRTS(false) + time.Sleep(5 * time.Millisecond) + + port.GetModemStatusBits() + + port.ResetInputBuffer() + obj.port = port + *obj.connect = true + return nil +} + +func (obj *SerialStruct) Restart() error { + if *obj.connect { + obj.port.Close() + time.Sleep(100 * time.Millisecond) + } + + err := obj.open() + if err != nil { + return err + } + + return nil +} + +func (obj *SerialStruct) Close() error { + *obj.connect = false + return obj.port.Close() +} From 79d1e21214a3630b6f76f423c2b5599483e1c367 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 20:00:02 +0200 Subject: [PATCH 15/29] refactoring [v0.1.13] trigs --- 0_test.go | 14 ++++++++++++-- acceptor.go | 6 ++++++ acceptor_loop.go | 4 +--- acceptor_raise.go | 8 ++++++++ acceptor_serial.go | 1 + acceptor_thread.go | 8 ++------ data.go | 2 -- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/0_test.go b/0_test.go index 168d305..5c246be 100644 --- a/0_test.go +++ b/0_test.go @@ -1,18 +1,28 @@ package mpost import ( + "fmt" "github.com/hard-soft-ware/mpost/enum" "testing" "time" ) -func TestName(t *testing.T) { +func TestConnect(t *testing.T) { a := NewCAcceptor(30*time.Second, 30*time.Second) + + a.AddHook(enum.EventConnected, func(acceptor *CAcceptor, i int) { + fmt.Println("Connect") + }) + a.AddHook(enum.EventDisconnected, func(acceptor *CAcceptor, i int) { + fmt.Println("Disconnect") + }) + a.Open("/dev/ttyUSB0", enum.PowerUpE) - time.Sleep(5 * time.Second) + time.Sleep(2 * time.Second) t.Log(a.GetDeviceSerialNumber()) t.Log(a.GetBNFStatus().String()) a.Close() + time.Sleep(1 * time.Second) } diff --git a/acceptor.go b/acceptor.go index f2b2362..2d048f7 100644 --- a/acceptor.go +++ b/acceptor.go @@ -75,3 +75,9 @@ func (a *CAcceptor) getTickCount() int64 { func (a *CAcceptor) SetEventHandler(event enum.EventType, eventHandler func(*CAcceptor, int)) { a.eventHandlers[event] = eventHandler } + +// + +func (a *CAcceptor) AddHook(ev enum.EventType, h EventHandler) { + a.eventHandlers[ev] = h +} diff --git a/acceptor_loop.go b/acceptor_loop.go index c045ebd..be058d0 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -21,9 +21,7 @@ func (a *CAcceptor) PollingLoop(lg *LogStruct) []byte { } if time.Since(startTickCount) > PollingDisconnectTimeout { - if acceptor.ShouldRaise.DisconnectedEvent { - a.RaiseDisconnectedEvent() - } + a.RaiseDisconnectedEvent() startTickCount = time.Now() } diff --git a/acceptor_raise.go b/acceptor_raise.go index 5ec9fc1..5568bdc 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -17,12 +17,20 @@ func (a *CAcceptor) raiseXX(e enum.EventType, b int) { //// func (a *CAcceptor) RaiseConnectedEvent() { + if acceptor.ShouldRaise.ConnectedEvent { + return + } + a.raiseXX(enum.EventConnected, 0) acceptor.ShouldRaise.DisconnectedEvent = true acceptor.ShouldRaise.ConnectedEvent = false } func (a *CAcceptor) RaiseDisconnectedEvent() { + if acceptor.ShouldRaise.DisconnectedEvent { + return + } + a.raiseXX(enum.EventDisconnected, 0) acceptor.ShouldRaise.DisconnectedEvent = false acceptor.ShouldRaise.ConnectedEvent = true diff --git a/acceptor_serial.go b/acceptor_serial.go index 0a665b0..74e704d 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -51,6 +51,7 @@ func (a *CAcceptor) Close() { a.workerThread.Wait() a.port.Close() + a.RaiseDisconnectedEvent() a.port = nil acceptor.Connected = false a.log.Msg("Close") diff --git a/acceptor_thread.go b/acceptor_thread.go index 7118eb0..a4bd6ef 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -24,9 +24,7 @@ func (a *CAcceptor) OpenThread() { if acceptor.Device.State != enum.StateDownloadRestart { a.SetUpBillTable() acceptor.Connected = true - if acceptor.ShouldRaise.ConnectedEvent { - a.RaiseConnectedEvent() - } + a.RaiseConnectedEvent() } else { a.RaiseDownloadRestartEvent() } @@ -50,9 +48,7 @@ func (a *CAcceptor) MessageLoopThread() { if time.Since(timeoutStart) > 30*time.Second { if acceptor.Device.State != enum.StateDownloading && acceptor.Device.State != enum.StateDownloadRestart { acceptor.Connected = false - if acceptor.ShouldRaise.DisconnectedEvent { - a.RaiseDisconnectedEvent() - } + a.RaiseDisconnectedEvent() acceptor.WasDisconnected = true timeoutStart = time.Now() } diff --git a/data.go b/data.go index 94603a5..f2d79f7 100644 --- a/data.go +++ b/data.go @@ -6,8 +6,6 @@ type CDataLinkLayer struct { log *LogStruct Acceptor *CAcceptor - AckToggleBit byte - NakCount uint8 CurrentCommand, EchoDetect []byte PreviousCommand, PreviousReply []byte IdenticalCommandAndReplyCount int From 52a057205c73d064edd85bb9e9f38cf5d2678585 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 20:07:34 +0200 Subject: [PATCH 16/29] refactoring [v0.1.13] ff --- acceptor.go | 10 ---------- acceptor_command.go | 30 ------------------------------ acceptor_loop.go | 2 +- acceptor_message.go | 29 +++++++++++++++++++++++++++++ acceptor_poll.go | 22 ---------------------- acceptor_process.go | 2 +- acceptor_thread.go | 2 +- 7 files changed, 32 insertions(+), 65 deletions(-) delete mode 100644 acceptor_command.go delete mode 100644 acceptor_poll.go diff --git a/acceptor.go b/acceptor.go index 2d048f7..747cc3c 100644 --- a/acceptor.go +++ b/acceptor.go @@ -68,16 +68,6 @@ func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor // -func (a *CAcceptor) getTickCount() int64 { - return time.Now().UnixNano() / int64(time.Millisecond) -} - -func (a *CAcceptor) SetEventHandler(event enum.EventType, eventHandler func(*CAcceptor, int)) { - a.eventHandlers[event] = eventHandler -} - -// - func (a *CAcceptor) AddHook(ev enum.EventType, h EventHandler) { a.eventHandlers[ev] = h } diff --git a/acceptor_command.go b/acceptor_command.go deleted file mode 100644 index 04fbef5..0000000 --- a/acceptor_command.go +++ /dev/null @@ -1,30 +0,0 @@ -package mpost - -import ( - "errors" - "time" -) - -//////////////////////////////////// - -func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { - - //todo неведомая но очень нужная херня - if !a.ss { - a.ss = true - } else { - a.ss = false - payload[0] += 1 - } - - a.messageQueue <- NewCMessage(payload, true) - - select { - case reply := <-a.replyQueue: - return reply, nil - case <-time.After(30 * time.Second): - return nil, errors.New("timeout waiting for response") - } - - return nil, errors.New("invalid response") -} diff --git a/acceptor_loop.go b/acceptor_loop.go index be058d0..61f5f3b 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -9,7 +9,7 @@ import ( //////////////////////////////////// -func (a *CAcceptor) PollingLoop(lg *LogStruct) []byte { +func (a *CAcceptor) PollingLoop() []byte { startTickCount := time.Now() for { diff --git a/acceptor_message.go b/acceptor_message.go index d9ca105..8155786 100644 --- a/acceptor_message.go +++ b/acceptor_message.go @@ -1,5 +1,10 @@ package mpost +import ( + "errors" + "time" +) + //////////////////////////////////// type CMessage struct { @@ -15,3 +20,27 @@ func NewCMessage(payload []byte, isSynchronous bool) *CMessage { IsSynchronous: isSynchronous, } } + +//// + +func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { + + //todo упрощенная версия согласования + if !a.ss { + a.ss = true + } else { + a.ss = false + payload[0] += 1 + } + + a.messageQueue <- NewCMessage(payload, true) + + select { + case reply := <-a.replyQueue: + return reply, nil + case <-time.After(30 * time.Second): + return nil, errors.New("timeout waiting for response") + } + + return nil, errors.New("invalid response") +} diff --git a/acceptor_poll.go b/acceptor_poll.go deleted file mode 100644 index 07e2086..0000000 --- a/acceptor_poll.go +++ /dev/null @@ -1,22 +0,0 @@ -package mpost - -import "github.com/hard-soft-ware/mpost/acceptor" - -//////////////////////////////////// - -type CSuppressStandardPoll struct { - acceptor *CAcceptor -} - -func NewCSuppressStandardPoll(a *CAcceptor) *CSuppressStandardPoll { - s := &CSuppressStandardPoll{ - acceptor: a, - } - - acceptor.SuppressStandardPoll = true - return s -} - -func (s *CSuppressStandardPoll) Release() { - acceptor.SuppressStandardPoll = false -} diff --git a/acceptor_process.go b/acceptor_process.go index 60ddbdb..b41d16a 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -180,7 +180,7 @@ func (a *CAcceptor) processData3(data3 byte) { } func (a *CAcceptor) processData4(data4 byte) { - acceptor.Device.Model = int(data4 & 0x7F) //todo проверить валидность перевода + acceptor.Device.Model = int(data4 & 0x7F) m := acceptor.Device.Model d := m diff --git a/acceptor_thread.go b/acceptor_thread.go index a4bd6ef..12ecf22 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -11,7 +11,7 @@ import ( func (a *CAcceptor) OpenThread() { lg := a.log.New("Thread") - replay := a.PollingLoop(lg) + replay := a.PollingLoop() if acceptor.WasStopped { lg.Msg("thread is stopped") From 676008c426a3d37cd1371da7d9cb62d5e36d0337 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 20:38:24 +0200 Subject: [PATCH 17/29] refactoring [v0.1.13] merging some methods --- acceptor_message.go | 13 +- acceptor_methods.go | 321 ++++++++++++++++++++++++++++++++++++++++---- data_escrow.go | 2 +- 3 files changed, 307 insertions(+), 29 deletions(-) diff --git a/acceptor_message.go b/acceptor_message.go index 8155786..e6f5cd5 100644 --- a/acceptor_message.go +++ b/acceptor_message.go @@ -24,8 +24,6 @@ func NewCMessage(payload []byte, isSynchronous bool) *CMessage { //// func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { - - //todo упрощенная версия согласования if !a.ss { a.ss = true } else { @@ -44,3 +42,14 @@ func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { return nil, errors.New("invalid response") } + +func (a *CAcceptor) SendAsynchronousCommand(payload []byte) { + if !a.ss { + a.ss = true + } else { + a.ss = false + payload[0] += 1 + } + + a.messageQueue <- NewCMessage(payload, false) +} diff --git a/acceptor_methods.go b/acceptor_methods.go index 0d0caef..0e99f4a 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -1,8 +1,10 @@ package mpost import ( + "errors" "fmt" "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/bill" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" ) @@ -29,6 +31,299 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st //// +func (a *CAcceptor) GetDeviceSerialNumber() string { + a.log.Msg("GetDeviceSerialNumber") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") + if err != nil { + a.log.Err("GetDeviceSerialNumber", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetDeviceSerialNumber", err) + return "" + } + + validCharIndex := 3 + for validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 22 { + validCharIndex++ + } + returnedStringLength := validCharIndex - 3 + + s := string(reply[3 : 3+returnedStringLength]) + return s +} + +func (a *CAcceptor) GetApplicationID() string { + a.log.Msg("GetApplicationID") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.ApplicationID, "GetApplicationID") + if err != nil { + a.log.Err("GetApplicationID", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorApplicationID.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetApplicationID", err) + return "" + } + + if len(reply) == 14 { + s := string(reply[3:12]) + return s + } + + return "" +} + +func (a *CAcceptor) GetApplicationPN() string { + a.log.Msg("GetApplicationPN") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.ApplicationPN, "ApplicationPN") + if err != nil { + a.log.Err("ApplicationPN", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorApplicationPartNumber.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetApplicationPN", err) + return "" + } + + if len(reply) == 14 { + s := string(reply[3:12]) + return s + } + + return "" +} + +func (a *CAcceptor) GetAuditLifeTimeTotals() []int { + a.log.Msg("GetAuditLifeTimeTotals") + values := []int{} + + err := a.verifyPropertyIsAllowed(acceptor.Cap.Audit, "GetAuditLifeTimeTotals") + if err != nil { + a.log.Err("GetAuditLifeTimeTotals", err) + return values + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorAuditLifeTimeTotals.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetAuditLifeTimeTotals", err) + return values + } + + if len(reply) < 13 || ((len(reply)-5)%8 != 0) { + return values + } + + fieldCount := (len(reply) - 5) / 8 + for i := 0; i < fieldCount; i++ { + offset := 8*i + 3 + value := (int((reply)[offset+0]&0x0F) << 28) + + (int((reply)[offset+1]&0x0F) << 24) + + (int((reply)[offset+2]&0x0F) << 20) + + (int((reply)[offset+3]&0x0F) << 16) + + (int((reply)[offset+4]&0x0F) << 12) + + (int((reply)[offset+5]&0x0F) << 8) + + (int((reply)[offset+6]&0x0F) << 4) + + int((reply)[offset+7]&0x0F) + + values = append(values, value) + } + + return values +} + +func (a *CAcceptor) GetAuditPerformance() []int { + a.log.Msg("GetAuditPerformance") + values := []int{} + + err := a.verifyPropertyIsAllowed(acceptor.Cap.Audit, "GetAuditPerformance") + if err != nil { + a.log.Err("GetAuditPerformance", err) + return values + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorAuditPerformanceMeasures.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetAuditPerformance", err) + return values + } + + if len(reply) < 9 || ((len(reply)-5)%4 != 0) { + return values + } + + fieldCount := (len(reply) - 5) / 4 + + for i := 0; i < fieldCount; i++ { + offset := 4*i + 3 + value := (int((reply)[offset+0]&0x0F) << 12) + + (int((reply)[offset+1]&0x0F) << 8) + + (int((reply)[offset+2]&0x0F) << 4) + + int((reply)[offset+3]&0x0F) + + values = append(values, value) + } + + return values +} + +func (a *CAcceptor) GetAuditQP() []int { + a.log.Msg("GetAuditQP") + values := []int{} + + err := a.verifyPropertyIsAllowed(acceptor.Cap.Audit, "GetAuditQP") + if err != nil { + a.log.Err("GetAuditQP", err) + return values + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorAuditQPMeasures.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetAuditQP", err) + return values + } + + if len(reply) < 9 || ((len(reply)-5)%4 != 0) { + return values + } + + fieldCount := (len(reply) - 5) / 4 + + for i := 0; i < fieldCount; i++ { + offset := 4*i + 3 + value := (int(reply[offset+0]&0x0F) << 12) + + (int(reply[offset+1]&0x0F) << 8) + + (int(reply[offset+2]&0x0F) << 4) + + int(reply[offset+3]&0x0F) + + values = append(values, value) + } + + return values +} + +func (a *CAcceptor) GetAutoStack() bool { + a.log.Msg("GetAutoStack") + return acceptor.AutoStack +} + +func (a *CAcceptor) SetAutoStack(newVal bool) { + a.log.Msg("SetAutoStack") + acceptor.AutoStack = newVal +} + +func (a *CAcceptor) GetBarCode() string { + a.log.Msg("GetBarCode") + return acceptor.BarCode +} + +func (a *CAcceptor) GetBill() bill.BillStruct { + a.log.Msg("GetBill") + return bill.Bill +} + +func (a *CAcceptor) GetBillTypes() []bill.BillStruct { + a.log.Msg("GetBillTypes") + return bill.Types +} + +func (a *CAcceptor) GetBillTypeEnables() []bool { + a.log.Msg("GetBillTypeEnables") + return bill.TypeEnables +} + +func (a *CAcceptor) SetBillTypeEnables(newVal []bool) { + a.log.Msg("SetBillTypeEnables") + + if !acceptor.Connected { + a.log.Err("SetBillTypeEnables", errors.New("calling BillTypeEnables not allowed when not connected")) + return + } + + if len(bill.TypeEnables) != len(bill.Types) { + a.log.Err("SetBillTypeEnables", fmt.Errorf("CBillTypeEnables size must match BillTypes size")) + return + } + + bill.TypeEnables = newVal + + if acceptor.ExpandedNoteReporting { + payload := make([]byte, 15) + acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, bill.TypeEnables) + payload[1] = 0x03 // Sub Type + + for i, enable := range bill.TypeEnables { + enableIndex := i / 7 + bitPosition := i % 7 + bit := 1 << bitPosition + + if enable { + payload[5+enableIndex] |= byte(bit) + } + } + + a.SendAsynchronousCommand(payload) + } +} + +func (a *CAcceptor) GetBillValues() []bill.BillStruct { + a.log.Msg("GetBillValues") + return bill.Values +} + +func (a *CAcceptor) GetBillValueEnables() []bool { + a.log.Msg("GetBillValueEnables") + return bill.ValueEnables +} + +func (a *CAcceptor) SetBillValueEnables(newVal []bool) { + a.log.Msg("SetBillValueEnables") + bill.ValueEnables = newVal + + for _, enabled := range bill.ValueEnables { + for j, billType := range bill.Types { + if billType.Value == bill.Values[j].Value && billType.Country == bill.Values[j].Country { + bill.TypeEnables[j] = enabled + } + } + } + + payload := make([]byte, 15) + acceptor.ConstructOmnibusCommand(payload, consts.CmdExpanded, 2, bill.TypeEnables) + payload[1] = 0x03 // Sub Type + + for i, enable := range bill.TypeEnables { + enableIndex := i / 7 + bitPosition := i % 7 + bit := 1 << bitPosition + + if enable { + payload[5+enableIndex] |= byte(bit) + } + } + + a.SendAsynchronousCommand(payload) +} + func (a *CAcceptor) GetBNFStatus() enum.BNFStatusType { a.log.Msg("Getting BNF status") err := a.verifyPropertyIsAllowed(acceptor.Cap.BNFStatus, "BNFStatus") @@ -60,29 +355,3 @@ func (a *CAcceptor) GetBNFStatus() enum.BNFStatusType { return enum.BNFStatusUnknown } - -func (a *CAcceptor) GetDeviceSerialNumber() string { - a.log.Msg("GetDeviceSerialNumber") - - err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") - if err != nil { - a.log.Err("GetDeviceSerialNumber", err) - return "" - } - - payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} - reply, err := a.SendSynchronousCommand(payload) - if err != nil { - a.log.Err("GetDeviceSerialNumber", err) - return "" - } - - validCharIndex := 3 - for validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 22 { - validCharIndex++ - } - returnedStringLength := validCharIndex - 3 - - s := string(reply[3 : 3+returnedStringLength]) - return s -} diff --git a/data_escrow.go b/data_escrow.go index 0f0cbbe..799d5c2 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -20,7 +20,7 @@ func (dl *CDataLinkLayer) escrowXX(b byte) { payload[2] |= b - dl.Acceptor.messageQueue <- NewCMessage(payload, false) + dl.Acceptor.SendAsynchronousCommand(payload) } func (dl *CDataLinkLayer) EscrowReturn() { From d22ff81192f273eaee6d7b8434f04cd857518879 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 20:50:19 +0200 Subject: [PATCH 18/29] refactoring [v0.1.13] merging some methods 2 --- acceptor_methods.go | 201 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/acceptor_methods.go b/acceptor_methods.go index 0e99f4a..633bd62 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -355,3 +355,204 @@ func (a *CAcceptor) GetBNFStatus() enum.BNFStatusType { return enum.BNFStatusUnknown } + +func (a *CAcceptor) GetBootPN() string { + a.log.Msg("GetBootPN") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.BootPN, "GetBootPN") + if err != nil { + a.log.Err("GetBootPN", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorBootPartNumber.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetBootPN", err) + return "" + } + + if len(reply) == 14 { + s := string(reply[3:12]) // Extracting the substring from byte slice + return s + } + + return "" +} + +func (a *CAcceptor) GetCapApplicationID() bool { + a.log.Msg("GetCapApplicationID") + return acceptor.Cap.ApplicationID +} + +func (a *CAcceptor) GetCapApplicationPN() bool { + a.log.Msg("GetCapApplicationPN") + return acceptor.Cap.ApplicationPN +} + +func (a *CAcceptor) GetCapAssetNumber() bool { + a.log.Msg("GetCapAssetNumber") + return acceptor.Cap.AssetNumber +} + +func (a *CAcceptor) GetCapAudit() bool { + a.log.Msg("GetCapAudit") + return acceptor.Cap.Audit +} + +func (a *CAcceptor) GetCapBarCodes() bool { + a.log.Msg("GetCapBarCodes") + return acceptor.Cap.BarCodes +} + +func (a *CAcceptor) GetCapBarCodesExt() bool { + a.log.Msg("GetCapBarCodesExt") + return acceptor.Cap.BarCodesExt +} + +func (a *CAcceptor) GetCapBNFStatus() bool { + a.log.Msg("GetCapBNFStatus") + return acceptor.Cap.BNFStatus +} + +func (a *CAcceptor) GetCapBookmark() bool { + a.log.Msg("GetCapBookmark") + return acceptor.Cap.Bookmark +} + +func (a *CAcceptor) GetCapBootPN() bool { + a.log.Msg("GetCapBootPN") + return acceptor.Cap.BootPN +} + +func (a *CAcceptor) GetCapCalibrate() bool { + a.log.Msg("GetCapCalibrate") + return acceptor.Cap.Calibrate +} + +func (a *CAcceptor) GetCapCashBoxTotal() bool { + a.log.Msg("GetCapCashBoxTotal") + return acceptor.Cap.CashBoxTotal +} + +func (a *CAcceptor) GetCapCouponExt() bool { + a.log.Msg("GetCapCouponExt") + return acceptor.Cap.CouponExt +} + +func (a *CAcceptor) GetCapDevicePaused() bool { + a.log.Msg("GetCapDevicePaused") + return acceptor.Cap.DevicePaused +} + +func (a *CAcceptor) GetCapDeviceSoftReset() bool { + a.log.Msg("GetCapDeviceSoftReset") + return acceptor.Cap.DeviceSoftReset +} + +func (a *CAcceptor) GetCapDeviceType() bool { + a.log.Msg("GetCapDeviceType") + return acceptor.Cap.DeviceType +} + +func (a *CAcceptor) GetCapDeviceResets() bool { + a.log.Msg("GetCapDeviceResets") + return acceptor.Cap.DeviceResets +} + +func (a *CAcceptor) GetCapDeviceSerialNumber() bool { + a.log.Msg("GetCapDeviceSerialNumber") + return acceptor.Cap.DeviceSerialNumber +} + +func (a *CAcceptor) GetCapEscrowTimeout() bool { + a.log.Msg("GetCapEscrowTimeout") + return acceptor.Cap.EscrowTimeout +} + +func (a *CAcceptor) GetCapFlashDownload() bool { + a.log.Msg("GetCapFlashDownload") + return acceptor.Cap.FlashDownload +} + +func (a *CAcceptor) GetCapNoPush() bool { + a.log.Msg("GetCapNoPush") + return acceptor.Cap.NoPush +} + +func (a *CAcceptor) GetCapOrientationExt() bool { + a.log.Msg("GetCapOrientationExt") + return acceptor.Cap.OrientationExt +} + +func (a *CAcceptor) GetCapPupExt() bool { + a.log.Msg("GetCapPupExt") + return acceptor.Cap.PupExt +} + +func (a *CAcceptor) GetCapTestDoc() bool { + a.log.Msg("GetCapTestDoc") + return acceptor.Cap.TestDoc +} + +func (a *CAcceptor) GetCapVariantID() bool { + a.log.Msg("GetCapVariantID") + return acceptor.Cap.VariantID +} + +func (a *CAcceptor) GetCapVariantPN() bool { + a.log.Msg("GetCapVariantPN") + return acceptor.Cap.VariantPN +} + +func (a *CAcceptor) GetCashBoxAttached() bool { + a.log.Msg("GetCashBoxAttached") + return acceptor.Cash.BoxAttached +} + +func (a *CAcceptor) GetCashBoxFull() bool { + a.log.Msg("GetCashBoxFull") + return acceptor.Cash.BoxFull +} + +func (a *CAcceptor) GetCashBoxTotal() int64 { + a.log.Msg("GetCashBoxTotal") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.CashBoxTotal, "GetCashBoxTotal") + if err != nil { + a.log.Err("GetCashBoxTotal", err) + return 0 + } + + payload := []byte{consts.CmdOmnibus.Byte(), 0x7F, 0x3C, 0x02} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetCashBoxTotal", err) + return 0 + } + + if len(reply) < 9 { + return 0 + } + + total := int64(reply[3]&0x0F)<<20 | + int64(reply[4]&0x0F)<<16 | + int64(reply[5]&0x0F)<<12 | + int64(reply[6]&0x0F)<<8 | + int64(reply[7]&0x0F)<<4 | + int64(reply[8]&0x0F) + + return total +} + +func (a *CAcceptor) GetConnected() bool { + a.log.Msg("GetConnected") + return acceptor.Connected +} + +func (a *CAcceptor) GetCoupon() *CCoupon { + a.log.Msg("GetCoupon") + return a.coupon +} From 91ce49be3bb34436ffbda30781a72e51907aaa0e Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 21:29:01 +0200 Subject: [PATCH 19/29] refactoring [v0.1.13] merging some methods 3 --- acceptor/acceptor.go | 1 - acceptor/device.go | 2 + acceptor_methods.go | 151 +++++++++++++++++++++++++++++++++++-------- acceptor_process.go | 4 +- data_escrow.go | 4 +- 5 files changed, 131 insertions(+), 31 deletions(-) diff --git a/acceptor/acceptor.go b/acceptor/acceptor.go index 42842cc..3775cee 100644 --- a/acceptor/acceptor.go +++ b/acceptor/acceptor.go @@ -23,7 +23,6 @@ var InSoftResetOneSecondIgnore bool var InSoftResetWaitForReply bool var ExpandedNoteReporting bool var IsQueryDeviceCapabilitiesSupported bool -var IsDeviceJammed bool var IsCheated bool var IsPoweredUp bool var IsInvalidCommand bool diff --git a/acceptor/device.go b/acceptor/device.go index c6d8aa2..20f5944 100644 --- a/acceptor/device.go +++ b/acceptor/device.go @@ -14,6 +14,8 @@ type DeviceStruct struct { Stalled bool State enum.StateType Type string + + Jammed bool } var Device DeviceStruct diff --git a/acceptor_methods.go b/acceptor_methods.go index 633bd62..21d8eee 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -31,32 +31,6 @@ func (a *CAcceptor) verifyPropertyIsAllowed(capabilityFlag bool, propertyName st //// -func (a *CAcceptor) GetDeviceSerialNumber() string { - a.log.Msg("GetDeviceSerialNumber") - - err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") - if err != nil { - a.log.Err("GetDeviceSerialNumber", err) - return "" - } - - payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} - reply, err := a.SendSynchronousCommand(payload) - if err != nil { - a.log.Err("GetDeviceSerialNumber", err) - return "" - } - - validCharIndex := 3 - for validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 22 { - validCharIndex++ - } - returnedStringLength := validCharIndex - 3 - - s := string(reply[3 : 3+returnedStringLength]) - return s -} - func (a *CAcceptor) GetApplicationID() string { a.log.Msg("GetApplicationID") @@ -556,3 +530,128 @@ func (a *CAcceptor) GetCoupon() *CCoupon { a.log.Msg("GetCoupon") return a.coupon } + +func (a *CAcceptor) GetDeviceBusy() bool { + a.log.Msg("GetDeviceBusy") + return acceptor.Device.State != enum.StateIdling +} + +func (a *CAcceptor) GetDeviceCRC() int64 { + a.log.Msg("GetDeviceCRC") + + err := a.verifyPropertyIsAllowed(true, "DeviceCRC") + if err != nil { + a.log.Err("GetDeviceCRC", err) + return 0 + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxSoftwareCRC.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetDeviceCRC", err) + return 0 + } + + if len(reply) < 7 { + return 0 + } + + crc := int64(reply[3]&0x0F)<<12 | + int64(reply[4]&0x0F)<<8 | + int64(reply[5]&0x0F)<<4 | + int64(reply[6]&0x0F) + + return crc +} + +func (a *CAcceptor) GetDeviceFailure() bool { + a.log.Msg("GetDeviceFailure") + return acceptor.Device.State == enum.StateFailed +} + +func (a *CAcceptor) GetDeviceJammed() bool { + a.log.Msg("GetDeviceJammed") + return acceptor.Device.Jammed +} + +func (a *CAcceptor) GetDeviceModel() int { + a.log.Msg("GetDeviceModel") + return acceptor.Device.Model +} + +func (a *CAcceptor) GetDevicePaused() bool { + a.log.Msg("GetDevicePaused") + return acceptor.Device.Paused +} + +func (a *CAcceptor) GetDevicePortName() string { + a.log.Msg("GetDevicePortName") + return a.port.PortName +} + +func (a *CAcceptor) GetDevicePowerUp() enum.PowerUpType { + a.log.Msg("GetDevicePowerUp") + return acceptor.Device.PowerUp +} + +func (a *CAcceptor) GetDeviceResets() int { + a.log.Msg("GetDeviceResets") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceResets, "DeviceResets") + if err != nil { + a.log.Err("GetDeviceResets", err) + return 0 + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxDeviceResets.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetDeviceResets", err) + return 0 + } + + if len(reply) < 9 { + return 0 + } + + resets := int(reply[3]&0x0F)<<20 | + int(reply[4]&0x0F)<<16 | + int(reply[5]&0x0F)<<12 | + int(reply[6]&0x0F)<<8 | + int(reply[7]&0x0F)<<4 | + int(reply[8]&0x0F) + + return resets +} + +func (a *CAcceptor) GetDeviceRevision() int { + return acceptor.Device.Revision +} + +func (a *CAcceptor) GetDeviceSerialNumber() string { + a.log.Msg("GetDeviceSerialNumber") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceSerialNumber, "DeviceSerialNumber") + if err != nil { + a.log.Err("GetDeviceSerialNumber", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorSerialNumber.Byte()} + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetDeviceSerialNumber", err) + return "" + } + + validCharIndex := 3 + for validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 22 { + validCharIndex++ + } + returnedStringLength := validCharIndex - 3 + + s := string(reply[3 : 3+returnedStringLength]) + return s +} diff --git a/acceptor_process.go b/acceptor_process.go index b41d16a..5878d6e 100644 --- a/acceptor_process.go +++ b/acceptor_process.go @@ -67,10 +67,10 @@ func (a *CAcceptor) processData1(data1 byte) { } if (data1 & 0x04) != 0 { - acceptor.IsDeviceJammed = true + acceptor.Device.Jammed = true acceptor.ShouldRaise.JamDetectedEvent = true } else { - acceptor.IsDeviceJammed = false + acceptor.Device.Jammed = false acceptor.ShouldRaise.JamClearedEvent = true } diff --git a/data_escrow.go b/data_escrow.go index 799d5c2..a1c58ce 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -112,12 +112,12 @@ func (dl *CDataLinkLayer) RaiseEvents() { acceptor.ShouldRaise.PauseClearedEvent = false } - if acceptor.IsDeviceJammed && acceptor.ShouldRaise.JamDetectedEvent { + if acceptor.Device.Jammed && acceptor.ShouldRaise.JamDetectedEvent { dl.log.Msg("Jam Detected Event Raised") acceptor.ShouldRaise.JamDetectedEvent = false } - if !acceptor.IsDeviceJammed && acceptor.ShouldRaise.JamClearedEvent { + if !acceptor.Device.Jammed && acceptor.ShouldRaise.JamClearedEvent { dl.log.Msg("Jam Cleared Event Raised") acceptor.ShouldRaise.JamClearedEvent = false } From 0e88cb2b01d6ffeb15f0661c9d790c5d465a8434 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 21:41:04 +0200 Subject: [PATCH 20/29] refactoring [v0.1.13] merging some methods 4 --- acceptor_methods.go | 155 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/acceptor_methods.go b/acceptor_methods.go index 21d8eee..aaa857a 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -7,6 +7,7 @@ import ( "github.com/hard-soft-ware/mpost/bill" "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" + "time" ) // ////////////////////////////////// @@ -627,6 +628,7 @@ func (a *CAcceptor) GetDeviceResets() int { } func (a *CAcceptor) GetDeviceRevision() int { + a.log.Msg("GetDeviceRevision") return acceptor.Device.Revision } @@ -655,3 +657,156 @@ func (a *CAcceptor) GetDeviceSerialNumber() string { s := string(reply[3 : 3+returnedStringLength]) return s } + +func (a *CAcceptor) GetDeviceStalled() bool { + a.log.Msg("GetDeviceStalled") + return acceptor.Device.Stalled +} + +func (a *CAcceptor) GetDeviceState() enum.StateType { + a.log.Msg("GetDeviceState") + return acceptor.Device.State +} + +func (a *CAcceptor) GetDeviceType() string { + a.log.Msg("GetDeviceType") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.DeviceType, "DeviceType") + if err != nil { + a.log.Err("GetDeviceType", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorType.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetDeviceType", err) + return "" + } + + // Specified to check for non-printable characters from 0x00 to 0x1F or 0x7F as termination. + validCharIndex := 3 + for ; validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 22; validCharIndex++ { + } + + returnedStringLength := validCharIndex - 3 + if returnedStringLength > 0 { + return string(reply[3 : 3+returnedStringLength]) + } + + return "" +} + +func (a *CAcceptor) GetDocType() enum.DocumentType { + a.log.Msg("GetDocType") + return a.docType +} + +func (a *CAcceptor) GetTransactionTimeout() time.Duration { + a.log.Msg("GetTransactionTimeout") + return acceptor.Timeout.Transaction +} + +func (a *CAcceptor) SetTransactionTimeout(newVal time.Duration) { + a.log.Msg("SetTransactionTimeout") + acceptor.Timeout.Transaction = newVal +} + +func (a *CAcceptor) GetDownloadTimeout() time.Duration { + a.log.Msg("GetDownloadTimeout") + return acceptor.Timeout.Download +} + +func (a *CAcceptor) SetDownloadTimeout(newVal time.Duration) { + a.log.Msg("SetDownloadTimeout") + acceptor.Timeout.Download = newVal +} + +func (a *CAcceptor) GetEnableAcceptance() bool { + a.log.Msg("GetEnableAcceptance") + return acceptor.Enable.Acceptance +} + +func (a *CAcceptor) SetEnableAcceptance(newVal bool) { + a.log.Msg("SetEnableAcceptance") + acceptor.Enable.Acceptance = newVal +} + +func (a *CAcceptor) GetEnableBarCodes() bool { + a.log.Msg("GetEnableBarCodes") + return acceptor.Enable.BarCodes +} + +func (a *CAcceptor) SetEnableBarCodes(newVal bool) { + a.log.Msg("SetEnableBarCodes") + acceptor.Enable.BarCodes = newVal +} + +func (a *CAcceptor) GetEnableBookmarks() bool { + a.log.Msg("GetEnableBookmarks") + return acceptor.Enable.Bookmarks +} + +func (a *CAcceptor) SetEnableBookmarks(newVal bool) { + a.log.Msg("SetEnableBookmarks") + acceptor.Enable.Bookmarks = newVal +} + +func (a *CAcceptor) GetEnableCouponExt() bool { + a.log.Msg("GetEnableCouponExt") + return acceptor.Enable.CouponExt +} + +func (a *CAcceptor) SetEnableCouponExt(newVal bool) { + a.log.Msg("SetEnableCouponExt") + acceptor.Enable.CouponExt = newVal +} + +func (a *CAcceptor) GetEnableNoPush() bool { + a.log.Msg("GetEnableNoPush") + return acceptor.Enable.NoPush +} + +func (a *CAcceptor) SetEnableNoPush(newVal bool) { + a.log.Msg("SetEnableNoPush") + acceptor.Enable.NoPush = newVal +} + +func (a *CAcceptor) GetEscrowOrientation() enum.OrientationType { + a.log.Msg("GetEscrowOrientation") + if acceptor.Cap.OrientationExt { + return acceptor.EscrowOrientation + } + return enum.OrientationUnknownOrientation +} + +func (a *CAcceptor) GetHighSecurity() bool { + a.log.Msg("GetHighSecurity") + return acceptor.HighSecurity +} + +func (a *CAcceptor) SetHighSecurity(newVal bool) { + a.log.Msg("SetHighSecurity") + acceptor.HighSecurity = newVal +} + +func (a *CAcceptor) GetOrientationControl() enum.OrientationControlType { + a.log.Msg("GetOrientationControl") + return acceptor.OrientationCtl +} + +func (a *CAcceptor) SetOrientationControl(newVal enum.OrientationControlType) { + a.log.Msg("SetOrientationControl") + acceptor.OrientationCtl = newVal +} + +func (a *CAcceptor) GetOrientationCtlExt() enum.OrientationControlType { + a.log.Msg("GetOrientationCtlExt") + return acceptor.OrientationCtlExt +} + +func (a *CAcceptor) SetOrientationCtlExt(newVal enum.OrientationControlType) { + a.log.Msg("SetOrientationCtlExt") + acceptor.OrientationCtlExt = newVal +} From e2f2affab56a2b27d601a11e70b293a34b4e0001 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 21:50:23 +0200 Subject: [PATCH 21/29] refactoring [v0.1.13] merging some methods end --- acceptor_methods.go | 126 ++++++++++++++++++++++++++++++++++++++++++++ acceptor_raise.go | 10 ++++ 2 files changed, 136 insertions(+) diff --git a/acceptor_methods.go b/acceptor_methods.go index aaa857a..2c68b30 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -810,3 +810,129 @@ func (a *CAcceptor) SetOrientationCtlExt(newVal enum.OrientationControlType) { a.log.Msg("SetOrientationCtlExt") acceptor.OrientationCtlExt = newVal } + +func (a *CAcceptor) GetVariantNames() []string { + a.log.Msg("GetVariantNames") + + err := a.verifyPropertyIsAllowed(true, "VariantNames") + if err != nil { + a.log.Err("GetVariantNames", err) + return nil + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorVariantName.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetVariantNames", err) + return nil + } + + var names []string + validCharIndex := 3 + + for validCharIndex < len(reply) && reply[validCharIndex] > 0x20 && reply[validCharIndex] < 0x7F && validCharIndex <= 34 { + if validCharIndex+2 < len(reply) { + names = append(names, string(reply[validCharIndex:validCharIndex+3])) + validCharIndex += 4 + } else { + break + } + } + + return names +} + +func (a *CAcceptor) GetVariantID() string { + a.log.Msg("GetVariantID") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.VariantID, "VariantID") + if err != nil { + a.log.Err("GetVariantID", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorVariantID.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetVariantID", err) + return "" + } + + if len(reply) == 14 { + return string(reply[3:12]) // Extracting a 9-byte string starting from index 3 + } + + return "" +} + +func (a *CAcceptor) GetVariantPN() string { + a.log.Msg("GetVariantPN") + + err := a.verifyPropertyIsAllowed(acceptor.Cap.VariantPN, "VariantPN") + if err != nil { + a.log.Err("GetVariantPN", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorVariantPartNumber.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("GetVariantPN", err) + return "" + } + + if len(reply) == 14 { + return string(reply[3:12]) + } + + return "" +} + +func (a *CAcceptor) GetVersion() string { + a.log.Msg("GetVersion") + return acceptor.Version +} + +func (a *CAcceptor) Calibrate() { + a.log.Msg("Calibrate") + if !acceptor.Connected { + a.log.Err("Calibrate", errors.New("Calibrate called when not connected")) + return + } + + if acceptor.Device.State != enum.StateIdling { + a.log.Err("Calibrate", errors.New("Calibrate allowed only when DeviceState == Idling")) + return + } + + payload := []byte{consts.CmdCalibrate.Byte(), 0x00, 0x00, 0x00} + + acceptor.SuppressStandardPoll = true + acceptor.Device.State = enum.StateCalibrateStart + + a.RaiseCalibrateStartEvent() + + acceptor.ShouldRaise.CalibrateProgressEvent = true + + startTickCount := time.Now() + + for { + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("Calibrate", errors.New("Failed to send synchronous command during calibration")) + return + } + + if len(reply) == 11 && (reply[2]&0x70) == 0x40 { + break + } + + if time.Since(startTickCount) > CalibrateTimeout { + a.RaiseCalibrateFinishEvent() + return + } + } +} diff --git a/acceptor_raise.go b/acceptor_raise.go index 5568bdc..a77ff11 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -41,7 +41,17 @@ func (a *CAcceptor) RaiseDownloadRestartEvent() { acceptor.ShouldRaise.DownloadRestartEvent = false } +// + func (a *CAcceptor) RaiseCalibrateProgressEvent() { a.raiseXX(enum.EventCalibrateProgress, 0) acceptor.ShouldRaise.CalibrateProgressEvent = false } + +func (a *CAcceptor) RaiseCalibrateStartEvent() { + a.raiseXX(enum.EventCalibrateStart, 0) +} + +func (a *CAcceptor) RaiseCalibrateFinishEvent() { + a.raiseXX(enum.EventCalibrateFinish, 0) +} From 7a349d382bd1b83db41e00649989bdfde34406da Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 22:16:40 +0200 Subject: [PATCH 22/29] refactoring [v0.1.13] merging flash --- acceptor_flash.go | 155 ++++++++++++++++++++++++++++++++++++++++++++ acceptor_methods.go | 54 +++++++-------- acceptor_raise.go | 22 +++++++ 3 files changed, 205 insertions(+), 26 deletions(-) create mode 100644 acceptor_flash.go diff --git a/acceptor_flash.go b/acceptor_flash.go new file mode 100644 index 0000000..9d0f425 --- /dev/null +++ b/acceptor_flash.go @@ -0,0 +1,155 @@ +package mpost + +import ( + "errors" + "fmt" + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" + "os" + "time" +) + +//////////////////////////////////// + +func (a *CAcceptor) FlashDownload(filePath string) (err error) { + a.log.Msg("FlashDownload") + + if !acceptor.Connected && acceptor.Device.State != enum.StateDownloadRestart { + err = errors.New("FlashDownload not allowed when not connected or not in DownloadRestart state") + a.log.Err("FlashDownload", err) + return + } + + if !(acceptor.Device.Model >= 65 && acceptor.Device.Model <= 71 || + acceptor.Device.Model == 77 || + acceptor.Device.Model == 80 || + acceptor.Device.Model == 81 || + acceptor.Device.Model == 87 || + acceptor.Device.Model == 88) { + if acceptor.Device.State != enum.StateIdling && acceptor.Device.State != enum.StateDownloadRestart { + err = errors.New("FlashDownload allowed only when DeviceState is Idling or DownloadRestart") + a.log.Err("FlashDownload", err) + return + } + } + + file, err := os.Open(filePath) + if err != nil { + err = errors.New(fmt.Sprintf("Failed to open flash download file: %s", err)) + a.log.Err("FlashDownload", err) + return + } + defer file.Close() + + // Determine the file size and check if divisible by 32 + fileInfo, err := file.Stat() + if err != nil { + err = errors.New(fmt.Sprintf("Failed to stat flash download file: %s", err)) + a.log.Err("FlashDownload", err) + return + } + fileSize := fileInfo.Size() + if fileSize%32 != 0 { + err = errors.New("Flash download file size must be divisible by 32") + a.log.Err("FlashDownload", err) + return + } + + go func() { + defer file.Close() + a.flashDownload(file, fileSize) + }() + + return +} + +func (a *CAcceptor) flashDownload(downloadFile *os.File, fileSize int64) { + a.log.Msg("FlashDownloadThread started") + + if acceptor.Device.State != enum.StateDownloadRestart { + acceptor.Device.State = enum.StateDownloadStart + } + + packetNum := 0 + finalPacketNum := int(fileSize / 32) + payload := []byte{consts.CmdCalibrate.Byte(), 0x00, 0x00, 0x00} + var reply []byte + var err error + + for { + if acceptor.StopFlashDownloadThread { + acceptor.Device.State = enum.StateIdling + return + } + + reply, err = a.SendSynchronousCommand(payload) + if err != nil || len(reply) == 0 { + if !acceptor.Connected { + a.RaiseDownloadFinishEvent(false) + acceptor.Device.State = enum.StateIdling + return + } + continue + } + + if (reply[2] & 0x70) == 0x20 { + break + } + packetNum = (int(reply[3]&0x0F)<<12 + int(reply[4]&0x0F)<<8 + int(reply[5]&0x0F)<<4 + int(reply[6]&0x0F) + 1) & 0xFFFF + } + + a.RaiseDownloadStartEvent(finalPacketNum) + timeoutStartTickCount := time.Now() + + for packetNum < finalPacketNum { + if acceptor.StopFlashDownloadThread { + acceptor.Device.State = enum.StateIdling + return + } + + dataBuffer := make([]byte, 32) + _, err := downloadFile.ReadAt(dataBuffer, int64(packetNum*32)) + if err != nil { + time.Sleep(200 * time.Millisecond) + continue + } + + payload = make([]byte, 69) + payload[0] = consts.CmdFlashDownload.Byte() + payload[1] = byte((packetNum & 0xF000) >> 12) + payload[2] = byte((packetNum & 0x0F00) >> 8) + payload[3] = byte((packetNum & 0x00F0) >> 4) + payload[4] = byte(packetNum & 0x000F) + copy(payload[5:], dataBuffer) + + reply, err = a.SendSynchronousCommand(payload) + if err != nil || len(reply) != 9 { + time.Sleep(200 * time.Millisecond) + continue + } + + if reply[0] == consts.DataSTX.Byte() { + acceptor.Device.State = enum.StateDownloading + if acceptor.ShouldRaise.DownloadProgressEvent { + a.RaiseDownloadProgressEvent(packetNum) + } + packetNum++ + } else { + time.Sleep(200 * time.Millisecond) + continue + } + + if time.Since(timeoutStartTickCount) > acceptor.Timeout.Download { + a.RaiseDownloadFinishEvent(false) + acceptor.Device.State = enum.StateIdling + return + } + } + + time.Sleep(30 * time.Millisecond) + a.RaiseDownloadFinishEvent(true) + acceptor.Device.State = enum.StateIdling + acceptor.Connected = true + a.RaiseConnectedEvent() +} diff --git a/acceptor_methods.go b/acceptor_methods.go index 2c68b30..6b2efe1 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -201,9 +201,9 @@ func (a *CAcceptor) GetAutoStack() bool { return acceptor.AutoStack } -func (a *CAcceptor) SetAutoStack(newVal bool) { +func (a *CAcceptor) SetAutoStack(v bool) { a.log.Msg("SetAutoStack") - acceptor.AutoStack = newVal + acceptor.AutoStack = v } func (a *CAcceptor) GetBarCode() string { @@ -226,7 +226,7 @@ func (a *CAcceptor) GetBillTypeEnables() []bool { return bill.TypeEnables } -func (a *CAcceptor) SetBillTypeEnables(newVal []bool) { +func (a *CAcceptor) SetBillTypeEnables(v []bool) { a.log.Msg("SetBillTypeEnables") if !acceptor.Connected { @@ -239,7 +239,7 @@ func (a *CAcceptor) SetBillTypeEnables(newVal []bool) { return } - bill.TypeEnables = newVal + bill.TypeEnables = v if acceptor.ExpandedNoteReporting { payload := make([]byte, 15) @@ -270,9 +270,9 @@ func (a *CAcceptor) GetBillValueEnables() []bool { return bill.ValueEnables } -func (a *CAcceptor) SetBillValueEnables(newVal []bool) { +func (a *CAcceptor) SetBillValueEnables(v []bool) { a.log.Msg("SetBillValueEnables") - bill.ValueEnables = newVal + bill.ValueEnables = v for _, enabled := range bill.ValueEnables { for j, billType := range bill.Types { @@ -708,9 +708,9 @@ func (a *CAcceptor) GetTransactionTimeout() time.Duration { return acceptor.Timeout.Transaction } -func (a *CAcceptor) SetTransactionTimeout(newVal time.Duration) { +func (a *CAcceptor) SetTransactionTimeout(v time.Duration) { a.log.Msg("SetTransactionTimeout") - acceptor.Timeout.Transaction = newVal + acceptor.Timeout.Transaction = v } func (a *CAcceptor) GetDownloadTimeout() time.Duration { @@ -718,9 +718,9 @@ func (a *CAcceptor) GetDownloadTimeout() time.Duration { return acceptor.Timeout.Download } -func (a *CAcceptor) SetDownloadTimeout(newVal time.Duration) { +func (a *CAcceptor) SetDownloadTimeout(v time.Duration) { a.log.Msg("SetDownloadTimeout") - acceptor.Timeout.Download = newVal + acceptor.Timeout.Download = v } func (a *CAcceptor) GetEnableAcceptance() bool { @@ -728,9 +728,9 @@ func (a *CAcceptor) GetEnableAcceptance() bool { return acceptor.Enable.Acceptance } -func (a *CAcceptor) SetEnableAcceptance(newVal bool) { +func (a *CAcceptor) SetEnableAcceptance(v bool) { a.log.Msg("SetEnableAcceptance") - acceptor.Enable.Acceptance = newVal + acceptor.Enable.Acceptance = v } func (a *CAcceptor) GetEnableBarCodes() bool { @@ -738,9 +738,9 @@ func (a *CAcceptor) GetEnableBarCodes() bool { return acceptor.Enable.BarCodes } -func (a *CAcceptor) SetEnableBarCodes(newVal bool) { +func (a *CAcceptor) SetEnableBarCodes(v bool) { a.log.Msg("SetEnableBarCodes") - acceptor.Enable.BarCodes = newVal + acceptor.Enable.BarCodes = v } func (a *CAcceptor) GetEnableBookmarks() bool { @@ -748,9 +748,9 @@ func (a *CAcceptor) GetEnableBookmarks() bool { return acceptor.Enable.Bookmarks } -func (a *CAcceptor) SetEnableBookmarks(newVal bool) { +func (a *CAcceptor) SetEnableBookmarks(v bool) { a.log.Msg("SetEnableBookmarks") - acceptor.Enable.Bookmarks = newVal + acceptor.Enable.Bookmarks = v } func (a *CAcceptor) GetEnableCouponExt() bool { @@ -758,9 +758,9 @@ func (a *CAcceptor) GetEnableCouponExt() bool { return acceptor.Enable.CouponExt } -func (a *CAcceptor) SetEnableCouponExt(newVal bool) { +func (a *CAcceptor) SetEnableCouponExt(v bool) { a.log.Msg("SetEnableCouponExt") - acceptor.Enable.CouponExt = newVal + acceptor.Enable.CouponExt = v } func (a *CAcceptor) GetEnableNoPush() bool { @@ -768,9 +768,9 @@ func (a *CAcceptor) GetEnableNoPush() bool { return acceptor.Enable.NoPush } -func (a *CAcceptor) SetEnableNoPush(newVal bool) { +func (a *CAcceptor) SetEnableNoPush(v bool) { a.log.Msg("SetEnableNoPush") - acceptor.Enable.NoPush = newVal + acceptor.Enable.NoPush = v } func (a *CAcceptor) GetEscrowOrientation() enum.OrientationType { @@ -786,9 +786,9 @@ func (a *CAcceptor) GetHighSecurity() bool { return acceptor.HighSecurity } -func (a *CAcceptor) SetHighSecurity(newVal bool) { +func (a *CAcceptor) SetHighSecurity(v bool) { a.log.Msg("SetHighSecurity") - acceptor.HighSecurity = newVal + acceptor.HighSecurity = v } func (a *CAcceptor) GetOrientationControl() enum.OrientationControlType { @@ -796,9 +796,9 @@ func (a *CAcceptor) GetOrientationControl() enum.OrientationControlType { return acceptor.OrientationCtl } -func (a *CAcceptor) SetOrientationControl(newVal enum.OrientationControlType) { +func (a *CAcceptor) SetOrientationControl(v enum.OrientationControlType) { a.log.Msg("SetOrientationControl") - acceptor.OrientationCtl = newVal + acceptor.OrientationCtl = v } func (a *CAcceptor) GetOrientationCtlExt() enum.OrientationControlType { @@ -806,9 +806,9 @@ func (a *CAcceptor) GetOrientationCtlExt() enum.OrientationControlType { return acceptor.OrientationCtlExt } -func (a *CAcceptor) SetOrientationCtlExt(newVal enum.OrientationControlType) { +func (a *CAcceptor) SetOrientationCtlExt(v enum.OrientationControlType) { a.log.Msg("SetOrientationCtlExt") - acceptor.OrientationCtlExt = newVal + acceptor.OrientationCtlExt = v } func (a *CAcceptor) GetVariantNames() []string { @@ -936,3 +936,5 @@ func (a *CAcceptor) Calibrate() { } } } + +//// diff --git a/acceptor_raise.go b/acceptor_raise.go index a77ff11..2679672 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -55,3 +55,25 @@ func (a *CAcceptor) RaiseCalibrateStartEvent() { func (a *CAcceptor) RaiseCalibrateFinishEvent() { a.raiseXX(enum.EventCalibrateFinish, 0) } + +// + +func (a *CAcceptor) RaiseDownloadFinishEvent(st bool) { + v := 0 + if st { + v = 1 + } + a.raiseXX(enum.EventDownloadFinish, v) + acceptor.ShouldRaise.DownloadFinishEvent = false +} + +func (a *CAcceptor) RaiseDownloadStartEvent(v int) { + a.raiseXX(enum.EventDownloadStart, v) + + acceptor.ShouldRaise.DownloadStartEvent = false + acceptor.ShouldRaise.DownloadProgressEvent = true +} + +func (a *CAcceptor) RaiseDownloadProgressEvent(v int) { + a.raiseXX(enum.EventDownloadProgress, v) +} From 88781e2882ac81ad31f033c4f0b9f1144b9939ff Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 22:20:30 +0200 Subject: [PATCH 23/29] refactoring [v0.1.13] xx --- acceptor_methods.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/acceptor_methods.go b/acceptor_methods.go index 6b2efe1..31ef919 100644 --- a/acceptor_methods.go +++ b/acceptor_methods.go @@ -938,3 +938,24 @@ func (a *CAcceptor) Calibrate() { } //// + +func (a *CAcceptor) ClearCashBoxTotal() (err error) { + a.log.Msg("ClearCashBoxTotal") + + if !acceptor.Connected { + err = errors.New("ClearCashBoxTotal called when not connected") + a.log.Err("ClearCashBoxTotal", err) + return + } + + payload := []byte{consts.CmdCalibrate.Byte(), 0x00, 0x00, consts.CmdAuxCashBoxTotal.Byte()} + + reply, err := a.SendSynchronousCommand(payload) + if err != nil { + a.log.Err("ClearCashBoxTotal", err) + return + } + + a.dataLinkLayer.ProcessReply(reply) + return +} From ed57c9572de41a2c93171acae7c94507eb3ad66f Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 22:43:55 +0200 Subject: [PATCH 24/29] refactoring [v0.1.14] fin ctx --- 0_test.go | 22 ++++++++++++++++++++-- _run/values/ver.txt | 2 +- acceptor.go | 44 +++++++++++++++++--------------------------- acceptor_message.go | 3 +++ acceptor_serial.go | 1 + acceptor_thread.go | 4 ++++ const.go | 8 ++++---- 7 files changed, 50 insertions(+), 34 deletions(-) diff --git a/0_test.go b/0_test.go index 5c246be..d8b7555 100644 --- a/0_test.go +++ b/0_test.go @@ -3,15 +3,30 @@ package mpost import ( "fmt" "github.com/hard-soft-ware/mpost/enum" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "os" "testing" "time" ) func TestConnect(t *testing.T) { - a := NewCAcceptor(30*time.Second, 30*time.Second) + a := DefAcceptor + a.AddLog( + log.Output(zerolog.ConsoleWriter{ + Out: os.Stdout, + NoColor: false, + TimeFormat: "15:04:05", + }), + "TEST", + ) a.AddHook(enum.EventConnected, func(acceptor *CAcceptor, i int) { fmt.Println("Connect") + + acceptor.SetEnableAcceptance(true) + acceptor.SetEnableBarCodes(true) + acceptor.SetEnableBookmarks(true) }) a.AddHook(enum.EventDisconnected, func(acceptor *CAcceptor, i int) { fmt.Println("Disconnect") @@ -21,7 +36,10 @@ func TestConnect(t *testing.T) { time.Sleep(2 * time.Second) t.Log(a.GetDeviceSerialNumber()) - t.Log(a.GetBNFStatus().String()) + t.Log(a.GetBill()) + t.Log(a.GetApplicationPN()) + t.Log(a.GetBootPN()) + t.Log(a.GetDeviceState().String()) a.Close() time.Sleep(1 * time.Second) diff --git a/_run/values/ver.txt b/_run/values/ver.txt index faceed6..600c007 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.13 +v0.1.14 diff --git a/acceptor.go b/acceptor.go index 747cc3c..107bed2 100644 --- a/acceptor.go +++ b/acceptor.go @@ -1,14 +1,11 @@ package mpost import ( - "github.com/hard-soft-ware/mpost/acceptor" + "context" "github.com/hard-soft-ware/mpost/enum" "github.com/hard-soft-ware/mpost/serial" "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "os" "sync" - "time" ) //////////////////////////////////// @@ -37,37 +34,30 @@ type CAcceptor struct { log LogStruct - ss bool + ctx context.Context + ctxCancel context.CancelFunc + ss bool } -func NewCAcceptor(transactionTimeout, downloadTimeout time.Duration) *CAcceptor { +var DefAcceptor = &CAcceptor{ + eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), - acceptor.Timeout.Transaction = transactionTimeout - acceptor.Timeout.Download = downloadTimeout - - a := &CAcceptor{ - eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), - - messageQueue: make(chan *CMessage, 1), - replyQueue: make(chan []byte, 1), - flashDownloadThread: make(chan bool, 1), - openThread: make(chan bool, 1), - - log: NewLog( - log.Output(zerolog.ConsoleWriter{ - Out: os.Stdout, - NoColor: false, - TimeFormat: "15:04:05", - }), - "Acceptor", - ), - } + messageQueue: make(chan *CMessage, 1), + replyQueue: make(chan []byte, 1), + flashDownloadThread: make(chan bool, 1), + openThread: make(chan bool, 1), +} - return a +func init() { + DefAcceptor.ctx, DefAcceptor.ctxCancel = context.WithCancel(context.Background()) } // +func (a *CAcceptor) AddLog(log zerolog.Logger, root string) { + a.log = NewLog(log, root) +} + func (a *CAcceptor) AddHook(ev enum.EventType, h EventHandler) { a.eventHandlers[ev] = h } diff --git a/acceptor_message.go b/acceptor_message.go index e6f5cd5..228b74c 100644 --- a/acceptor_message.go +++ b/acceptor_message.go @@ -34,6 +34,9 @@ func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { a.messageQueue <- NewCMessage(payload, true) select { + case <-a.ctx.Done(): + a.Close() + return nil, errors.New("close from context") case reply := <-a.replyQueue: return reply, nil case <-time.After(30 * time.Second): diff --git a/acceptor_serial.go b/acceptor_serial.go index 74e704d..7824072 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -33,6 +33,7 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { } func (a *CAcceptor) Close() { + defer a.ctxCancel() if !acceptor.Connected { acceptor.StopOpenThread = true diff --git a/acceptor_thread.go b/acceptor_thread.go index 12ecf22..2f59cda 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -61,6 +61,10 @@ func (a *CAcceptor) MessageLoopThread() { } select { + case <-a.ctx.Done(): + a.Close() + return + case message := <-a.messageQueue: a.dataLinkLayer.SendPacket(message.Payload) diff --git a/const.go b/const.go index 74400ee..842a855 100644 --- a/const.go +++ b/const.go @@ -2,11 +2,11 @@ package mpost const ( GlobalName string = "mpost" - GlobalDateUpdate string = "09-06-2024" - GlobalHash string = "67b83899ceb79169ae2df37f6cad255cf690a300" + GlobalDateUpdate string = "09-10-2024" + GlobalHash string = "88781e2882ac81ad31f033c4f0b9f1144b9939ff" - GlobalVersion string = "v0.1.13" + GlobalVersion string = "v0.1.14" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 13 + GlobalVersionPatch uint16 = 14 ) From a449c0026a5d6cee8828cb0aeeacb1712dbe8dee Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 22:48:46 +0200 Subject: [PATCH 25/29] refactoring [v0.1.14] test --- acceptor.go | 10 ++++------ acceptor_loop.go | 11 +++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/acceptor.go b/acceptor.go index 107bed2..e34293f 100644 --- a/acceptor.go +++ b/acceptor.go @@ -23,8 +23,8 @@ type CAcceptor struct { docType enum.DocumentType workerThread sync.WaitGroup - openThread chan bool - flashDownloadThread chan bool + openThread bool + flashDownloadThread bool dataLinkLayer *CDataLinkLayer messageQueue chan *CMessage @@ -42,10 +42,8 @@ type CAcceptor struct { var DefAcceptor = &CAcceptor{ eventHandlers: make(map[enum.EventType]EventHandler, enum.Event_End), - messageQueue: make(chan *CMessage, 1), - replyQueue: make(chan []byte, 1), - flashDownloadThread: make(chan bool, 1), - openThread: make(chan bool, 1), + messageQueue: make(chan *CMessage, 1), + replyQueue: make(chan []byte, 1), } func init() { diff --git a/acceptor_loop.go b/acceptor_loop.go index 61f5f3b..cd98cec 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -25,21 +25,20 @@ func (a *CAcceptor) PollingLoop() []byte { startTickCount = time.Now() } - if a.flashDownloadThread != nil { + if !a.flashDownloadThread { if acceptor.StopFlashDownloadThread { acceptor.StopFlashDownloadThread = true - <-a.flashDownloadThread + a.flashDownloadThread = true acceptor.Device.State = enum.StateIdling acceptor.WasStopped = true return nil } - } else if a.openThread != nil { + } + if !a.openThread { if acceptor.StopOpenThread { acceptor.StopOpenThread = false acceptor.StopWorkerThread = true - - <-a.openThread - + a.openThread = true acceptor.WasStopped = true a.Close() return nil From 4d61985f0f43cdfba1e57f332e29baad35286895 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 22:49:34 +0200 Subject: [PATCH 26/29] refactoring [v0.1.14] test --- acceptor.go | 6 +++--- acceptor_message.go | 2 +- acceptor_serial.go | 2 +- acceptor_thread.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/acceptor.go b/acceptor.go index e34293f..f62fb24 100644 --- a/acceptor.go +++ b/acceptor.go @@ -34,8 +34,8 @@ type CAcceptor struct { log LogStruct - ctx context.Context - ctxCancel context.CancelFunc + Ctx context.Context + CtxCancel context.CancelFunc ss bool } @@ -47,7 +47,7 @@ var DefAcceptor = &CAcceptor{ } func init() { - DefAcceptor.ctx, DefAcceptor.ctxCancel = context.WithCancel(context.Background()) + DefAcceptor.Ctx, DefAcceptor.CtxCancel = context.WithCancel(context.Background()) } // diff --git a/acceptor_message.go b/acceptor_message.go index 228b74c..1b92172 100644 --- a/acceptor_message.go +++ b/acceptor_message.go @@ -34,7 +34,7 @@ func (a *CAcceptor) SendSynchronousCommand(payload []byte) ([]byte, error) { a.messageQueue <- NewCMessage(payload, true) select { - case <-a.ctx.Done(): + case <-a.Ctx.Done(): a.Close() return nil, errors.New("close from context") case reply := <-a.replyQueue: diff --git a/acceptor_serial.go b/acceptor_serial.go index 7824072..448ef0a 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -33,7 +33,7 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { } func (a *CAcceptor) Close() { - defer a.ctxCancel() + defer a.CtxCancel() if !acceptor.Connected { acceptor.StopOpenThread = true diff --git a/acceptor_thread.go b/acceptor_thread.go index 2f59cda..05e9782 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -61,7 +61,7 @@ func (a *CAcceptor) MessageLoopThread() { } select { - case <-a.ctx.Done(): + case <-a.Ctx.Done(): a.Close() return From 3018bcf038f9318f2ffb47c26b4a52a1de65aa7f Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Tue, 10 Sep 2024 23:22:22 +0200 Subject: [PATCH 27/29] refactoring [v0.1.14] end --- 0_test.go | 2 +- acceptor.go | 2 -- acceptor_loop.go | 2 +- acceptor_raise.go | 2 +- acceptor_serial.go | 6 +++--- acceptor_thread.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/0_test.go b/0_test.go index d8b7555..db9659b 100644 --- a/0_test.go +++ b/0_test.go @@ -41,6 +41,6 @@ func TestConnect(t *testing.T) { t.Log(a.GetBootPN()) t.Log(a.GetDeviceState().String()) + time.Sleep(100 * time.Second) a.Close() - time.Sleep(1 * time.Second) } diff --git a/acceptor.go b/acceptor.go index f62fb24..6f7c7eb 100644 --- a/acceptor.go +++ b/acceptor.go @@ -5,7 +5,6 @@ import ( "github.com/hard-soft-ware/mpost/enum" "github.com/hard-soft-ware/mpost/serial" "github.com/rs/zerolog" - "sync" ) //////////////////////////////////// @@ -22,7 +21,6 @@ type CAcceptor struct { docType enum.DocumentType - workerThread sync.WaitGroup openThread bool flashDownloadThread bool dataLinkLayer *CDataLinkLayer diff --git a/acceptor_loop.go b/acceptor_loop.go index cd98cec..57aa7bb 100644 --- a/acceptor_loop.go +++ b/acceptor_loop.go @@ -21,7 +21,7 @@ func (a *CAcceptor) PollingLoop() []byte { } if time.Since(startTickCount) > PollingDisconnectTimeout { - a.RaiseDisconnectedEvent() + a.Close() startTickCount = time.Now() } diff --git a/acceptor_raise.go b/acceptor_raise.go index 2679672..a8bf100 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -27,7 +27,7 @@ func (a *CAcceptor) RaiseConnectedEvent() { } func (a *CAcceptor) RaiseDisconnectedEvent() { - if acceptor.ShouldRaise.DisconnectedEvent { + if !acceptor.ShouldRaise.DisconnectedEvent { return } diff --git a/acceptor_serial.go b/acceptor_serial.go index 448ef0a..556e6b2 100644 --- a/acceptor_serial.go +++ b/acceptor_serial.go @@ -33,6 +33,9 @@ func (a *CAcceptor) Open(portName string, powerUp enum.PowerUpType) error { } func (a *CAcceptor) Close() { + a.RaiseDisconnectedEvent() + a.port.Close() + defer a.CtxCancel() if !acceptor.Connected { @@ -49,10 +52,7 @@ func (a *CAcceptor) Close() { } acceptor.StopWorkerThread = true - a.workerThread.Wait() - a.port.Close() - a.RaiseDisconnectedEvent() a.port = nil acceptor.Connected = false a.log.Msg("Close") diff --git a/acceptor_thread.go b/acceptor_thread.go index 05e9782..0f4ee6b 100644 --- a/acceptor_thread.go +++ b/acceptor_thread.go @@ -2,6 +2,8 @@ package mpost import ( "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/bill" + "github.com/hard-soft-ware/mpost/consts" "github.com/hard-soft-ware/mpost/enum" "time" ) @@ -37,6 +39,7 @@ func (a *CAcceptor) MessageLoopThread() { a.dataLinkLayer = a.NewCDataLinkLayer(lg) timeoutStart := time.Now() + loopCycleCounter := 0 for { if !acceptor.InSoftResetWaitForReply { @@ -48,7 +51,7 @@ func (a *CAcceptor) MessageLoopThread() { if time.Since(timeoutStart) > 30*time.Second { if acceptor.Device.State != enum.StateDownloading && acceptor.Device.State != enum.StateDownloadRestart { acceptor.Connected = false - a.RaiseDisconnectedEvent() + a.Close() acceptor.WasDisconnected = true timeoutStart = time.Now() } @@ -66,6 +69,7 @@ func (a *CAcceptor) MessageLoopThread() { return case message := <-a.messageQueue: + loopCycleCounter = 0 a.dataLinkLayer.SendPacket(message.Payload) reply, err := a.dataLinkLayer.ReceiveReply() @@ -86,6 +90,46 @@ func (a *CAcceptor) MessageLoopThread() { a.dataLinkLayer.ProcessReply(reply) } } + + default: + loopCycleCounter++ + + if loopCycleCounter > 9 { + loopCycleCounter = 0 + + payload := make([]byte, 4) + acceptor.ConstructOmnibusCommand(payload, consts.CmdOmnibus, 1, bill.TypeEnables) + + a.dataLinkLayer.SendPacket(payload) + + reply, err := a.dataLinkLayer.ReceiveReply() + if err != nil { + a.Close() + a.log.Err("Invalid loopCycleCounter", err) + return + } + + if len(reply) > 0 { + timeoutStart = time.Now() + + if acceptor.WasDisconnected { + acceptor.WasDisconnected = false + + if reply[2]&0x70 != 0x50 { + acceptor.Connected = true + a.RaiseConnectedEvent() + } else { + a.RaiseDownloadRestartEvent() + } + } + + if acceptor.InSoftResetWaitForReply { + acceptor.InSoftResetWaitForReply = false + } + + a.dataLinkLayer.ProcessReply(reply) + } + } } } } From e33d475848c735d5072a6377e67ec3ccabae2f11 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Wed, 11 Sep 2024 00:17:25 +0200 Subject: [PATCH 28/29] refactoring [v0.1.15] raise --- 0_test.go | 4 ++ _run/values/ver.txt | 2 +- acceptor.go | 4 +- acceptor_raise.go | 96 ++++++++++++++++++++++++++++++++++++++++++++- const.go | 6 +-- data_escrow.go | 56 +++++++++----------------- log.go | 18 +++++---- 7 files changed, 133 insertions(+), 53 deletions(-) diff --git a/0_test.go b/0_test.go index db9659b..4598d36 100644 --- a/0_test.go +++ b/0_test.go @@ -19,6 +19,7 @@ func TestConnect(t *testing.T) { TimeFormat: "15:04:05", }), "TEST", + false, ) a.AddHook(enum.EventConnected, func(acceptor *CAcceptor, i int) { @@ -31,6 +32,9 @@ func TestConnect(t *testing.T) { a.AddHook(enum.EventDisconnected, func(acceptor *CAcceptor, i int) { fmt.Println("Disconnect") }) + a.AddHook(enum.EventRejected, func(acceptor *CAcceptor, i int) { + fmt.Println("EventRejected") + }) a.Open("/dev/ttyUSB0", enum.PowerUpE) diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 600c007..7c32f2d 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.14 +v0.1.15 diff --git a/acceptor.go b/acceptor.go index 6f7c7eb..0eaa490 100644 --- a/acceptor.go +++ b/acceptor.go @@ -50,8 +50,8 @@ func init() { // -func (a *CAcceptor) AddLog(log zerolog.Logger, root string) { - a.log = NewLog(log, root) +func (a *CAcceptor) AddLog(log zerolog.Logger, root string, printBytes bool) { + a.log = NewLog(log, root, printBytes) } func (a *CAcceptor) AddHook(ev enum.EventType, h EventHandler) { diff --git a/acceptor_raise.go b/acceptor_raise.go index a8bf100..4d40318 100644 --- a/acceptor_raise.go +++ b/acceptor_raise.go @@ -8,7 +8,10 @@ import ( //////////////////////////////////// func (a *CAcceptor) raiseXX(e enum.EventType, b int) { - a.log.Event(e) + if e != enum.EventJamCleared { + a.log.Event(e) + } + if handler, exists := a.eventHandlers[e]; exists && handler != nil { handler(a, b) } @@ -50,10 +53,12 @@ func (a *CAcceptor) RaiseCalibrateProgressEvent() { func (a *CAcceptor) RaiseCalibrateStartEvent() { a.raiseXX(enum.EventCalibrateStart, 0) + acceptor.ShouldRaise.CalibrateStartEvent = false } func (a *CAcceptor) RaiseCalibrateFinishEvent() { a.raiseXX(enum.EventCalibrateFinish, 0) + acceptor.ShouldRaise.CalibrateFinishEvent = false } // @@ -77,3 +82,92 @@ func (a *CAcceptor) RaiseDownloadStartEvent(v int) { func (a *CAcceptor) RaiseDownloadProgressEvent(v int) { a.raiseXX(enum.EventDownloadProgress, v) } + +// + +func (a *CAcceptor) RaisePowerUpEvent() { + a.raiseXX(enum.EventPowerUp, 0) + acceptor.ShouldRaise.PowerUpEvent = false +} + +func (a *CAcceptor) RaisePUPEscrowEvent() { + a.raiseXX(enum.EventPUPEscrow, 0) + acceptor.ShouldRaise.PUPEscrowEvent = false +} + +func (a *CAcceptor) RaiseEscrowEvent() { + a.raiseXX(enum.EventEscrow, 0) + acceptor.ShouldRaise.EscrowEvent = false +} + +func (a *CAcceptor) RaiseStackedEvent() { + a.raiseXX(enum.EventStacked, 0) + acceptor.ShouldRaise.StackedEvent = false +} + +func (a *CAcceptor) RaiseReturnedEvent() { + a.raiseXX(enum.EventReturned, 0) + acceptor.ShouldRaise.ReturnedEvent = false +} + +func (a *CAcceptor) RaiseRejectedEvent() { + a.raiseXX(enum.EventRejected, 0) + acceptor.ShouldRaise.RejectedEvent = false +} + +func (a *CAcceptor) RaiseStallDetectedEvent() { + a.raiseXX(enum.EventStallDetected, 0) + acceptor.ShouldRaise.StallDetectedEvent = false +} + +func (a *CAcceptor) RaiseStallClearedEvent() { + a.raiseXX(enum.EventStallCleared, 0) + acceptor.ShouldRaise.StallClearedEvent = false +} + +func (a *CAcceptor) RaiseStackerFullEvent() { + a.raiseXX(enum.EventStackerFull, 0) + acceptor.ShouldRaise.StackerFullEvent = false +} + +func (a *CAcceptor) RaiseCheatedEvent() { + a.raiseXX(enum.EventCheated, 0) + acceptor.ShouldRaise.CheatedEvent = false +} + +func (a *CAcceptor) RaiseCashBoxAttachedEvent() { + a.raiseXX(enum.EventCashBoxAttached, 0) + acceptor.ShouldRaise.CashBoxAttachedEvent = false + acceptor.ShouldRaise.CashBoxRemovedEvent = true +} + +func (a *CAcceptor) RaiseCashBoxRemovedEvent() { + a.raiseXX(enum.EventCashBoxRemoved, 0) + acceptor.ShouldRaise.CashBoxRemovedEvent = false + acceptor.ShouldRaise.CashBoxAttachedEvent = true +} + +func (a *CAcceptor) RaisePauseDetectedEvent() { + a.raiseXX(enum.EventPauseDetected, 0) + acceptor.ShouldRaise.PauseDetectedEvent = false +} + +func (a *CAcceptor) RaisePauseClearedEvent() { + a.raiseXX(enum.EventPauseCleared, 0) + acceptor.ShouldRaise.PauseClearedEvent = false +} + +func (a *CAcceptor) RaiseJamDetectedEvent() { + a.raiseXX(enum.EventJamDetected, 0) + acceptor.ShouldRaise.JamDetectedEvent = false +} + +func (a *CAcceptor) RaiseJamClearedEvent() { + a.raiseXX(enum.EventJamCleared, 0) + acceptor.ShouldRaise.JamClearedEvent = false +} + +func (a *CAcceptor) RaiseInvalidCommandEvent() { + a.raiseXX(enum.EventInvalidCommand, 0) + acceptor.ShouldRaise.InvalidCommandEvent = false +} diff --git a/const.go b/const.go index 842a855..4a671bc 100644 --- a/const.go +++ b/const.go @@ -3,10 +3,10 @@ package mpost const ( GlobalName string = "mpost" GlobalDateUpdate string = "09-10-2024" - GlobalHash string = "88781e2882ac81ad31f033c4f0b9f1144b9939ff" + GlobalHash string = "3018bcf038f9318f2ffb47c26b4a52a1de65aa7f" - GlobalVersion string = "v0.1.14" + GlobalVersion string = "v0.1.15" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 14 + GlobalVersionPatch uint16 = 15 ) diff --git a/data_escrow.go b/data_escrow.go index a1c58ce..4946e74 100644 --- a/data_escrow.go +++ b/data_escrow.go @@ -35,8 +35,7 @@ func (dl *CDataLinkLayer) EscrowStack() { func (dl *CDataLinkLayer) RaiseEvents() { if acceptor.IsPoweredUp && acceptor.ShouldRaise.PowerUpEvent { - dl.log.Msg("Power Up Event Raised") - acceptor.ShouldRaise.PowerUpEvent = false + dl.Acceptor.RaisePowerUpEvent() } if acceptor.IsVeryFirstPoll { @@ -47,88 +46,69 @@ func (dl *CDataLinkLayer) RaiseEvents() { switch acceptor.Device.State { case enum.StateEscrow: if acceptor.IsPoweredUp && acceptor.ShouldRaise.PUPEscrowEvent { - dl.log.Msg("PUP Escrow Event Raised") - acceptor.ShouldRaise.PUPEscrowEvent = false + dl.Acceptor.RaisePUPEscrowEvent() } else if acceptor.ShouldRaise.EscrowEvent { - dl.log.Msg("Escrow Event Raised") - acceptor.ShouldRaise.EscrowEvent = false + dl.Acceptor.RaiseEscrowEvent() } case enum.StateStacked: if acceptor.ShouldRaise.StackedEvent { - dl.log.Msg("Stacked Event Raised") - acceptor.ShouldRaise.StackedEvent = false + dl.Acceptor.RaiseStackedEvent() } case enum.StateReturned: if acceptor.ShouldRaise.ReturnedEvent { - dl.log.Msg("Returned Event Raised") - acceptor.ShouldRaise.ReturnedEvent = false + dl.Acceptor.RaiseReturnedEvent() } case enum.StateRejected: if acceptor.ShouldRaise.RejectedEvent { - dl.log.Msg("Rejected Event Raised") - acceptor.ShouldRaise.RejectedEvent = false + dl.Acceptor.RaiseRejectedEvent() } case enum.StateStalled: if acceptor.ShouldRaise.StallDetectedEvent { - dl.log.Msg("Stall Detected Event Raised") - acceptor.ShouldRaise.StallDetectedEvent = false + dl.Acceptor.RaiseStallDetectedEvent() } } if acceptor.Device.State != enum.StateStalled && acceptor.ShouldRaise.StallClearedEvent { - dl.log.Msg("Stall Cleared Event Raised") - acceptor.ShouldRaise.StallClearedEvent = false + dl.Acceptor.RaiseStallClearedEvent() } if acceptor.Cash.BoxFull && acceptor.ShouldRaise.StackerFullEvent { - dl.log.Msg("Stacker Full Event Raised") - acceptor.ShouldRaise.StackerFullEvent = false + dl.Acceptor.RaiseStackerFullEvent() } if acceptor.IsCheated && acceptor.ShouldRaise.CheatedEvent { - dl.log.Msg("Cheated Event Raised") - acceptor.ShouldRaise.CheatedEvent = false + dl.Acceptor.RaiseCheatedEvent() } if acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxAttachedEvent { - dl.log.Msg("Cash Box Attached Event Raised") - acceptor.ShouldRaise.CashBoxAttachedEvent = false - acceptor.ShouldRaise.CashBoxRemovedEvent = true + dl.Acceptor.RaiseCashBoxAttachedEvent() } if !acceptor.Cash.BoxAttached && acceptor.ShouldRaise.CashBoxRemovedEvent { - dl.log.Msg("Cash Box Removed Event Raised") - acceptor.ShouldRaise.CashBoxRemovedEvent = false - acceptor.ShouldRaise.CashBoxAttachedEvent = true + dl.Acceptor.RaiseCashBoxRemovedEvent() } if acceptor.Device.Paused && acceptor.ShouldRaise.PauseDetectedEvent { - dl.log.Msg("Pause Detected Event Raised") - acceptor.ShouldRaise.PauseDetectedEvent = false + dl.Acceptor.RaisePauseDetectedEvent() } if !acceptor.Device.Paused && acceptor.ShouldRaise.PauseClearedEvent { - dl.log.Msg("Pause Cleared Event Raised") - acceptor.ShouldRaise.PauseClearedEvent = false + dl.Acceptor.RaisePauseClearedEvent() } if acceptor.Device.Jammed && acceptor.ShouldRaise.JamDetectedEvent { - dl.log.Msg("Jam Detected Event Raised") - acceptor.ShouldRaise.JamDetectedEvent = false + dl.Acceptor.RaiseJamDetectedEvent() } if !acceptor.Device.Jammed && acceptor.ShouldRaise.JamClearedEvent { - dl.log.Msg("Jam Cleared Event Raised") - acceptor.ShouldRaise.JamClearedEvent = false + dl.Acceptor.RaiseJamClearedEvent() } if acceptor.IsInvalidCommand && acceptor.ShouldRaise.InvalidCommandEvent { - dl.log.Msg("Invalid Command Event Raised") - acceptor.ShouldRaise.InvalidCommandEvent = false + dl.Acceptor.RaiseInvalidCommandEvent() } if acceptor.ShouldRaise.CalibrateFinishEvent { - dl.log.Msg("Calibrate Finish Event Raised") - acceptor.ShouldRaise.CalibrateFinishEvent = false + dl.Acceptor.RaiseCalibrateFinishEvent() } } diff --git a/log.go b/log.go index 79e1789..a58477a 100644 --- a/log.go +++ b/log.go @@ -10,16 +10,18 @@ import ( ///////////////////////////////////////////////////////// type LogStruct struct { - isEnable bool - log zerolog.Logger - index string + isEnable bool + log zerolog.Logger + index string + printBytes bool } -func NewLog(log zerolog.Logger, root string) LogStruct { +func NewLog(log zerolog.Logger, root string, printBytes bool) LogStruct { obj := LogStruct{ - isEnable: true, - index: root, - log: log, + isEnable: true, + index: root, + log: log, + printBytes: printBytes, } return obj } @@ -47,7 +49,7 @@ func (obj *LogStruct) Err(msg string, err error) { } func (obj *LogStruct) Bytes(msg string, data []byte) { - if !obj.isEnable { + if !obj.isEnable || !obj.printBytes { return } From 997db46e7af912c10d278ef30cc421486f259b74 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Wed, 11 Sep 2024 00:20:47 +0200 Subject: [PATCH 29/29] refactoring [v0.1.16] valid test --- 0_test.go | 4 +++- _run/values/ver.txt | 2 +- const.go | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/0_test.go b/0_test.go index 4598d36..2cefc36 100644 --- a/0_test.go +++ b/0_test.go @@ -11,6 +11,8 @@ import ( ) func TestConnect(t *testing.T) { + return + a := DefAcceptor a.AddLog( log.Output(zerolog.ConsoleWriter{ @@ -45,6 +47,6 @@ func TestConnect(t *testing.T) { t.Log(a.GetBootPN()) t.Log(a.GetDeviceState().String()) - time.Sleep(100 * time.Second) + time.Sleep(10 * time.Second) a.Close() } diff --git a/_run/values/ver.txt b/_run/values/ver.txt index 7c32f2d..00a2c6f 100644 --- a/_run/values/ver.txt +++ b/_run/values/ver.txt @@ -1 +1 @@ -v0.1.15 +v0.1.16 diff --git a/const.go b/const.go index 4a671bc..366c2fa 100644 --- a/const.go +++ b/const.go @@ -2,11 +2,11 @@ package mpost const ( GlobalName string = "mpost" - GlobalDateUpdate string = "09-10-2024" - GlobalHash string = "3018bcf038f9318f2ffb47c26b4a52a1de65aa7f" + GlobalDateUpdate string = "09-11-2024" + GlobalHash string = "e33d475848c735d5072a6377e67ec3ccabae2f11" - GlobalVersion string = "v0.1.15" + GlobalVersion string = "v0.1.16" GlobalVersionMajor string = "v0" GlobalVersionMinor uint16 = 1 - GlobalVersionPatch uint16 = 15 + GlobalVersionPatch uint16 = 16 )