Matthew Barth | f24d774 | 2020-03-17 16:12:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 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 |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 10 | from argparse import ArgumentParser |
Patrick Williams | c7f68be | 2022-12-08 06:18:00 -0600 | [diff] [blame] | 11 | |
| 12 | import yaml |
Matthew Barth | 702c4a5 | 2018-02-28 16:23:11 -0600 | [diff] [blame] | 13 | from mako.lookup import TemplateLookup |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 14 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 15 | |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 16 | def parse_cpp_type(typeName): |
| 17 | """ |
| 18 | Take a list of dbus types from YAML and convert it to a recursive cpp |
| 19 | formed data structure. Each entry of the original list gets converted |
| 20 | into a tuple consisting of the type name and a list with the params |
| 21 | for this type, |
| 22 | e.g. |
| 23 | ['dict', ['string', 'dict', ['string', 'int64']]] |
| 24 | is converted to |
| 25 | [('dict', [('string', []), ('dict', [('string', []), |
| 26 | ('int64', [])]]] |
| 27 | """ |
| 28 | |
| 29 | if not typeName: |
| 30 | return None |
| 31 | |
| 32 | # Type names are _almost_ valid YAML. Insert a , before each [ |
| 33 | # and then wrap it in a [ ] and it becomes valid YAML (assuming |
| 34 | # the user gave us a valid typename). |
| 35 | typeArray = yaml.safe_load("[" + ",[".join(typeName.split("[")) + "]") |
| 36 | typeTuple = preprocess_yaml_type_array(typeArray).pop(0) |
| 37 | return get_cpp_type(typeTuple) |
| 38 | |
| 39 | |
| 40 | def preprocess_yaml_type_array(typeArray): |
| 41 | """ |
| 42 | Flattens an array type into a tuple list that can be used to get the |
| 43 | supported cpp type from each element. |
| 44 | """ |
| 45 | |
| 46 | result = [] |
| 47 | |
| 48 | for i in range(len(typeArray)): |
| 49 | # Ignore lists because we merge them with the previous element |
| 50 | if type(typeArray[i]) is list: |
| 51 | continue |
| 52 | |
| 53 | # If there is a next element and it is a list, merge it with the |
| 54 | # current element. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 55 | if i < len(typeArray) - 1 and type(typeArray[i + 1]) is list: |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 56 | result.append( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 57 | (typeArray[i], preprocess_yaml_type_array(typeArray[i + 1])) |
| 58 | ) |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 59 | else: |
| 60 | result.append((typeArray[i], [])) |
| 61 | |
| 62 | return result |
| 63 | |
| 64 | |
| 65 | def get_cpp_type(typeTuple): |
| 66 | """ |
| 67 | Take a list of dbus types and perform validity checking, such as: |
| 68 | [ variant [ dict [ int32, int32 ], double ] ] |
| 69 | This function then converts the type-list into a C++ type string. |
| 70 | """ |
| 71 | |
| 72 | propertyMap = { |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 73 | "byte": {"cppName": "uint8_t", "params": 0}, |
| 74 | "boolean": {"cppName": "bool", "params": 0}, |
| 75 | "int16": {"cppName": "int16_t", "params": 0}, |
| 76 | "uint16": {"cppName": "uint16_t", "params": 0}, |
| 77 | "int32": {"cppName": "int32_t", "params": 0}, |
| 78 | "uint32": {"cppName": "uint32_t", "params": 0}, |
| 79 | "int64": {"cppName": "int64_t", "params": 0}, |
| 80 | "uint64": {"cppName": "uint64_t", "params": 0}, |
| 81 | "double": {"cppName": "double", "params": 0}, |
| 82 | "string": {"cppName": "std::string", "params": 0}, |
| 83 | "array": {"cppName": "std::vector", "params": 1}, |
| 84 | "dict": {"cppName": "std::map", "params": 2}, |
| 85 | } |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 86 | |
| 87 | if len(typeTuple) != 2: |
| 88 | raise RuntimeError("Invalid typeTuple %s" % typeTuple) |
| 89 | |
| 90 | first = typeTuple[0] |
| 91 | entry = propertyMap[first] |
| 92 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 93 | result = entry["cppName"] |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 94 | |
| 95 | # Handle 0-entry parameter lists. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 96 | if entry["params"] == 0: |
| 97 | if len(typeTuple[1]) != 0: |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 98 | raise RuntimeError("Invalid typeTuple %s" % typeTuple) |
| 99 | else: |
| 100 | return result |
| 101 | |
| 102 | # Get the parameter list |
| 103 | rest = typeTuple[1] |
| 104 | |
| 105 | # Confirm parameter count matches. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 106 | if (entry["params"] != -1) and (entry["params"] != len(rest)): |
| 107 | raise RuntimeError("Invalid entry count for %s : %s" % (first, rest)) |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 108 | |
| 109 | # Parse each parameter entry, if appropriate, and create C++ template |
| 110 | # syntax. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 111 | result += "<" |
| 112 | if entry.get("noparse"): |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 113 | # Do not parse the parameter list, just use the first element |
| 114 | # of each tuple and ignore possible parameters |
| 115 | result += ", ".join([e[0] for e in rest]) |
| 116 | else: |
Matthew Barth | 867a31c | 2020-02-13 13:13:44 -0600 | [diff] [blame] | 117 | result += ", ".join([get_cpp_type(e) for e in rest]) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 118 | result += ">" |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 119 | |
| 120 | return result |
| 121 | |
| 122 | |
Matthew Barth | bb12c92 | 2017-06-13 13:57:40 -0500 | [diff] [blame] | 123 | def convertToMap(listOfDict): |
| 124 | """ |
| 125 | Converts a list of dictionary entries to a std::map initialization list. |
| 126 | """ |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 127 | listOfDict = listOfDict.replace("'", '"') |
| 128 | listOfDict = listOfDict.replace("[", "{") |
| 129 | listOfDict = listOfDict.replace("]", "}") |
| 130 | listOfDict = listOfDict.replace(":", ",") |
Matthew Barth | bb12c92 | 2017-06-13 13:57:40 -0500 | [diff] [blame] | 131 | return listOfDict |
| 132 | |
| 133 | |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 134 | def genEvent(event): |
| 135 | """ |
| 136 | Generates the source code of an event and returns it as a string |
| 137 | """ |
| 138 | e = "SetSpeedEvent{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 139 | e += '"' + event["name"] + '",\n' |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 140 | e += "Group{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 141 | for group in event["groups"]: |
| 142 | for member in group["members"]: |
| 143 | e += '{"' + member["object"] + '",\n' |
| 144 | e += '"' + member["interface"] + '",\n' |
| 145 | e += '"' + member["property"] + '"},\n' |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 146 | e += "},\n" |
| 147 | |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 148 | e += "ActionData{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 149 | for d in event["action"]: |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 150 | e += "{Group{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 151 | for g in d["groups"]: |
| 152 | for m in g["members"]: |
| 153 | e += '{"' + m["object"] + '",\n' |
| 154 | e += '"' + m["interface"] + '",\n' |
| 155 | e += '"' + m["property"] + '"},\n' |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 156 | e += "},\n" |
| 157 | e += "std::vector<Action>{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 158 | for a in d["actions"]: |
| 159 | if len(a["parameters"]) != 0: |
| 160 | e += "make_action(action::" + a["name"] + "(\n" |
Matthew Barth | 8a697b6 | 2018-12-14 13:23:47 -0600 | [diff] [blame] | 161 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 162 | e += "make_action(action::" + a["name"] + "\n" |
| 163 | for i, p in enumerate(a["parameters"]): |
| 164 | if (i + 1) != len(a["parameters"]): |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 165 | e += p + ",\n" |
| 166 | else: |
| 167 | e += p + "\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 168 | if len(a["parameters"]) != 0: |
Matthew Barth | 8a697b6 | 2018-12-14 13:23:47 -0600 | [diff] [blame] | 169 | e += ")),\n" |
| 170 | else: |
| 171 | e += "),\n" |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 172 | e += "}},\n" |
| 173 | e += "},\n" |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 174 | |
Matthew Barth | 1b4de26 | 2018-03-06 13:03:16 -0600 | [diff] [blame] | 175 | e += "std::vector<Trigger>{\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 176 | if ("timer" in event["triggers"]) and ( |
| 177 | event["triggers"]["timer"] is not None |
| 178 | ): |
| 179 | e += "\tmake_trigger(trigger::timer(TimerConf{\n" |
| 180 | e += "\t" + event["triggers"]["timer"]["interval"] + ",\n" |
| 181 | e += "\t" + event["triggers"]["timer"]["type"] + "\n" |
| 182 | e += "\t})),\n" |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 183 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 184 | if ("signals" in event["triggers"]) and ( |
| 185 | event["triggers"]["signals"] is not None |
| 186 | ): |
| 187 | for s in event["triggers"]["signals"]: |
Matthew Barth | 016bd24 | 2018-03-07 16:06:06 -0600 | [diff] [blame] | 188 | e += "\tmake_trigger(trigger::signal(\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 189 | e += "match::" + s["match"] + "(\n" |
| 190 | for i, mp in enumerate(s["mparams"]["params"]): |
| 191 | if (i + 1) != len(s["mparams"]["params"]): |
| 192 | e += "\t\t\t" + s["mparams"][mp] + ",\n" |
Matthew Barth | 016bd24 | 2018-03-07 16:06:06 -0600 | [diff] [blame] | 193 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 194 | e += "\t\t\t" + s["mparams"][mp] + "\n" |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 195 | e += "\t\t),\n" |
| 196 | e += "\t\tmake_handler<SignalHandler>(\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 197 | if ("type" in s["sparams"]) and (s["sparams"]["type"] is not None): |
| 198 | e += s["signal"] + "<" + s["sparams"]["type"] + ">(\n" |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 199 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 200 | e += s["signal"] + "(\n" |
| 201 | for sp in s["sparams"]["params"]: |
| 202 | e += s["sparams"][sp] + ",\n" |
| 203 | if ("type" in s["hparams"]) and (s["hparams"]["type"] is not None): |
| 204 | e += ( |
| 205 | "handler::" |
| 206 | + s["handler"] |
| 207 | + "<" |
| 208 | + s["hparams"]["type"] |
| 209 | + ">(\n" |
| 210 | ) |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 211 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 212 | e += "handler::" + s["handler"] + "(\n)" |
| 213 | for i, hp in enumerate(s["hparams"]["params"]): |
| 214 | if (i + 1) != len(s["hparams"]["params"]): |
| 215 | e += s["hparams"][hp] + ",\n" |
Matthew Barth | 016bd24 | 2018-03-07 16:06:06 -0600 | [diff] [blame] | 216 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 217 | e += s["hparams"][hp] + "\n" |
Matthew Barth | 016bd24 | 2018-03-07 16:06:06 -0600 | [diff] [blame] | 218 | e += "))\n" |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 219 | e += "\t\t)\n" |
Matthew Barth | 016bd24 | 2018-03-07 16:06:06 -0600 | [diff] [blame] | 220 | e += "\t)),\n" |
| 221 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 222 | if "init" in event["triggers"]: |
| 223 | for i in event["triggers"]["init"]: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 224 | e += "\tmake_trigger(trigger::init(\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 225 | if "method" in i: |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 226 | e += "\t\tmake_handler<MethodHandler>(\n" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 227 | if ("type" in i["mparams"]) and ( |
| 228 | i["mparams"]["type"] is not None |
| 229 | ): |
| 230 | e += i["method"] + "<" + i["mparams"]["type"] + ">(\n" |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 231 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 232 | e += i["method"] + "(\n" |
| 233 | for ip in i["mparams"]["params"]: |
| 234 | e += i["mparams"][ip] + ",\n" |
| 235 | if ("type" in i["hparams"]) and ( |
| 236 | i["hparams"]["type"] is not None |
| 237 | ): |
| 238 | e += ( |
| 239 | "handler::" |
| 240 | + i["handler"] |
| 241 | + "<" |
| 242 | + i["hparams"]["type"] |
| 243 | + ">(\n" |
| 244 | ) |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 245 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 246 | e += "handler::" + i["handler"] + "(\n)" |
| 247 | for i, hp in enumerate(i["hparams"]["params"]): |
| 248 | if (i + 1) != len(i["hparams"]["params"]): |
| 249 | e += i["hparams"][hp] + ",\n" |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 250 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 251 | e += i["hparams"][hp] + "\n" |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 252 | e += "))\n" |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 253 | e += "\t\t)\n" |
| 254 | e += "\t)),\n" |
| 255 | |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 256 | e += "},\n" |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 257 | |
| 258 | e += "}" |
| 259 | |
| 260 | return e |
| 261 | |
| 262 | |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 263 | def getGroups(zNum, zCond, edata, events): |
| 264 | """ |
| 265 | Extract and construct the groups for the given event. |
| 266 | """ |
| 267 | groups = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 268 | if ("groups" in edata) and (edata["groups"] is not None): |
| 269 | for eGroups in edata["groups"]: |
| 270 | if ("zone_conditions" in eGroups) and ( |
| 271 | eGroups["zone_conditions"] is not None |
| 272 | ): |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 273 | # Zone conditions are optional in the events yaml but skip |
| 274 | # if this event's condition is not in this zone's conditions |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 275 | if all( |
| 276 | "name" in z |
| 277 | and z["name"] is not None |
| 278 | and not any(c["name"] == z["name"] for c in zCond) |
| 279 | for z in eGroups["zone_conditions"] |
| 280 | ): |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 281 | continue |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 282 | |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 283 | # Zone numbers are optional in the events yaml but skip if this |
| 284 | # zone's zone number is not in the event's zone numbers |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 285 | if all( |
| 286 | "zones" in z |
| 287 | and z["zones"] is not None |
| 288 | and zNum not in z["zones"] |
| 289 | for z in eGroups["zone_conditions"] |
| 290 | ): |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 291 | continue |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 292 | eGroup = next( |
| 293 | g for g in events["groups"] if g["name"] == eGroups["name"] |
| 294 | ) |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 295 | |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 296 | group = {} |
| 297 | members = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 298 | group["name"] = eGroup["name"] |
| 299 | for m in eGroup["members"]: |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 300 | member = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 301 | member["path"] = eGroup["type"] |
| 302 | member["object"] = eGroup["type"] + m |
| 303 | member["interface"] = eGroups["interface"] |
| 304 | member["property"] = eGroups["property"]["name"] |
| 305 | member["type"] = eGroups["property"]["type"] |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 306 | # Use defined service to note member on zone object |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 307 | if ("service" in eGroup) and (eGroup["service"] is not None): |
| 308 | member["service"] = eGroup["service"] |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 309 | # Add expected group member's property value if given |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 310 | if ("value" in eGroups["property"]) and ( |
| 311 | eGroups["property"]["value"] is not None |
| 312 | ): |
| 313 | if ( |
| 314 | isinstance(eGroups["property"]["value"], str) |
| 315 | or "string" in str(member["type"]).lower() |
| 316 | ): |
| 317 | member["value"] = ( |
| 318 | '"' + eGroups["property"]["value"] + '"' |
| 319 | ) |
| 320 | else: |
| 321 | member["value"] = eGroups["property"]["value"] |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 322 | members.append(member) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 323 | group["members"] = members |
Matthew Barth | f7e1cb3 | 2018-11-19 11:19:09 -0600 | [diff] [blame] | 324 | groups.append(group) |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 325 | return groups |
| 326 | |
| 327 | |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 328 | def getParameters(member, groups, section, events): |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 329 | """ |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 330 | Extracts and constructs a section's parameters |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 331 | """ |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 332 | params = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 333 | if ("parameters" in section) and (section["parameters"] is not None): |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 334 | plist = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 335 | for sp in section["parameters"]: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 336 | p = str(sp) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 337 | if p != "type": |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 338 | plist.append(p) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 339 | if p != "group": |
| 340 | params[p] = '"' + member[p] + '"' |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 341 | else: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 342 | params[p] = "Group\n{\n" |
| 343 | for g in groups: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 344 | for m in g["members"]: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 345 | params[p] += ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 346 | '{"' |
| 347 | + str(m["object"]) |
| 348 | + '",\n' |
| 349 | + '"' |
| 350 | + str(m["interface"]) |
| 351 | + '",\n' |
| 352 | + '"' |
| 353 | + str(m["property"]) |
| 354 | + '"},\n' |
| 355 | ) |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 356 | params[p] += "}" |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 357 | else: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 358 | params[p] = member[p] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 359 | params["params"] = plist |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 360 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 361 | params["params"] = [] |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 362 | return params |
| 363 | |
| 364 | |
| 365 | def getInit(eGrps, eTrig, events): |
| 366 | """ |
| 367 | Extracts and constructs an init trigger for the event's groups |
| 368 | which are required to be of the same type. |
| 369 | """ |
| 370 | method = {} |
| 371 | methods = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 372 | if len(eGrps) > 0: |
Matthew Barth | 0377401 | 2018-10-26 13:25:43 -0500 | [diff] [blame] | 373 | # Use the first group member for retrieving the type |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 374 | member = eGrps[0]["members"][0] |
| 375 | if ("method" in eTrig) and (eTrig["method"] is not None): |
Matthew Barth | 0377401 | 2018-10-26 13:25:43 -0500 | [diff] [blame] | 376 | # Add method parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 377 | eMethod = next( |
| 378 | m for m in events["methods"] if m["name"] == eTrig["method"] |
| 379 | ) |
| 380 | method["method"] = eMethod["name"] |
| 381 | method["mparams"] = getParameters(member, eGrps, eMethod, events) |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 382 | |
Matthew Barth | 0377401 | 2018-10-26 13:25:43 -0500 | [diff] [blame] | 383 | # Add handler parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 384 | eHandler = next( |
| 385 | h for h in events["handlers"] if h["name"] == eTrig["handler"] |
| 386 | ) |
| 387 | method["handler"] = eHandler["name"] |
| 388 | method["hparams"] = getParameters(member, eGrps, eHandler, events) |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 389 | |
| 390 | methods.append(method) |
| 391 | |
| 392 | return methods |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 393 | |
| 394 | |
Matthew Barth | a69465a | 2018-03-02 13:50:59 -0600 | [diff] [blame] | 395 | def getSignal(eGrps, eTrig, events): |
| 396 | """ |
| 397 | Extracts and constructs for each group member a signal |
| 398 | subscription of each match listed in the trigger. |
| 399 | """ |
| 400 | signals = [] |
| 401 | for group in eGrps: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 402 | for member in group["members"]: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 403 | signal = {} |
| 404 | # Add signal parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 405 | eSignal = next( |
| 406 | s for s in events["signals"] if s["name"] == eTrig["signal"] |
| 407 | ) |
| 408 | signal["signal"] = eSignal["name"] |
| 409 | signal["sparams"] = getParameters(member, eGrps, eSignal, events) |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 410 | |
| 411 | # If service not given, subscribe to signal match |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 412 | if "service" not in member: |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 413 | # Add signal match parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 414 | eMatch = next( |
| 415 | m |
| 416 | for m in events["matches"] |
| 417 | if m["name"] == eSignal["match"] |
| 418 | ) |
| 419 | signal["match"] = eMatch["name"] |
| 420 | signal["mparams"] = getParameters( |
| 421 | member, eGrps, eMatch, events |
| 422 | ) |
Matthew Barth | a69465a | 2018-03-02 13:50:59 -0600 | [diff] [blame] | 423 | |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 424 | # Add handler parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 425 | eHandler = next( |
| 426 | h for h in events["handlers"] if h["name"] == eTrig["handler"] |
| 427 | ) |
| 428 | signal["handler"] = eHandler["name"] |
| 429 | signal["hparams"] = getParameters(member, eGrps, eHandler, events) |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 430 | |
Matthew Barth | cd3bfbc | 2018-03-07 16:26:03 -0600 | [diff] [blame] | 431 | signals.append(signal) |
Matthew Barth | 73379f9 | 2018-03-15 11:37:10 -0500 | [diff] [blame] | 432 | |
Matthew Barth | a69465a | 2018-03-02 13:50:59 -0600 | [diff] [blame] | 433 | return signals |
| 434 | |
| 435 | |
Matthew Barth | d0b90fc | 2018-03-05 09:38:45 -0600 | [diff] [blame] | 436 | def getTimer(eTrig): |
| 437 | """ |
| 438 | Extracts and constructs the required parameters for an |
| 439 | event timer. |
| 440 | """ |
| 441 | timer = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 442 | timer["interval"] = ( |
| 443 | "static_cast<std::chrono::microseconds>" |
| 444 | + "(" |
| 445 | + str(eTrig["interval"]) |
| 446 | + ")" |
| 447 | ) |
| 448 | timer["type"] = "TimerType::" + str(eTrig["type"]) |
Matthew Barth | d0b90fc | 2018-03-05 09:38:45 -0600 | [diff] [blame] | 449 | return timer |
| 450 | |
| 451 | |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 452 | def getActions(zNum, zCond, edata, actions, events): |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 453 | """ |
| 454 | Extracts and constructs the make_action function call for |
| 455 | all the actions within the given event. |
| 456 | """ |
| 457 | action = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 458 | for eActions in actions["actions"]: |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 459 | actions = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 460 | eAction = next( |
| 461 | a for a in events["actions"] if a["name"] == eActions["name"] |
| 462 | ) |
| 463 | actions["name"] = eAction["name"] |
| 464 | actions["groups"] = getGroups(zNum, zCond, eActions, events) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 465 | params = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 466 | if ("parameters" in eAction) and (eAction["parameters"] is not None): |
| 467 | for p in eAction["parameters"]: |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 468 | param = "static_cast<" |
| 469 | if type(eActions[p]) is not dict: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 470 | if p == "actions": |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 471 | param = "std::vector<Action>{" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 472 | pActs = getActions( |
| 473 | zNum, zCond, edata, eActions, events |
| 474 | ) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 475 | for a in pActs: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 476 | if len(a["parameters"]) != 0: |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 477 | param += ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 478 | "make_action(action::" + a["name"] + "(\n" |
| 479 | ) |
| 480 | for i, ap in enumerate(a["parameters"]): |
| 481 | if (i + 1) != len(a["parameters"]): |
| 482 | param += ap + "," |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 483 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 484 | param += ap + ")" |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 485 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 486 | param += "make_action(action::" + a["name"] |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 487 | param += ")," |
| 488 | param += "}" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 489 | elif p == "defevents" or p == "altevents" or p == "events": |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 490 | param = "std::vector<SetSpeedEvent>{\n" |
| 491 | for i, e in enumerate(eActions[p]): |
| 492 | aEvent = getEvent(zNum, zCond, e, events) |
| 493 | if not aEvent: |
| 494 | continue |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 495 | if (i + 1) != len(eActions[p]): |
Matthew Barth | a1aef7a | 2019-01-16 11:02:57 -0600 | [diff] [blame] | 496 | param += genEvent(aEvent) + ",\n" |
| 497 | else: |
| 498 | param += genEvent(aEvent) + "\n" |
| 499 | param += "\t}" |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 500 | elif p == "property": |
| 501 | if ( |
| 502 | isinstance(eActions[p], str) |
| 503 | or "string" in str(eActions[p]["type"]).lower() |
| 504 | ): |
Matthew Barth | 9a5b699 | 2018-01-23 15:32:26 -0600 | [diff] [blame] | 505 | param += ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 506 | str(eActions[p]["type"]).lower() |
| 507 | + '>("' |
| 508 | + str(eActions[p]) |
| 509 | + '")' |
| 510 | ) |
Matthew Barth | 9a5b699 | 2018-01-23 15:32:26 -0600 | [diff] [blame] | 511 | else: |
| 512 | param += ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 513 | str(eActions[p]["type"]).lower() |
| 514 | + ">(" |
| 515 | + str(eActions[p]["value"]).lower() |
| 516 | + ")" |
| 517 | ) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 518 | else: |
| 519 | # Default type to 'size_t' when not given |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 520 | param += "size_t>(" + str(eActions[p]).lower() + ")" |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 521 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 522 | if p == "timer": |
Matthew Barth | d0b90fc | 2018-03-05 09:38:45 -0600 | [diff] [blame] | 523 | t = getTimer(eActions[p]) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 524 | param = ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 525 | "TimerConf{" |
| 526 | + t["interval"] |
| 527 | + "," |
| 528 | + t["type"] |
| 529 | + "}" |
| 530 | ) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 531 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 532 | param += str(eActions[p]["type"]).lower() + ">(" |
| 533 | if p != "map": |
| 534 | if ( |
| 535 | isinstance(eActions[p]["value"], str) |
| 536 | or "string" in str(eActions[p]["type"]).lower() |
| 537 | ): |
| 538 | param += '"' + str(eActions[p]["value"]) + '")' |
Matthew Barth | 9a5b699 | 2018-01-23 15:32:26 -0600 | [diff] [blame] | 539 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 540 | param += ( |
| 541 | str(eActions[p]["value"]).lower() + ")" |
| 542 | ) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 543 | else: |
| 544 | param += ( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 545 | str(eActions[p]["type"]).lower() |
| 546 | + convertToMap(str(eActions[p]["value"])) |
| 547 | + ")" |
| 548 | ) |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 549 | params.append(param) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 550 | actions["parameters"] = params |
Matthew Barth | 9df7475 | 2017-10-11 14:39:31 -0500 | [diff] [blame] | 551 | action.append(actions) |
| 552 | return action |
| 553 | |
| 554 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 555 | def getEvent(zone_num, zone_conditions, e, events_data): |
| 556 | """ |
| 557 | Parses the sections of an event and populates the properties |
| 558 | that construct an event within the generated source. |
| 559 | """ |
| 560 | event = {} |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 561 | |
Matthew Barth | 621a577 | 2018-11-14 14:55:11 -0600 | [diff] [blame] | 562 | # Add set speed event name |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 563 | event["name"] = e["name"] |
Matthew Barth | 621a577 | 2018-11-14 14:55:11 -0600 | [diff] [blame] | 564 | |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 565 | # Add set speed event groups |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 566 | event["groups"] = getGroups(zone_num, zone_conditions, e, events_data) |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 567 | |
Matthew Barth | e3d1c4a | 2018-01-11 13:53:49 -0600 | [diff] [blame] | 568 | # Add optional set speed actions and function parameters |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 569 | event["action"] = [] |
| 570 | if ("actions" in e) and (e["actions"] is not None): |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 571 | # List of dicts containing the list of groups and list of actions |
| 572 | sseActions = [] |
| 573 | eActions = getActions(zone_num, zone_conditions, e, e, events_data) |
| 574 | for eAction in eActions: |
| 575 | # Skip events that have no groups defined for the event or actions |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 576 | if not event["groups"] and not eAction["groups"]: |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 577 | continue |
| 578 | # Find group in sseActions |
| 579 | grpExists = False |
| 580 | for sseDict in sseActions: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 581 | if eAction["groups"] == sseDict["groups"]: |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 582 | # Extend 'actions' list |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 583 | del eAction["groups"] |
| 584 | sseDict["actions"].append(eAction) |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 585 | grpExists = True |
| 586 | break |
| 587 | if not grpExists: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 588 | grps = eAction["groups"] |
| 589 | del eAction["groups"] |
Matthew Barth | 06fa781 | 2018-11-20 09:54:30 -0600 | [diff] [blame] | 590 | actList = [] |
| 591 | actList.append(eAction) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 592 | sseActions.append({"groups": grps, "actions": actList}) |
| 593 | event["action"] = sseActions |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 594 | |
Matthew Barth | a69465a | 2018-03-02 13:50:59 -0600 | [diff] [blame] | 595 | # Add event triggers |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 596 | event["triggers"] = {} |
| 597 | for trig in e["triggers"]: |
Matthew Barth | a69465a | 2018-03-02 13:50:59 -0600 | [diff] [blame] | 598 | triggers = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 599 | if trig["name"] == "timer": |
| 600 | event["triggers"]["timer"] = getTimer(trig) |
| 601 | elif trig["name"] == "signal": |
| 602 | if "signals" not in event["triggers"]: |
| 603 | event["triggers"]["signals"] = [] |
| 604 | triggers = getSignal(event["groups"], trig, events_data) |
| 605 | event["triggers"]["signals"].extend(triggers) |
| 606 | elif trig["name"] == "init": |
| 607 | triggers = getInit(event["groups"], trig, events_data) |
| 608 | event["triggers"]["init"] = triggers |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 609 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 610 | return event |
| 611 | |
| 612 | |
| 613 | def addPrecondition(zNum, zCond, event, events_data): |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 614 | """ |
| 615 | Parses the precondition section of an event and populates the necessary |
| 616 | structures to generate a precondition for a set speed event. |
| 617 | """ |
| 618 | precond = {} |
Matthew Barth | 621a577 | 2018-11-14 14:55:11 -0600 | [diff] [blame] | 619 | |
| 620 | # Add set speed event precondition name |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 621 | precond["pcname"] = event["name"] |
Matthew Barth | 621a577 | 2018-11-14 14:55:11 -0600 | [diff] [blame] | 622 | |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 623 | # Add set speed event precondition group |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 624 | precond["pcgrps"] = getGroups( |
| 625 | zNum, zCond, event["precondition"], events_data |
| 626 | ) |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 627 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 628 | # Add set speed event precondition actions |
| 629 | pc = [] |
| 630 | pcs = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 631 | pcs["name"] = event["precondition"]["name"] |
| 632 | epc = next( |
| 633 | p |
| 634 | for p in events_data["preconditions"] |
| 635 | if p["name"] == event["precondition"]["name"] |
| 636 | ) |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 637 | params = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 638 | for p in epc["parameters"] or []: |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 639 | param = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 640 | if p == "groups": |
| 641 | param["type"] = "std::vector<PrecondGroup>" |
| 642 | param["open"] = "{" |
| 643 | param["close"] = "}" |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 644 | values = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 645 | for group in precond["pcgrps"]: |
| 646 | for pcgrp in group["members"]: |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 647 | value = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 648 | value["value"] = ( |
| 649 | 'PrecondGroup{"' |
| 650 | + str(pcgrp["object"]) |
| 651 | + '","' |
| 652 | + str(pcgrp["interface"]) |
| 653 | + '","' |
| 654 | + str(pcgrp["property"]) |
| 655 | + '",' |
| 656 | + "static_cast<" |
| 657 | + str(pcgrp["type"]).lower() |
| 658 | + ">" |
| 659 | ) |
| 660 | if ( |
| 661 | isinstance(pcgrp["value"], str) |
| 662 | or "string" in str(pcgrp["type"]).lower() |
| 663 | ): |
| 664 | value["value"] += "(" + str(pcgrp["value"]) + ")}" |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 665 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 666 | value["value"] += ( |
| 667 | "(" + str(pcgrp["value"]).lower() + ")}" |
| 668 | ) |
Matthew Barth | 6c05069 | 2017-12-05 15:30:09 -0600 | [diff] [blame] | 669 | values.append(value) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 670 | param["values"] = values |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 671 | params.append(param) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 672 | pcs["params"] = params |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 673 | pc.append(pcs) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 674 | precond["pcact"] = pc |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 675 | |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 676 | pcevents = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 677 | for pce in event["precondition"]["events"]: |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 678 | pcevent = getEvent(zNum, zCond, pce, events_data) |
| 679 | if not pcevent: |
| 680 | continue |
| 681 | pcevents.append(pcevent) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 682 | precond["pcevts"] = pcevents |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 683 | |
Matthew Barth | f20c321 | 2018-03-02 14:42:55 -0600 | [diff] [blame] | 684 | # Add precondition event triggers |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 685 | precond["triggers"] = {} |
| 686 | for trig in event["precondition"]["triggers"]: |
Matthew Barth | f20c321 | 2018-03-02 14:42:55 -0600 | [diff] [blame] | 687 | triggers = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 688 | if trig["name"] == "timer": |
| 689 | precond["triggers"]["pctime"] = getTimer(trig) |
| 690 | elif trig["name"] == "signal": |
| 691 | if "pcsigs" not in precond["triggers"]: |
| 692 | precond["triggers"]["pcsigs"] = [] |
| 693 | triggers = getSignal(precond["pcgrps"], trig, events_data) |
| 694 | precond["triggers"]["pcsigs"].extend(triggers) |
| 695 | elif trig["name"] == "init": |
| 696 | triggers = getInit(precond["pcgrps"], trig, events_data) |
| 697 | precond["triggers"]["init"] = triggers |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 698 | |
| 699 | return precond |
| 700 | |
| 701 | |
Gunnar Mills | b751f32 | 2017-06-06 15:14:11 -0500 | [diff] [blame] | 702 | def getEventsInZone(zone_num, zone_conditions, events_data): |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 703 | """ |
| 704 | Constructs the event entries defined for each zone using the events yaml |
| 705 | provided. |
| 706 | """ |
| 707 | events = [] |
Matthew Barth | ba102b3 | 2017-05-16 16:13:56 -0500 | [diff] [blame] | 708 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 709 | if "events" in events_data: |
| 710 | for e in events_data["events"]: |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 711 | event = {} |
Matthew Barth | 621a577 | 2018-11-14 14:55:11 -0600 | [diff] [blame] | 712 | |
Matthew Barth | 9af190c | 2017-08-08 14:20:43 -0500 | [diff] [blame] | 713 | # Add precondition if given |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 714 | if ("precondition" in e) and (e["precondition"] is not None): |
| 715 | event["pc"] = addPrecondition( |
| 716 | zone_num, zone_conditions, e, events_data |
| 717 | ) |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 718 | else: |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 719 | event = getEvent(zone_num, zone_conditions, e, events_data) |
Matthew Barth | a6f7516 | 2018-11-20 13:50:42 -0600 | [diff] [blame] | 720 | # Remove empty events and events that have |
| 721 | # no groups defined for the event or any of the actions |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 722 | if not event or ( |
| 723 | not event["groups"] |
| 724 | and all(not a["groups"] for a in event["action"]) |
| 725 | ): |
Matthew Barth | 7f272fd | 2017-09-12 16:16:56 -0500 | [diff] [blame] | 726 | continue |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 727 | events.append(event) |
| 728 | |
| 729 | return events |
| 730 | |
| 731 | |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 732 | def getFansInZone(zone_num, profiles, fan_data): |
| 733 | """ |
| 734 | Parses the fan definition YAML files to find the fans |
| 735 | that match both the zone passed in and one of the |
| 736 | cooling profiles. |
| 737 | """ |
| 738 | |
| 739 | fans = [] |
| 740 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 741 | for f in fan_data["fans"]: |
| 742 | if zone_num != f["cooling_zone"]: |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 743 | continue |
| 744 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 745 | # 'cooling_profile' is optional (use 'all' instead) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 746 | if f.get("cooling_profile") is None: |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 747 | profile = "all" |
| 748 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 749 | profile = f["cooling_profile"] |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 750 | |
| 751 | if profile not in profiles: |
| 752 | continue |
| 753 | |
| 754 | fan = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 755 | fan["name"] = f["inventory"] |
| 756 | fan["sensors"] = f["sensors"] |
| 757 | fan["target_interface"] = f.get( |
| 758 | "target_interface", "xyz.openbmc_project.Control.FanSpeed" |
| 759 | ) |
| 760 | fan["target_path"] = f.get( |
| 761 | "target_path", "/xyz/openbmc_project/sensors/fan_tach/" |
| 762 | ) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 763 | fans.append(fan) |
| 764 | |
| 765 | return fans |
| 766 | |
| 767 | |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 768 | def getIfacesInZone(zone_ifaces): |
| 769 | """ |
| 770 | Parse given interfaces for a zone for associating a zone with an interface |
| 771 | and set any properties listed to defined values upon fan control starting |
| 772 | on the zone. |
| 773 | """ |
| 774 | |
| 775 | ifaces = [] |
| 776 | for i in zone_ifaces: |
| 777 | iface = {} |
| 778 | # Interface name not needed yet for fan zones but |
| 779 | # may be necessary as more interfaces are extended by the zones |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 780 | iface["name"] = i["name"] |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 781 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 782 | if ("properties" in i) and (i["properties"] is not None): |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 783 | props = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 784 | for p in i["properties"]: |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 785 | prop = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 786 | prop["name"] = p["name"] |
| 787 | prop["func"] = str(p["name"]).lower() |
| 788 | prop["type"] = parse_cpp_type(p["type"]) |
| 789 | if "persist" in p: |
| 790 | persist = p["persist"] |
| 791 | if persist is not None: |
| 792 | if isinstance(persist, bool): |
| 793 | prop["persist"] = "true" if persist else "false" |
Matthew Barth | 59096e5 | 2019-02-18 12:23:38 -0600 | [diff] [blame] | 794 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 795 | prop["persist"] = "false" |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 796 | vals = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 797 | for v in p["values"]: |
| 798 | val = v["value"] |
| 799 | if val is not None: |
| 800 | if isinstance(val, bool): |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 801 | # Convert True/False to 'true'/'false' |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 802 | val = "true" if val else "false" |
| 803 | elif isinstance(val, str): |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 804 | # Wrap strings with double-quotes |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 805 | val = '"' + val + '"' |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 806 | vals.append(val) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 807 | prop["values"] = vals |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 808 | props.append(prop) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 809 | iface["props"] = props |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 810 | ifaces.append(iface) |
| 811 | |
| 812 | return ifaces |
| 813 | |
| 814 | |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 815 | def getConditionInZoneConditions(zone_condition, zone_conditions_data): |
| 816 | """ |
| 817 | Parses the zone conditions definition YAML files to find the condition |
| 818 | that match both the zone condition passed in. |
| 819 | """ |
| 820 | |
| 821 | condition = {} |
| 822 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 823 | for c in zone_conditions_data["conditions"]: |
| 824 | if zone_condition != c["name"]: |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 825 | continue |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 826 | condition["type"] = c["type"] |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 827 | properties = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 828 | for p in c["properties"]: |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 829 | property = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 830 | property["property"] = p["property"] |
| 831 | property["interface"] = p["interface"] |
| 832 | property["path"] = p["path"] |
| 833 | property["type"] = p["type"].lower() |
| 834 | property["value"] = str(p["value"]).lower() |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 835 | properties.append(property) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 836 | condition["properties"] = properties |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 837 | |
| 838 | return condition |
| 839 | |
| 840 | |
| 841 | def buildZoneData(zone_data, fan_data, events_data, zone_conditions_data): |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 842 | """ |
| 843 | Combines the zone definition YAML and fan |
| 844 | definition YAML to create a data structure defining |
| 845 | the fan cooling zones. |
| 846 | """ |
| 847 | |
| 848 | zone_groups = [] |
| 849 | |
Matthew Barth | 005ff2f | 2020-02-18 11:17:41 -0600 | [diff] [blame] | 850 | # Allow zone_conditions to not be in yaml (since its optional) |
| 851 | if not isinstance(zone_data, list) and zone_data != {}: |
| 852 | zone_data = [zone_data] |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 853 | for group in zone_data: |
| 854 | conditions = [] |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 855 | # zone conditions are optional |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 856 | if "zone_conditions" in group and group["zone_conditions"] is not None: |
| 857 | for c in group["zone_conditions"]: |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 858 | if not zone_conditions_data: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 859 | sys.exit( |
| 860 | "No zone_conditions YAML file but " |
| 861 | + "zone_conditions used in zone YAML" |
| 862 | ) |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 863 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 864 | condition = getConditionInZoneConditions( |
| 865 | c["name"], zone_conditions_data |
| 866 | ) |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 867 | |
| 868 | if not condition: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 869 | sys.exit("Missing zone condition " + c["name"]) |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 870 | |
| 871 | conditions.append(condition) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 872 | |
| 873 | zone_group = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 874 | zone_group["conditions"] = conditions |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 875 | |
| 876 | zones = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 877 | for z in group["zones"]: |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 878 | zone = {} |
| 879 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 880 | # 'zone' is required |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 881 | if ("zone" not in z) or (z["zone"] is None): |
| 882 | sys.exit("Missing fan zone number in " + z) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 883 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 884 | zone["num"] = z["zone"] |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 885 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 886 | zone["full_speed"] = z["full_speed"] |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 887 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 888 | zone["default_floor"] = z["default_floor"] |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 889 | |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 890 | # 'increase_delay' is optional (use 0 by default) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 891 | key = "increase_delay" |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 892 | zone[key] = z.setdefault(key, 0) |
| 893 | |
| 894 | # 'decrease_interval' is optional (use 0 by default) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 895 | key = "decrease_interval" |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 896 | zone[key] = z.setdefault(key, 0) |
| 897 | |
Gunnar Mills | 67e9551 | 2017-06-02 14:35:18 -0500 | [diff] [blame] | 898 | # 'cooling_profiles' is optional (use 'all' instead) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 899 | if ("cooling_profiles" not in z) or ( |
| 900 | z["cooling_profiles"] is None |
| 901 | ): |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 902 | profiles = ["all"] |
| 903 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 904 | profiles = z["cooling_profiles"] |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 905 | |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 906 | # 'interfaces' is optional (no default) |
Matthew Barth | 64099cd | 2019-02-18 09:43:12 -0600 | [diff] [blame] | 907 | ifaces = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 908 | if ("interfaces" in z) and (z["interfaces"] is not None): |
| 909 | ifaces = getIfacesInZone(z["interfaces"]) |
Matthew Barth | 7883f58 | 2019-02-14 14:24:46 -0600 | [diff] [blame] | 910 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 911 | fans = getFansInZone(z["zone"], profiles, fan_data) |
| 912 | events = getEventsInZone( |
| 913 | z["zone"], group.get("zone_conditions", {}), events_data |
| 914 | ) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 915 | |
| 916 | if len(fans) == 0: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 917 | sys.exit("Didn't find any fans in zone " + str(zone["num"])) |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 918 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 919 | if ifaces: |
| 920 | zone["ifaces"] = ifaces |
| 921 | zone["fans"] = fans |
| 922 | zone["events"] = events |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 923 | zones.append(zone) |
| 924 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 925 | zone_group["zones"] = zones |
Matt Spinler | 78498c9 | 2017-04-11 13:59:46 -0500 | [diff] [blame] | 926 | zone_groups.append(zone_group) |
| 927 | |
| 928 | return zone_groups |
| 929 | |
| 930 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 931 | if __name__ == "__main__": |
| 932 | parser = ArgumentParser(description="Phosphor fan zone definition parser") |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 933 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 934 | parser.add_argument( |
| 935 | "-z", |
| 936 | "--zone_yaml", |
| 937 | dest="zone_yaml", |
| 938 | default="example/zones.yaml", |
| 939 | help="fan zone definitional yaml", |
| 940 | ) |
| 941 | parser.add_argument( |
| 942 | "-f", |
| 943 | "--fan_yaml", |
| 944 | dest="fan_yaml", |
| 945 | default="example/fans.yaml", |
| 946 | help="fan definitional yaml", |
| 947 | ) |
| 948 | parser.add_argument( |
| 949 | "-e", |
| 950 | "--events_yaml", |
| 951 | dest="events_yaml", |
| 952 | help="events to set speeds yaml", |
| 953 | ) |
| 954 | parser.add_argument( |
| 955 | "-c", |
| 956 | "--zone_conditions_yaml", |
| 957 | dest="zone_conditions_yaml", |
| 958 | help="conditions to determine zone yaml", |
| 959 | ) |
| 960 | parser.add_argument( |
| 961 | "-o", |
| 962 | "--output_dir", |
| 963 | dest="output_dir", |
| 964 | default=".", |
| 965 | help="output directory", |
| 966 | ) |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 967 | args = parser.parse_args() |
| 968 | |
| 969 | if not args.zone_yaml or not args.fan_yaml: |
| 970 | parser.print_usage() |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 971 | sys.exit(1) |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 972 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 973 | with open(args.zone_yaml, "r") as zone_input: |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 974 | zone_data = yaml.safe_load(zone_input) or {} |
| 975 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 976 | with open(args.fan_yaml, "r") as fan_input: |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 977 | fan_data = yaml.safe_load(fan_input) or {} |
| 978 | |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 979 | events_data = {} |
| 980 | if args.events_yaml: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 981 | with open(args.events_yaml, "r") as events_input: |
Matthew Barth | d4d0f08 | 2017-05-16 13:51:10 -0500 | [diff] [blame] | 982 | events_data = yaml.safe_load(events_input) or {} |
| 983 | |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 984 | zone_conditions_data = {} |
| 985 | if args.zone_conditions_yaml: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 986 | with open(args.zone_conditions_yaml, "r") as zone_conditions_input: |
Gunnar Mills | ee8a281 | 2017-06-02 14:26:47 -0500 | [diff] [blame] | 987 | zone_conditions_data = yaml.safe_load(zone_conditions_input) or {} |
| 988 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 989 | zone_config = buildZoneData( |
| 990 | zone_data.get("zone_configuration", {}), |
| 991 | fan_data, |
| 992 | events_data, |
| 993 | zone_conditions_data, |
| 994 | ) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 995 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 996 | manager_config = zone_data.get("manager_configuration", {}) |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 997 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 998 | if manager_config.get("power_on_delay") is None: |
| 999 | manager_config["power_on_delay"] = 0 |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 1000 | |
Matthew Barth | 702c4a5 | 2018-02-28 16:23:11 -0600 | [diff] [blame] | 1001 | tmpls_dir = os.path.join( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 1002 | os.path.dirname(os.path.realpath(__file__)), "templates" |
| 1003 | ) |
Matt Spinler | d08dbe2 | 2017-04-11 13:52:54 -0500 | [diff] [blame] | 1004 | output_file = os.path.join(args.output_dir, "fan_zone_defs.cpp") |
Matthew Barth | 702c4a5 | 2018-02-28 16:23:11 -0600 | [diff] [blame] | 1005 | if sys.version_info < (3, 0): |
| 1006 | lkup = TemplateLookup( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 1007 | directories=tmpls_dir.split(), disable_unicode=True |
| 1008 | ) |
Matthew Barth | 702c4a5 | 2018-02-28 16:23:11 -0600 | [diff] [blame] | 1009 | else: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 1010 | lkup = TemplateLookup(directories=tmpls_dir.split()) |
| 1011 | tmpl = lkup.get_template("fan_zone_defs.mako.cpp") |
| 1012 | with open(output_file, "w") as output: |
| 1013 | output.write(tmpl.render(zones=zone_config, mgr_data=manager_config)) |