I have a gps device that records data e.g. datetime, latitude, longitude
I have an sdk that reads the data from the device. The way the data is read:
A command packet (basically a combination of int values in a struct) is sent to the device.
The device responds with the data in fixed size chunks e.g. 64bytes
Depending on the command issued I will get back differect data structs
e.g.
sending command 1 to the device returns a struct like
struct x
{
id int,
name char[20]
}
command 2 returns a collection of the following structs (basically it boils down to an array of the structs – y[12])
struct y
{
date datetime,
lat decimal,
lon decimal
}
I would then want to convert the struct to a class and save the data to a database.
What would be the best way to encapsulate the entire process, preferably using some established design pattern?
Many thanks
M
I would start with base classes for commands and responses and each specific command or response derives from those.
Then a class for sending and receiving – either the command knows how to serialize itself or another paired object can do that if you want to really decouple things.
Some factory object would create the proper response type object based on parsing the response (perhaps along with knowledge of the last sent request/command)
The response object ctor takes the struct response as a parameter.
You then make a database/persistence object that knows how to tell the objects to save themselves (or you have a pairing of response objects to persistence objects for more decoupling)
There are probably better ways to do it, but the above seems reasonable to me and a lot of this I did for writing an rs232 application that talked to a medical lab device.