/* Model for standard/IU records */ package model import ( "bytes" "encoding/gob" "encoding/json" "time" ) /* Standard tags for several properties */ const ( TAG_TIMESTAMP = 1 TAG_PRIORITY = 2 TAG_LONGITUDE = 3 TAG_LATITUDE = 4 TAG_ALTITUDE = 5 TAG_ANGLE = 6 TAG_SATELITES = 7 TAG_SPEED = 8 TAG_HDOP = 9 TAG_EVENT_SRC = 10 DEV_ONLINE = 1 DEV_OFFLINE = 0 CMD_RUP_MODULE = 0 CMD_RUP_RECORD = 1 CMD_RUP_RECORD_RESP = 100 CMD_RUP_CONFIG = 2 CMD_RUP_CONFIG_RESP = 102 CMD_RUP_VER = 3 CMD_RUP_VER_RESP = 103 CMD_RUP_FIRMW = 4 CMD_RUP_FIRMW_RESP = 104 CMD_RUP_SCARD = 5 CMD_RUP_SCARD_RESP = 107 CMD_RUP_SCARDZ = 6 CMD_RUP_SCARDZ_RESP = 107 CMD_RUP_SMGPRS = 7 CMD_RUP_SMGPRS_RESP = 108 CMD_RUP_DIAG = 9 CMD_RUP_DIAG_RESP = 109 CMD_RUP_TACHOC = 10 CMD_RUP_TACHOC_RESP = 110 CMD_RUP_TACHOD = 11 CMD_RUP_TACHOD_RESP = 111 CMD_RUP_TACHOI = 12 CMD_RUP_TACHOI_RESP = 111 CMD_RUP_TRCH = 14 CMD_RUP_TRCH_RESP = 114 CMD_RUP_GARMR = 30 CMD_RUP_GARMR_RESP = 130 CMD_RUP_GARMD = 31 CMD_RUP_GARMD_RESP = 131 CMD_RUP_CONN_PARM = 105 CMD_RUP_ODO = 106 // add CMD_RUP_FUEL_LEVEL = 205 CMD_RUP_ENGINE_SPEED = 197 CMD_RUP_HIGH_RESOL_ENGINE_TOTAL_FUEL_USED = 92 CMD_RUP_HIGH_RESOL_TOTAL_VEHICLE_DISTANCE = 114 CMD_FUEL_LEVEL1 = 207 ) // Device and standard Tag type type DevTag uint16 type StdTag uint16 type IOMap map[DevTag]StdTag type DeviceData struct { Command byte Data []byte } // Data structure for Device I/O record (8-bit) type DeviceIo8 struct { Tag StdTag Value int8 } // Data structure for Device I/O record (16-bit) type DeviceIo16 struct { Tag StdTag Value int16 } // Data structure for Device I/O record (32-bit) type DeviceIo32 struct { Tag StdTag Value int32 } // Data structure for Device I/O record (64-bit) type DeviceIo64 struct { Tag StdTag Value int64 } // Data structure for real/double I/O type DeviceIoReal struct { Tag StdTag Value float64 } // Data structure for string I/O type DeviceIoString struct { Tag StdTag Value string } // IO group type DeviceIo struct { V8 []DeviceIo8 V16 []DeviceIo16 V32 []DeviceIo32 V64 []DeviceIo64 VReal []DeviceIoReal VStr []DeviceIoString } // Data structure for standard IU records type DeviceRecord struct { Timestamp time.Time Priority byte Longitude float64 Latitude float64 Altitude float32 Angle float32 Satelites byte Speed float32 IO DeviceIo } // Basic device information type DeviceDescriptor struct { IMEI uint64 QoS byte Id uint64 Model string } // Collection of data readed from device type DeviceRecords struct { TimeReceived time.Time Descriptor DeviceDescriptor Records []DeviceRecord } //-------------------------------------------------------------------- type DeviceIoMapping struct { Descriptor DeviceDescriptor IOMapping map[DevTag]StdTag } //-------------------------------------------------------------------- type DisplayRequest struct { Timestamp time.Time IMEI uint64 PortID byte Data []byte } //-------------------------------------------------------------------- //Parser interface that must be implemented by device type Parser interface { GetMaxPacketSize() int GetMinPacketSize() int GetBufferSize() int GetStream() []byte Payload() []byte GetError() error GetCommand() byte // GetRecords() *DeviceRecords GetIMEI() uint64 GetClientResponse() []byte ExecuteAsync() } //-------------------------------------------------------------------- //Convert record to/from stream type RecordsConverter struct { } /* Convert records to JSON */ func (self RecordsConverter) ToJson(recs *DeviceRecords) (jstr string, err error) { bin, err := json.Marshal(recs) if err != nil { return "", err } jstr = string(bin) return jstr, nil } /* Convert json to records */ func (self RecordsConverter) FromJson(jstr string) (recs *DeviceRecords, err error) { var devRec DeviceRecords err = json.Unmarshal([]byte(jstr), &devRec) if err != nil { return nil, err } recs = &devRec return recs, nil } /* Convert device records to bytes */ func (self RecordsConverter) ToStream(recs *DeviceRecords) (stream []byte, err error) { var buf bytes.Buffer // Stand-in for byte stream buffer enc := gob.NewEncoder(&buf) // Will write stream. // Encode (send) some values. err = enc.Encode(recs) if err != nil { return nil, err } //Gob bytes stream = buf.Bytes() return stream, nil } /* Convert records from stream */ func (self RecordsConverter) FromStream(stream []byte) (recs *DeviceRecords, err error) { buf := bytes.NewBuffer(stream) dec := gob.NewDecoder(buf) // Will read from buffer. // Decode (receive) and print the values. var dpkt DeviceRecords err = dec.Decode(&dpkt) if err != nil { return nil, err } recs = &dpkt return recs, nil }