· 6 years ago · Jul 09, 2019, 12:26 PM
1#!/usr/bin/lua5.1
2
3dofile('config.lua')
4luasql = require "luasql.sqlite3"
5socket = require("socket")
6env = luasql.sqlite3()
7conn = env:connect(cfg.path)
8
9-- create a TCP socket and bind it to the host and port
10local server = socket.bind(cfg.ip, cfg.port)
11
12-- create table in db "data"
13
14conn:execute("create table if not exists data(id INTEGER PRIMARY KEY AUTOINCREMENT, data_id INTEGER UNIQUE, value TEXT NOT NULL, timestamp INTEGER NOT NULL)")
15
16cursor = conn:execute("SELECT * FROM data")
17
18
19function split(command)
20 tblofsplit = {};
21 for match in (command.."/"):gmatch("(.-)/") do
22 table.insert(tblofsplit, match);
23 end
24 return tblofsplit;
25end
26
27
28while( true ) do
29 client=server:accept()
30 print ("Client connected")
31
32while( true ) do
33 client:send("waiting for command...".."\n")
34 command = client:receive()
35 if string.find(command,"get") == 1 then
36 split (command)
37 tblofsplit[2] = string.gsub (tblofsplit[2],"#","")
38 if(string.find (tblofsplit[2], "%D")) then
39 client:send ("only digits are allowed".."\n") else
40-- client:send(table.concat(row, '|').."\n")
41 client:send(tblofsplit[2].."\n")
42 end
43 elseif string.find(command,"set") == 1
44 then
45 conn:execute("insert into data values(8,10,'tesst',15)")
46 client:send(table.concat(row, '|').."\n")
47 client:send("set")
48 elseif string.find(command,"del") == 1 then
49 split (command)
50 tblofsplit[2] = string.gsub (tblofsplit[2],"#","")
51 if(string.find (tblofsplit[2], "%D")) then
52 client:send ("only digits are allowed".."\n") else
53-- client:send(table.concat(row, '|').."\n")
54 client:send(tblofsplit[2].."\n")
55 end
56 elseif string.find(command,"quit") == 1
57 then
58 client:close()
59 break
60 else client:send("unknown command".."\n") end
61end
62end