All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.diirt.pods.web.common.MessageWrite Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.pods.web.common;

import java.io.Writer;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.stream.JsonGenerator;

/**
 * Message to write a new value to a channel.
 *
 * @author carcassi
 */
public class MessageWrite extends Message {

    private final Object value;

    /**
     * Creates a new message based on the JSON representation.
     * 
     * @param obj JSON object
     * @throws MessageDecodeException if json format is incorrect
     */
    public MessageWrite(JsonObject obj) throws MessageDecodeException {
        super(obj);
        value = readValueFromJson(obj.get("value"));
    }

    /**
     * Creates a new message based on the given parameters.
     * 
     * @param id the channel id
     * @param value the value to write
     */
    public MessageWrite(int id, Object value) {
        super(MessageType.WRITE, id);
        this.value = value;
    }

    /**
     * The value to write.
     * 
     * @return the new value
     */
    public Object getValue() {
        return value;
    }
    
    @Override
    public void toJson(Writer writer) {
        JsonGenerator gen = Json.createGenerator(writer).writeStartObject();
        gen.write("message", getMessage().toString().toLowerCase())
            .write("id", getId());
        writeValueToJson(gen, "value", value);
        gen.writeEnd()
            .close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy