示例#1
0
        // Generate 1 command
        public int GenerateCommand(Dictionary <string, string> dicOptValue, ref object objStruct)
        {
            // Set value from dictionary to structre
            ComFunc.SetValueToStruct(dicOptValue, ref objStruct);

            return(DIOConst.RET_OK);
        }
示例#2
0
        // Generate command byte
        public byte[] GenerateCommandByte(string CommandName, int ReqId, int SecID)
        {
            switch (CommandName)
            {
            case "CONNECT":
                stCONNECT = (CONNECT)ListCommand[0];
                stCONNECT.CONNECTRequestId = ReqId;
                stCONNECT.CONNECTSessionId = SecID;
                return(ComFunc.StructureToByteArray(stCONNECT));

            case "SHAKE":
                stSHAKE = (SHAKE)ListCommand[1];
                stSHAKE.SHAKERequestId = ReqId;
                stSHAKE.SHAKESessionId = SecID;
                return(ComFunc.StructureToByteArray(stSHAKE));

            case "TERMINATE":
                stTERMINATE = (TERMINATE)ListCommand[2];
                stTERMINATE.TERMINATERequestId = ReqId;
                stTERMINATE.TERMINATESessionId = SecID;
                return(ComFunc.StructureToByteArray(stTERMINATE));

            case "CHECKIN":
                stCHECKIN = (CHECKIN)ListCommand[3];
                stCHECKIN.CHECKINRequestId = ReqId;
                stCHECKIN.CHECKINSessionId = SecID;
                return(ComFunc.StructureToByteArray(stCHECKIN));

            case "COMMIT":
                stCOMMIT = (COMMIT)ListCommand[4];
                stCOMMIT.COMMITRequestId = ReqId;
                stCOMMIT.COMMITSessionId = SecID;
                return(ComFunc.StructureToByteArray(stCOMMIT));

            case "ROLLBACK":
                stROLLBACK = (ROLLBACK)ListCommand[5];
                stROLLBACK.ROLLBACKRequestId = ReqId;
                stROLLBACK.ROLLBACKSessionId = SecID;
                return(ComFunc.StructureToByteArray(stROLLBACK));

            case "CHARGE":
                stCHARGE = (CHARGE)ListCommand[6];
                stCHARGE.CHARGERequestId = ReqId;
                stCHARGE.CHARGESessionId = SecID;
                return(ComFunc.StructureToByteArray(stCHARGE));

            default:
                return(null);
            }
        }
