@@ -3,16 +3,13 @@ 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"
10- "github.com/go-git/go-git/v5/plumbing"
118)
129
1310const (
14- RepoUrl = "https://github.com/HyperloopUPV-H8/JSON_ADE .git" // URL of the ADJ repository
15- RepoPath = "./JSON_ADE /" // Path where the ADJ repository is cloned
11+ RepoUrl = "https://github.com/HyperloopUPV-H8/adj .git" // URL of the ADJ repository
12+ RepoPath = "./adj /" // Path where the ADJ repository is cloned
1613)
1714
1815func NewADJ (AdjBranch string ) (ADJ , error ) {
@@ -90,249 +87,3 @@ func downloadADJ(AdjBranch string) (json.RawMessage, json.RawMessage, error) {
9087
9188 return info , boardsList , nil
9289}
93-
94- // WARNING: Doing tricks on it
95- func updateRepo (AdjBranch string ) error {
96- var repo * git.Repository
97- var err error
98-
99- if AdjBranch == "" {
100- // Makes use of submodule
101- return nil
102- } else {
103- if _ , err = os .Stat (RepoPath ); os .IsNotExist (err ) {
104- repo , err = git .PlainClone (RepoPath , false , & git.CloneOptions {
105- URL : RepoUrl ,
106- ReferenceName : plumbing .NewBranchReferenceName (AdjBranch ),
107- SingleBranch : true ,
108- Depth : 1 ,
109- })
110- if err != nil {
111- return err
112- }
113- } else {
114- repo , err = git .PlainOpen (RepoPath )
115- if err != nil {
116- return err
117- }
118- }
119-
120- err = repo .Fetch (& git.FetchOptions {
121- RemoteName : "origin" ,
122- Force : true ,
123- })
124- if err != nil && err != git .NoErrAlreadyUpToDate {
125- return err
126- }
127-
128- head , err := repo .Head ()
129- if err != nil {
130- return err
131- }
132-
133- branch := head .Name ().Short ()
134-
135- println ("actual branch is: " , branch )
136-
137- worktree , err := repo .Worktree ()
138- if err != nil {
139- return err
140- }
141-
142- println (head .Name ().Short (), AdjBranch )
143-
144- if branch != AdjBranch {
145- localBranchRef := plumbing .NewBranchReferenceName (AdjBranch )
146- _ , err = repo .Reference (localBranchRef , false )
147- if err != nil {
148- remoteBranchRef := plumbing .NewRemoteReferenceName ("origin" , AdjBranch )
149- remoteRef , err2 := repo .Reference (remoteBranchRef , true )
150- if err2 != nil {
151- return err2
152- }
153-
154- err = worktree .Checkout (& git.CheckoutOptions {
155- Branch : localBranchRef ,
156- Create : true ,
157- Force : true ,
158- Hash : remoteRef .Hash (),
159- })
160- if err != nil {
161- println (err .Error ())
162- return err
163- }
164- } else {
165- err = worktree .Checkout (& git.CheckoutOptions {
166- Branch : localBranchRef ,
167- Force : true ,
168- })
169- }
170- }
171-
172- err = worktree .Pull (& git.PullOptions {
173- RemoteName : "origin" ,
174- SingleBranch : true ,
175- })
176- if err != nil {
177- if err == git .NoErrAlreadyUpToDate {
178- return nil
179- } else {
180- return err
181- }
182- }
183- }
184-
185- return nil
186- }
187-
188- func getBoards (boardsList map [string ]string ) (map [string ]Board , error ) {
189- boards := make (map [string ]Board , len (boardsList ))
190- for boardName , boardPath := range boardsList {
191- fullPath := path .Join (RepoPath , boardPath )
192- boardRaw , err := os .ReadFile (fullPath )
193- if err != nil {
194- return nil , err
195- }
196-
197- var boardJSON BoardJSON
198- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
199- return nil , err
200- }
201-
202- measPathsFr := make ([]string , 0 )
203- for _ , measPath := range boardJSON .MeasurementsPaths {
204- measPathsFr = append (measPathsFr , path .Join (RepoPath , "boards" , boardName , measPath ))
205- }
206- boardJSON .MeasurementsPaths = measPathsFr
207-
208- packetPathsFr := make ([]string , 0 )
209- for _ , packetPath := range boardJSON .PacketsPaths {
210- packetPathsFr = append (packetPathsFr , path .Join (RepoPath , "boards" , boardName , packetPath ))
211- }
212- boardJSON .PacketsPaths = packetPathsFr
213-
214- board := Board {
215- Name : boardName ,
216- IP : boardJSON .IP ,
217- }
218- board .Packets , err = getBoardPackets (boardJSON .PacketsPaths )
219- if err != nil {
220- return nil , err
221- }
222-
223- board .Measurements , err = getBoardMeasurements (boardJSON .MeasurementsPaths )
224- if err != nil {
225- return nil , err
226- }
227- board .LookUpMeasurements = make (map [string ]Measurement , len (board .Measurements ))
228-
229- for _ , measurement := range board .Measurements {
230- board .LookUpMeasurements [measurement .Id ] = measurement
231- }
232- board .Structures = getBoardStructures (board )
233-
234- boards [boardName ] = board
235- }
236-
237- return boards , nil
238- }
239-
240- func getBoardPackets (packetsPaths []string ) ([]Packet , error ) {
241- packets := make ([]Packet , 0 )
242- for _ , packetPath := range packetsPaths {
243- if _ , err := os .Stat (packetPath ); os .IsNotExist (err ) {
244- continue
245- }
246-
247- packetRaw , err := os .ReadFile (packetPath )
248- if err != nil {
249- return nil , err
250- }
251-
252- // Magic happens here
253- type PacketJSON struct {
254- Packet []Packet `json:"packets"`
255- }
256-
257- packetsJSON := PacketJSON {}
258- if err = json .Unmarshal (packetRaw , & packetsJSON ); err != nil {
259- return nil , err
260- }
261- for _ , packetTMP := range packetsJSON .Packet {
262- packets = append (packets , packetTMP )
263- }
264- }
265-
266- return packets , nil
267- }
268-
269- func getBoardMeasurements (measurementsPaths []string ) ([]Measurement , error ) {
270- measurements := make ([]Measurement , 0 )
271-
272- for _ , measurementPath := range measurementsPaths {
273- if _ , err := os .Stat (measurementPath ); os .IsNotExist (err ) {
274- continue
275- }
276-
277- measurementRaw , err := os .ReadFile (measurementPath )
278- if err != nil {
279- return nil , err
280- }
281-
282- // Absolutely doing tricks on it AGAIN - @msanlli
283- type MeasurementJSON struct {
284- Measurements []Measurement `json:"measurements"`
285- }
286-
287- measurementsJSON := MeasurementJSON {}
288- if err = json .Unmarshal (measurementRaw , & measurementsJSON ); err != nil {
289- return nil , err
290- }
291- for _ , measurementTMP := range measurementsJSON .Measurements {
292- measurements = append (measurements , measurementTMP )
293- }
294- }
295-
296- return measurements , nil
297- }
298-
299- func getBoardIds (boards map [string ]string ) (map [string ]uint16 , error ) {
300- boardIds := make (map [string ]uint16 , len (boards ))
301- for boardName , boardPath := range boards {
302- fullPath := path .Join (RepoPath , boardPath )
303- boardRaw , err := os .ReadFile (fullPath )
304- if err != nil {
305- return nil , err
306- }
307-
308- var boardJSON BoardJSON
309- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
310- return nil , err
311- }
312-
313- boardIds [boardName ] = boardJSON .ID
314- }
315-
316- return boardIds , nil
317- }
318-
319- func getBoardStructures (board Board ) []Structure {
320- structures := make ([]Structure , len (board .Packets ))
321- for i , packet := range board .Packets {
322- structures [i ] = Structure {
323- Packet : packet ,
324- Measurements : board .Measurements ,
325- }
326- }
327-
328- return structures
329- }
330-
331- func getAddresses (boards map [string ]Board ) (map [string ]string , error ) {
332- addresses := make (map [string ]string , len (boards ))
333- for boardName , board := range boards {
334- addresses [boardName ] = board .IP
335- }
336-
337- return addresses , nil
338- }
0 commit comments