001/*
002# Licensed Materials - Property of IBM
003# Copyright IBM Corp. 2016 
004 */
005package com.ibm.streamsx.iot;
006
007import java.io.IOException;
008import java.io.Serializable;
009import java.time.Instant;
010
011import com.ibm.json.java.JSONObject;
012import com.ibm.streamsx.iot.spl.Schemas;
013import com.ibm.streamsx.topology.tuple.JSONAble;
014
015/**
016 * A device event.
017 */
018public class DeviceEvent implements JSONAble, Serializable {
019
020    private static final long serialVersionUID = 1L;
021
022    private final Device device;
023    private final String eventId;
024    private final Instant ts;
025    private final JSONObject data;
026    
027    public DeviceEvent(Device device, String eventId, Instant ts, JSONObject data) {
028        super();
029        this.device = device;
030        this.eventId = eventId;
031        this.ts = ts;
032        this.data = data;
033    }
034
035    public Device getDevice() {
036        return device;
037    }
038
039    public String getEventId() {
040        return eventId;
041    }
042
043    public JSONObject getData() {
044        return data;
045    }
046
047    public Instant getTs() {
048        return ts;
049    }
050
051    @Override
052    public JSONObject toJSON() {
053        JSONObject json = getDevice().toJSON();
054        json.put(Schemas.EVENT_ID, getEventId());
055        if (getTs() != null)
056            json.put("ts", getTs().toString()); //$NON-NLS-1$
057        json.put("d", getData()); //$NON-NLS-1$
058        return json;
059    }
060
061    @Override
062    public String toString() {
063        try {
064            return toJSON().serialize();
065        } catch (IOException e) {
066            throw new RuntimeException(e);
067        }
068    }
069
070}