Example #1
0
            public static List <object> ReadWriteFifo(Mode mode, StateObjectDB stateObjectDB)
            {
                List <object> row       = null;
                Object        thisLock2 = new Object();

                lock (thisLock2)
                {
                    switch (mode)
                    {
                    case Mode.READ:
                        if (fifo.Count > 0)
                        {
                            row = fifo[0];
                            fifo.RemoveAt(0);
                        }
                        break;

                    case Mode.WRITE:
                        row = new List <object>()
                        {
                            stateObjectDB.terminalID, stateObjectDB.date
                        };
                        fifo.Add(row);
                        break;
                    }
                }
                return(row);
            }
Example #2
0
        public void ProcessMessages()
        {
            UInt16   sendCRC = 0;
            DateTime date;
            int      year   = 0;
            int      month  = 0;
            int      day    = 0;
            int      hour   = 0;
            int      minute = 0;
            int      second = 0;
            KeyValuePair <List <byte>, StateObject> byteState;
            KeyValuePair <UNPACK_STATUS, byte[]>    status;

            byte[]      receiveMessage = null;
            StateObject state          = null;

            byte[]          serialNumber   = null;
            PROTOCOL_NUMBER protocolNumber = PROTOCOL_NUMBER.NONE;

            try
            {
                Boolean firstMessage = true;
                acceptDone.Set();
                //loop forever
                while (true)
                {
                    allDone.WaitOne();
                    //read fifo until empty
                    while (true)
                    {
                        //read one connection until buffer doesn't contain any more packets
                        byteState = ReadWrite(PROCESS_STATE.PROCESS, null, null, -1);
                        if (byteState.Value.fifoCount == -1)
                        {
                            break;
                        }
                        state = byteState.Value;
                        while (true)
                        {
                            status = Unpack(byteState);
                            if (status.Key == UNPACK_STATUS.NOT_ENOUGH_BYTES)
                            {
                                break;
                            }
                            if (status.Key == UNPACK_STATUS.ERROR)
                            {
                                Console.WriteLine("Error : Bad Receive Message, Data");
                                break;
                            }
                            //message is 2 start bytes + 1 byte (message length) + 1 byte message length + 2 end bytes
                            receiveMessage = status.Value;
                            int messageLength = receiveMessage[2];
                            Console.WriteLine("Status : '{0}', Receive Message : '{1}'", status.Key == UNPACK_STATUS.GOOD_MESSAGE ? "Good" : "Bad", BytesToString(receiveMessage.Take(messageLength + 5).ToArray()));
                            if (status.Key != UNPACK_STATUS.GOOD_MESSAGE)
                            {
                                break;
                            }
                            else
                            {
                                if (firstMessage)
                                {
                                    if (receiveMessage[3] != 0x01)
                                    {
                                        Console.WriteLine("Error : Expected Login Message : '{0}'", BytesToString(receiveMessage));
                                        break;
                                    }
                                    firstMessage = false;
                                }
                                //skip start bytes, message length.  then go back 4 bytes (CRC and serial number)
                                serialNumber   = receiveMessage.Skip(2 + 1 + messageLength - 4).Take(2).ToArray();
                                protocolNumber = (PROTOCOL_NUMBER)receiveMessage[3];
                                Console.WriteLine("Protocol Number : '{0}'", protocolNumber.ToString());
                                switch (protocolNumber)
                                {
                                case PROTOCOL_NUMBER.LOGIN_MESSAGE:
                                    serialNumber.CopyTo(loginResponse, 4);
                                    sendCRC = crc_bytes(loginResponse.Skip(2).Take(loginResponse.Length - 6).ToArray());
                                    loginResponse[loginResponse.Length - 4] = (byte)((sendCRC >> 8) & 0xFF);
                                    loginResponse[loginResponse.Length - 3] = (byte)((sendCRC) & 0xFF);
                                    //
                                    string terminalID = Encoding.ASCII.GetString(receiveMessage.Skip(4).Take(messageLength - 5).ToArray());
                                    Console.WriteLine("Received good login message from Serial Number : '{0}', Terminal ID = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), terminalID);
                                    Console.WriteLine("Send Message : '{0}'", BytesToString(loginResponse));
                                    Send(state.workSocket, loginResponse);
                                    StateObjectDB stateObjectDB = new StateObjectDB()
                                    {
                                        terminalID = terminalID, date = DateTime.Now
                                    };
                                    WriteDBAsync.ReadWriteFifo(WriteDBAsync.Mode.WRITE, stateObjectDB);
                                    Console.WriteLine("Wrote to database");
                                    break;

                                case PROTOCOL_NUMBER.LOCATION_DATA:
                                    year   = receiveMessage[4];
                                    month  = receiveMessage[5];
                                    day    = receiveMessage[6];
                                    hour   = receiveMessage[7];
                                    minute = receiveMessage[8];
                                    second = receiveMessage[9];
                                    date   = new DateTime(2000 + year, month, day, hour, minute, second);
                                    Console.WriteLine("Received good location message from Serial Number '{0}', Time = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), date.ToLongDateString());
                                    break;

                                case PROTOCOL_NUMBER.ALARM_DATA:
                                    year   = receiveMessage[4];
                                    month  = receiveMessage[5];
                                    day    = receiveMessage[6];
                                    hour   = receiveMessage[7];
                                    minute = receiveMessage[8];
                                    second = receiveMessage[9];
                                    date   = new DateTime(2000 + year, month, day, hour, minute, second);
                                    Console.WriteLine("Received good alarm message from Serial Number '{0}', Time = '{1}'", "0x" + serialNumber[0].ToString("X2") + serialNumber[1].ToString("X2"), date.ToLongDateString());
                                    int packetLen = alarmResponse.Length - 5;
                                    alarmResponse[2] = (byte)(packetLen & 0xFF);
                                    serialNumber.CopyTo(alarmResponse, packetLen - 1);
                                    sendCRC = crc_bytes(alarmResponse.Skip(2).Take(packetLen - 1).ToArray());
                                    alarmResponse[packetLen + 1] = (byte)((sendCRC >> 8) & 0xFF);
                                    alarmResponse[packetLen + 2] = (byte)((sendCRC) & 0xFF);
                                    Console.WriteLine("Send Message : '{0}'", BytesToString(alarmResponse));
                                    Send(state.workSocket, alarmResponse);
                                    break;
                                } //end switch
                            }     // End if
                        }         //end while
                    }             //end while fifo > 0
                    allDone.Reset();
                }                 //end while true
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }