aboutsummaryrefslogtreecommitdiffstats
path: root/conf.d/project/lua.d/onload-aaaa-03-controls.lua
blob: ae1269bfbd2af452828d535a33d590b8bd0defc1 (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
152
153
154
155
156
157
158
--[[
  Copyright (C) 2016 "IoT.bzh"
  Author Fulup Ar Foll <fulup@iot.bzh>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  Following function are called when a control activate a label with
  labeller api -> APi=label VERB=dispatch
  arguments are
    - source (0) when requesting the label (-1) when releasing
    - label comme from config given with 'control' in onload-middlename-xxxxx.json
    - control is the argument part of the query as providing by control requesting the label.

--]]

_CurrentHalVolume={}



--[[ Apply control
 * source=1  permanent change requested
 * source=0  temporally request control
 * source=-1 temporally release control
-- ------------------------------------ --]]
local function Apply_Hal_Control(source, label, adjustment) 
 local HAL = _Global_Context["registry"]
 
    -- check we really got some data
    if (adjustment == nil) then
        AFB:error ("--* (Hoops) Control should provide volume adjustment")  
        return 1
    end

    -- loop on each HAL save current volume and push adjustment
    for key,hal in pairs(_Global_Context["registry"]) do
       printf ("--- HAL=%s", Dump_Table(hal))

        -- action set loop on active HAL and get current volume
        -- if label respond then do volume adjustment

        if (source >= 0) then

            -- get current volume for each HAL
            local err,result= AFB:servsync(hal["api"],"ctlget", {["label"]=label})

            -- if no error save current volume and set adjustment
            if (err ~= nil) then
                local response= result["response"]
                printf ("--- Response %s=%s", hal["api"], Dump_Table(response))

                if (response == nil) then
                   printf ("--- Fail to Activate '%s'='%s' result=%s", hal["api"], label, Dump_Table(result)) 
                   return 1 -- unhappy
                end

                -- save response in global space 
                _CurrentHalVolume [hal["api"]] = response

                -- finally set the new value
                local query= {
                    ["tag"]= response["tag"],
                    ["val"]= adjustment
                }

                -- best effort to set adjustment value
                AFB:servsync(hal["api"],"ctlset",query)
            end

        else  -- when label is release reverse action at preempt time
           
            if (_CurrentHalVolume [hal["api"]] ~= nil) then

                printf("--- Restoring initial volume HAL=%s Control=%s", hal["api"], _CurrentHalVolume [hal["api"]])

                AFB:servsync(hal["api"],"ctlset", _CurrentHalVolume [hal["api"]])
            end
        end    

    end
    return 0 -- happy end
end


-- Temporally adjust volume
function _Temporarily_Control(source, control, client)

    printf ("[--> _Temporarily_Control -->] source=%d control=%s client=%s", source, Dump_Table(control), Dump_Table(client))

    -- Init should have been properly done
    if (_Global_Context["registry"] == nil) then
       AFB:error ("--* (Hoops) No Hal in _Global_Context=%s", Dump_Table(_Global_Context)) 
       return 1
    end

    -- make sure label as valid
    if (control["ctl"] == nil or control["val"] == nil) then
        AFB:error ("--* Action Ignore no/invalid control=%s", Dump_Table(control)) 
        return 1 -- unhappy
    end

    if (source == 0) then
        AFB:info("-- Adjust %s=%d", control["ctl"], control["val"])
        local error=Apply_Hal_Control(source, control["ctl"], control["val"])
        if (error == nil) then 
            return 1 -- unhappy
        end
        AFB:notice ("[<-- _Temporarily_Control Granted<--]")
    else
        Apply_Hal_Control(source, control["ctl"],0)
        AFB:notice ("[<-- _Temporarily_Control Restore--]")
    end

    return 0 -- happy return
end


-- Permanent Adjust volume
function _Permanent_Control(source, control, client)

    printf ("[--> _Permanent_Control -->] source=%d control=%s client=%s", source, Dump_Table(control), Dump_Table(client))

    -- Init should have been properly done
    if (_Global_Context["registry"] == nil) then
       AFB:error ("--* (Hoops) No Hal in _Global_Context=%s", Dump_Table(_Global_Context)) 
       return 1
    end

    -- make sure label as valid
    if (control["ctl"] == nil or control["val"] == nil) then
        AFB:error ("--* Action Ignore no/invalid control=%s", Dump_Table(control)) 
        return 1 -- unhappy
    end

    if (source == 0) then
        AFB:info("-- Adjust %s=%d", control["ctl"], control["val"])
        local error=Apply_Hal_Control(1, control["ctl"], control["val"])
        if (error == nil) then 
            return 1 -- unhappy
        end
        AFB:notice ("[<-- _Permanent_Control Granted<--]")
    else
        Apply_Hal_Control(source, control["ctl"],0)
        AFB:notice ("[<-- _Permanent_Control Restore--]")
    end

    return 0 -- happy return
end