The responsibility of the middleware program:
1) to the cabinet machine to provide soket long connection server side, is Soket server. can provide tens of thousands of simultaneous client connections. In order to respond to control requests in real time, the middleware must always know the online status of a particular cabinet, and external requests can determine if the message can be forwarded.
The middleware listening port is the 5880,socket connection protocol is TCP/IP. There is currently no encryption, clear text transmission.
Process Description:
Registration :
Cabinet Machine Socket Client initiates a connection request, and the middleware socket accepts the connection,
The Cabinet machine socket client first sends a message to the middleware socket server (format: "bufferbox_sign_in" + ":" + device number, such as bufferbox_sign_in:kdl36000001)
The middleware program displays the KDL36000001 of the cabinet machine as an online status.
Reconnection after long connection interruption
When the socket client and the middleware socket server are connected, the heartbeat packet is sent periodically.
The Cabinet machine socket client checks the connection status of the middleware socket serverd every 30 seconds, initiates a 0-byte packet request, and if an exception occurs, the actual connection condition changes.
The cabinet machine socket client disconnects and re-attempts to establish a connection to re-initiate the check-in.
2) externally exposed restful-style APIs for external applications and program calls to respond to external HTTP GET/POST requests.
API prototypes,. NET Webapi:
[HttpGet]/// <summary> ///Locker Pick-up Interface (pickup)/// </summary> /// <param name= "Stationno" >device Unique number device does not exist or the device number is wrong! </param> /// <param name= "Cellno" >Box Number</param> /// <returns></returns> PublicApiactionresult Bufferbox_collect (stringStationno,stringCellno) { varresult =NewApiactionresult () {Success=false, Result=NULL, Message="The operation failed. " }; stringmsg =string. Empty; using(vardb =Newbufferboxdbentities ()) { varStationentity = db.station_signin_session. Where (st = St. Sessiondict = =Stationno). FirstOrDefault (); if(Stationentity = =NULL) {result. Message="The device does not exist or the device number is wrong! "; Result. Result=""; returnresult; } #regionApi_request_sessionvarRequestentity =Newapi_request_session {api_request_ip=request.getclientipaddress (), RequestID=Guid.NewGuid (), RequestData= Cellno +"| Collect", Requestdatatime=DateTime.Now, Resultdata= Jsonconvert.serializeobject (Newcommandresultdto {Cellno = Cellno, Action ="Pickup Items", Resultmessage ="the middleware has been forwarded and the device has not yet replied. "}), Executeflag=false, Stationno=Stationno}; Db. Api_request_session. AddObject (requestentity); #endregiondb. SaveChanges (); //Com.DataCool.DotNetExpand.LogHelper.Info (Jsonconvert.serializeobject (requestentity));msg ="api_request:"+Jsonconvert.serializeobject (requestentity); Result. Success=true; Result. Message="The device has accepted the request. "; Result. Result=requestEntity.RequestID.ToString (); } if(!string. IsNullOrEmpty (msg)) {Socket Clientsocket=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Try{clientsocket.connect (NewIPEndPoint (Ipaddress.parse (CONF. appsettings.settings["middleware_ip"]. Value), Convert.ToInt32 (CONF. appsettings.settings["Middleware_port"]. Value)); Clientsocket.send (Encoding.UTF8.GetBytes (msg)); } Catch { } } returnresult; }
Process Description:
After the WEBAPI responds to an external HTTP GET request, the device and corresponding box for this request are obtained according to the parameters of the API Stationno,cellno. A socket client connection is then initiated to the middleware socket server, the connection is made after the message "Api_request" + ":" + the request content (appended with the format), the middleware in the memory context to find the cabinet machine socket The client Connection object forwards the message sent by the Webapi just now. The cabinet machine socket client receives the API_REQUEST message from the middleware socket server and immediately executes the unlock command on the control Panel. And then immediately reply to the middleware Socket Server message format is "Command_result" + ":" + Processed request content (appended format) middleware socket server received a message to modify the results of this request to execute successfully, thus completing a complete operation
JSON format with request content:
api_request:{"$id": "1", "RequestID": "066a395a-486f-4e93-b782-fbf889e1d52f", "Token": null, "RequestData": "11| Deposit "," Resultdata ":" {\ "cellno\": \ "\", \ "resultmessage\": \ "Middleware has been forwarded, the device has not replied. \ ", \" action\ ": \" Storage \ "}", "Requestdatatime": "2016-07-16t12:00:53.9329622+08:00", "resultdatatime": null, " Executeflag ": false," api_request_ip ":" 183.70.83.165 "," Stationno ":" SHYH24000001 "," EntityKey ": {" $id ":" 2 "," entitySetName ":" Api_request_session "," Entitycontainername ":" Bufferboxdbentities "," entitykeyvalues ": [{" Key ":" RequestID "," Type ":" System.Guid "," Value ":" 066a395a-486f-4e93-b782-fbf889e1d52f "}]}}
RequestID is the GUID of each external request, which represents the unique request number for each request,
RequestData is the actual content of the request
Resultdata is another API to reply to external requests for content
Requestdatatime is the time that the request was initiated
Resultdatatime is the time of the counter client response
Locker Soket Communication Protocol and middleware implementation technical details