Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using BitAPI.Models;
- using BitAPI.Controllers;
- using BitAPI.Repositories;
- using Opc.UaFx.Client;
- namespace BitAPI.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class CommandsController : ControllerBase
- {
- private readonly CommandContext _context;
- public CommandsController(CommandContext context)
- {
- _context = context;
- }
- // Simple return-string method
- /*
- ## Works -> Postman and React ##
- */
- [HttpGet("string")]
- public string ReturnString()
- {
- return "The returned string is here";
- }
- [HttpGet("batch")]
- public object GrabLatestBatch()
- {
- //ServerRepository connection = new ServerRepository();
- //var CntrlCmd = accessPoint.ReadNode("ns=6;s=::Program:Cube.Command.CntrlCmd");
- // Tries to write to server. Gives message if completed (Maybe try-clause)
- return ServerRepository.ReadData();
- }
- [HttpGet("machine")]
- public void CallMachine()
- {
- ServerRepository connection = new ServerRepository();
- // Tries to write to server. Gives message if completed (Maybe try-clause)
- if(connection.WriteFloat("ns=6;s=::Program:Cube.Command.Parameter[2]", 100)) {
- Console.WriteLine("Write was successful. Check UAExpert!");
- } else {
- Console.WriteLine("Write was not successful... Maybe the types aren't right?");
- }
- }
- /*
- Everything below here is other methods that can be called
- in the API. They are simple CRUD-methods made from
- C# scaffolding
- */
- // GET: api/Commands
- [HttpGet]
- public async Task<ActionResult<IEnumerable<Command>>> GetCommands()
- {
- return await _context.Commands.ToListAsync();
- }
- // GET: api/Commands/5
- [HttpGet("{id}")]
- public async Task<ActionResult<Command>> GetCommand(long id)
- {
- var command = await _context.Commands.FindAsync(id);
- if (command == null)
- {
- return NotFound();
- }
- return command;
- }
- // PUT: api/Commands/5
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://aka.ms/RazorPagesCRUD.
- [HttpPut("{id}")]
- public async Task<IActionResult> PutCommand(long id, Command command)
- {
- if (id != command.id)
- {
- return BadRequest();
- }
- _context.Entry(command).State = EntityState.Modified;
- try
- {
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!CommandExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return NoContent();
- }
- // POST: api/Commands
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://aka.ms/RazorPagesCRUD.
- [HttpPost]
- public async Task<ActionResult<Command>> PostCommand(Command command)
- {
- _context.Commands.Add(command);
- await _context.SaveChangesAsync();
- return CreatedAtAction("GetCommand", new { id = command.id }, command);
- }
- // DELETE: api/Commands/5
- [HttpDelete("{id}")]
- public async Task<ActionResult<Command>> DeleteCommand(long id)
- {
- var command = await _context.Commands.FindAsync(id);
- if (command == null)
- {
- return NotFound();
- }
- _context.Commands.Remove(command);
- await _context.SaveChangesAsync();
- return command;
- }
- private bool CommandExists(long id)
- {
- return _context.Commands.Any(e => e.id == id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement