#include "poweritem.h"
#include <QProcess>
#include <QApplication>
#include <QDebug>

PowerItem::PowerItem(uint32_t itemIdIn, QString name,  uint8_t value, QObject* parent):
	Item(itemIdIn, name, value, parent)
{
	globalSensors.sensorGotState(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 0, "Shutdown Imminent", true), SENSOR_UPDATE_BACKEND);
	value_ = true;
	hidden_ = true;
	type_ = ITEM_VALUE_NO_VALUE;
}

void PowerItem::enactValue(uint8_t value)
{
	if(!value)
	{
		qDebug()<<"shutdown";
		QTimer::singleShot(5000, this, &PowerItem::timeout);
		globalSensors.sensorGotState(Sensor(Sensor::TYPE_SHUTDOWN_IMMINENT, 0, 1, "Shutdown Imminent", true), SENSOR_UPDATE_BACKEND);
	}
}

void PowerItem::timeout()
{
	qDebug()<<"shutdown timeout";
	QProcess::startDetached("syncoff", QStringList());
}

void PowerItem::store(QJsonObject& json)
{
	json["Type"] = "Power";
	Item::store(json);
}