示例#3
0
        // Set value from directory to structure
        public static int SetValueToStruct(Dictionary <string, string> dicOptValue, ref object objStruct)
        {
            try
            {
                // Scan all field in input struct object
                Type tyObj = objStruct.GetType();
                foreach (FieldInfo fiField in tyObj.GetFields())
                {
                    // If type of field is unsigned integer 32 bit
                    if (fiField.FieldType == typeof(System.Int32))
                    {
                        // Get value from dictionary
                        string string_value = dicOptValue[fiField.Name];
                        // Convert to integer
                        Int32 int_value = ComFunc.StringToInt32(string_value);
                        // Set value to structure
                        fiField.SetValue(objStruct, (Int32)int_value);
                    }
                    else if (fiField.FieldType == typeof(System.Char[]))
                    {
                        // Get value from dictionary
                        string string_value = dicOptValue[fiField.Name];
                        // Get field length
                        char[] field_value = (char [])fiField.GetValue(objStruct);

                        // Copy value from directory to field
                        Array.Copy(Encoding.ASCII.GetBytes(string_value),
                                   field_value,
                                   System.Math.Min(field_value.Length, string_value.Length));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            return(DIOConst.RET_OK);
        }
示例#4
0
        // Handle action: User click Start button
        private void tbarStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (listFile.SelectedItem == null)
                {
                    MessageBox.Show("Select 1 file in list box to continue !");
                    return;
                }

                // Get connection option
                int    iCon     = ComFunc.StringToInt32(tbarConnectionTxt.Text);
                int    iLoop    = ComFunc.StringToInt32(tbarLoopTxt.Text);
                string stIPAdd  = objSettingConfig.allItems["SocketIP"];
                int    iPort    = ComFunc.StringToInt32(objSettingConfig.allItems["SocketPort"]);
                string stAES    = objSettingConfig.allItems["SocketAES"];
                string st3DES   = objSettingConfig.allItems["Socket3DES"];
                int    iTimeout = ComFunc.StringToInt32(objSettingConfig.allItems["CONNECTTimeout"]);
                string stUser   = objSettingConfig.allItems["CONNECTUsername"];
                string stPass   = objSettingConfig.allItems["CONNECTPassword"];
                txtStatus.Text = "";
                listStatus.Clear();
                listConnection.Clear();

                // Get scenario step from GUI
                List <string> listScenarioStep = new List <string>();
                for (int i = 0; i < gridFileStep.RowCount; i++)
                {
                    if (gridFileStep.Rows[i].Cells[2].Value != null)
                    {
                        string stStep = gridFileStep.Rows[i].Cells[2].Value.ToString();
                        if (stStep != "")
                        {
                            listScenarioStep.Add(stStep);
                        }
                    }
                }

                // Create connection
                for (int i = 0; i < iCon; i++)
                {
                    // Set parameter for worker thread
                    RunScenarioWorkerThread con = new RunScenarioWorkerThread();
                    con.ThreadID         = i;
                    con.IPAdd            = stIPAdd;
                    con.Port             = iPort;
                    con.Loop             = iLoop;
                    con.EncryptAES       = stAES;
                    con.Encrypt3DES      = st3DES;
                    con.Timeout          = iTimeout;
                    con.objSettingConfig = objSettingConfig;
                    con.listScenario     = listScenarioStep;
                    con.User             = stUser;
                    con.Pass             = stPass;

                    // Add event listener
                    con.EventHappned += OnComNotification;

                    // Add connection to list
                    listConnection.Add(con);

                    // Add list status
                    string status = "";
                    listStatus.Add(status);

                    // Start thread
                    listConnection[i].Run();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error to create connection !\nError =  " + ex.Message);
            }
        }
示例#5
0
        private void tbarTest_Click(object sender, EventArgs e)
        {
            // Get connection option
            int    iCon     = ComFunc.StringToInt32(tbarConnectionTxt.Text);
            int    iLoop    = ComFunc.StringToInt32(tbarLoopTxt.Text);
            string stIPAdd  = objSettingConfig.allItems["SocketIP"];
            int    iPort    = ComFunc.StringToInt32(objSettingConfig.allItems["SocketPort"]);
            string stAES    = objSettingConfig.allItems["SocketAES"];
            string st3DES   = objSettingConfig.allItems["Socket3DES"];
            int    iTimeout = ComFunc.StringToInt32(objSettingConfig.allItems["CONNECTTimeout"]);
            string stUser   = objSettingConfig.allItems["CONNECTUsername"];
            string stPass   = objSettingConfig.allItems["CONNECTPassword"];

            txtStatus.Text = "";
            listStatus.Clear();
            listConnection.Clear();

            // Get scenario step from GUI
            List <string> listScenarioStep = new List <string>();

            for (int i = 0; i < gridFileStep.RowCount; i++)
            {
                if (gridFileStep.Rows[i].Cells[2].Value != null)
                {
                    string stStep = gridFileStep.Rows[i].Cells[2].Value.ToString();
                    if (stStep != "")
                    {
                        listScenarioStep.Add(stStep);
                    }
                }
            }

            // Test to run script
            // Set parameter for worker thread
            RunScenarioWorkerThread con = new RunScenarioWorkerThread();

            con.ThreadID         = 0;
            con.IPAdd            = stIPAdd;
            con.Port             = iPort;
            con.Loop             = iLoop;
            con.EncryptAES       = stAES;
            con.Encrypt3DES      = st3DES;
            con.Timeout          = iTimeout;
            con.objSettingConfig = objSettingConfig;
            con.listScenario     = listScenarioStep;
            con.User             = stUser;
            con.Pass             = stPass;

            // Add event listener
            con.EventHappned += OnComNotification;

            // Add connection to list
            listConnection.Add(con);

            // Add list status
            string status = "";

            listStatus.Add(status);

            // Start thread
            listConnection[0].Run();
        }
        // Main work of thread
        public async void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            try
            {
                // Create new connection
                byte[] byIVArray = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                var    p         = new APIClient(IPAdd, Port, Timeout, EncryptAES, byIVArray);

                // Loop for repeat
                for (round = 0; round < Loop; round++)
                {
                    // Loop for all step
                    for (int i = 0; i < listScenario.Count; i++)
                    {
                        switch (listScenario[i])
                        {
                        case "CONNECT":
                            // Get command parameter
                            int    stationID  = ComFunc.StringToInt32(objSettingConfig.allItems["CONNECTStation"]);
                            string serverIP   = objSettingConfig.allItems["SocketServerIP"];
                            int    serverPort = ComFunc.StringToInt32(objSettingConfig.allItems["SocketServerPort"]);
                            // Create command
                            ReportProgress("CONNECT to server ...");
                            var connectReq = new ConnectRequestBody(User, Pass, stationID);
                            // Send and Wait response
                            var re = await p.ConnectAsync(serverIP, serverPort, connectReq);

                            // Report status
                            ReportProgress("CONNECT result: " + re?.Status.ToString());
                            break;

                        case "SHAKE":
                            // Get command parameter
                            // Send command to server and wait response
                            ReportProgress("Send SHAKE to server ...");
                            var re2 = await p.ShakeAsync();

                            // Report status
                            ReportProgress("SHAKE result: " + re2?.Status.ToString());
                            break;

                        case "TERMINATE":
                            // Get command parameter
                            // Send command to server and Wait response
                            ReportProgress("Send TERMINATE to server ...");
                            var re3 = await p.TerminateAsync();

                            // Report status
                            ReportProgress("TERMINATE result: " + re3?.Status.ToString());
                            break;

                        case "CHECKIN":
                            // Get command parameter
                            string eTagCHECKIN    = objSettingConfig.allItems["CHECKINEtag"];
                            int    stationCHECKIN = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                            int    laneCHECKIN    = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                            // Send command to server and Wait response
                            ReportProgress("Send CHECKIN to server ...");
                            var checkin = new ChargeRequestBody(eTagCHECKIN, stationCHECKIN, laneCHECKIN);
                            var re4     = await p.CheckinAsync(checkin);

                            // Report status
                            ReportProgress("CHECKIN result: " + re4?.Status.ToString());
                            PlateRes  = re4.Plate;
                            TicketRes = re4.TicketId;
                            break;

                        case "COMMIT":
                            // Get command parameter
                            string eTagCOMMIT    = objSettingConfig.allItems["COMMITEtag"];
                            int    stationCOMMIT = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITStation"]);
                            int    laneCOMMIT    = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITLane"]);
                            int    imageCOMMIT   = ComFunc.StringToInt32(objSettingConfig.allItems["COMMITImageCount"]);
                            // Send command to serve rand wait response
                            ReportProgress("Send COMMIT to server ...");
                            var transaction = new TransactionRequestBody(eTagCOMMIT, stationCOMMIT, laneCOMMIT, TicketRes, PlateStatus.Match, PlateRes, imageCOMMIT);
                            var re5         = await p.CommitAsync(transaction);

                            // Report status
                            ReportProgress("COMMIT result: " + re5?.Status.ToString());
                            break;

                        case "ROLLBACK":
                            // Get command parameter
                            string eTagROLLBACK    = objSettingConfig.allItems["ROLLBACKEtag"];
                            int    stationROLLBACK = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKStation"]);
                            int    laneROLLBACK    = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKLane"]);
                            int    imageROLLBACK   = ComFunc.StringToInt32(objSettingConfig.allItems["ROLLBACKImageCount"]);
                            // Send command to serve rand wait response
                            ReportProgress("Send ROLLBACK to server ...");
                            var rollback = new TransactionRequestBody(eTagROLLBACK, stationROLLBACK, laneROLLBACK, TicketRes, PlateStatus.Match, PlateRes, imageROLLBACK);
                            var re6      = await p.RollbackAsync(rollback);

                            // Report status
                            ReportProgress("ROLLBACK result: " + re6?.Status.ToString());
                            break;

                        case "CHARGE":
                            // Get command parameter
                            string eTagCHARGE    = objSettingConfig.allItems["CHARGEEtag"];
                            int    stationCHARGE = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINStation"]);
                            int    laneCHARGE    = ComFunc.StringToInt32(objSettingConfig.allItems["CHECKINLane"]);
                            // Send command to server and Wait response
                            ReportProgress("Send CHARGE to server ...");
                            var charge_req = new ChargeRequestBody(eTagCHARGE, stationCHARGE, laneCHARGE);
                            var re7        = await p.ChargeAsync(charge_req);

                            // Report status
                            break;

                        default:
                            ReportProgress("Error = Wrong Command");
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Report Error
                ReportProgress("Error = " + ex.Message);
            }

            // Report progress: complete
            ReportProgress("COMPLETED !");
        }