From 5fba1ab3752006e09b39412d9e23149158c00a59 Mon Sep 17 00:00:00 2001 From: SunSung W541 Date: Wed, 11 Sep 2024 21:45:00 +0200 Subject: [PATCH] refactoring [v0.3.4] methods end --- 0_test.go | 4 +- methods.go | 219 ++-------------------------------------------- methods_BNF.go | 66 ++++++++++++++ methods_others.go | 176 +++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 214 deletions(-) create mode 100644 methods_BNF.go create mode 100644 methods_others.go diff --git a/0_test.go b/0_test.go index 58278ca..856ed78 100644 --- a/0_test.go +++ b/0_test.go @@ -108,10 +108,10 @@ func TestConnect(t *testing.T) { t.Log(a.Method.Device.Get.SerialNumber()) t.Log(a.Method.Bill.Get.Self()) t.Log(a.Method.Application.Get.PN()) - t.Log(a.Method.Get.BootPN()) + t.Log(a.Method.Other.GetBootPN()) t.Log(a.Method.Device.Get.Type()) - t.Log(a.Method.Get.BNFStatus().String()) + t.Log(a.Method.BNF.Get.Status().String()) default: continue diff --git a/methods.go b/methods.go index b10b33d..fd04709 100644 --- a/methods.go +++ b/methods.go @@ -1,22 +1,10 @@ package mpost -import ( - "errors" - "github.com/hard-soft-ware/mpost/acceptor" - "github.com/hard-soft-ware/mpost/consts" - "github.com/hard-soft-ware/mpost/enum" - "github.com/hard-soft-ware/mpost/hook" - "time" -) - //////////////////////////////////// type MethodsObj struct { a *MpostObj - Get MethodsGetObj - Set MethodsSetObj - Enable *MethodsEnableObj Application *MethodsApplicationObj Audit *MethodsAuditObj @@ -27,22 +15,16 @@ type MethodsObj struct { Device *MethodsDeviceObj Orientation *MethodsOrientationObj Variant *MethodsVariantObj + Timeout *MethodsTimeoutObj + BNF *MethodsBNFObj - Timeout *MethodsTimeoutObj + Other *MethodsOtherObj } -type MethodsGetObj struct{ a *MpostObj } - -type MethodsSetObj struct{ a *MpostObj } - //// func (a *MpostObj) newMethods() *MethodsObj { - obj := MethodsObj{} - - obj.a = a - obj.Get.a = a - obj.Set.a = a + obj := MethodsObj{a: a} obj.Enable = obj.newEnable() obj.Application = obj.newApplication() @@ -54,199 +36,12 @@ func (a *MpostObj) newMethods() *MethodsObj { obj.Device = obj.newDevice() obj.Orientation = obj.newOrientation() obj.Variant = obj.newVariant() - obj.Timeout = obj.newTimeout() + obj.BNF = obj.newBNF() + + obj.Other = obj.newOther() return &obj } //////////////// - -func (m *MethodsObj) Calibrate() { - m.a.Log.Method("Calibrate", nil) - if !acceptor.Connected { - m.a.Log.Err("Calibrate", errors.New("Calibrate called when not connected")) - return - } - - if acceptor.Device.State != enum.StateIdling { - m.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 - - hook.Raise.Calibrate.Start() - - hook.CalibrateProgress = true - - startTickCount := time.Now() - - for { - reply, err := m.a.SendSynchronousCommand(payload) - if err != nil { - m.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 { - hook.Raise.Calibrate.Finish() - return - } - } -} - -//// - -func (m *MethodsGetObj) Connected() bool { - m.a.Log.Method("GetConnected", nil) - return acceptor.Connected -} - -func (m *MethodsGetObj) DocType() enum.DocumentType { - m.a.Log.Method("GetDocType", nil) - return m.a.DocType -} - -func (m *MethodsGetObj) Version() string { - m.a.Log.Method("GetVersion", nil) - return acceptor.Version -} - -func (m *MethodsGetObj) AutoStack() bool { - m.a.Log.Method("GetAutoStack", nil) - return acceptor.AutoStack -} - -func (m *MethodsGetObj) HighSecurity() bool { - m.a.Log.Method("GetHighSecurity", nil) - return acceptor.HighSecurity -} - -func (m *MethodsGetObj) BNFStatus() enum.BNFStatusType { - m.a.Log.Method("Getting BNF status", nil) - err := acceptor.Verify(acceptor.Cap.BNFStatus, "BNFStatus") - - if err != nil { - m.a.Log.Err("GetBNFStatus", err) - return enum.BNFStatusUnknown - } - - payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxBNFStatus.Byte()} - - reply, err := m.a.SendSynchronousCommand(payload) - if err != nil { - m.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 (m *MethodsGetObj) BootPN() string { - m.a.Log.Method("GetBootPN", nil) - - err := acceptor.Verify(acceptor.Cap.BootPN, "GetBootPN") - if err != nil { - m.a.Log.Err("GetBootPN", err) - return "" - } - - payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorBootPartNumber.Byte()} - - reply, err := m.a.SendSynchronousCommand(payload) - if err != nil { - m.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 (m *MethodsGetObj) CapAssetNumber() bool { - m.a.Log.Method("GetCapAssetNumber", nil) - return acceptor.Cap.AssetNumber -} - -func (m *MethodsGetObj) CapEscrowTimeout() bool { - m.a.Log.Method("GetCapEscrowTimeout", nil) - return acceptor.Cap.EscrowTimeout -} - -func (m *MethodsGetObj) CapFlashDownload() bool { - m.a.Log.Method("GetCapFlashDownload", nil) - return acceptor.Cap.FlashDownload -} - -func (m *MethodsGetObj) CapPupExt() bool { - m.a.Log.Method("GetCapPupExt", nil) - return acceptor.Cap.PupExt -} - -func (m *MethodsGetObj) CapTestDoc() bool { - m.a.Log.Method("GetCapTestDoc", nil) - return acceptor.Cap.TestDoc -} - -func (m *MethodsGetObj) CapBNFStatus() bool { - m.a.Log.Method("GetCapBNFStatus", nil) - return acceptor.Cap.BNFStatus -} - -func (m *MethodsGetObj) CapCalibrate() bool { - m.a.Log.Method("GetCapCalibrate", nil) - return acceptor.Cap.Calibrate -} - -func (m *MethodsGetObj) CapBookmark() bool { - m.a.Log.Method("GetCapBookmark", nil) - return acceptor.Cap.Bookmark -} - -func (m *MethodsGetObj) CapNoPush() bool { - m.a.Log.Method("GetCapNoPush", nil) - return acceptor.Cap.NoPush -} - -func (m *MethodsGetObj) CapBootPN() bool { - m.a.Log.Method("GetCapBootPN", nil) - return acceptor.Cap.BootPN -} - -//// - -func (m *MethodsSetObj) AutoStack(v bool) { - m.a.Log.Method("SetAutoStack", nil) - acceptor.AutoStack = v -} - -func (m *MethodsSetObj) HighSecurity(v bool) { - m.a.Log.Method("SetHighSecurity", nil) - acceptor.HighSecurity = v -} diff --git a/methods_BNF.go b/methods_BNF.go new file mode 100644 index 0000000..4d34d8a --- /dev/null +++ b/methods_BNF.go @@ -0,0 +1,66 @@ +package mpost + +import ( + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" +) + +//////////////////////////////////// + +type MethodsBNFObj struct { + a *MpostObj + Get MethodsBNFGetObj +} + +type MethodsBNFGetObj struct{ a *MpostObj } + +func (m *MethodsObj) newBNF() *MethodsBNFObj { + obj := MethodsBNFObj{} + + obj.a = m.a + obj.Get.a = m.a + + return &obj +} + +//////////////// + +func (m *MethodsBNFGetObj) Status() enum.BNFStatusType { + m.a.Log.Method("Getting BNF status", nil) + err := acceptor.Verify(acceptor.Cap.BNFStatus, "BNFStatus") + + if err != nil { + m.a.Log.Err("GetBNFStatus", err) + return enum.BNFStatusUnknown + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxBNFStatus.Byte()} + + reply, err := m.a.SendSynchronousCommand(payload) + if err != nil { + m.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 (m *MethodsBNFGetObj) CapStatus() bool { + m.a.Log.Method("GetCapBNFStatus", nil) + return acceptor.Cap.BNFStatus +} diff --git a/methods_others.go b/methods_others.go new file mode 100644 index 0000000..830c32f --- /dev/null +++ b/methods_others.go @@ -0,0 +1,176 @@ +package mpost + +import ( + "errors" + "github.com/hard-soft-ware/mpost/acceptor" + "github.com/hard-soft-ware/mpost/consts" + "github.com/hard-soft-ware/mpost/enum" + "github.com/hard-soft-ware/mpost/hook" + "time" +) + +//////////////////////////////////// + +type MethodsOtherObj struct { + a *MpostObj +} + +func (m *MethodsObj) newOther() *MethodsOtherObj { + obj := MethodsOtherObj{} + obj.a = m.a + return &obj +} + +//////////////// + +func (m *MethodsOtherObj) Calibrate() { + m.a.Log.Method("Calibrate", nil) + if !acceptor.Connected { + m.a.Log.Err("Calibrate", errors.New("Calibrate called when not connected")) + return + } + + if acceptor.Device.State != enum.StateIdling { + m.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 + + hook.Raise.Calibrate.Start() + + hook.CalibrateProgress = true + + startTickCount := time.Now() + + for { + reply, err := m.a.SendSynchronousCommand(payload) + if err != nil { + m.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 { + hook.Raise.Calibrate.Finish() + return + } + } +} + +//// + +func (m *MethodsOtherObj) GetConnected() bool { + m.a.Log.Method("GetConnected", nil) + return acceptor.Connected +} + +func (m *MethodsOtherObj) GetDocType() enum.DocumentType { + m.a.Log.Method("GetDocType", nil) + return m.a.DocType +} + +func (m *MethodsOtherObj) GetVersion() string { + m.a.Log.Method("GetVersion", nil) + return acceptor.Version +} + +func (m *MethodsOtherObj) GetAutoStack() bool { + m.a.Log.Method("GetAutoStack", nil) + return acceptor.AutoStack +} + +func (m *MethodsOtherObj) GetHighSecurity() bool { + m.a.Log.Method("GetHighSecurity", nil) + return acceptor.HighSecurity +} + +func (m *MethodsOtherObj) GetBootPN() string { + m.a.Log.Method("GetBootPN", nil) + + err := acceptor.Verify(acceptor.Cap.BootPN, "GetBootPN") + if err != nil { + m.a.Log.Err("GetBootPN", err) + return "" + } + + payload := []byte{consts.CmdAuxiliary.Byte(), 0, 0, consts.CmdAuxAcceptorBootPartNumber.Byte()} + + reply, err := m.a.SendSynchronousCommand(payload) + if err != nil { + m.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 (m *MethodsOtherObj) GetCapAssetNumber() bool { + m.a.Log.Method("GetCapAssetNumber", nil) + return acceptor.Cap.AssetNumber +} + +func (m *MethodsOtherObj) GetCapEscrowTimeout() bool { + m.a.Log.Method("GetCapEscrowTimeout", nil) + return acceptor.Cap.EscrowTimeout +} + +func (m *MethodsOtherObj) GetCapFlashDownload() bool { + m.a.Log.Method("GetCapFlashDownload", nil) + return acceptor.Cap.FlashDownload +} + +func (m *MethodsOtherObj) GetCapPupExt() bool { + m.a.Log.Method("GetCapPupExt", nil) + return acceptor.Cap.PupExt +} + +func (m *MethodsOtherObj) GetCapTestDoc() bool { + m.a.Log.Method("GetCapTestDoc", nil) + return acceptor.Cap.TestDoc +} + +func (m *MethodsOtherObj) GetCapCalibrate() bool { + m.a.Log.Method("GetCapCalibrate", nil) + return acceptor.Cap.Calibrate +} + +func (m *MethodsOtherObj) GetCapBookmark() bool { + m.a.Log.Method("GetCapBookmark", nil) + return acceptor.Cap.Bookmark +} + +func (m *MethodsOtherObj) GetCapNoPush() bool { + m.a.Log.Method("GetCapNoPush", nil) + return acceptor.Cap.NoPush +} + +func (m *MethodsOtherObj) GetCapBootPN() bool { + m.a.Log.Method("GetCapBootPN", nil) + return acceptor.Cap.BootPN +} + +//// + +func (m *MethodsOtherObj) SetAutoStack(v bool) { + m.a.Log.Method("SetAutoStack", nil) + acceptor.AutoStack = v +} + +func (m *MethodsOtherObj) SetHighSecurity(v bool) { + m.a.Log.Method("SetHighSecurity", nil) + acceptor.HighSecurity = v +}