Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using BitAPI.Models;
  9. using BitAPI.Controllers;
  10. using BitAPI.Repositories;
  11. using Opc.UaFx.Client;
  12.  
  13.  
  14. namespace BitAPI.Controllers
  15. {
  16. [Route("api/[controller]")]
  17. [ApiController]
  18. public class CommandsController : ControllerBase
  19. {
  20. private readonly CommandContext _context;
  21.  
  22. public CommandsController(CommandContext context)
  23. {
  24. _context = context;
  25. }
  26.  
  27. // Simple return-string method
  28. /*
  29. ## Works -> Postman and React ##
  30. */
  31. [HttpGet("string")]
  32. public string ReturnString()
  33. {
  34. return "The returned string is here";
  35. }
  36.  
  37.  
  38. [HttpGet("batch")]
  39. public object GrabLatestBatch()
  40. {
  41. //ServerRepository connection = new ServerRepository();
  42.  
  43. //var CntrlCmd = accessPoint.ReadNode("ns=6;s=::Program:Cube.Command.CntrlCmd");
  44. // Tries to write to server. Gives message if completed (Maybe try-clause)
  45.  
  46.  
  47.  
  48. return ServerRepository.ReadData();
  49.  
  50. }
  51.  
  52.  
  53. [HttpGet("machine")]
  54. public void CallMachine()
  55. {
  56. ServerRepository connection = new ServerRepository();
  57.  
  58. // Tries to write to server. Gives message if completed (Maybe try-clause)
  59. if(connection.WriteFloat("ns=6;s=::Program:Cube.Command.Parameter[2]", 100)) {
  60. Console.WriteLine("Write was successful. Check UAExpert!");
  61. } else {
  62. Console.WriteLine("Write was not successful... Maybe the types aren't right?");
  63. }
  64. }
  65.  
  66. /*
  67. Everything below here is other methods that can be called
  68. in the API. They are simple CRUD-methods made from
  69. C# scaffolding
  70. */
  71.  
  72. // GET: api/Commands
  73. [HttpGet]
  74. public async Task<ActionResult<IEnumerable<Command>>> GetCommands()
  75. {
  76. return await _context.Commands.ToListAsync();
  77. }
  78.  
  79. // GET: api/Commands/5
  80. [HttpGet("{id}")]
  81. public async Task<ActionResult<Command>> GetCommand(long id)
  82. {
  83. var command = await _context.Commands.FindAsync(id);
  84.  
  85. if (command == null)
  86. {
  87. return NotFound();
  88. }
  89.  
  90. return command;
  91. }
  92.  
  93. // PUT: api/Commands/5
  94. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  95. // more details see https://aka.ms/RazorPagesCRUD.
  96. [HttpPut("{id}")]
  97. public async Task<IActionResult> PutCommand(long id, Command command)
  98. {
  99. if (id != command.id)
  100. {
  101. return BadRequest();
  102. }
  103.  
  104. _context.Entry(command).State = EntityState.Modified;
  105.  
  106. try
  107. {
  108. await _context.SaveChangesAsync();
  109. }
  110. catch (DbUpdateConcurrencyException)
  111. {
  112. if (!CommandExists(id))
  113. {
  114. return NotFound();
  115. }
  116. else
  117. {
  118. throw;
  119. }
  120. }
  121.  
  122. return NoContent();
  123. }
  124.  
  125. // POST: api/Commands
  126. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  127. // more details see https://aka.ms/RazorPagesCRUD.
  128. [HttpPost]
  129. public async Task<ActionResult<Command>> PostCommand(Command command)
  130. {
  131. _context.Commands.Add(command);
  132. await _context.SaveChangesAsync();
  133.  
  134. return CreatedAtAction("GetCommand", new { id = command.id }, command);
  135. }
  136.  
  137. // DELETE: api/Commands/5
  138. [HttpDelete("{id}")]
  139. public async Task<ActionResult<Command>> DeleteCommand(long id)
  140. {
  141. var command = await _context.Commands.FindAsync(id);
  142. if (command == null)
  143. {
  144. return NotFound();
  145. }
  146.  
  147. _context.Commands.Remove(command);
  148. await _context.SaveChangesAsync();
  149.  
  150. return command;
  151. }
  152.  
  153. private bool CommandExists(long id)
  154. {
  155. return _context.Commands.Any(e => e.id == id);
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement