How to store data through web service when data co

2019-07-09 00:38发布

I have one device "installed" on a users desk (a desk is nothing but a chair or table on which user will sit), and I will be supporting thousands of desks.

A user will have one "chip" and the user will scan this chip on the device which is installed on their desk.

The device will read the data off the chip and will send it to my laptop which will also have one of the devices installed, except this device is the main device responsible for collecting all user scan chip data.

All the data will be routed to my device via a wifi router and I will listen to this from my Main device and read data from this device from my laptop via serial port connection.

This data sending will happen as each user number scans his/her chip.

I have created a windows form application which will continuously run in the background on my laptop, and will be listening to my serial port on which main device is connected.

This is my code taken from here: Source Code Reference:

public partial class MainUI : Form
    {
        SerialPortManager _spManager;
        public MainUI()
        {
            InitializeComponent();
            UserInitialization();
        }
   }

    private void UserInitialization()
        {
            _spManager = new SerialPortManager();
            _spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            this.FormClosing += new FormClosingEventHandler(MainUI_FormClosing);
        }

  private void MainUI_Load(object sender, EventArgs e)
        {
           _spManager.StartListening()
        }

 void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int dataLength = _serialPort.BytesToRead;
            byte[] data = new byte[dataLength];
            int nbrDataRead = _serialPort.Read(data, 0, dataLength);
            if (nbrDataRead == 0)
                return;

            // Send data to whom ever interested
            if (NewSerialDataRecieved != null)
            {
                NewSerialDataRecieved(this, new SerialDataEventArgs(data));
            }
        }

  void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
                this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
                return;
            }
            //data is converted to text
            string str = Encoding.ASCII.GetString(e.Data);

            if (!string.IsNullOrEmpty(str))
            {
                  //Here i will store that data in to my database through web service.
                  //What i should use whether WCF service or Web Api because data will be continuos like at a
                  //time more than 10 or 100 user can scan data at the same time so this event will be fired continuously.
                  //I am using entity framework to store data in to my database and how to ansynchornously call web service to store my data
              //so that my call doesnt block incoming data to serial port
            }
        }

My main concern is I will have numerous users who will scan data at the same time and how I will handle when more than 10 or 100 user scan the data at the same time.

How can I mitigate this potential issue?

2条回答
姐就是有狂的资本
2楼-- · 2019-07-09 01:10

close your serial port and load every some-amount-of-time. After that some-amount-of-time open the port and scan all devices, then close it again.

        public void MainUI.Load(Object sender, Eventargs e)
        {
            if (_spmanager != null && !_spManager.IsOpen)
                //*write the code here where it opens and starts listening
                _spmanager.StartListening();
                //*write the code here where it waits a little bit then 
                _spmanager.Close();
 }

Therefore everytime it loads it starts when the port is closed, it opens for a little bit, scans whatever values are true and then closes again.

I am not very sure about this but it is just an idea of how to handle it. The code might not be accurate or currect I just wrote it quickly. Take the idea from this

查看更多
混吃等死
3楼-- · 2019-07-09 01:22

Ok, if i got the question right you need to do something like this ...

  void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int dataLength = _serialPort.BytesToRead;
            byte[] data = new byte[dataLength];
            int nbrDataRead = _serialPort.Read(data, 0, dataLength);
            if (nbrDataRead == 0)
                return;

            // Send data to api
            string str = Encoding.ASCII.GetString(e.Data);
                if (!string.IsNullOrEmpty(str))
                {
                      var api = new HttpClient();
                      api.BaseUrl("http://somewhere.com");
                      api.PostAsJsonAsync("api/Something", str)
                }

            if (this.InvokeRequired)
            {
                // Using this.Invoke causes deadlock when closing serial port,
                // and BeginInvoke is good practice anyway.
                this.BeginInvoke(new EventHandler<SerialDataEventArgs>(
                    _spManager_NewSerialDataRecieved), new object[] { sender, e
                });
                return;
            }
        }


// i think this can go completely ...
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)

That posts the data to webapi but whilst that post is taking place on another thread the serial port can carry on receiving data

查看更多
登录 后发表回答