summaryrefslogtreecommitdiffstats
path: root/ahl-binding/interrupt.cpp
blob: a0fa563f63ae907e19055fb5cd053c266afb4d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <json-c/json.h>
#include "ahl-binding.hpp"
#include "interrupt.hpp"
#include "role.hpp"

interrupt_t::interrupt_t(json_object* o)
{
	jcast(type_, o, "type");
	json_object * value = NULL;
	json_object_object_get_ex(o, "args", &value);
	args_ = value;
}

interrupt_t& interrupt_t::operator<<(json_object* o)
{
	jcast(type_, o, "type");
	json_object * value = NULL;
	json_object_object_get_ex(o, "args", &value);
	args_  = value;
	return *this;
}

std::string interrupt_t::type() const
{
	return type_;
}

json_object* interrupt_t::args() const
{
	return args_;
}

void interrupt_t::type(std::string v)
{
	type_ = v;
}

void interrupt_t::args(json_object* v)
{
	args_ = v;
}

int interrupt_t::apply(afb_req_t req, const role_t& role)
{
	/*if (type_ == "mute")
	{
	}
	else if (type_ == "continue")
	{
	}
	else if (type_ == "cancel")
	{
	}
	else */if (type_ == "ramp")
	{
		for(const auto& r: ahl_binding_t::instance().roles())
		{
			if (r.opened() && role.priority() > r.priority())
			{
				// { "ramp" : { "uid" : "ramp-slow", "volume" : 30 } }
				json_object* arg = json_object_new_object();
				json_object_object_add(arg, "ramp", args_);
				json_object_get(args_);
				json_object* result = nullptr;

				AFB_API_DEBUG(ahl_binding_t::instance().handle(),
					"Call '%s'/'%s' '%s'",
					r.hal().c_str(), r.stream().c_str(), json_object_to_json_string(arg));

				if(afb_api_call_sync(ahl_binding_t::instance().handle(), r.hal().c_str(), r.stream().c_str(), arg, &result, nullptr, nullptr))
				{
					afb_req_fail(req, "Failed to call 'ramp' action on stream", nullptr);
					return -1;
				}
				json_object* jvolold = nullptr;
				if (json_object_object_get_ex(result, "volold", &jvolold))
				{
					applied_on_.push_back(std::make_tuple<std::string, int>(r.uid(), json_object_get_int(jvolold)));
					AFB_API_DEBUG(ahl_binding_t::instance().handle(),
						"POLICY: Applying a ramp to '%s' stream because '%s' is opened and have higher priority!",
						r.stream().c_str(), role.stream().c_str());
				}
			}
		}
		return 0;
	}
	else
	{
		afb_req_fail(req, "Unkown interrupt uid!", nullptr);
		return -1;
	}
}

void interrupt_t::clear()
{
	for (const std::tuple<std::string, int>& state : applied_on_)
	{
		std::string role;
		int vol;
		std::tie(role, vol) = state;
		/*if (type_ == "mute")
		{
		}
		else if (type_ == "continue")
		{
		}
		else if (type_ == "cancel")
		{
		}
		else */if (type_ == "ramp")
		{
			for(const auto& r: ahl_binding_t::instance().roles())
			{
				if (r.uid() == role)
				{
					// { "ramp" : { "uid" : "ramp-slow", "volume" : 30 } }
					// Create an fake-interrupt, with the old volume
					json_object* interrupt = json_tokener_parse(json_object_to_json_string(args_));
					json_object_object_add(interrupt, "volume", json_object_new_int(vol)); // Replace the volume
					/*
					json_object* volume = nullptr;
					if (json_object_object_get_ex(interrupt, "volume", &volume))
					{
						json_object_set_int(volume, vol);
					}
					*/

					json_object* arg = json_object_new_object();
					json_object_object_add(arg, "ramp", interrupt);
					json_object* result = nullptr;

					AFB_API_DEBUG(ahl_binding_t::instance().handle(),
						"Call '%s'/'%s' '%s",
						r.hal().c_str(), r.stream().c_str(), json_object_to_json_string(arg));

					if(afb_api_call_sync(ahl_binding_t::instance().handle(), r.hal().c_str(), r.stream().c_str(), arg, &result, nullptr, nullptr))
					{
						AFB_API_ERROR(ahl_binding_t::instance().handle(),
							"Failed to call 'ramp' action on '%s'", role.c_str());
					}
					else
					{
						AFB_API_DEBUG(ahl_binding_t::instance().handle(),
							"Called 'ramp' action on '%s'", role.c_str());
					}
				}
			}
		}
	}
	applied_on_.clear();
}