mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-03 19:24:48 +08:00
Merge pull request #3451 from artoscinote/ma_SCI_5911
Implemented printer management [SCI-5911]
This commit is contained in:
commit
46a9388d8d
10 changed files with 321 additions and 25 deletions
78
app/controllers/label_printers_controller.rb
Normal file
78
app/controllers/label_printers_controller.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class LabelPrintersController < ApplicationController
|
||||
include InputSanitizeHelper
|
||||
|
||||
before_action :find_label_printer, only: %i(edit update destroy)
|
||||
|
||||
def index
|
||||
@label_printers = LabelPrinter.all
|
||||
end
|
||||
|
||||
def new
|
||||
@label_printer = LabelPrinter.new
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def create
|
||||
@label_printer = LabelPrinter.new(label_printer_params)
|
||||
|
||||
if @label_printer.save
|
||||
flash[:success] = t('label_printers.create.success', { printer_name: @label_printer.name })
|
||||
redirect_to edit_label_printer_path(@label_printer)
|
||||
else
|
||||
flash[:error] = t('label_printers.create.error', { printer_name: @label_printer.name })
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @label_printer.update(label_printer_params)
|
||||
flash[:success] = t('label_printers.update.success', { printer_name: @label_printer.name })
|
||||
redirect_to edit_label_printer_path(@label_printer)
|
||||
else
|
||||
flash[:error] = t('label_printers.update.error', { printer_name: @label_printer.name })
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @label_printer.destroy
|
||||
flash[:success] = t('label_printers.destroy.success', { printer_name: @label_printer.name })
|
||||
else
|
||||
flash[:error] = t('label_printers.destroy.error', { printer_name: @label_printer.name })
|
||||
end
|
||||
|
||||
redirect_to addons_path
|
||||
end
|
||||
|
||||
def create_fluics
|
||||
# Placeholder for FLUICS printer management
|
||||
|
||||
LabelPrinters::Fluics::ApiClient.new(params[:fluics_api_key]).list.each do |fluics_printer|
|
||||
label_printer = LabelPrinter.find_or_initialize_by(
|
||||
fluics_api_key: params[:fluics_api_key],
|
||||
fluics_lid: fluics_printer['LID'],
|
||||
type_of: :fluics,
|
||||
language_type: :zpl
|
||||
)
|
||||
|
||||
label_printer.update(name: fluics_printer['serviceName'])
|
||||
end
|
||||
|
||||
redirect_to addons_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def label_printer_params
|
||||
params.require(:label_printer).permit(
|
||||
:name, :type_of, :fluics_api_key, :host, :port
|
||||
)
|
||||
end
|
||||
|
||||
def find_label_printer
|
||||
@label_printer = LabelPrinter.find(params[:id])
|
||||
end
|
||||
end
|
|
@ -4,16 +4,8 @@ module Users
|
|||
class AddonsController < ApplicationController
|
||||
layout 'fluid'
|
||||
|
||||
before_action :load_printers, only: :index
|
||||
|
||||
def label_printer
|
||||
@printer = { printer_type: :fluics, ready: true, api_key: 'ISVO42192IUDV168ATFI314UVYGU151USHYEV42' }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_printers
|
||||
@fluics_printer = { ready: true, enabled: true }
|
||||
def index
|
||||
@label_printers = LabelPrinter.all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
14
app/models/label_printer.rb
Normal file
14
app/models/label_printer.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class LabelPrinter < ApplicationRecord
|
||||
enum type_of: { fluics: 0 }
|
||||
enum language_type: { zpl: 0 }
|
||||
|
||||
validates :name, presence: true
|
||||
validates :type_of, presence: true
|
||||
validates :language_type, presence: true
|
||||
|
||||
def ready?
|
||||
true # TODO
|
||||
end
|
||||
end
|
63
app/services/label_printers/fluics/api_client.rb
Normal file
63
app/services/label_printers/fluics/api_client.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module LabelPrinters
|
||||
module Fluics
|
||||
class ApiClient
|
||||
class NotFoundError < StandardError; end
|
||||
class ServerError < StandardError; end
|
||||
class BadRequestError < StandardError; end
|
||||
|
||||
include HTTParty
|
||||
base_uri 'https://print-api.fluics.com/latest'
|
||||
|
||||
def initialize(api_key)
|
||||
self.class.headers(
|
||||
'Accept' => 'application/json',
|
||||
'x-api-key' => api_key
|
||||
)
|
||||
end
|
||||
|
||||
def list
|
||||
do_request(:get, '/get_printers_list')
|
||||
end
|
||||
|
||||
def status(lid)
|
||||
do_request(:get, "/#{lid}/status")
|
||||
end
|
||||
|
||||
def calibrate(lid)
|
||||
do_request(:post, "/#{lid}/calibrate")
|
||||
end
|
||||
|
||||
def print(lid, zpl)
|
||||
do_request(
|
||||
:post,
|
||||
"/#{lid}/print",
|
||||
params: {
|
||||
headers: { 'Content-Type' => 'text/plain' },
|
||||
body: zpl
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def do_request(http_method, path, params: {})
|
||||
response = self.class.public_send(http_method, path, params)
|
||||
|
||||
case response.code
|
||||
when 200..299
|
||||
# success response
|
||||
when 404
|
||||
raise NotFoundError, "#{response.code}: #{response.message}"
|
||||
when 400..499
|
||||
raise BadRequestError, "#{response.code}: #{response.message}"
|
||||
when 500...600
|
||||
raise ServerError, "#{response.code}: #{response.message}"
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
60
app/views/label_printers/edit.html.erb
Normal file
60
app/views/label_printers/edit.html.erb
Normal file
|
@ -0,0 +1,60 @@
|
|||
<% provide(:head_title, t('users.settings.account.addons.head_title')) %>
|
||||
<% provide(:container_class, "no-second-nav-container") %>
|
||||
|
||||
<%= render partial: 'users/settings/sidebar.html.erb' %>
|
||||
|
||||
<div class="content-pane flexible label-printer-show">
|
||||
<div class="content-header">
|
||||
<div id="breadcrumbsWrapper">
|
||||
<div class="breadcrumbs-container">
|
||||
<%= render partial: 'shared/breadcrumbs', locals: {
|
||||
links: [{label: 'Add-ons', url: addons_path}]
|
||||
} %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="title-row">
|
||||
<h1 class="printer-title">
|
||||
<%= t("users.settings.account.label_printer.#{@label_printer.type_of}_printer") %>
|
||||
</h1>
|
||||
<div class="status" data-ready="<%= @label_printer.ready? %>">
|
||||
<% if @label_printer.ready? %>
|
||||
<%= t('users.settings.account.addons.printers.ready') %>
|
||||
<% else %>
|
||||
<%= t('users.settings.account.addons.printers.not_ready') %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<div class="collapse-row">
|
||||
<i class="fas fa-caret-down" data-toggle="collapse" href="#InstructionsSection" aria-expanded="false"></i>
|
||||
<div class="row-title"><%= t("users.settings.account.label_printer.instructions") %></div>
|
||||
</div>
|
||||
<ul class="collapse in" id="InstructionsSection">
|
||||
Instruction here
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
<div class="collapse-row">
|
||||
<i class="fas fa-caret-down" data-toggle="collapse" href="#SettingsSection" aria-expanded="false"></i>
|
||||
<div class="row-title"><%= t("users.settings.account.label_printer.settings") %></div>
|
||||
</div>
|
||||
<ul class="collapse in" id="SettingsSection">
|
||||
<% if @label_printer.fluics? %>
|
||||
<%= form_with model: @label_printer do |form| %>
|
||||
<div class="sci-input-container">
|
||||
<%= form.label :fluics_api_key, t("users.settings.account.label_printer.api_key_label") %>
|
||||
<div class="api-key-container">
|
||||
<%= form.text_field :fluics_api_key, class: "sci-input-field api-key-input" %>
|
||||
<%= form.submit t('general.save'), class: "btn btn-secondary" %>
|
||||
</div>
|
||||
</div>
|
||||
<%= link_to t("users.settings.account.label_printer.api_key_information"), '' %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
|
@ -18,34 +18,39 @@
|
|||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-12">
|
||||
<h1 class="addons-title"><%= t('users.settings.account.addons.label_printers') %></h1>
|
||||
<div class="printers-container">
|
||||
<% if @fluics_printer %>
|
||||
<% if @label_printers.none? %>
|
||||
<%= form_with url: create_fluics_label_printers_path, method: :post do |f| %>
|
||||
<h2>FLUICS Printer Management Placeholder</h2>
|
||||
<%= f.text_field :fluics_api_key %>
|
||||
<%= f.submit "Fetch FLUICS Printers" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% @label_printers.each do |label_printer| %>
|
||||
<div class="printers-container">
|
||||
<div class="printer">
|
||||
<div class="header">
|
||||
<div class="title">
|
||||
<%= t('users.settings.account.addons.fluics_printer.title') %>
|
||||
</div>
|
||||
<div class="status" data-ready="<%= @fluics_printer[:ready] %>">
|
||||
<% if @fluics_printer[:ready] %>
|
||||
<div class="status" data-ready="<%= label_printer.ready? %>">
|
||||
<% if label_printer.ready? %>
|
||||
<%= t('users.settings.account.addons.printers.ready') %>
|
||||
<% else %>
|
||||
<%= t('users.settings.account.addons.printers.not_ready') %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="control">
|
||||
<% if @fluics_printer[:enabled] %>
|
||||
<%= t('users.settings.account.addons.printers.enabled') %>
|
||||
<i class="fas fa-check"></i>
|
||||
<% end %>
|
||||
<%= t('users.settings.account.addons.printers.enabled') %>
|
||||
<i class="fas fa-check"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="description">
|
||||
<%= t('users.settings.account.addons.fluics_printer.description') %>
|
||||
</div>
|
||||
<%= link_to t('users.settings.account.addons.printers.printer_details'), label_printer_path, class: 'printer-details' %>
|
||||
<%= link_to t('users.settings.account.addons.printers.printer_details'), edit_label_printer_path(label_printer), class: 'printer-details' %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -762,6 +762,18 @@ en:
|
|||
add_user_generic_error: "An error occurred. "
|
||||
can_add_user_to_project: "Can not add user to the project."
|
||||
|
||||
label_printers:
|
||||
create:
|
||||
success: "Successfully added %{printer_name}"
|
||||
error: "Could not add %{printer_name}"
|
||||
update:
|
||||
success: "Successfully updated %{printer_name}"
|
||||
error: "Could not update %{printer_name}"
|
||||
delete:
|
||||
success: "Successfully deleted %{printer_name}."
|
||||
error: "Could not delete %{printer_name}."
|
||||
|
||||
|
||||
my_modules:
|
||||
details:
|
||||
title: "Details"
|
||||
|
|
|
@ -41,9 +41,11 @@ Rails.application.routes.draw do
|
|||
get 'users/settings/account/addons',
|
||||
to: 'users/settings/account/addons#index',
|
||||
as: 'addons'
|
||||
get 'users/settings/account/addons/label_printer',
|
||||
to: 'users/settings/account/addons#label_printer',
|
||||
as: 'label_printer'
|
||||
|
||||
resources :label_printers, except: :show, path: 'users/settings/account/addons/label_printers' do
|
||||
post :create_fluics, on: :collection
|
||||
end
|
||||
|
||||
get 'users/settings/account/connected_accounts',
|
||||
to: 'users/settings/account/connected_accounts#index',
|
||||
as: 'connected_accounts'
|
||||
|
|
17
db/migrate/20210720112050_create_label_printers.rb
Normal file
17
db/migrate/20210720112050_create_label_printers.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateLabelPrinters < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :label_printers do |t|
|
||||
t.string :name, null: false
|
||||
t.integer :type_of, null: false
|
||||
t.integer :language_type, null: false
|
||||
t.string :host
|
||||
t.integer :port
|
||||
t.string :fluics_api_key
|
||||
t.string :fluics_lid
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -555,6 +555,43 @@ CREATE SEQUENCE public.experiments_id_seq
|
|||
ALTER SEQUENCE public.experiments_id_seq OWNED BY public.experiments.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_printers; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.label_printers (
|
||||
id bigint NOT NULL,
|
||||
name character varying NOT NULL,
|
||||
type_of integer NOT NULL,
|
||||
language_type integer NOT NULL,
|
||||
host character varying,
|
||||
port integer,
|
||||
fluics_api_key character varying,
|
||||
fluics_lid character varying,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_printers_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE public.label_printers_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_printers_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE public.label_printers_id_seq OWNED BY public.label_printers.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_templates; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -3009,6 +3046,13 @@ ALTER TABLE ONLY public.delayed_jobs ALTER COLUMN id SET DEFAULT nextval('public
|
|||
ALTER TABLE ONLY public.experiments ALTER COLUMN id SET DEFAULT nextval('public.experiments_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_printers id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.label_printers ALTER COLUMN id SET DEFAULT nextval('public.label_printers_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_templates id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -3582,6 +3626,14 @@ ALTER TABLE ONLY public.experiments
|
|||
ADD CONSTRAINT experiments_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_printers label_printers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.label_printers
|
||||
ADD CONSTRAINT label_printers_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: label_templates label_templates_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
@ -7308,6 +7360,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20210410100006'),
|
||||
('20210506125657'),
|
||||
('20210622101238'),
|
||||
('20210716124649');
|
||||
('20210716124649'),
|
||||
('20210720112050');
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue