A kind of method of data synchronization again to server from bracelet to APP
Technical field
The present invention relates to a kind of bracelet be worn in user's wrist, be specifically related to the method for data synchronization of this bracelet and server, relate to a kind of method of data synchronization again to server from bracelet to APP more specifically, belong to wearable device technical field.
Background technology
App operates on the mobile devices such as IPhone or IPad, carry out being connected and communication with bracelet by Bluetoothlowenergy (BLE) agreement of bluetooth 4.0, the sign data that bracelet collects is synchronized to server in two steps, the sign initial data that first step bracelet collects uploads to APP by bluetooth BLE under message mechanism data integrity, and second step APP carries out data syn-chronization by network and server under timestamp and maximum ID value mechanism data integrity.
Android is to the support of BLE agreement, read at continuous big data quantity and have delayed response, data packetloss, data packet disorder phenomenon, current Apple bracelet is because to the support of ble agreement comparatively early, agreement support is more mature and stable, and other company's bracelets read at continuous big data quantity and have more obvious delayed response, data packetloss, data packet disorder phenomenon.
Therefore, be necessary to propose a kind of technical scheme, effectively can solve reading data delay and respond late, data packetloss, the problems such as data packet disorder phenomenon, are user-friendly to, become a kind of new technical need.
Summary of the invention
For prior art above shortcomings, the present invention proposes a kind of method of data synchronization again to server from bracelet to APP, the notify action listener of the built-in character pair item of APP also keeps data syn-chronization; Press message format assembling message data, and trigger notify event; APP termination is received and analytic message head Header, receives corresponding byte number, put into buffer memory by current message content word joint number length, and overall verification is resolved, and write-back receives successful content word joint number; Bracelet termination receives write-back and sends content word joint number with last message and compare, and verification is passed through, and deletes memory data, initiates data syn-chronization next time; According to uplink time stamp and the maximum ID value acquisition uploading data of APP local record, execution is uploaded; Connection server uploads interface, resolves uploading data content, processes and is successfully back into merit mark; By adopting this method of data synchronization, realizing the correct and integrality of data in the data synchronization process of bracelet, APP and server, effectively solving loss and the imperfection of data in download and upload procedure.
The present invention solves its technical problem, and the technical scheme adopted is: a kind of method of data synchronization again to server from bracelet to APP, said method comprising the steps of:
The notify action listener of the built-in character pair item of step 100:APP also keeps data syn-chronization, and the action listener cycle is that in APP connection, bracelet disconnects to bracelet;
Step 200: whether bracelet end consults memory exists data, if there are data, by message format assembling message data, and triggers notify event;
Step 300:APP termination is received and analytic message head Header, calculate current message content word joint number and spare word joint number and message content CRC16 value, corresponding byte number is received by current message content word joint number length, put into buffer memory, and overall byte CRC16 verification, if verification is passed through, write-back receives successful content word joint number;
Step 400: bracelet termination receives write-back byte number, and sent byte number last time and contrasts, if successfully delete memory corresponding data byte number, and concurrent data syn-chronization next time;
Step 500: stab according to the uplink time of APP local record and upload and record maximum ID value acquisition uploading data, specifically whether have renewal and maximum ID value identification data to upload an execution according to timestamp mark and upload; Connection server uploads interface, resolves uploading data content, in every bar data record, injects current millisecond value operateTime field and insertignoreinto tables of data, processes and is successfully back into merit mark; APP resolves interface success return data content, upgrades APP local data uplink time stamp and uploads and record maximum ID value;
Step 600: after Account Logon APP success, stab according to APP local record server download time and server downloading data maximum ID value application download, server obtains data-base recording according to timestamp and maximum ID value, process successfully, in return data and data maximum time stamp and data in maximum ID value; APP resolves interface success return data content, and the operateTime field injecting-1 is used for being distinguished as downloading data, and renewal APP home server download time stabs and the maximum ID value of downloading data.
Further, when described APP is connected in real time with bracelet, and APP has triggered bracelet data upload function, and the data that bracelet collects to be stored in memory and real-time by BLE agreement and APP synchrodata; If APP is not when connecting in real time, the data that bracelet collects can be stored in memory, in next APP connection status and triggering synchronous data flow time start synchrodata.
Further, described APP receives sign data, judge APP and server network interface whether unobstructed, if unobstructed, upload onto the server; If APP can not be connected to server in real time, perform and upload in network recovery situation, run the detection service of timing cycle when APP starts, temporally stamp and maximum ID value mechanism detect non-uploading data and upload.
Further, set up bracelet uploading message data transmission mechanism, contents of mechanism comprises: APP be connected with bracelet and triggering synchronous flow process time, data in hardware bracelet end consults memory, assembled by data message form, need in assembling process to ensure that each sign data unit is complete, with 20 bytes for data cell byte stream form, NOTIFY to APP holds, data heading Header is resolved, APP termination receives complete message data flow, if successfully resolved write-back successfully receives content word joint number, bracelet end can delete sign data in data message from memory; If also have data, then initiate the data message of a new data, circulate with this.
Further, described heading Header formal definition: current message data content length field is that 2 byte hexadecimals are expressed, within message data content-length remains on 256 bytes, remaining data total length field is that 4 byte hexadecimals are expressed, current message data content CRC16 value 2 byte hexadecimal is expressed, and described message data content does not comprise the length of header self; When abnormal to data heading Header process: APP end resolves failure or transmitting procedure is abnormal, APP end directly initiates synch command again, and abandons data, and bracelet does not do deletion action and to initiate a new data message mutual.
Further, expanding two bytes at heading Header is current all the elements byte crc16 check value, after a message finishes receiving, APP end is to all the elements byte verification crc16 value, app verifies crc16 value and guarantees data integrity, the successful reception content word joint number of APP write-back two word, bracelet end ensures to delete and sent content same word joint number with last time.
Further, set up APP and server data timestamp and maximum ID value and upload download synchronization mechanism, described contents of mechanism comprises: when APP needs to operate a large amount of local zone time continuous print off-line data, need guarantee complete consistent with server data, when there being new bracelet image data to produce, need timely upload server, ensure data consistent.
Further, described machine-processed appearance is abnormal: when when uploading, generation exceptional interface can not successfully return, APP holds opening timing periodic service, and startup uploading data is left over detection execution and uploaded; Upload interface and return link appearance extremely at interface, when APP successfully records uplink time stamp and maximum ID value, can again execution upload, service end can receive repeating data, re-uses INSERTIGNORE elimination of duplicate data.
Further, described mechanism improvement expansion: during APP logon server, it is same APP that server returns the APP whether logged in the last time, and time not identical, APP just performs down operation.
Further, described APP local record table and service end tables of data are provided with from increasing ID, APP end upload successfully return time, renewal uplink time stamp and local maximum data storehouse table record are from increasing unique ID value, to upload timestamp and the maximum ID value of uploading data table for filter condition, perform and upload next time, upload interface and return successfully, upgrade local uplink time stamp and upload and record maximum ID value; APP end is when downloading data, it is time point before month that initial default download time stabs parameter, maximum ID value is 0, download interface success return service end data table record is from increasing unique maximum ID value, APP end renewal local record download time stamp and maximum ID value, and when upper once downloading data, return this two parameters, service end interface filters with this Parameter Conditions.
Further, described Bluetooth chip adopts nRF51822 chip.
Further, BLEProfile configuration: character pair item possesses NOTIFY ability; BLE protocol capability: each data package size of NOTIFY communication is restricted to 20byte.
Further, character pair item enable switch configures: 0x01 is for starting, and 0x00 is for stopping.
Further, described APP represents that APP operates in the mobile bracelet hardware and software systems supporting BLE function.
Further, when bracelet set of time: APP is connected with bracelet at every turn, first write APP current time to bracelet.
Further, APP local storage function: APP supports sign data local structuring SQLITE database purchase.
Further, synchronization data types definition: the sign data that hardware bracelet collects.
Further, service end capacity scheme: from increasing ID value, sign data table int supports that temporally stamp and maximum ID value mechanism upload download interface, return current service end time interface.
Compared with prior art, the invention has the beneficial effects as follows:
1, pass through to adopt this method of data synchronization, realize the correct and integrality of data in the data synchronization process of bracelet, APP and server, effectively solve loss and the imperfection of data in download and upload procedure;
2, improve method of data synchronization, facilitate and upload or the convenience of downloading data and accuracy, saved time and resource, facilitated the use of client.
Accompanying drawing explanation
Below in conjunction with accompanying drawing, specific embodiment of the invention scheme is elaborated.
Fig. 1 be the preferred embodiment of the present invention a kind of from bracelet to APP again to the structural representation of the method for data synchronization of server.
Embodiment
For making the object, technical solutions and advantages of the present invention clearly understand, below in conjunction with embodiment also with reference to accompanying drawing, the present invention is described in more detail.Should be appreciated that, these describe just exemplary, and do not really want to limit the scope of the invention.In addition, in the following description, the description to known features and technology is eliminated, to avoid unnecessarily obscuring concept of the present invention.
Fig. 1 be the preferred embodiment of the present invention a kind of from bracelet to APP again to the structural representation of the method for data synchronization of server;
As shown in Figure 1, said method comprising the steps of:
Step 100:APP opens the notify action listener of character pair item and enable data is synchronous, and the action listener cycle is that in APP connection, bracelet disconnects to bracelet.
Step 200: whether bracelet end consults memory exists data, if there are data, by message format assembling message data, and triggers notifyevent event.
Step 300:APP termination is received and analytic message head, calculate current message content word joint number, spare word joint number, corresponding byte number is received by current message content word joint number length, put into buffer memory, and overall verification is resolved, test condition comprises following two: a) by current message content word joint number, b) by concrete sign data Context resolution; If verification is passed through, write-back receives successful content word joint number.
Step 400: bracelet termination receives write-back and sends content byte with last message and compare, if verification is passed through, deletes memory data, initiates data syn-chronization next time.
Step 500: the uplink time according to APP local record obtains uploading data to stab, and execution is uploaded; Server uploads interface, resolves uploading data content, and injecting operateTime field is current millisecond value, replace tables of data, processes and is successfully back into merit mark; APP resolves interface success return data content, upgrades APP local data uplink time stamp.
Step 600: after Account login APP success, downloads according to the stamp application of APP local record server download time; Server obtains data-base recording according to APP uplink time stamp, processes successfully, and in return data and data, maximum time stabs; APP resolves interface success return data content, and injecting operateTime field is-1, and renewal APP home server download time stabs.
Data syn-chronization opportunity:
When APP is connected in real time with bracelet, and APP has triggered bracelet data upload function, and the data that bracelet collects to be stored in memory and real-time by BLE agreement and APP synchrodata; If APP is not when connecting in real time, the data that bracelet collects can be stored in memory, in next APP connection status and triggering synchronous data flow time start synchrodata.
Whether APP and server receive sign data, judge with server network interface unobstructed in time, if unobstructed, upload onto the server.If APP can not be connected to server in real time, perform and upload in network recovery situation, run the detection service of timing cycle when APP starts, mechanism of temporally stabbing detects non-uploading data and uploads.
Described sign data is the data value that bracelet transducer can gather, and has issued bracelet hardware version at present and can gather heart rate value bpm, blood oxygen levels % and wrist temperature value DEG C.
When user logs in APP, when needing to operate a large amount of sign data source continuously, temporally stab machine-processed downloading data when logging in.
Bracelet uploading message data transmission mechanism:
Requirement definition: bracelet image data is complete as one man uploads to APP.
Mechanism principle: APP be connected with bracelet and triggering synchronous flow process time, data in hardware bracelet end consults memory, assembled by { data message } form, need in assembling process to ensure that each sign data unit is complete, with 20 bytes for data cell byte stream form, NOTIFY to APP holds, by { data message head Header} resolves, APP termination receives complete message data flow, if successfully resolved write-back { successfully receives content word joint number }.Bracelet end can delete sign data in { data message } from memory, if also have data, then initiates { data message } of a new data, circulates with this;
Mechanism description: heading Header formal definition: (message data content-length controls within 256 bytes the current message data content length field of 2 byte hexadecimals expression; Do not comprise the length of header self)+4 byte hexadecimals express remaining data total length field (comprise current data packet, do not comprise the length of header self);
Mechanism abnormality processing and recovery capability: if data message head Header APP holds parsing failure or transmitting procedure extremely, APP holds not write-back message, and abandons data; Bracelet end at wait timeout or when having new data to produce, or when being triggered by APP, can initiate new once { data message } alternately.
Mechanism defect: when the failure of write-back link, APP end may receive repetition uploading data, needs to do repeating data process; When after abnormal generation, the resume speed of mechanism is relevant according to going wrong, if carried into execution a plan, relevant with frequency acquisition configuration; Disconnect if there is connection, relevant with reconnecting speed;
At APP end every bar data of the sign data parsed, bracelet generates data with { data acquisition time } and { type of sign data }, app checks local data repeatability before warehouse-in, with { data acquisition time } and { type of sign data } for condition filter repeats.
Mechanism improvement is expanded: expand two bytes current all the elements byte crc16 check value at heading Header, after a message finishes receiving, APP write-back two byte { successfully receives content word joint number }, and+two byte APP hold the crc16 value verifying out to all the elements byte, and the verification of bracelet end guarantees data integrity.
Calculate check value before being transmitted, APP end finishes receiving, the check value that write-back APP calculates, and whether bracelet end judges whether complete to transfer to APP according to write-back check value, repeat to send the last content sent.
APP and server data timestamp upload download synchronization mechanism:
Requirement definition: when APP needs to operate a large amount of local zone time continuous print off-line data, needs guarantee complete consistent with server data; When there being new bracelet image data to produce, needing timely upload server, ensureing data consistent.
Mechanism principle: each sign data record and login account are bound, and data record is stabbed if having time and time shaft positive direction is recorded as basis.APP and service end sign data table all need to add operateTime field, respectively the timestamp of record data APP end and service end operating data.When APP has resolved the sign data that bracelet uploads, parse each record and be stored into local SQLITE database, every bar record writes the millisecond value of the operating time of current APP to operateTime field, stores the sign data in a message and trigger in real time to upload interface; At APP local record { timestamp that last APP uploads onto the server }, filter operateTime field for condition with { timestamp that last APP uploads onto the server }, the local SQLITE database of inquiry APP, obtains non-uploading data.Perform by interface definition and upload.Return successfully at interface, upgrading local { timestamp that last APP uploads onto the server } is operateTime value maximum in uploading data.When APP needs downloading data to arrive local SQLITE database, in download interface, APP local record { timestamps of last APP download server data } is interface parameters, with { timestamps of last APP download server data } as interface parameters value filtering services end data.When download interface returns successfully, APP is revised as-1, stored in local SQLITE database the operateTime field in bar record every in return data; And { timestamps of last APP download server data } assignment is the maximum server-end time stamp of this downloading data.
Mechanism description: APP end needs to distinguish login { timestamp that last APP uploads onto the server } { timestamps of last APP download server data } two values to different pieces of information table difference, user, { timestamp that last APP uploads onto the server } initial value can assignment be 0L, { timestamps of last APP download server data } initial value is the time value that returns of server and to deducting based on one month section, deducts the time period to be determined by business demand.
Because APP supports multi-user login, and be all placed in same database table at each user data of APP end, each user data is distinguished with user name.Therefore during different user switching login, { different user: M} uploads download situation to need record.If different user just carries out synchronously { sign data table }.Then need record M*2*1 (app uploads this locality and service end download time stamp) individual record.In like manner have below same user different pieces of information table: N}, then need record M*2*N record.
Mechanism exception and recovery capability: if when when uploading, generation exceptional interface can not successfully return, APP holds opening timing periodic service, startup uploading data is left over detection execution and uploaded.
Startup uploading data is left over detection execution and is uploaded, and upload procedure supports the function of breakpoint transmission, and when namely data have not been uploaded, upload without the need to starting anew, the place do not completed from the last time continues to upload next time.
Mechanism defect: if APP system is many accounts many data syn-chronization streams, the timestamp of APP record can be multiplied; Return link appearance extremely when uploading interface at interface, when APP successfully records uplink time stamp, can again execution upload, service end can receive repeating data, and service end needs by replace mode process repeating data problem; Because database processing ability is comparatively strong, operateTime millisecond value likely can repeat.Cause execution to upload in download, lose the data that operateTime millisecond value repeats.
Mechanism improvement is expanded: if after APP uploading data completes, and performs when downloading next time, because { timestamps of last APP download server data } of this locality do not upgrade, can cause data segment repeated downloads problem, consumed flow.When Account login, APP unique identification such as deviceID etc. can be imported into.Whether this account of server record APP logs in same APP with the last time, and upload if coexist mutually { timestamps of last APP download server data } that return service device when successfully returning is up-to-date, APP also upgrades local record.
For APP and service end timestamp possibility replication problem, if APP and service end tables of data have from increasing ID when, APP end can when uploading interface and successfully returning, upgrade { uplink time stamp } for { local maximum data storehouse table record is from increasing unique ID value }, with data-base recording ID for filter condition, perform and upload next time, download interface successfully returns { service end data-base recording is from increasing unique ID value }, APP end upgrades local record { timestamps of last APP download server data }, and upper once carry data time, return this parameter, service end interface is with this condition filter.
After logining successfully, apply for a secondary data download interface, if service end does not have new data to produce, a downloading data sets of interfaces can be wasted and call, consumption of natural resource problem.All can to perform at the data download interface collection that APP is all and after successfully returning, APP is local downloads success flag to current down operation record one, relend service interface when helping login and return to synchronous bracelet login banner position, namely the last time complete download successfully and be same bracelet log in, then do not perform down operation, otherwise perform download interface.
Should be understood that, above-mentioned embodiment of the present invention only for exemplary illustration or explain principle of the present invention, and is not construed as limiting the invention.Therefore, any amendment made when without departing from the spirit and scope of the present invention, equivalent replacement, improvement etc., all should be included within protection scope of the present invention.