【发布时间】:2018-08-20 00:47:56
【问题描述】:
我正在尝试制作我在网上找到的自动浇水系统,现在泵仅在用户按下启动时运行,当用户按下停止时停止。我想让它每两天运行一次。我不确定是否需要将其放入我的 javascript 水泵文件或我的 ruby Sinatra 应用程序脚本中。我尝试过使用发条,每当 gem 时,我认为我的语法是错误的,我尝试将它放在 app.rb 中的 CLIENT.publish 周围。同样在本教程中,我正在关注创建者说要在 app.rb 文件的末尾 CLIENT.publish('pump/data', {state:{pump: params['action']}}.to_json) 添加正确的参数。我也尝试在 water-pump.js 中执行 setInterval,但不确定如何执行。我没有对 index.haml 文件做任何事情,我需要在 .pump-control 中添加一些东西吗?这里是教程,如果你想看的话https://www.hackster.io/demirhanaydin/waterpi-houseplant-remote-watering-and-monitoring-system-340400
这是我的 water-pump.js 文件
function takeAction() {
var button = $('#pumper'),
action = $('#action_input').val();
$('#action-form').trigger('submit');
if (action == 'start') {
$('#action_input').val('stop');
button.text('stop');
} else {
$('#action_input').val('start');
button.text('start');
}
}
$(document).ready(function() {
$('#action-form').submit(function(event) {
event.preventDefault(); // Prevent the form from submitting via the browser
var form = $(this);
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize()
}).done(function(data) {
// Optionally alert the user of success here...
}).fail(function(data) {
// Optionally alert the user of an error here...
});
});
});
这是我的 app.rb
# app.rb
require 'sinatra'
require './boot'
set :sessions, true
set :session_secret, SESSION_SECRET
enable :sessions
get '/' do
haml 'welcome/index'.to_sym
end
post '/take-action' do
CLIENT.publish('pump/data', {state:{pump: params['action']}}.to_json)
200
end
这是我的 index.haml 文件
%head
%script{type: "text/javascript", src: url('javascripts/jquery.min.js')}
%script{type: "text/javascript", src: url('/javascripts/water-pump.js')}
%iframe{:align => "center",:height => "450", :src => "https://app.redash.io/taylor/embed/query/76316/visualization/132291?api_key=EfPwD8WgLKybMagKriu7JrLFXvYWpVVQrV5g0qqp", :width => "820",:style => "float:left;margin-top:30px;"}
%link{rel: :stylesheet, type: :"text/css", href: url('/stylesheets/default.css')}
#container
#synchronized
.gauge
#container-speed
.pump-control
%form{method: :post, action: '/take-action', id: 'action-form' }
%input{type: 'hidden', name: 'action', value: 'start', id: 'action_input'}
%span{:align => "center",id: 'pumper', onclick: 'takeAction()'}
start
【问题讨论】:
-
什么是cron?什么是间隔?
-
我可以只使用节点调度程序吗?
-
最后我会怎么做 CLIENT.publish('pump/data', {state:{pump: params['action']}}.to_json)
标签: javascript ruby sinatra haml