Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | This script reads in fan definition and zone definition YAML |
| 5 | files and generates a set of structures for use by the fan control code. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import yaml |
| 11 | from argparse import ArgumentParser |
| 12 | from mako.template import Template |
| 13 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 14 | tmpl = '''\ |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 15 | <%! |
| 16 | def indent(str, depth): |
| 17 | return ''.join(4*' '*depth+line for line in str.splitlines(True)) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 18 | %>\ |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 19 | <%def name="genSSE(event)" buffered="True"> |
| 20 | Group{ |
| 21 | %for member in event['group']: |
| 22 | { |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 23 | "${member['object']}", |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 24 | {"${member['interface']}", |
| 25 | "${member['property']}"} |
| 26 | }, |
| 27 | %endfor |
| 28 | }, |
| 29 | std::vector<Action>{ |
| 30 | %for a in event['action']: |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 31 | %if len(a['parameters']) != 0: |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 32 | make_action(action::${a['name']}( |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 33 | %else: |
| 34 | make_action(action::${a['name']} |
| 35 | %endif |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 36 | %for i, p in enumerate(a['parameters']): |
| 37 | %if (i+1) != len(a['parameters']): |
| 38 | static_cast<${p['type']}>(${p['value']}), |
| 39 | %else: |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 40 | static_cast<${p['type']}>(${p['value']})) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 41 | %endif |
| 42 | %endfor |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 43 | ), |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 44 | %endfor |
| 45 | }, |
| 46 | Timer{ |
| 47 | ${event['timer']['interval']} |
| 48 | }, |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 49 | std::vector<Signal>{ |
| 50 | %for s in event['signals']: |
| 51 | Signal{ |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 52 | match::${s['match']}( |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 53 | %for i, mp in enumerate(s['mparams']): |
| 54 | %if (i+1) != len(s['mparams']): |
| 55 | "${mp}", |
| 56 | %else: |
| 57 | "${mp}" |
| 58 | %endif |
| 59 | %endfor |
| 60 | ), |
| 61 | make_handler( |
| 62 | %if ('type' in s['sparams']) and \ |
| 63 | (s['sparams']['type'] is not None): |
| 64 | ${s['signal']}<${s['sparams']['type']}>( |
| 65 | %else: |
| 66 | ${s['signal']}( |
| 67 | %endif |
| 68 | %for spk, spv in s['sparams'].iteritems(): |
| 69 | %if spk != 'type': |
| 70 | "${spv}", |
| 71 | %endif |
| 72 | %endfor |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 73 | handler::setProperty<${s['type']}>( |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 74 | "${s['object']}", |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 75 | "${s['interface']}", |
| 76 | "${s['property']}" |
| 77 | ) |
| 78 | )) |
| 79 | }, |
| 80 | %endfor |
| 81 | } |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 82 | </%def>\ |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 83 | /* This is a generated file. */ |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 84 | #include "manager.hpp" |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 85 | #include "functor.hpp" |
| 86 | #include "actions.hpp" |
Matthew Barth | 7e527c1 | 2017-05-17 10:14:15 -0500 | [diff] [blame] | 87 | #include "handlers.hpp" |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 88 | #include "preconditions.hpp" |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 89 | #include "matches.hpp" |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 90 | |
| 91 | using namespace phosphor::fan::control; |
| 92 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 93 | const unsigned int Manager::_powerOnDelay{${mgr_data['power_on_delay']}}; |
| 94 | |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 95 | const std::vector<ZoneGroup> Manager::_zoneLayouts |
| 96 | { |
| 97 | %for zone_group in zones: |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 98 | ZoneGroup{ |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 99 | std::vector<Condition>{ |
| 100 | %for condition in zone_group['conditions']: |
| 101 | Condition{ |
| 102 | "${condition['type']}", |
| 103 | std::vector<ConditionProperty>{ |
| 104 | %for property in condition['properties']: |
| 105 | ConditionProperty{ |
| 106 | "${property['property']}", |
| 107 | "${property['interface']}", |
| 108 | "${property['path']}", |
| 109 | static_cast<${property['type']}>(${property['value']}), |
| 110 | }, |
| 111 | %endfor |
| 112 | }, |
| 113 | }, |
| 114 | %endfor |
| 115 | }, |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 116 | std::vector<ZoneDefinition>{ |
| 117 | %for zone in zone_group['zones']: |
| 118 | ZoneDefinition{ |
| 119 | ${zone['num']}, |
| 120 | ${zone['full_speed']}, |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 121 | ${zone['default_floor']}, |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 122 | ${zone['increase_delay']}, |
| 123 | ${zone['decrease_interval']}, |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 124 | std::vector<FanDefinition>{ |
| 125 | %for fan in zone['fans']: |
| 126 | FanDefinition{ |
| 127 | "${fan['name']}", |
| 128 | std::vector<std::string>{ |
| 129 | %for sensor in fan['sensors']: |
| 130 | "${sensor}", |
| 131 | %endfor |
| 132 | } |
| 133 | }, |
| 134 | %endfor |
| 135 | }, |
| 136 | std::vector<SetSpeedEvent>{ |
| 137 | %for event in zone['events']: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 138 | %if ('pc' in event) and \ |
| 139 | (event['pc'] is not None): |
| 140 | SetSpeedEvent{ |
| 141 | Group{ |
| 142 | %for member in event['pc']['pcgrp']: |
| 143 | { |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 144 | "${member['object']}", |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 145 | {"${member['interface']}", |
| 146 | "${member['property']}"} |
| 147 | }, |
| 148 | %endfor |
| 149 | }, |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 150 | std::vector<Action>{ |
| 151 | %for i, a in enumerate(event['pc']['pcact']): |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 152 | %if len(a['params']) != 0: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 153 | make_action( |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 154 | precondition::${a['name']}( |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 155 | %else: |
| 156 | make_action( |
| 157 | precondition::${a['name']} |
| 158 | %endif |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 159 | %for p in a['params']: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 160 | ${p['type']}${p['open']} |
| 161 | %for j, v in enumerate(p['values']): |
| 162 | %if (j+1) != len(p['values']): |
| 163 | ${v['value']}, |
| 164 | %else: |
| 165 | ${v['value']} |
| 166 | %endif |
| 167 | %endfor |
| 168 | ${p['close']}, |
| 169 | %endfor |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 170 | %if (i+1) != len(event['pc']['pcact']): |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 171 | %if len(a['params']) != 0: |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 172 | )), |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 173 | %else: |
| 174 | ), |
| 175 | %endif |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 176 | %endif |
| 177 | %endfor |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 178 | std::vector<SetSpeedEvent>{ |
| 179 | %for pcevt in event['pc']['pcevts']: |
| 180 | SetSpeedEvent{\ |
| 181 | ${indent(genSSE(event=pcevt), 6)}\ |
| 182 | }, |
| 183 | %endfor |
| 184 | %else: |
| 185 | SetSpeedEvent{\ |
| 186 | ${indent(genSSE(event=event), 6)} |
| 187 | %endif |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 188 | %if ('pc' in event) and (event['pc'] is not None): |
| 189 | } |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 190 | %if len(event['pc']['pcact'][-1]['params']) != 0: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 191 | )), |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 192 | %else: |
| 193 | ), |
| 194 | %endif |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 195 | }, |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 196 | Timer{ |
| 197 | ${event['pc']['pctime']['interval']} |
| 198 | }, |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 199 | std::vector<Signal>{ |
| 200 | %for s in event['pc']['pcsigs']: |
| 201 | Signal{ |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 202 | match::${s['match']}( |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 203 | %for i, mp in enumerate(s['mparams']): |
| 204 | %if (i+1) != len(s['mparams']): |
| 205 | "${mp}", |
| 206 | %else: |
| 207 | "${mp}" |
| 208 | %endif |
| 209 | %endfor |
| 210 | ), |
| 211 | make_handler( |
| 212 | %if ('type' in s['sparams']) and \ |
| 213 | (s['sparams']['type'] is not None): |
| 214 | ${s['signal']}<${s['sparams']['type']}>( |
| 215 | %else: |
| 216 | ${s['signal']}( |
| 217 | %endif |
| 218 | %for spk, spv in s['sparams'].iteritems(): |
| 219 | %if spk != 'type': |
| 220 | "${spv}", |
| 221 | %endif |
| 222 | %endfor |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 223 | handler::setProperty<${s['type']}>( |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 224 | "${s['object']}", |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 225 | "${s['interface']}", |
| 226 | "${s['property']}" |
| 227 | ) |
| 228 | )) |
| 229 | }, |
| 230 | %endfor |
| 231 | } |
| 232 | %endif |
Matthew Barth | 9c72667 | 2017-05-18 13:44:46 -0500 | [diff] [blame] | 233 | }, |
| 234 | %endfor |
| 235 | } |
| 236 | }, |
| 237 | %endfor |
| 238 | } |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 239 | }, |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 240 | %endfor |
| 241 | }; |
| 242 | ''' |
| 243 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 244 | |
Matthew Barth | bb12c92 | 2017-06-13 13:57:40 -0500 | [diff] [blame] | 245 | def convertToMap(listOfDict): |
| 246 | """ |
| 247 | Converts a list of dictionary entries to a std::map initialization list. |
| 248 | """ |
| 249 | listOfDict = listOfDict.replace('[', '{') |
| 250 | listOfDict = listOfDict.replace(']', '}') |
| 251 | listOfDict = listOfDict.replace(':', ',') |
| 252 | return listOfDict |
| 253 | |
| 254 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 255 | def getEvent(zone_num, zone_conditions, e, events_data): |
| 256 | """ |
| 257 | Parses the sections of an event and populates the properties |
| 258 | that construct an event within the generated source. |
| 259 | """ |
| 260 | event = {} |
| 261 | # Zone numbers are optional in the events yaml but skip if this |
| 262 | # zone's zone number is not in the event's zone numbers |
| 263 | if all('zones' in z and |
| 264 | z['zones'] is not None and |
| 265 | zone_num not in z['zones'] |
| 266 | for z in e['zone_conditions']): |
| 267 | return |
| 268 | |
| 269 | # Zone conditions are optional in the events yaml but skip |
| 270 | # if this event's condition is not in this zone's conditions |
| 271 | if all('name' in z and z['name'] is not None and |
| 272 | not any(c['name'] == z['name'] for c in zone_conditions) |
| 273 | for z in e['zone_conditions']): |
| 274 | return |
| 275 | |
| 276 | # Add set speed event group |
| 277 | group = [] |
| 278 | groups = next(g for g in events_data['groups'] |
| 279 | if g['name'] == e['group']) |
| 280 | for member in groups['members']: |
| 281 | members = {} |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 282 | members['object'] = (groups['type'] + |
| 283 | member) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 284 | members['interface'] = e['interface'] |
| 285 | members['property'] = e['property']['name'] |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 286 | members['type'] = e['property']['type'] |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 287 | group.append(members) |
| 288 | event['group'] = group |
| 289 | |
| 290 | # Add set speed actions and function parameters |
| 291 | action = [] |
| 292 | for eActions in e['actions']: |
| 293 | actions = {} |
| 294 | eAction = next(a for a in events_data['actions'] |
| 295 | if a['name'] == eActions['name']) |
| 296 | actions['name'] = eAction['name'] |
| 297 | params = [] |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 298 | if ('parameters' in eAction) and \ |
| 299 | (eAction['parameters'] is not None): |
| 300 | for p in eAction['parameters']: |
| 301 | param = {} |
| 302 | if type(eActions[p]) is not dict: |
| 303 | if p == 'property': |
| 304 | param['value'] = str(eActions[p]).lower() |
| 305 | param['type'] = str( |
| 306 | e['property']['type']).lower() |
| 307 | else: |
| 308 | # Default type to 'size_t' when not given |
| 309 | param['value'] = str(eActions[p]).lower() |
| 310 | param['type'] = 'size_t' |
| 311 | params.append(param) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 312 | else: |
Matthew Barth | 4c04a01 | 2017-09-15 13:05:30 -0500 | [diff] [blame] | 313 | param['type'] = str(eActions[p]['type']).lower() |
| 314 | if p != 'map': |
| 315 | param['value'] = str( |
| 316 | eActions[p]['value']).lower() |
| 317 | else: |
| 318 | emap = convertToMap(str(eActions[p]['value'])) |
| 319 | param['value'] = param['type'] + emap |
| 320 | params.append(param) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 321 | actions['parameters'] = params |
| 322 | action.append(actions) |
| 323 | event['action'] = action |
| 324 | |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 325 | # Add signal handlers |
| 326 | signals = [] |
| 327 | for member in group: |
| 328 | for eMatches in e['matches']: |
| 329 | signal = {} |
| 330 | eMatch = next(m for m in events_data['matches'] |
| 331 | if m['name'] == eMatches['name']) |
| 332 | signal['match'] = eMatch['name'] |
| 333 | params = [] |
| 334 | if ('parameters' in eMatch) and \ |
| 335 | (eMatch['parameters'] is not None): |
| 336 | for p in eMatch['parameters']: |
| 337 | params.append(member[str(p)]) |
| 338 | signal['mparams'] = params |
| 339 | eSignal = next(s for s in events_data['signals'] |
| 340 | if s['name'] == eMatch['signal']) |
| 341 | signal['signal'] = eSignal['name'] |
| 342 | sparams = {} |
| 343 | if ('parameters' in eSignal) and \ |
| 344 | (eSignal['parameters'] is not None): |
| 345 | for p in eSignal['parameters']: |
| 346 | sparams[str(p)] = member[str(p)] |
| 347 | signal['sparams'] = sparams |
| 348 | # Add member attributes |
| 349 | signal['object'] = member['object'] |
| 350 | signal['interface'] = member['interface'] |
| 351 | signal['property'] = member['property'] |
| 352 | signal['type'] = member['type'] |
| 353 | signals.append(signal) |
| 354 | event['signals'] = signals |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 355 | |
| 356 | # Add optional action call timer |
| 357 | timer = {} |
| 358 | interval = "static_cast<std::chrono::seconds>" |
| 359 | if ('timer' in e) and \ |
| 360 | (e['timer'] is not None): |
| 361 | timer['interval'] = (interval + |
| 362 | "(" + |
| 363 | str(e['timer']['interval']) + |
| 364 | ")") |
| 365 | else: |
| 366 | timer['interval'] = (interval + |
| 367 | "(" + str(0) + ")") |
| 368 | event['timer'] = timer |
| 369 | |
| 370 | return event |
| 371 | |
| 372 | |
| 373 | def addPrecondition(zNum, zCond, event, events_data): |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 374 | """ |
| 375 | Parses the precondition section of an event and populates the necessary |
| 376 | structures to generate a precondition for a set speed event. |
| 377 | """ |
| 378 | precond = {} |
| 379 | # Add set speed event precondition group |
| 380 | group = [] |
| 381 | for grp in event['precondition']['groups']: |
| 382 | groups = next(g for g in events_data['groups'] |
| 383 | if g['name'] == grp['name']) |
| 384 | for member in groups['members']: |
| 385 | members = {} |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 386 | members['object'] = (groups['type'] + |
| 387 | member) |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 388 | members['interface'] = grp['interface'] |
| 389 | members['property'] = grp['property']['name'] |
| 390 | members['type'] = grp['property']['type'] |
| 391 | members['value'] = grp['property']['value'] |
| 392 | group.append(members) |
| 393 | precond['pcgrp'] = group |
| 394 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 395 | # Add set speed event precondition actions |
| 396 | pc = [] |
| 397 | pcs = {} |
| 398 | pcs['name'] = event['precondition']['name'] |
| 399 | epc = next(p for p in events_data['preconditions'] |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 400 | if p['name'] == event['precondition']['name']) |
| 401 | params = [] |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 402 | for p in epc['parameters']: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 403 | param = {} |
| 404 | if p == 'groups': |
| 405 | param['type'] = "std::vector<PrecondGroup>" |
| 406 | param['open'] = "{" |
| 407 | param['close'] = "}" |
| 408 | values = [] |
| 409 | for pcgrp in group: |
| 410 | value = {} |
| 411 | value['value'] = ( |
| 412 | "PrecondGroup{\"" + |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 413 | str(pcgrp['object']) + "\",\"" + |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 414 | str(pcgrp['interface']) + "\",\"" + |
| 415 | str(pcgrp['property']) + "\"," + |
| 416 | "static_cast<" + |
| 417 | str(pcgrp['type']).lower() + ">" + |
| 418 | "(" + str(pcgrp['value']).lower() + ")}") |
| 419 | values.append(value) |
| 420 | param['values'] = values |
| 421 | params.append(param) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 422 | pcs['params'] = params |
| 423 | pc.append(pcs) |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 424 | precond['pcact'] = pc |
| 425 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 426 | pcevents = [] |
| 427 | for pce in event['precondition']['events']: |
| 428 | pcevent = getEvent(zNum, zCond, pce, events_data) |
| 429 | if not pcevent: |
| 430 | continue |
| 431 | pcevents.append(pcevent) |
| 432 | precond['pcevts'] = pcevents |
| 433 | |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 434 | # Add precondition signal handlers |
| 435 | signals = [] |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 436 | for member in group: |
Matthew Barth | 67967f9 | 2017-09-22 12:43:57 -0500 | [diff] [blame] | 437 | for eMatches in event['precondition']['matches']: |
| 438 | signal = {} |
| 439 | eMatch = next(m for m in events_data['matches'] |
| 440 | if m['name'] == eMatches['name']) |
| 441 | signal['match'] = eMatch['name'] |
| 442 | params = [] |
| 443 | if ('parameters' in eMatch) and \ |
| 444 | (eMatch['parameters'] is not None): |
| 445 | for p in eMatch['parameters']: |
| 446 | params.append(member[str(p)]) |
| 447 | signal['mparams'] = params |
| 448 | eSignal = next(s for s in events_data['signals'] |
| 449 | if s['name'] == eMatch['signal']) |
| 450 | signal['signal'] = eSignal['name'] |
| 451 | sparams = {} |
| 452 | if ('parameters' in eSignal) and \ |
| 453 | (eSignal['parameters'] is not None): |
| 454 | for p in eSignal['parameters']: |
| 455 | sparams[str(p)] = member[str(p)] |
| 456 | signal['sparams'] = sparams |
| 457 | # Add member attributes |
| 458 | signal['object'] = member['object'] |
| 459 | signal['interface'] = member['interface'] |
| 460 | signal['property'] = member['property'] |
| 461 | signal['type'] = member['type'] |
| 462 | signals.append(signal) |
| 463 | precond['pcsigs'] = signals |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 464 | |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 465 | # Add optional action call timer |
| 466 | timer = {} |
| 467 | interval = "static_cast<std::chrono::seconds>" |
| 468 | if ('timer' in event['precondition']) and \ |
| 469 | (event['precondition']['timer'] is not None): |
| 470 | timer['interval'] = (interval + |
| 471 | "(" + |
| 472 | str(event['precondition']['timer']['interval']) + |
| 473 | ")") |
| 474 | else: |
| 475 | timer['interval'] = (interval + |
| 476 | "(" + str(0) + ")") |
| 477 | precond['pctime'] = timer |
| 478 | |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 479 | return precond |
| 480 | |
| 481 | |
Gunnar Mills | b751f32 | 2017-06-06 15:14:11 -0500 | [diff] [blame] | 482 | def getEventsInZone(zone_num, zone_conditions, events_data): |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 483 | """ |
| 484 | Constructs the event entries defined for each zone using the events yaml |
| 485 | provided. |
| 486 | """ |
| 487 | events = [] |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 488 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 489 | if 'events' in events_data: |
| 490 | for e in events_data['events']: |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 491 | event = {} |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 492 | # Add precondition if given |
| 493 | if ('precondition' in e) and \ |
| 494 | (e['precondition'] is not None): |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 495 | event['pc'] = addPrecondition(zone_num, |
| 496 | zone_conditions, |
| 497 | e, |
| 498 | events_data) |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 499 | else: |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 500 | event = getEvent(zone_num, zone_conditions, e, events_data) |
| 501 | if not event: |
| 502 | continue |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 503 | events.append(event) |
| 504 | |
| 505 | return events |
| 506 | |
| 507 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 508 | def getFansInZone(zone_num, profiles, fan_data): |
| 509 | """ |
| 510 | Parses the fan definition YAML files to find the fans |
| 511 | that match both the zone passed in and one of the |
| 512 | cooling profiles. |
| 513 | """ |
| 514 | |
| 515 | fans = [] |
| 516 | |
| 517 | for f in fan_data['fans']: |
| 518 | |
| 519 | if zone_num != f['cooling_zone']: |
| 520 | continue |
| 521 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 522 | # 'cooling_profile' is optional (use 'all' instead) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 523 | if f.get('cooling_profile') is None: |
| 524 | profile = "all" |
| 525 | else: |
| 526 | profile = f['cooling_profile'] |
| 527 | |
| 528 | if profile not in profiles: |
| 529 | continue |
| 530 | |
| 531 | fan = {} |
| 532 | fan['name'] = f['inventory'] |
| 533 | fan['sensors'] = f['sensors'] |
| 534 | fans.append(fan) |
| 535 | |
| 536 | return fans |
| 537 | |
| 538 | |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 539 | def getConditionInZoneConditions(zone_condition, zone_conditions_data): |
| 540 | """ |
| 541 | Parses the zone conditions definition YAML files to find the condition |
| 542 | that match both the zone condition passed in. |
| 543 | """ |
| 544 | |
| 545 | condition = {} |
| 546 | |
| 547 | for c in zone_conditions_data['conditions']: |
| 548 | |
| 549 | if zone_condition != c['name']: |
| 550 | continue |
| 551 | condition['type'] = c['type'] |
| 552 | properties = [] |
| 553 | for p in c['properties']: |
| 554 | property = {} |
| 555 | property['property'] = p['property'] |
| 556 | property['interface'] = p['interface'] |
| 557 | property['path'] = p['path'] |
| 558 | property['type'] = p['type'].lower() |
| 559 | property['value'] = str(p['value']).lower() |
| 560 | properties.append(property) |
| 561 | condition['properties'] = properties |
| 562 | |
| 563 | return condition |
| 564 | |
| 565 | |
| 566 | def buildZoneData(zone_data, fan_data, events_data, zone_conditions_data): |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 567 | """ |
| 568 | Combines the zone definition YAML and fan |
| 569 | definition YAML to create a data structure defining |
| 570 | the fan cooling zones. |
| 571 | """ |
| 572 | |
| 573 | zone_groups = [] |
| 574 | |
| 575 | for group in zone_data: |
| 576 | conditions = [] |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 577 | # zone conditions are optional |
| 578 | if 'zone_conditions' in group and group['zone_conditions'] is not None: |
| 579 | for c in group['zone_conditions']: |
| 580 | |
| 581 | if not zone_conditions_data: |
Gunnar Mills | b751f32 | 2017-06-06 15:14:11 -0500 | [diff] [blame] | 582 | sys.exit("No zone_conditions YAML file but " + |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 583 | "zone_conditions used in zone YAML") |
| 584 | |
| 585 | condition = getConditionInZoneConditions(c['name'], |
| 586 | zone_conditions_data) |
| 587 | |
| 588 | if not condition: |
| 589 | sys.exit("Missing zone condition " + c['name']) |
| 590 | |
| 591 | conditions.append(condition) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 592 | |
| 593 | zone_group = {} |
| 594 | zone_group['conditions'] = conditions |
| 595 | |
| 596 | zones = [] |
| 597 | for z in group['zones']: |
| 598 | zone = {} |
| 599 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 600 | # 'zone' is required |
| 601 | if ('zone' not in z) or (z['zone'] is None): |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 602 | sys.exit("Missing fan zone number in " + zone_yaml) |
| 603 | |
| 604 | zone['num'] = z['zone'] |
| 605 | |
| 606 | zone['full_speed'] = z['full_speed'] |
| 607 | |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 608 | zone['default_floor'] = z['default_floor'] |
| 609 | |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 610 | # 'increase_delay' is optional (use 0 by default) |
| 611 | key = 'increase_delay' |
| 612 | zone[key] = z.setdefault(key, 0) |
| 613 | |
| 614 | # 'decrease_interval' is optional (use 0 by default) |
| 615 | key = 'decrease_interval' |
| 616 | zone[key] = z.setdefault(key, 0) |
| 617 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 618 | # 'cooling_profiles' is optional (use 'all' instead) |
| 619 | if ('cooling_profiles' not in z) or \ |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 620 | (z['cooling_profiles'] is None): |
| 621 | profiles = ["all"] |
| 622 | else: |
| 623 | profiles = z['cooling_profiles'] |
| 624 | |
| 625 | fans = getFansInZone(z['zone'], profiles, fan_data) |
Gunnar Mills | b751f32 | 2017-06-06 15:14:11 -0500 | [diff] [blame] | 626 | events = getEventsInZone(z['zone'], group['zone_conditions'], |
| 627 | events_data) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 628 | |
| 629 | if len(fans) == 0: |
| 630 | sys.exit("Didn't find any fans in zone " + str(zone['num'])) |
| 631 | |
| 632 | zone['fans'] = fans |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 633 | zone['events'] = events |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 634 | zones.append(zone) |
| 635 | |
| 636 | zone_group['zones'] = zones |
| 637 | zone_groups.append(zone_group) |
| 638 | |
| 639 | return zone_groups |
| 640 | |
| 641 | |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 642 | if __name__ == '__main__': |
| 643 | parser = ArgumentParser( |
| 644 | description="Phosphor fan zone definition parser") |
| 645 | |
| 646 | parser.add_argument('-z', '--zone_yaml', dest='zone_yaml', |
| 647 | default="example/zones.yaml", |
| 648 | help='fan zone definitional yaml') |
| 649 | parser.add_argument('-f', '--fan_yaml', dest='fan_yaml', |
| 650 | default="example/fans.yaml", |
| 651 | help='fan definitional yaml') |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 652 | parser.add_argument('-e', '--events_yaml', dest='events_yaml', |
| 653 | help='events to set speeds yaml') |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 654 | parser.add_argument('-c', '--zone_conditions_yaml', |
| 655 | dest='zone_conditions_yaml', |
| 656 | help='conditions to determine zone yaml') |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 657 | parser.add_argument('-o', '--output_dir', dest='output_dir', |
| 658 | default=".", |
| 659 | help='output directory') |
| 660 | args = parser.parse_args() |
| 661 | |
| 662 | if not args.zone_yaml or not args.fan_yaml: |
| 663 | parser.print_usage() |
| 664 | sys.exit(-1) |
| 665 | |
| 666 | with open(args.zone_yaml, 'r') as zone_input: |
| 667 | zone_data = yaml.safe_load(zone_input) or {} |
| 668 | |
| 669 | with open(args.fan_yaml, 'r') as fan_input: |
| 670 | fan_data = yaml.safe_load(fan_input) or {} |
| 671 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 672 | events_data = {} |
| 673 | if args.events_yaml: |
| 674 | with open(args.events_yaml, 'r') as events_input: |
| 675 | events_data = yaml.safe_load(events_input) or {} |
| 676 | |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 677 | zone_conditions_data = {} |
| 678 | if args.zone_conditions_yaml: |
| 679 | with open(args.zone_conditions_yaml, 'r') as zone_conditions_input: |
| 680 | zone_conditions_data = yaml.safe_load(zone_conditions_input) or {} |
| 681 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 682 | zone_config = buildZoneData(zone_data.get('zone_configuration', {}), |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 683 | fan_data, events_data, zone_conditions_data) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 684 | |
| 685 | manager_config = zone_data.get('manager_configuration', {}) |
| 686 | |
| 687 | if manager_config.get('power_on_delay') is None: |
| 688 | manager_config['power_on_delay'] = 0 |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 689 | |
| 690 | output_file = os.path.join(args.output_dir, "fan_zone_defs.cpp") |
| 691 | with open(output_file, 'w') as output: |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 692 | output.write(Template(tmpl).render(zones=zone_config, |
| 693 | mgr_data=manager_config)) |