|
| 1 | +//! Conversion from L5X types to plcmodel. |
| 2 | +//! |
| 3 | +//! This module provides the [`ToPlcModel`] implementation for L5X projects, |
| 4 | +//! converting parsed Rockwell L5X files into the vendor-neutral `plcmodel` representation. |
| 5 | +
|
| 6 | +use plcmodel::{Pou, Project, ToPlcModel, Variable}; |
| 7 | +use iectypes::{PouType, VarClass}; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + Project as L5xProject, |
| 11 | + AProgram, |
| 12 | + Tag, |
| 13 | + Routine as L5xRoutine, |
| 14 | + RoutineContent, |
| 15 | + RungCollection, |
| 16 | + RungContent, |
| 17 | + TextWideContent, |
| 18 | + UDIDefinitionContent, |
| 19 | + UDIDefinition, |
| 20 | +}; |
| 21 | + |
| 22 | +impl ToPlcModel for L5xProject { |
| 23 | + fn to_plc_model(&self) -> Project { |
| 24 | + let name = self.controller.as_ref() |
| 25 | + .map(|c| c.name.clone()) |
| 26 | + .unwrap_or_else(|| "Unnamed".to_string()); |
| 27 | + |
| 28 | + let mut project = Project::new(name); |
| 29 | + project.source_format = Some("L5X".to_string()); |
| 30 | + |
| 31 | + if let Some(ref controller) = self.controller { |
| 32 | + // Convert programs to POUs |
| 33 | + if let Some(ref programs) = controller.programs { |
| 34 | + for program in &programs.program { |
| 35 | + project.pous.push(convert_program(program)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Convert AOIs to POUs (FunctionBlocks) |
| 40 | + if let Some(ref aois) = controller.add_on_instruction_definitions { |
| 41 | + for aoi in &aois.add_on_instruction_definition { |
| 42 | + project.pous.push(convert_aoi(aoi)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + project |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn convert_program(program: &AProgram) -> Pou { |
| 52 | + let mut pou = Pou::new(&program.name, PouType::Program); |
| 53 | + |
| 54 | + // Program-scoped tags become locals |
| 55 | + if let Some(ref tags) = program.tags { |
| 56 | + for tag in &tags.tag { |
| 57 | + pou.interface.locals.push(convert_tag(tag, VarClass::Local)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Extract routine bodies and combine into a single body |
| 62 | + if let Some(ref routines) = program.routines { |
| 63 | + let mut body_parts = Vec::new(); |
| 64 | + for routine in &routines.routine { |
| 65 | + if let Some(rll_text) = extract_routine_rll(routine) { |
| 66 | + body_parts.push(format!("// Routine: {}\n{}", routine.name, rll_text)); |
| 67 | + } |
| 68 | + } |
| 69 | + if !body_parts.is_empty() { |
| 70 | + pou.body = Some(plcmodel::Body::Raw { |
| 71 | + language: "RLL".to_string(), |
| 72 | + content: body_parts.join("\n\n"), |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pou |
| 78 | +} |
| 79 | + |
| 80 | +fn convert_aoi(aoi: &UDIDefinition) -> Pou { |
| 81 | + let mut pou = Pou::new(&aoi.name, PouType::FunctionBlock); |
| 82 | + |
| 83 | + // Process UDI content (parameters, local tags, routines) |
| 84 | + for content in &aoi.content { |
| 85 | + match content { |
| 86 | + UDIDefinitionContent::Parameters(params) => { |
| 87 | + for param in ¶ms.parameter { |
| 88 | + let var_class = match param.usage.as_str() { |
| 89 | + "Input" => VarClass::Input, |
| 90 | + "Output" => VarClass::Output, |
| 91 | + "InOut" => VarClass::InOut, |
| 92 | + _ => VarClass::Local, |
| 93 | + }; |
| 94 | + |
| 95 | + let data_type = param.data_type.clone().unwrap_or_else(|| "DINT".to_string()); |
| 96 | + let mut var = Variable::new(¶m.name, data_type); |
| 97 | + var.var_class = var_class.clone(); |
| 98 | + |
| 99 | + match var_class { |
| 100 | + VarClass::Input => pou.interface.inputs.push(var), |
| 101 | + VarClass::Output => pou.interface.outputs.push(var), |
| 102 | + VarClass::InOut => pou.interface.in_outs.push(var), |
| 103 | + _ => pou.interface.locals.push(var), |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + UDIDefinitionContent::LocalTags(local_tags) => { |
| 108 | + for tag in &local_tags.local_tag { |
| 109 | + let data_type = tag.data_type.clone(); |
| 110 | + let mut var = Variable::new(&tag.name, data_type); |
| 111 | + var.var_class = VarClass::Local; |
| 112 | + pou.interface.locals.push(var); |
| 113 | + } |
| 114 | + } |
| 115 | + UDIDefinitionContent::Routines(routines) => { |
| 116 | + let mut body_parts = Vec::new(); |
| 117 | + for routine in &routines.routine { |
| 118 | + if let Some(rll_text) = extract_routine_rll(routine) { |
| 119 | + body_parts.push(format!("// Routine: {}\n{}", routine.name, rll_text)); |
| 120 | + } |
| 121 | + } |
| 122 | + if !body_parts.is_empty() { |
| 123 | + pou.body = Some(plcmodel::Body::Raw { |
| 124 | + language: "RLL".to_string(), |
| 125 | + content: body_parts.join("\n\n"), |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + _ => {} |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + pou |
| 134 | +} |
| 135 | + |
| 136 | +fn extract_routine_rll(routine: &L5xRoutine) -> Option<String> { |
| 137 | + for content in &routine.content { |
| 138 | + if let RoutineContent::RLLContent(rll) = content { |
| 139 | + return Some(extract_rll_text(rll)); |
| 140 | + } |
| 141 | + } |
| 142 | + None |
| 143 | +} |
| 144 | + |
| 145 | +fn extract_rll_text(rll: &RungCollection) -> String { |
| 146 | + let mut rungs = Vec::new(); |
| 147 | + |
| 148 | + for rung in &rll.rung { |
| 149 | + for content in &rung.content { |
| 150 | + if let RungContent::Text(text_wide) = content { |
| 151 | + // Extract text from TextWide content |
| 152 | + for tc in &text_wide.content { |
| 153 | + if let TextWideContent::TextContent(text) = tc { |
| 154 | + let trimmed = text.trim(); |
| 155 | + if !trimmed.is_empty() { |
| 156 | + rungs.push(trimmed.to_string()); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + rungs.join("\n") |
| 165 | +} |
| 166 | + |
| 167 | +fn convert_tag(tag: &Tag, default_class: VarClass) -> Variable { |
| 168 | + let data_type = tag.data_type.clone().unwrap_or_else(|| "DINT".to_string()); |
| 169 | + let mut var = Variable::new(&tag.name, data_type); |
| 170 | + |
| 171 | + // Determine var class from Usage attribute |
| 172 | + var.var_class = match tag.usage.as_deref() { |
| 173 | + Some("Input") => VarClass::Input, |
| 174 | + Some("Output") => VarClass::Output, |
| 175 | + Some("InOut") => VarClass::InOut, |
| 176 | + _ => default_class, |
| 177 | + }; |
| 178 | + |
| 179 | + // Handle array dimensions - parse to Vec<u32> |
| 180 | + if let Some(ref dims) = tag.dimensions { |
| 181 | + let parsed: Vec<u32> = dims |
| 182 | + .split(&[' ', ',', 'x'][..]) |
| 183 | + .filter_map(|s| s.trim().parse().ok()) |
| 184 | + .collect(); |
| 185 | + if !parsed.is_empty() { |
| 186 | + var.dimensions = parsed; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + var |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(test)] |
| 194 | +mod tests { |
| 195 | + use super::*; |
| 196 | + use crate::from_str; |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_convert_simple_project() { |
| 200 | + let xml = r#"<?xml version="1.0" encoding="UTF-8"?> |
| 201 | + <RSLogix5000Content SchemaRevision="1.0" SoftwareRevision="32.00"> |
| 202 | + <Controller Name="MainController"> |
| 203 | + <Programs> |
| 204 | + <Program Name="MainProgram" MainRoutineName="MainRoutine"> |
| 205 | + <Tags> |
| 206 | + <Tag Name="Counter" DataType="DINT"/> |
| 207 | + <Tag Name="Running" DataType="BOOL"/> |
| 208 | + </Tags> |
| 209 | + <Routines> |
| 210 | + <Routine Name="MainRoutine" Type="RLL"> |
| 211 | + <RLLContent> |
| 212 | + <Rung Number="0"> |
| 213 | + <Text>XIC(Start)OTE(Motor);</Text> |
| 214 | + </Rung> |
| 215 | + </RLLContent> |
| 216 | + </Routine> |
| 217 | + </Routines> |
| 218 | + </Program> |
| 219 | + </Programs> |
| 220 | + </Controller> |
| 221 | + </RSLogix5000Content>"#; |
| 222 | + |
| 223 | + let l5x: L5xProject = from_str(xml).expect("Failed to parse"); |
| 224 | + let model = l5x.to_plc_model(); |
| 225 | + |
| 226 | + assert_eq!(model.name, "MainController"); |
| 227 | + assert_eq!(model.pous.len(), 1); |
| 228 | + |
| 229 | + let main = &model.pous[0]; |
| 230 | + assert_eq!(main.name, "MainProgram"); |
| 231 | + assert!(matches!(main.pou_type, PouType::Program)); |
| 232 | + assert_eq!(main.interface.locals.len(), 2); |
| 233 | + assert!(main.body.is_some()); |
| 234 | + } |
| 235 | + |
| 236 | + #[test] |
| 237 | + fn test_convert_real_file() { |
| 238 | + let path = std::path::Path::new(env!("HOME")) |
| 239 | + .join("devpublic/dataplc/l5x/sample-project.L5X"); |
| 240 | + |
| 241 | + if !path.exists() { |
| 242 | + eprintln!("Skipping test: file not found at {:?}", path); |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + let xml = std::fs::read_to_string(&path).expect("Failed to read"); |
| 247 | + let l5x: L5xProject = from_str(&xml).expect("Failed to parse"); |
| 248 | + let model = l5x.to_plc_model(); |
| 249 | + |
| 250 | + assert!(!model.name.is_empty()); |
| 251 | + eprintln!("Converted project: {}", model.name); |
| 252 | + eprintln!(" {} POUs", model.pous.len()); |
| 253 | + |
| 254 | + for pou in &model.pous { |
| 255 | + eprintln!(" {}: {:?}, {} locals, has_body={}", |
| 256 | + pou.name, pou.pou_type, |
| 257 | + pou.interface.locals.len(), |
| 258 | + pou.body.is_some()); |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments