@@ -3,19 +3,17 @@ package adj
33import (
44 "encoding/json"
55 "os"
6- "path"
76
87 "github.com/HyperloopUPV-H8/h9-backend/internal/utils"
9- "github.com/go-git/go-git/v5"
108)
119
1210const (
1311 RepoUrl = "https://github.com/HyperloopUPV-H8/adj.git" // URL of the ADJ repository
1412 RepoPath = "./adj/" // Path where the ADJ repository is cloned
1513)
1614
17- func NewADJ () (ADJ , error ) {
18- infoRaw , boardsRaw , err := downloadADJ ()
15+ func NewADJ (AdjBranch string ) (ADJ , error ) {
16+ infoRaw , boardsRaw , err := downloadADJ (AdjBranch )
1917 if err != nil {
2018 return ADJ {}, err
2119 }
@@ -73,15 +71,8 @@ func NewADJ() (ADJ, error) {
7371 return adj , nil
7472}
7573
76- func downloadADJ () (json.RawMessage , json.RawMessage , error ) {
77- if ! checkRepo () {
78- _ , err := git .PlainClone (RepoPath , false , & git.CloneOptions {
79- URL : RepoUrl ,
80- })
81- if err != nil {
82- return nil , nil , err
83- }
84- }
74+ func downloadADJ (AdjBranch string ) (json.RawMessage , json.RawMessage , error ) {
75+ updateRepo (AdjBranch )
8576
8677 // The BoardIds are applied in the NewADJ function by the getBoardIds function
8778 info , err := os .ReadFile (RepoPath + "general_info.json" )
@@ -96,163 +87,3 @@ func downloadADJ() (json.RawMessage, json.RawMessage, error) {
9687
9788 return info , boardsList , nil
9889}
99-
100- func checkRepo () bool {
101- if _ , err := os .Stat (RepoPath ); os .IsNotExist (err ) {
102- return false
103- }
104-
105- return true
106- }
107-
108- func getBoards (boardsList map [string ]string ) (map [string ]Board , error ) {
109- boards := make (map [string ]Board , len (boardsList ))
110- for boardName , boardPath := range boardsList {
111- fullPath := path .Join (RepoPath , boardPath )
112- boardRaw , err := os .ReadFile (fullPath )
113- if err != nil {
114- return nil , err
115- }
116-
117- var boardJSON BoardJSON
118- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
119- return nil , err
120- }
121-
122- measPathsFr := make ([]string , 0 )
123- for _ , measPath := range boardJSON .MeasurementsPaths {
124- measPathsFr = append (measPathsFr , path .Join (RepoPath , "boards" , boardName , measPath ))
125- }
126- boardJSON .MeasurementsPaths = measPathsFr
127-
128- packetPathsFr := make ([]string , 0 )
129- for _ , packetPath := range boardJSON .PacketsPaths {
130- packetPathsFr = append (packetPathsFr , path .Join (RepoPath , "boards" , boardName , packetPath ))
131- }
132- boardJSON .PacketsPaths = packetPathsFr
133-
134- board := Board {
135- Name : boardName ,
136- IP : boardJSON .IP ,
137- }
138- board .Packets , err = getBoardPackets (boardJSON .PacketsPaths )
139- if err != nil {
140- return nil , err
141- }
142-
143- board .Measurements , err = getBoardMeasurements (boardJSON .MeasurementsPaths )
144- if err != nil {
145- return nil , err
146- }
147- board .LookUpMeasurements = make (map [string ]Measurement , len (board .Measurements ))
148-
149- for _ , measurement := range board .Measurements {
150- board .LookUpMeasurements [measurement .Id ] = measurement
151- }
152- board .Structures = getBoardStructures (board )
153-
154- boards [boardName ] = board
155- }
156-
157- return boards , nil
158- }
159-
160- func getBoardPackets (packetsPaths []string ) ([]Packet , error ) {
161- packets := make ([]Packet , 0 )
162- for _ , packetPath := range packetsPaths {
163- if _ , err := os .Stat (packetPath ); os .IsNotExist (err ) {
164- continue
165- }
166-
167- packetRaw , err := os .ReadFile (packetPath )
168- if err != nil {
169- return nil , err
170- }
171-
172- // Magic happens here
173- type PacketJSON struct {
174- Packet []Packet `json:"packets"`
175- }
176-
177- packetsJSON := PacketJSON {}
178- if err = json .Unmarshal (packetRaw , & packetsJSON ); err != nil {
179- return nil , err
180- }
181- for _ , packetTMP := range packetsJSON .Packet {
182- packets = append (packets , packetTMP )
183- }
184- }
185-
186- return packets , nil
187- }
188-
189- func getBoardMeasurements (measurementsPaths []string ) ([]Measurement , error ) {
190- measurements := make ([]Measurement , 0 )
191-
192- for _ , measurementPath := range measurementsPaths {
193- if _ , err := os .Stat (measurementPath ); os .IsNotExist (err ) {
194- continue
195- }
196-
197- measurementRaw , err := os .ReadFile (measurementPath )
198- if err != nil {
199- return nil , err
200- }
201-
202- // Absolutely doing tricks on it AGAIN - @msanlli
203- type MeasurementJSON struct {
204- Measurements []Measurement `json:"measurements"`
205- }
206-
207- measurementsJSON := MeasurementJSON {}
208- if err = json .Unmarshal (measurementRaw , & measurementsJSON ); err != nil {
209- return nil , err
210- }
211- for _ , measurementTMP := range measurementsJSON .Measurements {
212- measurements = append (measurements , measurementTMP )
213- }
214- }
215-
216- return measurements , nil
217- }
218-
219- func getBoardIds (boards map [string ]string ) (map [string ]uint16 , error ) {
220- boardIds := make (map [string ]uint16 , len (boards ))
221- for boardName , boardPath := range boards {
222- fullPath := path .Join (RepoPath , boardPath )
223- boardRaw , err := os .ReadFile (fullPath )
224- if err != nil {
225- return nil , err
226- }
227-
228- var boardJSON BoardJSON
229- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
230- return nil , err
231- }
232-
233- boardIds [boardName ] = boardJSON .ID
234- }
235-
236- return boardIds , nil
237- }
238-
239- func getBoardStructures (board Board ) []Structure {
240- structures := make ([]Structure , len (board .Packets ))
241- for i , packet := range board .Packets {
242- structures [i ] = Structure {
243- Packet : packet ,
244- Measurements : board .Measurements ,
245- }
246- }
247-
248- return structures
249- }
250-
251- func getAddresses (boards map [string ]Board ) (map [string ]string , error ) {
252- addresses := make (map [string ]string , len (boards ))
253- for boardName , board := range boards {
254- addresses [boardName ] = board .IP
255- }
256-
257- return addresses , nil
258- }
0 commit comments