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